PreviousBtrieve API Guide (v10) Next

Create (14)

Chapter contents

The Create operation (B_CREATE) lets you generate a new data file from within your application. The Create operation also has subfunctions that allow you to delete or rename a file (see Delete and Rename Subfunctions for the Create Operation ).


Note
In the same directory, no two files should share the same file name and differ only in their file name extension. For example, do not name a data file Invoice.btr and another one Invoice.mkd in the same directory. This restriction applies because the database engine uses the file name for various areas of functionality while ignoring the file name extension. Since only the file name is used to differentiate files, files that differ only in their file name extension look identical to the database engine.

Parameters

 
Op Code
Pos Block
Data Buf
Data Buf Len
Key Buffer
Key Number
Sent

 

Returned
           

Prerequisites

If you are creating an empty file over an existing file, ensure that the existing file is closed before executing the Create operation.

Procedure

  1. Set the Operation Code to 14.
  2. Specify the file specifications, key specifications, and any alternate collating sequences in the Data Buffer as described in Details. (All the values for the file specifications and key specifications you store in the Data Buffer must be in binary format.)
  3. Specify the Data Buffer Length. This is the length of the buffer that contains the Create specifications, not the length of the records in the file.
  4. Specify the pathname for the file in the Key Buffer. Be sure to terminate the pathname with a blank or binary zero. The pathname can be up to 80 characters long, including the volume name and the terminator.
  5. For more information about path names the transactional interface supports, refer to Getting Started With Pervasive PSQL.

  6. Specify a value for the Key Number parameter, using one of the values in Table 2-7 .

Details

Table 2-1 illustrates the order in which the file and key specifications must be stored.

Table 2-1 Data Buffer Structure for Create Operation 
Description
Data Type1
Byte #
Example Value2
File Specification
Logical Fixed Record Length3
Short Int4
0, 1
Size of all fields combined.
Page Size.
Pervasive PSQL 6.x through 9.4
Short Int
2, 3
512
Pervasive PSQL 6.x and later
1,024
Pervasive PSQL 6.x through 9.4
1,536
Pervasive PSQL 6.x and later
2,048
Pervasive PSQL 6.x through 9.4
3,072
3,584
Pervasive PSQL 6.x and later
4,096
Pervasive PSQL 9.0 through 9.4x
8,192
Pervasive PSQL 9.5 and later
16,384
A minimum size of 4096 bytes works best for most files. If you want to fine-tune this, refer to Creating a File with Page Level Compression for more information.
When creating 9.5 file format files, if the logical page size specified is valid for the file format, the MicroKernel rounds the specified value to the next higher valid value if one exists. For all other values and file formats, the operation fails with status code 24. No rounding is done for the older file formats.
Number of Keys (Indexes)
Byte
4
File Version
Byte
5
0x60
Version 6.0
0x70
Version 7.0
0x80
Version 8.0
0x90
Version 9.0
0x95
Version 9.5
0x00
Use database engine default
Reserved. (Not used during a Create operation.)
Reserved
6- 9
0
File Flags. Specifies the file attributes. The example file does not use any.
Short Int
10, 11
0
Number of Extra Pointers. Sets the number of duplicate pointers to reserve for future key additions. Used if the file attributes specify Reserve Duplicate Pointers.
Byte
12
0
Reserved. (Not used during a Create operation.)
Reserved
13
0
Preallocated Pages. Sets the number of pages to preallocate. Used if the file attributes specify Page Preallocation.
Short Int
14, 15
0
Key Specification for Key 0 (Last Name)
Key Position. Provides the position of the first byte of the key within the record. The first byte in the record is 1.
Short Int
16, 17
1
Key Length. Specifies the length of the key, in bytes.
Short Int
18, 19
25
Key Flags. Specifies the key attributes.
Short Int
20, 21
EXTTYPE_KEY + NOCASE_KEY + DUP + MOD
Not Used for a Create.
Byte
22-25
0
Extended Key Type. Used if the key flags specify Use Extended Key Type. Specifies one of the extended data types.
Byte
26
ZSTRING
Null Value (legacy nulls only). Used if the key flags specify Null Key (All Segments) or Null Key (Any Segment). Specifies an exclusion value for the key. See Null Value for more conceptual information on legacy nulls and true nulls.
Byte
27
0
Not Used for a Create.
Byte
28, 29
0
Manually Assigned Key Number. Used if the file attributes specify Key Number. Assigns a key number.
Byte
30
0
ACS Number. Used if the key flags specify Use Default ACS, Use Numbered ACS in File, or Use Named ACS. Specifies the ACS number to use.
Byte
31
0
Key Specification for Key 1 (Employee ID)
Key Position. (Employee ID starts at first byte after Middle Initial.)
Short Int
32, 33
52
Key Length.
Short Int
34, 35
4
Key Flags.
Short Int
36, 37
EXTTYPE_KEY + MOD
Not Used for a Create.
Byte
38-41
0
Extended Key Type.
Byte
42
INTEGER
Null Value.
Byte
43
0
Not Used for a Create.
Byte
44, 45
0
Manually Assigned Key Number.
Byte
46
0
ACS Number.
Byte
47
0
Key Specification for Page Compression
Physical Page Size5
Char
A
512
(default value)
1Unless specified otherwise, all data types are unsigned.
2For simplification, the non-numeric example values are for C applications.
3For files with variable-length records, the logical record length refers only to the fixed-length portion of the record.
4Short Integers (Short Int) must be stored in the "Little Endian" byte order, which is the Low To High ordering of Intel-class computers.
5Only used with page-level compression. Must be used in conjunction with the Page Compression file flag (see Table 4-4). See Creating a File with Page Level Compression for more information.


Note
You must allocate the "not used" and "reserved" areas of the Data Buffer, even though the transactional interface does not use them for a Create operation. Initialize the reserved areas to zero to maintain compatibility with future releases.

File Specification

Store the file specification in the first 16 bytes of the Data Buffer. The bytes are numbered beginning with 0. Store the information for the record length, page size, and number of indexes as integers.

Logical Record Length. (Offset 0x00) The logical record length is the number of bytes of fixed-length data in the file. (Do not include variable-length data in the logical record length.)

Page Size. (Offset 0x02) Page size is determined by your file format version. See Choosing a Page Size in Pervasive PSQL Programmer's Guide.

Number of Indexes. (Offset 0x04) The number of indexes is the number of keys (not key segments) you are defining for the file. To create a data-only file, set the number of indexes to 0.

File Version. (Offset 0x05) The transactional interface file version to be created. In prior releases, the transactional interface used a two byte integer to receive the number of indexes on a create operation. The high order byte of this integer was always zero because the maximum number of indexes is 119. This high-order byte has always been used in the Stat operation to return the file version and it can now be used to specify the file version in the Create operation without breaking any previous applications. The supported file versions for Create are 6.x, 7.x, 8.x, 9.x, 9.5 which are represented in the specified byte with hex values 0x50, 0x60, 0x70, 0x80, 0x90, 0x95, respectively.

Physical Page Size. (Offset 0xA). This field was previously marked "Not Used." This field is used in conjunction with the "Page Compression" file flag. If the "Page Compression" flag is not specified then this field is ignored.

In data files version 6.x and later, logical pages map to physical pages, stored in a Page Allocation Table (PAT). A physical page is exactly the same size as a logical page. Page compression can be used with file format 9.5 and later. Database pages are compressed at the page level. Each logical page is compressed into one or more physical page units. These individual physical pages are smaller in size than a logical page.

The physical page size field can be used to specify the physical page size to be used for the file. The value specified in this field is multiplied by 512 to determine the actual physical page size used. If zero is specified the engine will use a default value for the physical page size. The default value is 512 bytes.

The value specified for the physical page size cannot be larger than the value specified for the logical page size. If it is then the engine will round down the value specified for the physical page size so that it is the same as the logical page size. The logical page size needs to be an exact multiple of the physical page size. If it is not then the logical page size is rounded down so that it becomes an exact multiple of the physical page size. If, as a result of these manipulations, the logical and physical values end up to be the same, then page level compression will not turned on for this file. See also Creating a File with Page Level Compression in Pervasive PSQL Programmer's Guide

File Flags. (Offset 0x0A) The bit settings in the File Flags word specify file attributes. Table 2-2 shows the binary, hexadecimal, and decimal representations of the file flag values.

Table 2-2 File Flag Values 
Attribute
Constant
Binary
Hex
Decimal
Variable Length Records
VAR_RECS
0000 0000 0000 0001
1
1
Blank Truncation
BLANK_TRUNC
0000 0000 0000 0010
2
2
Page Preallocation
PRE_ALLOC
0000 0000 0000 0100
04
4
Data Compression
DATA_COMP
0000 0000 0000 1000
08
8
Key-Only File
KEY_ONLY
0000 0000 0001 0000
10
16
Balanced Index
BALANCED_KEYS
0000 0000 0010 0000
20
32
10% Free Space
FREE_10
0000 0000 0100 0000
40
64
20% Free Space
FREE_20
0000 0000 10 00 0000
80
128
30% Free Space
FREE_30
0000 0000 1100 0000
C0
192
Reserve Duplicate Pointers
DUP_PTRS
0000 0001 0000 0000
100
256
Include System Data1
INCLUDE_SYSTEM_DATA
0000 0010 0000 0000
200
512
Do Not Include System Data
NO_INCLUDE_SYSTEM_DATA
0001 0010 0000 0000
1200
4,608
Key Number
SPECIFY_KEY_NUMS
0000 0100 0000 0000
400
1,024
Use VATs
VATS_SUPPORT
0000 1000 0000 0000
800
2,048
Use Page Compression2
PAGE_COMPRESSED
0010 0000 0000 0000
2000
8,192
1If you do not explicitly specify whether to include system data in the file, the Btrieve API uses the current setting of the transactional interface configuration option "Include System Data."
2Only used with page level compression. Used in conjunction with the Physical Page Size key specification. See Pervasive PSQL Programmer's Guide Creating a File with Page Level Compression for more information.

Avoid using incompatible flags; flags are incompatible if they use the same bit positions. Unused bits are reserved for future use. Set them to 0.

To combine file attributes, add their respective File Flag values. For example, to specify a file that allows variable-length records and uses blank truncation, initialize the File Flags word to 3 (2 + 1). The transactional interface ignores the Blank Truncation and Free Space bits if the Variable Length Records bit is set to 0.

If you set the Page Preallocation bit, use the last 2 bytes in the file specification block (allocation) to store an integer value that specifies the number of pages to preallocate to the file. If you set the Data Compression bit, the transactional interface ignores the Variable Length Records bit. If you set the Key-only File bit, the transactional interface ignores the System Data bits.

The database engine automatically uses data compression on files that use system data and have a record length that exceeds the maximum length allowed. See Table 4-10 .

Set the Duplicate Pointers bit if you anticipate adding an index sometime after creating a file, and if that index has many duplicate values. Setting this bit causes the transactional interface to reserve space in each record of the file for pointers that link the duplicate values. By reserving this space, you can possibly lower retrieval time and save disk space, especially if the keys are long and you anticipate that many records will have duplicate key values.


Note
You can reserve duplicate pointer space only for indexes that are added after file creation. Therefore, when reserving space for pointers to duplicate values, do not include space for the indexes created during this Create operation. Also, the transactional interface does not reserve duplicate pointer space for any key you specify as a repeating-duplicatable key.

Set the Key Number bit if you need to assign a specific number to a key, and place the desired key number in the Manually Assigned Key Number element (offset 0x0E) of the Key Specification block. The transactional interface does not require consecutive key numbers; files can have gaps between key numbers. When a key is created, by default the transactional interface assigns the lowest available key number to that index (beginning with 0). However, some applications may require a key number different from the default assignment.

Set the Use VATs bit if the file uses Variable-tail Allocation Tables. To use VATs, a file must use variable-length records.

Number of Duplicate Pointers to Reserve. (Offset 0x0C) You can specify the number of duplicate pointers to reserve for each file. Use this element only if you specified the Reserve Duplicate Pointers file flag. For more information about duplicatable keys, refer to the following topic in Pervasive PSQL Programmer's Guide: Duplicatable Keys .

Allocation. (Offset 0x0E) You can specify the number of pages to preallocate. Use this element only if you specified the Page Preallocation file flag. For more information about page preallocation, see the following topic in Pervasive PSQL Programmer's Guide: Page Preallocation .

Key Specification Blocks

Place the key specification blocks immediately after the file specification. Allocate a 16-byte key specification block for each key segment in the file. Store the information for the Key Position and the Key Length as integers.

The maximum number of key segments allowed depends on the file's page size.

Page Size (bytes)
Maximum Key Segments byFile Version
8.x and prior
9.0
9.5
512
  8
  8
rounded up2
1,024
 23
 23
97
1,536
 24
 24
rounded up2
2,048
 54
 54
97
2,560
 54
 54
rounded up2
3,072
 54
 54
rounded up2
3,584
 54
 54
rounded up2
4,096
119
119
204 (or 119)3
8,192
n/a1
119
420 (or 119)3
16,384
n/a1
n/a1
420 (or 119)3
1"n/a" stands for "not applicable"
2"rounded up" means that the page size is rounded up to the next size supported by the file version. For example, 512 is rounded up to 1,024, 2,560 is rounded up to 4,096, and so forth.
3The maximum number of index segments that can be used with the relational interface is 119. For the transactional interface, the maximum number is 204 for a page size of 4,096, and 420 for page sizes 8,192 and 16,384.

See also the status codes 26: The number of keys specified is invalid and 29: The key length is invalid , both in Status Codes and Messages.

Key Position. (Offset 0x00) The key position is the byte offset at which the key or key segment begins. Positions are relative to 1; A key at the beginning of the record starts at position 1. There is no position 0.

Key Length. (Offset 0x02) The length of the key or key segment. The maximum length of a key, including all key segments, is 255 bytes.

Key Flags. (Offset 0x04) The bit settings in the Key Flags word specify key attributes. Table 2-3 shows the binary, hexadecimal, and decimal representations of the Key Flags values.

Table 2-3 Key Flag Values 
Attribute
Constant
Binary
Hex
Decimal
Duplicates allowed (linked duplicates is default, or combine with REPEAT_DUPS_KEY for repeating duplicates)
DUP
0000 0000 0000 0001
1
   1
Modifiable Key Values
MOD
0000 0000 0000 0010
2
   2
Use Old Style BINARY Data Type
BIN
0000 0000 0000 0100
4
   4
Use Old Style STRING Data Type (bits 2 and 8 must be 0)
 
0000 0000 0000 0000
0
0
Null Key (All Segments)
NUL
0000 0000 0000 1000
8
   8
Segmented Key
SEG
0000 0000 0001 0000
10
  16
Use Default ACS
ALT
0000 0000 0010 0000
20
  32
Use Numbered ACS in File
NUMBERED_ACS
0000 0100 0010 0000
420
1,056
Use Named ACS
NAMED_ACS
0000 1100 0010 0000
C20
3,104
Descending Sort Order
DESC_KEY
0000 0000 0100 0000
40
  64
Repeating Duplicates (combine with attribute DUP)
REPEAT_DUPS_KEY
0000 0000 1000 0000
80
 128
Use Extended Data Type
EXTTYPE_KEY
0000 0001 0000 0000
100
 256
Null Key (Any Segment)
MANUAL_KEY
0000 0010 0000 0000
200
 512
Case Insensitive Key
NOCASE_KEY
0000 0100 0000 0000
400
1,024

Avoid using incompatible flags; flags are incompatible if they use the same bit positions. Unused bits are reserved for future use. Set them to 0.

To combine key attributes, add their respective Key Flags values. For example, if the key is an extended type, part of a segmented key, and to be collated in descending order, initialize the Key Flags word to 336 (256 + 16 + 64).

The Segmented Key attribute indicates that the next key specification block in the Data Buffer refers to the next segment of the same key. Follow these rules for segmented keys:

ACSs are applicable only to STRING, LSTRING, and ZSTRING keys. You cannot define a key that is both case-insensitive and uses an ACS. In a file in which a key has an ACS designated for some segments but not for others, the segments that designate an ACS are sorted by the specified ACS; the segments with no ACSs are sorted according to their respective types.

Extended Data Type. (Offset 0x0A) You specify the Extended Data Type in byte 10 of the Key Specification block as a binary value. Table 2-4 shows the codes for the extended data types.

Table 2-4 Extended Data Types 
Type
Code
Type
Code
CHAR
0
ZSTRING
11
INTEGER
1
UNSIGNED BINARY
14
FLOAT
2
AUTOINCREMENT
15
DATE
3
NUMERICSTS
17
TIME
4
NUMERICSA
18
DECIMAL
5
CURRENCY
19
MONEY
6
TIMESTAMP
20
LOGICAL
7
WSTRING
25
NUMERIC
8
WZSTRING
26
BFLOAT
9
GUID
27
LSTRING
10
NULL INDICATOR SEGMENT
255

Extended data type codes 12, 13, 16, and 21 through 24 are reserved for future use.

You can define the STRING and UNSIGNED BINARY data types as either standard or extended types. This maintains compatibility with applications that were developed with earlier versions of Btrieve API, while allowing newer applications to use extended data types exclusively.

Regarding the data type you assign to a key, transactional interface does not ensure that the records you input adhere to the data types defined for the keys. For example, you could define a TIMESTAMP key in a file, but store a character string there. Your Btrieve API application would work fine, but an ODBC application that tries to access the same data using the ODBC TIMESTAMP format might fail, because the byte format could be different and the algorithms used to generate the timestamp value could be different. For complete descriptions of the data types, refer to SQL Engine Reference.

Null Value. (Offset 0x0B) of the Key Specification block represents an exclusion value for the key. If you have defined a key as a null key, you must supply a value for the transactional interface to recognize as the null value for each key segment. This is in reference to the legacy null and does not reflect true nulls. For a discussion of null support, see the following topic in Pervasive PSQL Programmer's Guide: Null Value .

Manually Assigned Key Number. (Offset 0x0E) The transactional interface allows an application to assign specific key numbers when creating a file with indexes. To manually assign key numbers to each index for a file, specify each key's number as a binary value in byte 14 of the key specification block, and set the Key Number bit (0x400) in the File Flags word.

Key numbers must be unique to the file and must be specified in ascending order, beginning with key 0. They must also be valid (less than the maximum number of keys for the file's page size).

The ability to manually assign key numbers complements to the ability to delete a key and not have the transactional interface renumber all keys that have a key number greater than the deleted key. For example, if an application drops an index and instructs the transactional interface not to renumber higher-numbered keys, and if a user then clones the affected file without assigning specific key numbers, the cloned file has different key numbers than the original.

ACS Number. (Offset 0x0F) For keys that use a specific ACS, offset 0x0F in the key specification block provides the ACS number by which to collate the key. The ACS number is based on its position in the Data Buffer. The first ACS following the last key specification block is ACS number 0. Following ACS 0 is ACS 1, which is followed by ACS 2, and so on.

Alternate Collating Sequence

Your application specifies ACSs one after the other immediately following the last key specification block in the Data Buffer.

User-Defined ACSs. To create an ACS that sorts string values differently from the ASCII standard, your application must place 265 bytes directly into the Data Buffer, using the format displayed in Table 2-5.

Table 2-5 Data Buffer for Creating a User-Defined ACS
Offset
Length
(Bytes)
Description
0
1
Signature byte. Specify 0xAC.
1
8
A unique 8-byte name that identifies the ACS to the transactional interface.
9
256
A 256-byte map. Each 1-byte position in the map corresponds to the code point having the same value as the position's offset in the map. The value of the byte at that position is the collating weight assigned to the code point. For example, to force code point 0x61 ('a') to sort with the same weight as code point 0x41 ('A'), place the same values at offsets 0x61 and 0x41.

For examples of user-defined ACSs, refer to the following topic in Pervasive PSQL Programmer's Guide: Alternate Collating Sequences .

International Sort Rules (ISRs). To specify an ISR, your application must place 265 bytes directly into the Data Buffer, using the following format:

Table 2-6 Data Buffer for Specifying an ISR ACS 
Offset
Length (Bytes)
Description
0
  1
Signature byte. Specify 0xAE.
1
   16
A unique 16-byte name that identifies the ISR table to the transactional interface. Refer to the Pervasive PSQL Programmer's Guide for a list of ISR table names.
17
248
Filler.

Data Buffer Length

The Data Buffer Length must be long enough to include the file specifications, the key specifications, and any ACSs that have been defined. Do not specify the file's record length in this parameter.

For example, to create a file that has two keys of one segment each and an ACS, the Data Buffer for the Create operation should be at least 313 bytes long, as follows:

File Spec
+
Key Spec
+
Key2 Spec
+
ACS
 
16
+
16
+
16+
+
265
= 313

Key Number

The Create operation's Key Number parameter is used to determine if the transactional interface warns you when a file of the same name already exists, and also to determine whether the transactional interface should use a local or remote engine when creating the file.

Use Table 2-7 to determine which value you should use for the Key Number parameter.


Note
The Create operation makes no distinction between workstation, workgroup, and server engines when you specify that the local engine should create the file.

Table 2-7 Key Number Parameter for Create Operation
CREATE Operation
No preference
Force local engine to create file
Force remote engine to create file
Normal create (overwrite file if it already exists)
0
6
99
Return Status 59 if file already exists
-1
7
100

Delete and Rename Subfunctions for the Create Operation

The Create operation has two additional subfunctions that you can use to delete or rename files.

In releases prior to Pervasive.SQL v8.5, it was always possible to manipulate transactional interface files through the operating system, because the transactional interface depended on the rights and privileges given by the operating system to the Pervasive PSQL user.

Now, if you have a secure Pervasive PSQL database, these operating system access rights may have been removed in the process of securing the database from unauthorized access. This makes programmatically deleting or moving the files difficult since the rights required are not always available.

The rename and delete subfunctions are implemented as Create operations with alternate key numbers. You do not need to provide a file specification as you do when creating a new data file. The following table shows how you set up the Create operation to use the rename or delete subfunctions.

Table 2-8 Create Operation Subfunctions
Function
Key Number to Use
Description
Place in Data Buffer
Place in Key Buffer
Rename File
-127
Rename an existing file in the data buffer to the name in the key buffer
Existing File Name
New File Name
Delete File
-128
Delete a file
N/A
Existing File Name

These subfunctions have been modified to work with the security model in that they will accept a URI in place of a file name in the key buffer and data buffer, if needed, to indicate a transactional interface file to delete or rename. This allows you to provide security information with the operation.

The security information is processed just like a normal Create or Open operation. The user must be authenticated and have DB_RIGHT_CREATE, DB_RIGHT_ALTER and DB_RIGHT_OPEN privileges for the existing files and for the directory where the new file will be located if applicable.

Table 2-9 Create Subfunctions - Use of URI Parameters in Key Buffer
Function
URI parameter file=
URI parameter dbfile=
URI parameter table=
Rename

Delete

Table 2-10 Create Subfunctions - Use of URI Parameters in Data Buffer
Function
file=
dbfile=
table=
Rename

Delete
Not applicable
Not applicable
Not applicable

Notes on Rename and Delete Subfunctions

Result

If the Create operation is successful, the transactional interface either warns you of the existence of a file with the same name or creates the new file according to your specifications. The new file does not contain any records. The Create operation does not open the file. Your application must perform an Open operation before it can access the file.

If the Create operation is unsuccessful, the transactional interface returns one of the following status codes:

2
The application encountered an I/O error.
11
The specified file name is invalid.
18
The disk is full.
22
The data buffer length is too short.
24
The page size or data buffer size is invalid.
25
The application cannot create the specified file.
26
The number of keys specified is invalid.
27
The key position is invalid.
28
The record length is invalid.
29
The key length is invalid.
48
The alternate collating sequence definition is invalid.
49
The extended data type is invalid.
59
The specified file already exists.
104
The transactional interface does not recognize the locale.
105
The file cannot be created with Variable-tail Allocation Tables (VATs).
134
The transactional interface cannot read the International Sorting Rule.
135
The specified International Sort Rule table is corrupt or otherwise invalid.

Positioning

The Create operation establishes no currency on the file.


Chapter contents
Book contents

Prev topic: Continuous Operation (42)
Next topic: Create Index (31)