The "padValue" property is used by the server as a byte value to pad all "char" and "binary" fields in a table when the contents of these fixed-length fields is shorter than the field size. This property is an optional integer with a default of 32.
All fixed-length fields in a table are padded with the same pad value.
Note Padding does not apply to the variable-length fields of the table, including
"varchar","lvarchar","varbinary", and"lvarbinary".
- Fixed-length fields are an optimization for the specific use case of storing fixed-length data that matches the exact length of the field.
- For fixed-length fields, the following use cases work well:
- Bit flags
- A
"binary"field is useful for efficiently storing a fixed set of bit flags where each bit represents a different on/off setting.
- A
- Legacy identifiers
- A
"char"field is useful for efficiently storing legacy fixed-length identifiers, such as a VIN number, where characters in specific positions represent different subsets of data.
- A
- Very small strings
- A
"char"field is useful for efficiently storing strings that are smaller than six characters because they do not have the overhead of requiring two extra bytes to track the length of the field.
- A
- Bit flags
- A fixed-length field is more efficient than a variable-length field because it does not require extra bytes to track the length of the field:
- The
"varchar"and"varbinary"fields require two extra bytes to track the length of the field, which can be up to 64K. - The
"lvarchar","lvarchar", and"lvarbinary"fields require four extra bytes to track the length of the field, which can be up to 2GB.
- The
- Padding is required for fixed-length fields because data can be assigned to a field with a shorter length than the field's length.
- Padding is useful in the following use cases:
- The padding value of
0in a"char"field makes a fixed-length string behave like a variable-length string. It works because the database can treat a fixed-length string as a null-terminated string that can be smaller than the fixed width. - A padding value of
32in a"char"field is the ASCII space character, which automatically left-aligns fixed-length strings. - The padding value of
0in a"binary"field works for bit flags when zero is the desired default state of a bit. Otherwise, padding in a"binary"field is rarely a good thing because it alters the binary value.
- The padding value of
- When the server assigns a value to a
"char"or"binary"field (which are fixed-length field types), it puts the value in the field starting on the left and uses the pad value to fill in any remaining bytes on the right. - In other words, when the number of bytes in a value being assigned to a
"char"or"binary"field is less than the number of bytes in the field, the server fills in the remaining bytes with the pad value.
- The pad value of
0causes a special padding behavior for"char"fields.- It causes the JSON DB API to treat fixed-length string fields as if they were variable-length strings with a maximum length.
- In other words, the same string value going into a
"char"field comes out the same, no padding is added — for example, when a table's"padValue"is set to0and a JSON string of"12"is inserted into a"char"field, the JSON string of"12"is returned from that field.
- When you want the server to add padding to strings that are stored in a
"char"field, set the"padValue"property to a non-zero number that is the ASCII character you want to pad the string.- For example, set
"padValue"to32to pad strings with the space character. When a table's"padValue"is set to32and a JSON string of"12"is inserted into a"char"field with a"length"of4bytes, the JSON string of"12 "is returned from that field.
- For example, set
- The server always applies padding when the number of bytes in a binary value is less than the number of bytes in the fixed-length,
"binary"field. When the server adds padding to a binary value, it permanently modifies the binary value. If this is not desirable, ensure the binary value exactly matches the size of the"binary"field. - The JSON DB API always returns the entire stored value of a
"binary"field. This binary value is encoded as a JSON array of bytes or a string containing a base64 or hexadecimal representation of the binary value. The length of the JSON string encoded in base64 or hexadecimal is always longer than the length of the binary field. - If a table's
"padValue"is set to0, and a binary value of0x3132is stored in a"binary"field with a"length"of4bytes, the server pads the value and stores the value of0x31320000into the field. When the value is returned by the JSON DB API, it is returned as0x31320000. Thus, padding alters a binary value when the value is smaller than the field. The default padding behavior of a"binary"field is different from the default padding behavior of a"char"field because the server can assume that anASCII NULLpadding value is a string terminator, but it cannot assume the same for a binary value.
If a table's "padValue" is set to 0, the ASCII NULL character with the hexadecimal value of 0x00 is used to pad unfilled byte positions. So when the JSON string "12" is inserted into a "char" field that has a "length" of 4 bytes, the binary value of 0x31320000 is stored in the field. The value returned is a JSON string of "12" because it recognizes the ASCII NULL character as a string terminator.
In other words, the ASCII NULL character allows the server to automatically trim the length of a string that is stored in a fixed-length field before it returns the string in JSON. So, by default, the same JSON string inserted into a "char" field is always returned by the JSON DB API. This behavior is safe for UTF-8 strings, which is the string encoding used by JSON.
When a table's "padValue" is set to 32, which is hexadecimal 0x20, and a JSON string of "12" is assigned to a "char" field that has a "length" of 4 bytes, the binary value of 0x31322020 is stored in the field. When the JSON DB API retrieves the value, it returns it as a JSON string of "12". This is because the value 32 is the ASCII space character " ".
If a table's "padValue" is set to 54, which is hexadecimal 0x36, and a JSON string of "12" is assigned to a "char" field that has a "length" of 4 bytes, the binary value of 0x31323636 is stored in the field. When the JSON DB API retrieves the value, it returns it as a JSON string of "12--". This is because the value 54 is the ASCII character "-".
Assigning "12**" to a "char" field that has a "length" of 4 bytes stores "12**" into the field regardless of the table's "padValue".
Assigning " $12.34" to a "char" field that has a "length" of 8 bytes stores " $12.34" into the field regardless of the table's "padValue".
Use the variable-length char and binary field types ("varchar", "lvarchar", "lvarchar", "varbinary", and "lvarbinary").
Each of these field types is stored in the table with length information, so the data returned matches the data stored.
Request example
{
"api": "db",
"action": "createTable",
"params": {
"padValue": 32
},
"authToken": "replaceWithAuthTokenFromCreateSession"
}