00001 /****************************************************** 00002 Data types 00003 00004 (c) 1996 Innobase Oy 00005 00006 Created 1/16/1996 Heikki Tuuri 00007 *******************************************************/ 00008 00009 #ifndef data0type_h 00010 #define data0type_h 00011 00012 #include "univ.i" 00013 00014 extern ulint data_mysql_default_charset_coll; 00015 #define DATA_MYSQL_LATIN1_SWEDISH_CHARSET_COLL 8 00016 #define DATA_MYSQL_BINARY_CHARSET_COLL 63 00017 00018 /* SQL data type struct */ 00019 typedef struct dtype_struct dtype_t; 00020 00021 /* This variable is initialized as the standard binary variable length 00022 data type */ 00023 extern dtype_t* dtype_binary; 00024 00025 /*-------------------------------------------*/ 00026 /* The 'MAIN TYPE' of a column */ 00027 #define DATA_VARCHAR 1 /* character varying of the 00028 latin1_swedish_ci charset-collation; note 00029 that the MySQL format for this, DATA_BINARY, 00030 DATA_VARMYSQL, is also affected by whether the 00031 'precise type' contains 00032 DATA_MYSQL_TRUE_VARCHAR */ 00033 #define DATA_CHAR 2 /* fixed length character of the 00034 latin1_swedish_ci charset-collation */ 00035 #define DATA_FIXBINARY 3 /* binary string of fixed length */ 00036 #define DATA_BINARY 4 /* binary string */ 00037 #define DATA_BLOB 5 /* binary large object, or a TEXT type; 00038 if prtype & DATA_BINARY_TYPE == 0, then this is 00039 actually a TEXT column (or a BLOB created 00040 with < 4.0.14; since column prefix indexes 00041 came only in 4.0.14, the missing flag in BLOBs 00042 created before that does not cause any harm) */ 00043 #define DATA_INT 6 /* integer: can be any size 1 - 8 bytes */ 00044 #define DATA_SYS_CHILD 7 /* address of the child page in node pointer */ 00045 #define DATA_SYS 8 /* system column */ 00046 00047 /* Data types >= DATA_FLOAT must be compared using the whole field, not as 00048 binary strings */ 00049 00050 #define DATA_FLOAT 9 00051 #define DATA_DOUBLE 10 00052 #define DATA_DECIMAL 11 /* decimal number stored as an ASCII string */ 00053 #define DATA_VARMYSQL 12 /* any charset varying length char */ 00054 #define DATA_MYSQL 13 /* any charset fixed length char */ 00055 /* NOTE that 4.1.1 used DATA_MYSQL and 00056 DATA_VARMYSQL for all character sets, and the 00057 charset-collation for tables created with it 00058 can also be latin1_swedish_ci */ 00059 #define DATA_MTYPE_MAX 63 /* dtype_store_for_order_and_null_size() 00060 requires the values are <= 63 */ 00061 /*-------------------------------------------*/ 00062 /* The 'PRECISE TYPE' of a column */ 00063 /* 00064 Tables created by a MySQL user have the following convention: 00065 00066 - In the least significant byte in the precise type we store the MySQL type 00067 code (not applicable for system columns). 00068 00069 - In the second least significant byte we OR flags DATA_NOT_NULL, 00070 DATA_UNSIGNED, DATA_BINARY_TYPE. 00071 00072 - In the third least significant byte of the precise type of string types we 00073 store the MySQL charset-collation code. In DATA_BLOB columns created with 00074 < 4.0.14 we do not actually know if it is a BLOB or a TEXT column. Since there 00075 are no indexes on prefixes of BLOB or TEXT columns in < 4.0.14, this is no 00076 problem, though. 00077 00078 Note that versions < 4.1.2 or < 5.0.1 did not store the charset code to the 00079 precise type, since the charset was always the default charset of the MySQL 00080 installation. If the stored charset code is 0 in the system table SYS_COLUMNS 00081 of InnoDB, that means that the default charset of this MySQL installation 00082 should be used. 00083 00084 When loading a table definition from the system tables to the InnoDB data 00085 dictionary cache in main memory, InnoDB versions >= 4.1.2 and >= 5.0.1 check 00086 if the stored charset-collation is 0, and if that is the case and the type is 00087 a non-binary string, replace that 0 by the default charset-collation code of 00088 this MySQL installation. In short, in old tables, the charset-collation code 00089 in the system tables on disk can be 0, but in in-memory data structures 00090 (dtype_t), the charset-collation code is always != 0 for non-binary string 00091 types. 00092 00093 In new tables, in binary string types, the charset-collation code is the 00094 MySQL code for the 'binary charset', that is, != 0. 00095 00096 For binary string types and for DATA_CHAR, DATA_VARCHAR, and for those 00097 DATA_BLOB which are binary or have the charset-collation latin1_swedish_ci, 00098 InnoDB performs all comparisons internally, without resorting to the MySQL 00099 comparison functions. This is to save CPU time. 00100 00101 InnoDB's own internal system tables have different precise types for their 00102 columns, and for them the precise type is usually not used at all. 00103 */ 00104 00105 #define DATA_ENGLISH 4 /* English language character string: this 00106 is a relic from pre-MySQL time and only used 00107 for InnoDB's own system tables */ 00108 #define DATA_ERROR 111 /* another relic from pre-MySQL time */ 00109 00110 #define DATA_MYSQL_TYPE_MASK 255 /* AND with this mask to extract the MySQL 00111 type from the precise type */ 00112 #define DATA_MYSQL_TRUE_VARCHAR 15 /* MySQL type code for the >= 5.0.3 00113 format true VARCHAR */ 00114 00115 /* Precise data types for system columns and the length of those columns; 00116 NOTE: the values must run from 0 up in the order given! All codes must 00117 be less than 256 */ 00118 #define DATA_ROW_ID 0 /* row id: a dulint */ 00119 #define DATA_ROW_ID_LEN 6 /* stored length for row id */ 00120 00121 #define DATA_TRX_ID 1 /* transaction id: 6 bytes */ 00122 #define DATA_TRX_ID_LEN 6 00123 00124 #define DATA_ROLL_PTR 2 /* rollback data pointer: 7 bytes */ 00125 #define DATA_ROLL_PTR_LEN 7 00126 00127 #define DATA_MIX_ID 3 /* mixed index label: a dulint, stored in 00128 a row in a compressed form */ 00129 #define DATA_MIX_ID_LEN 9 /* maximum stored length for mix id (in a 00130 compressed dulint form) */ 00131 #define DATA_N_SYS_COLS 4 /* number of system columns defined above */ 00132 00133 /* Flags ORed to the precise data type */ 00134 #define DATA_NOT_NULL 256 /* this is ORed to the precise type when 00135 the column is declared as NOT NULL */ 00136 #define DATA_UNSIGNED 512 /* this id ORed to the precise type when 00137 we have an unsigned integer type */ 00138 #define DATA_BINARY_TYPE 1024 /* if the data type is a binary character 00139 string, this is ORed to the precise type: 00140 this only holds for tables created with 00141 >= MySQL-4.0.14 */ 00142 /* #define DATA_NONLATIN1 2048 This is a relic from < 4.1.2 and < 5.0.1. 00143 In earlier versions this was set for some 00144 BLOB columns. 00145 */ 00146 #define DATA_LONG_TRUE_VARCHAR 4096 /* this is ORed to the precise data 00147 type when the column is true VARCHAR where 00148 MySQL uses 2 bytes to store the data len; 00149 for shorter VARCHARs MySQL uses only 1 byte */ 00150 /*-------------------------------------------*/ 00151 00152 /* This many bytes we need to store the type information affecting the 00153 alphabetical order for a single field and decide the storage size of an 00154 SQL null*/ 00155 #define DATA_ORDER_NULL_TYPE_BUF_SIZE 4 00156 /* In the >= 4.1.x storage format we add 2 bytes more so that we can also 00157 store the charset-collation number; one byte is left unused, though */ 00158 #define DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE 6 00159 00160 /************************************************************************* 00161 Gets the MySQL type code from a dtype. */ 00162 UNIV_INLINE 00163 ulint 00164 dtype_get_mysql_type( 00165 /*=================*/ 00166 /* out: MySQL type code; this is NOT an InnoDB 00167 type code! */ 00168 dtype_t* type); /* in: type struct */ 00169 /************************************************************************* 00170 Determine how many bytes the first n characters of the given string occupy. 00171 If the string is shorter than n characters, returns the number of bytes 00172 the characters in the string occupy. */ 00173 00174 ulint 00175 dtype_get_at_most_n_mbchars( 00176 /*========================*/ 00177 /* out: length of the prefix, 00178 in bytes */ 00179 const dtype_t* dtype, /* in: data type */ 00180 ulint prefix_len, /* in: length of the requested 00181 prefix, in characters, multiplied by 00182 dtype_get_mbmaxlen(dtype) */ 00183 ulint data_len, /* in: length of str (in bytes) */ 00184 const char* str); /* in: the string whose prefix 00185 length is being determined */ 00186 /************************************************************************* 00187 Checks if a data main type is a string type. Also a BLOB is considered a 00188 string type. */ 00189 00190 ibool 00191 dtype_is_string_type( 00192 /*=================*/ 00193 /* out: TRUE if string type */ 00194 ulint mtype); /* in: InnoDB main data type code: DATA_CHAR, ... */ 00195 /************************************************************************* 00196 Checks if a type is a binary string type. Note that for tables created with 00197 < 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column. For 00198 those DATA_BLOB columns this function currently returns FALSE. */ 00199 00200 ibool 00201 dtype_is_binary_string_type( 00202 /*========================*/ 00203 /* out: TRUE if binary string type */ 00204 ulint mtype, /* in: main data type */ 00205 ulint prtype);/* in: precise type */ 00206 /************************************************************************* 00207 Checks if a type is a non-binary string type. That is, dtype_is_string_type is 00208 TRUE and dtype_is_binary_string_type is FALSE. Note that for tables created 00209 with < 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column. 00210 For those DATA_BLOB columns this function currently returns TRUE. */ 00211 00212 ibool 00213 dtype_is_non_binary_string_type( 00214 /*============================*/ 00215 /* out: TRUE if non-binary string type */ 00216 ulint mtype, /* in: main data type */ 00217 ulint prtype);/* in: precise type */ 00218 /************************************************************************* 00219 Sets a data type structure. */ 00220 UNIV_INLINE 00221 void 00222 dtype_set( 00223 /*======*/ 00224 dtype_t* type, /* in: type struct to init */ 00225 ulint mtype, /* in: main data type */ 00226 ulint prtype, /* in: precise type */ 00227 ulint len, /* in: length of type */ 00228 ulint prec); /* in: precision of type */ 00229 /************************************************************************* 00230 Copies a data type structure. */ 00231 UNIV_INLINE 00232 void 00233 dtype_copy( 00234 /*=======*/ 00235 dtype_t* type1, /* in: type struct to copy to */ 00236 dtype_t* type2); /* in: type struct to copy from */ 00237 /************************************************************************* 00238 Gets the SQL main data type. */ 00239 UNIV_INLINE 00240 ulint 00241 dtype_get_mtype( 00242 /*============*/ 00243 dtype_t* type); 00244 /************************************************************************* 00245 Gets the precise data type. */ 00246 UNIV_INLINE 00247 ulint 00248 dtype_get_prtype( 00249 /*=============*/ 00250 dtype_t* type); 00251 /************************************************************************* 00252 Gets the MySQL charset-collation code for MySQL string types. */ 00253 00254 ulint 00255 dtype_get_charset_coll_noninline( 00256 /*=============================*/ 00257 ulint prtype);/* in: precise data type */ 00258 /************************************************************************* 00259 Gets the MySQL charset-collation code for MySQL string types. */ 00260 UNIV_INLINE 00261 ulint 00262 dtype_get_charset_coll( 00263 /*===================*/ 00264 ulint prtype);/* in: precise data type */ 00265 /************************************************************************* 00266 Forms a precise type from the < 4.1.2 format precise type plus the 00267 charset-collation code. */ 00268 00269 ulint 00270 dtype_form_prtype( 00271 /*==============*/ 00272 ulint old_prtype, /* in: the MySQL type code and the flags 00273 DATA_BINARY_TYPE etc. */ 00274 ulint charset_coll); /* in: MySQL charset-collation code */ 00275 /************************************************************************* 00276 Gets the type length. */ 00277 UNIV_INLINE 00278 ulint 00279 dtype_get_len( 00280 /*==========*/ 00281 dtype_t* type); 00282 /************************************************************************* 00283 Gets the type precision. */ 00284 UNIV_INLINE 00285 ulint 00286 dtype_get_prec( 00287 /*===========*/ 00288 dtype_t* type); 00289 /************************************************************************* 00290 Gets the minimum length of a character, in bytes. */ 00291 UNIV_INLINE 00292 ulint 00293 dtype_get_mbminlen( 00294 /*===============*/ 00295 /* out: minimum length of a char, in bytes, 00296 or 0 if this is not a character type */ 00297 const dtype_t* type); /* in: type */ 00298 /************************************************************************* 00299 Gets the maximum length of a character, in bytes. */ 00300 UNIV_INLINE 00301 ulint 00302 dtype_get_mbmaxlen( 00303 /*===============*/ 00304 /* out: maximum length of a char, in bytes, 00305 or 0 if this is not a character type */ 00306 const dtype_t* type); /* in: type */ 00307 /************************************************************************* 00308 Gets the padding character code for the type. */ 00309 UNIV_INLINE 00310 ulint 00311 dtype_get_pad_char( 00312 /*===============*/ 00313 /* out: padding character code, or 00314 ULINT_UNDEFINED if no padding specified */ 00315 const dtype_t* type); /* in: type */ 00316 /*************************************************************************** 00317 Returns the size of a fixed size data type, 0 if not a fixed size type. */ 00318 UNIV_INLINE 00319 ulint 00320 dtype_get_fixed_size( 00321 /*=================*/ 00322 /* out: fixed size, or 0 */ 00323 dtype_t* type); /* in: type */ 00324 /*************************************************************************** 00325 Returns the minimum size of a data type. */ 00326 UNIV_INLINE 00327 ulint 00328 dtype_get_min_size( 00329 /*===============*/ 00330 /* out: minimum size */ 00331 const dtype_t* type); /* in: type */ 00332 /*************************************************************************** 00333 Returns the maximum size of a data type. Note: types in system tables may be 00334 incomplete and return incorrect information. */ 00335 00336 ulint 00337 dtype_get_max_size( 00338 /*===============*/ 00339 /* out: maximum size (ULINT_MAX for 00340 unbounded types) */ 00341 const dtype_t* type); /* in: type */ 00342 /*************************************************************************** 00343 Returns a stored SQL NULL size for a type. For fixed length types it is 00344 the fixed length of the type, otherwise 0. */ 00345 UNIV_INLINE 00346 ulint 00347 dtype_get_sql_null_size( 00348 /*====================*/ 00349 /* out: SQL null storage size */ 00350 dtype_t* type); /* in: type */ 00351 /*************************************************************************** 00352 Returns TRUE if a type is of a fixed size. */ 00353 UNIV_INLINE 00354 ibool 00355 dtype_is_fixed_size( 00356 /*================*/ 00357 /* out: TRUE if fixed size */ 00358 dtype_t* type); /* in: type */ 00359 /************************************************************************** 00360 Reads to a type the stored information which determines its alphabetical 00361 ordering and the storage size of an SQL NULL value. */ 00362 UNIV_INLINE 00363 void 00364 dtype_read_for_order_and_null_size( 00365 /*===============================*/ 00366 dtype_t* type, /* in: type struct */ 00367 byte* buf); /* in: buffer for the stored order info */ 00368 /************************************************************************** 00369 Stores for a type the information which determines its alphabetical ordering 00370 and the storage size of an SQL NULL value. This is the >= 4.1.x storage 00371 format. */ 00372 UNIV_INLINE 00373 void 00374 dtype_new_store_for_order_and_null_size( 00375 /*====================================*/ 00376 byte* buf, /* in: buffer for 00377 DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE 00378 bytes where we store the info */ 00379 dtype_t* type); /* in: type struct */ 00380 /************************************************************************** 00381 Reads to a type the stored information which determines its alphabetical 00382 ordering and the storage size of an SQL NULL value. This is the 4.1.x storage 00383 format. */ 00384 UNIV_INLINE 00385 void 00386 dtype_new_read_for_order_and_null_size( 00387 /*===================================*/ 00388 dtype_t* type, /* in: type struct */ 00389 byte* buf); /* in: buffer for stored type order info */ 00390 00391 /************************************************************************* 00392 Validates a data type structure. */ 00393 00394 ibool 00395 dtype_validate( 00396 /*===========*/ 00397 /* out: TRUE if ok */ 00398 dtype_t* type); /* in: type struct to validate */ 00399 /************************************************************************* 00400 Prints a data type structure. */ 00401 00402 void 00403 dtype_print( 00404 /*========*/ 00405 dtype_t* type); /* in: type */ 00406 00407 /* Structure for an SQL data type. 00408 If you add fields to this structure, be sure to initialize them everywhere. 00409 This structure is initialized in the following functions: 00410 dtype_set() 00411 dtype_read_for_order_and_null_size() 00412 dtype_new_read_for_order_and_null_size() 00413 sym_tab_add_null_lit() */ 00414 00415 struct dtype_struct{ 00416 ulint mtype; /* main data type */ 00417 ulint prtype; /* precise type; MySQL data type, charset code, 00418 flags to indicate nullability, signedness, 00419 whether this is a binary string, whether this 00420 is a true VARCHAR where MySQL uses 2 bytes to 00421 store the length */ 00422 00423 /* the remaining fields do not affect alphabetical ordering: */ 00424 00425 ulint len; /* length; for MySQL data this is 00426 field->pack_length(), except that for a 00427 >= 5.0.3 type true VARCHAR this is the 00428 maximum byte length of the string data 00429 (in addition to the string, MySQL uses 1 or 00430 2 bytes to store the string length) */ 00431 ulint prec; /* precision */ 00432 00433 ulint mbminlen; /* minimum length of a character, in bytes */ 00434 ulint mbmaxlen; /* maximum length of a character, in bytes */ 00435 }; 00436 00437 #ifndef UNIV_NONINL 00438 #include "data0type.ic" 00439 #endif 00440 00441 #endif
1.4.7

