Commit 74ab94259af754c91f0f3512b753dbac27b0326e
1 parent
4ed10477
Adding a beginning of an implementation of MySQL client.
Showing
1 changed file
with
1480 additions
and
0 deletions
Show diff stats
| 1 | + | |
| 2 | + The Anubis Project. | |
| 3 | + | |
| 4 | + A MySQL client. | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + ////////////////// The MySQL Licence ////////////////////////////////////////////////// | |
| 13 | + | |
| 14 | + Licensing Notice | |
| 15 | + | |
| 16 | + The MySQL Protocol is proprietary. | |
| 17 | + | |
| 18 | + The MySQL Protocol is part of the MySQL Database Management System. As such, it falls | |
| 19 | + under the provisions of the GNU Public License (GPL). A copy of the GNU Public License | |
| 20 | + is available on MySQL's web site, and in the product download. | |
| 21 | + | |
| 22 | + Because this is a GPL protocol, any product which uses it to connect to a MySQL server, | |
| 23 | + or to emulate a MySQL server, or to interpose between any client and server which uses | |
| 24 | + the protocol, or for any similar purpose, is also bound by the GPL. Therefore if you | |
| 25 | + use this description to write a program, you must release your program as GPL. Contact | |
| 26 | + MySQL AB if you need clarification of these terms or if you need to ask about | |
| 27 | + alternative arrangements. | |
| 28 | + | |
| 29 | + /////////////////////////////////////////////////////////////////////////////////////// | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + --- That's all for the public part ! -------------------------------------------------- | |
| 38 | + | |
| 39 | +read tools/basis.anubis | |
| 40 | +read tools/words.anubis | |
| 41 | + | |
| 42 | + | |
| 43 | + *** [] Organization | |
| 44 | + | |
| 45 | + The topic is: the contents of logical packets in MySQL version 5.0 client/server | |
| 46 | + communication. | |
| 47 | + | |
| 48 | + The description is of logical packets. There will be only passing mention of | |
| 49 | + non-logical considerations, such as physical packets, transport, buffering, and | |
| 50 | + compression. If you are interested in those topics, you may wish to consult another | |
| 51 | + document: "MySQL Client - Server Protocol Documentation" in the file net_doc.txt in the | |
| 52 | + internals directory of the mysqldoc MySQL documentation repository. | |
| 53 | + | |
| 54 | + The description is of the version-5.0 protocol at the time of writing. Most of the | |
| 55 | + examples show version-4.1 tests, which is okay because the changes from version-4.1 to | |
| 56 | + version-5.0 were small. | |
| 57 | + | |
| 58 | + A typical description of a packet will include: | |
| 59 | + | |
| 60 | + "Bytes and Names". This is intended as a quick summary of the lengths and identifiers | |
| 61 | + for every field in the packet, in order of appearance. The "Bytes" column contains the | |
| 62 | + length in bytes. The Names column contains names which are taken from the MySQL source | |
| 63 | + code whenever possible. If the version-4.0 and version-4.1 formats differ | |
| 64 | + significantly, we will show both formats. | |
| 65 | + | |
| 66 | + Descriptions for each field. This contains text notes about the usage and possible | |
| 67 | + contents. | |
| 68 | + | |
| 69 | + (If necessary) notes about alternative terms. Naming in this document is not | |
| 70 | + authoritative and you will often see different words used for the same things, in other | |
| 71 | + documents. | |
| 72 | + | |
| 73 | + (If necessary) references to program or header files in the MySQL source code. An | |
| 74 | + example of such a reference is: sql/protocol.cc net_store_length() which means "in the | |
| 75 | + sql subdirectory, in the protocol.cc file, the function named net_store_length". | |
| 76 | + | |
| 77 | + An Example. All examples have three columns: | |
| 78 | + | |
| 79 | + -- the field name | |
| 80 | + -- a hexadecimal dump | |
| 81 | + -- an ascii dump, if the field has character data | |
| 82 | + | |
| 83 | + All spaces and carriage returns in the hexadecimal dump are there for formatting | |
| 84 | + purposes only. | |
| 85 | + | |
| 86 | + In the later sections, related to prepared statements, the notes should be considered | |
| 87 | + unreliable and there are no examples. | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + *** [] Tools. | |
| 94 | + | |
| 95 | + Reading a given number of bytes: | |
| 96 | + | |
| 97 | + | |
| 98 | +define Maybe(List(Word8)) | |
| 99 | + read_n_bytes_aux | |
| 100 | + ( | |
| 101 | + RWStream s, | |
| 102 | + List(Word8) so_far, // already read bytes (in reverse order) | |
| 103 | + Nat n // number of bytes still to be read | |
| 104 | + ) = | |
| 105 | + if n = 0 then success(reverse(so_far)) else | |
| 106 | + if *s is | |
| 107 | + { | |
| 108 | + failure then failure, | |
| 109 | + success(c) then read_n_bytes_aux(s,[c . so_far],n|-|1) | |
| 110 | + }. | |
| 111 | + | |
| 112 | +define Maybe(List(Word8)) | |
| 113 | + read_n_bytes | |
| 114 | + ( | |
| 115 | + RWStream s, | |
| 116 | + Nat n // number of bytes to read | |
| 117 | + ) = | |
| 118 | + read_n_bytes_aux(s,[],n). | |
| 119 | + | |
| 120 | + | |
| 121 | + Reading a small number of bytes: | |
| 122 | + | |
| 123 | +define Maybe((Word8,Word8)) | |
| 124 | + read_2_bytes | |
| 125 | + ( | |
| 126 | + RWStream s | |
| 127 | + ) = | |
| 128 | + if *s is | |
| 129 | + { | |
| 130 | + failure then failure, | |
| 131 | + success(b1) then if *s is | |
| 132 | + { | |
| 133 | + failure then failure, | |
| 134 | + success(b2) then success((b1,b2)) | |
| 135 | + } | |
| 136 | + }. | |
| 137 | + | |
| 138 | +define Maybe((Word8,Word8,Word8)) | |
| 139 | + read_3_bytes | |
| 140 | + ( | |
| 141 | + RWStream s | |
| 142 | + ) = | |
| 143 | + if *s is | |
| 144 | + { | |
| 145 | + failure then failure, | |
| 146 | + success(b1) then if *s is | |
| 147 | + { | |
| 148 | + failure then failure, | |
| 149 | + success(b2) then if *s is | |
| 150 | + { | |
| 151 | + failure then failure, | |
| 152 | + success(b3) then success((b1,b2,b3)) | |
| 153 | + } | |
| 154 | + } | |
| 155 | + }. | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + *** [] Elements | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + *** [] Null-Terminated String. | |
| 165 | + | |
| 166 | + Used for some variable-length character strings. The value '\0' (sometimes written | |
| 167 | + 0x00) denotes the end of the string. | |
| 168 | + | |
| 169 | +define Maybe(String) | |
| 170 | + read_null_terminated_string_aux | |
| 171 | + ( | |
| 172 | + RWStream s, | |
| 173 | + List(Word8) so_far | |
| 174 | + ) = | |
| 175 | + if *s is | |
| 176 | + { | |
| 177 | + failure then failure, | |
| 178 | + success(c) then | |
| 179 | + if c = 0 | |
| 180 | + then success(implode(reverse(so_far))) | |
| 181 | + else read_null_terminated_string_aux(s,[c . so_far]) | |
| 182 | + }. | |
| 183 | + | |
| 184 | +define Maybe(String) | |
| 185 | + read_null_terminated_string | |
| 186 | + ( | |
| 187 | + RWStream s | |
| 188 | + ) = | |
| 189 | + read_null_terminated_string_aux(s,[]). | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + *** [] Length Coded Binary. | |
| 196 | + | |
| 197 | + A variable-length number. To compute the value of a Length Coded Binary, one must | |
| 198 | + examine the value of its first byte. | |
| 199 | + | |
| 200 | + Value Of # Of Bytes Description | |
| 201 | + First Byte Following | |
| 202 | + ---------- ----------- ----------- | |
| 203 | + 0-250 0 = value of first byte | |
| 204 | + 251 0 column value = NULL | |
| 205 | + only appropriate in a Row Data Packet | |
| 206 | + 252 2 = value of following 16-bit word | |
| 207 | + 253 3 = value of following 24-bit word | |
| 208 | + 254 8 = value of following 64-bit word | |
| 209 | + | |
| 210 | + Thus the length of a Length Coded Binary, including the first byte, will vary from 1 to | |
| 211 | + 9 bytes. The relevant MySQL source program is sql/protocol.cc net_store_length(). | |
| 212 | + | |
| 213 | + All numbers are stored with least significants byte first. All numbers are unsigned. | |
| 214 | + | |
| 215 | + | |
| 216 | +type MySQL_Binary_Or_Null: | |
| 217 | + null, | |
| 218 | + binary(Nat). | |
| 219 | + | |
| 220 | + | |
| 221 | +define Maybe(MySQL_Binary_Or_Null) | |
| 222 | + read_length_coded_binary | |
| 223 | + ( | |
| 224 | + RWStream s | |
| 225 | + ) = | |
| 226 | + if *s is | |
| 227 | + { | |
| 228 | + failure then failure, | |
| 229 | + success(c) then | |
| 230 | + if c = 251 then success(null) else | |
| 231 | + if c = 252 then if read_n_bytes(s,2) is | |
| 232 | + { | |
| 233 | + failure then failure, | |
| 234 | + success(bytes) then success(binary(to_Nat(bytes))) | |
| 235 | + } else | |
| 236 | + if c = 253 then if read_n_bytes(s,3) is | |
| 237 | + { | |
| 238 | + failure then failure, | |
| 239 | + success(bytes) then success(binary(to_Nat(bytes))) | |
| 240 | + } else | |
| 241 | + if c = 254 then if read_n_bytes(s,8) is | |
| 242 | + { | |
| 243 | + failure then failure, | |
| 244 | + success(bytes) then success(binary(to_Nat(bytes))) | |
| 245 | + } else | |
| 246 | + if c = 255 then failure else | |
| 247 | + success(binary(to_Nat([c]))) | |
| 248 | + }. | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + Length Coded String: a variable-length string. Used instead of Null-Terminated String, | |
| 254 | + especially for character strings which might contain '\0' or might be very long. The | |
| 255 | + first part of a Length Coded String is a Length Coded Binary number (the length); the | |
| 256 | + second part of a Length Coded String is the actual data. An example of a short Length | |
| 257 | + Coded String is these three hexadecimal bytes: 02 61 62, which means "length = 2, | |
| 258 | + contents = 'ab'". | |
| 259 | + | |
| 260 | + | |
| 261 | +define Maybe(String) | |
| 262 | + read_length_coded_string | |
| 263 | + ( | |
| 264 | + RWStream s | |
| 265 | + ) = | |
| 266 | + if read_length_coded_binary(s) is | |
| 267 | + { | |
| 268 | + failure then failure, | |
| 269 | + success(r) then if r is | |
| 270 | + { | |
| 271 | + null then failure, | |
| 272 | + binary(n) then if read_n_bytes(s,n) is | |
| 273 | + { | |
| 274 | + failure then failure, | |
| 275 | + success(bytes) then success(implode(bytes)) | |
| 276 | + } | |
| 277 | + } | |
| 278 | + }. | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + *** [] The Packet Header. | |
| 286 | + | |
| 287 | + Bytes Name | |
| 288 | + ----- ---- | |
| 289 | + 3 Packet Length | |
| 290 | + 1 Packet Number | |
| 291 | + | |
| 292 | + Packet Length: The length, in bytes, of the packet | |
| 293 | + that follows the Packet Header. There | |
| 294 | + may be some special values in the most | |
| 295 | + significant byte. Since 2**24 = MB, | |
| 296 | + the maximum packet length is 16MB. | |
| 297 | + | |
| 298 | + Packet Number: A serial number which can be used to | |
| 299 | + ensure that all packets are present | |
| 300 | + and in order. The first packet of a | |
| 301 | + client query will have Packet Number = 0 | |
| 302 | + Thus, when a new SQL statement starts, | |
| 303 | + the packet number is re-initialised. | |
| 304 | + | |
| 305 | + | |
| 306 | + The Packet Header will not be shown in the descriptions of packets that follow this | |
| 307 | + section. Think of it as always there. But logically, it "precedes the packet" rather | |
| 308 | + than "is included in the packet". | |
| 309 | + | |
| 310 | + Alternative terms: Packet Length is also called "packetsize". Packet Number is also | |
| 311 | + called "Packet no". | |
| 312 | + | |
| 313 | + | |
| 314 | +type PacketHeader: | |
| 315 | + ph(Nat length, Word8 packet_number). | |
| 316 | + | |
| 317 | +define Maybe(PacketHeader) | |
| 318 | + read_packet_header | |
| 319 | + ( | |
| 320 | + RWStream s | |
| 321 | + ) = | |
| 322 | + if read_n_bytes(s,3) is | |
| 323 | + { | |
| 324 | + failure then failure, | |
| 325 | + success(l) then if *s is | |
| 326 | + { | |
| 327 | + failure then failure, | |
| 328 | + success(pn) then success(ph(to_Nat(l),pn)) | |
| 329 | + } | |
| 330 | + }. | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + *** [] Packet Types | |
| 338 | + | |
| 339 | + This is what happens in a typical session: | |
| 340 | + | |
| 341 | + The Handshake (when client connects): | |
| 342 | + Server Sends To Client: Handshake Initialisation Packet | |
| 343 | + Client Sends To Server: Client Authentication Packet | |
| 344 | + Server Sends To Client: OK Packet, or Error Packet | |
| 345 | + | |
| 346 | + The Commands (for every action the client wants the server to do): | |
| 347 | + Client Sends To Server: Command Packet | |
| 348 | + Server Sends To Client: OK Packet, or Error Packet, or Result Set Packet | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + In the rest of this chapter, you will find a description for each packet type, in | |
| 353 | + separate sections. | |
| 354 | + | |
| 355 | + Alternative terms: The Handshake is also called "client login" or "login procedure" or | |
| 356 | + "connecting". | |
| 357 | + | |
| 358 | + Handshake Initialization Packet | |
| 359 | + | |
| 360 | + From server to client during initial handshake. | |
| 361 | + | |
| 362 | + Bytes Name | |
| 363 | + ----- ---- | |
| 364 | + 1 protocol_version | |
| 365 | + n (Null-Terminated String) server_version | |
| 366 | + 4 thread_id | |
| 367 | + 8 scramble_buff | |
| 368 | + 1 (filler) always 0x00 | |
| 369 | + 2 server_capabilities | |
| 370 | + 1 server_language | |
| 371 | + 2 server_status | |
| 372 | + 13 (filler) always 0x00 ... | |
| 373 | + 13 rest of scramble_buff (4.1) | |
| 374 | + | |
| 375 | + protocol_version: The server takes this from PROTOCOL_VERSION | |
| 376 | + in /include/mysql_version.h. Example value = 10. | |
| 377 | + | |
| 378 | + server_version: The server takes this from MYSQL_SERVER_VERSION | |
| 379 | + in /include/mysql_version.h. Example value = "4.1.1-alpha". | |
| 380 | + | |
| 381 | + thread_number: ID of the server thread for this connection. | |
| 382 | + | |
| 383 | + scramble_buff: The password mechanism uses this. The second part are the | |
| 384 | + last 13 bytes. | |
| 385 | + (See "Password functions" section elsewhere in this document.) | |
| 386 | + | |
| 387 | + | |
| 388 | + server_capabilities: CLIENT_XXX options. The possible flag values at time of | |
| 389 | + writing (taken from include/mysql_com.h): | |
| 390 | + | |
| 391 | + CLIENT_LONG_PASSWORD 1 /* new more secure passwords */ | |
| 392 | + CLIENT_FOUND_ROWS 2 /* Found instead of affected rows */ | |
| 393 | + CLIENT_LONG_FLAG 4 /* Get all column flags */ | |
| 394 | + CLIENT_CONNECT_WITH_DB 8 /* One can specify db on connect */ | |
| 395 | + CLIENT_NO_SCHEMA 16 /* Don't allow database.table.column */ | |
| 396 | + CLIENT_COMPRESS 32 /* Can use compression protocol */ | |
| 397 | + CLIENT_ODBC 64 /* Odbc client */ | |
| 398 | + CLIENT_LOCAL_FILES 128 /* Can use LOAD DATA LOCAL */ | |
| 399 | + CLIENT_IGNORE_SPACE 256 /* Ignore spaces before '(' */ | |
| 400 | + CLIENT_PROTOCOL_41 512 /* New 4.1 protocol */ | |
| 401 | + CLIENT_INTERACTIVE 1024 /* This is an interactive client */ | |
| 402 | + CLIENT_SSL 2048 /* Switch to SSL after handshake */ | |
| 403 | + CLIENT_IGNORE_SIGPIPE 4096 /* IGNORE sigpipes */ | |
| 404 | + CLIENT_TRANSACTIONS 8192 /* Client knows about transactions */ | |
| 405 | + CLIENT_RESERVED 16384 /* Old flag for 4.1 protocol */ | |
| 406 | + CLIENT_SECURE_CONNECTION 32768 /* New 4.1 authentication */ | |
| 407 | + CLIENT_MULTI_STATEMENTS 65536 /* Enable/disable multi-stmt support */ | |
| 408 | + CLIENT_MULTI_RESULTS 131072 /* Enable/disable multi-results */ | |
| 409 | + | |
| 410 | + server_language: current server character set number | |
| 411 | + | |
| 412 | + server_status: SERVER_STATUS_xxx flags: e.g. SERVER_STATUS_AUTOCOMMIT | |
| 413 | + | |
| 414 | + | |
| 415 | + Alternative terms: Handshake Initialization Packet is also called "greeting | |
| 416 | + packet". Protocol version is also called "Prot. version". server_version is also called | |
| 417 | + "Server Version String". thread_number is also called "Thread Number". current server | |
| 418 | + charset number is also called "charset_no". scramble_buff is also called "crypt | |
| 419 | + seed". server_status is also called "SERVER_STATUS_xxx flags" or "Server status | |
| 420 | + variables". | |
| 421 | + | |
| 422 | + | |
| 423 | + Example Handshake Initialization Packet | |
| 424 | + | |
| 425 | + Hexadecimal ASCII | |
| 426 | + ----------- ----- | |
| 427 | + protocol_version 0a . | |
| 428 | + server_version 34 2e 31 2e 31 2d 71 6c 4.1.1-al | |
| 429 | + 70 68 61 2d 64 65 62 75 pha-debu | |
| 430 | + 67 00 g. | |
| 431 | + thread_number 01 00 00 00 .... | |
| 432 | + scramble_buff 3a 23 3d 4b 43 4a 2e 43 ........ | |
| 433 | + (filler) 00 . | |
| 434 | + server_capabilities 2c 82 .. | |
| 435 | + server_language 08 . | |
| 436 | + server_status 02 00 .. | |
| 437 | + (filler) 00 00 00 00 00 00 00 00 ........ | |
| 438 | + 00 00 00 00 00 | |
| 439 | + | |
| 440 | + In the example, the server is telling the client that its server_capabilities include | |
| 441 | + CLIENT_MULTI_RESULTS, CLIENT_SSL, CLIENT_COMPRESS, CLIENT_CONNECT_WITH_DB, | |
| 442 | + CLIENT_FOUND_ROWS. | |
| 443 | + | |
| 444 | + | |
| 445 | + The "server_language" (or "charset") corresponds to the character_set_server variable | |
| 446 | + in the MySQL server. This number also contains the collation used. Technically this | |
| 447 | + number determines the collation and the character set is implicit for the | |
| 448 | + collation. You can use the following SQL statement to get the cleartext information: | |
| 449 | + | |
| 450 | + mysql> SELECT CHARACTER_SET_NAME, COLLATION_NAME | |
| 451 | + -> FROM INFORMATION_SCHEMA.COLLATIONS | |
| 452 | + -> WHERE ID=8; | |
| 453 | + +--------------------+-------------------+ | |
| 454 | + | CHARACTER_SET_NAME | COLLATION_NAME | | |
| 455 | + +--------------------+-------------------+ | |
| 456 | + | latin1 | latin1_swedish_ci | | |
| 457 | + +--------------------+-------------------+ | |
| 458 | + 1 row in set (0,00 sec) | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | +type ServerCap: | |
| 464 | + server_cap | |
| 465 | + ( | |
| 466 | + Bool client_long_password, | |
| 467 | + Bool client_found_rows, | |
| 468 | + Bool client_long_flag, | |
| 469 | + Bool client_connect_with_db, | |
| 470 | + Bool client_no_schema, | |
| 471 | + Bool client_compress, | |
| 472 | + Bool client_odbc, | |
| 473 | + Bool client_local_file, | |
| 474 | + Bool client_ignore_space, | |
| 475 | + Bool client_protocol_41, | |
| 476 | + Bool client_interactive, | |
| 477 | + Bool client_ssl, | |
| 478 | + Bool client_ignore_sigpipe, | |
| 479 | + Bool client_transactions, | |
| 480 | + Bool client_reserved, | |
| 481 | + Bool client_secure_connection | |
| 482 | + ). | |
| 483 | + | |
| 484 | + | |
| 485 | +define ServerCap | |
| 486 | + to_server_cap | |
| 487 | + ( | |
| 488 | + Word8 first, | |
| 489 | + Word8 second | |
| 490 | + ) = | |
| 491 | + server_cap | |
| 492 | + ( | |
| 493 | + (first & 1) /= 0, | |
| 494 | + (first & 2) /= 0, | |
| 495 | + (first & 4) /= 0, | |
| 496 | + (first & 8) /= 0, | |
| 497 | + (first & 16) /= 0, | |
| 498 | + (first & 32) /= 0, | |
| 499 | + (first & 64) /= 0, | |
| 500 | + (first & 128) /= 0, | |
| 501 | + (second & 1) /= 0, | |
| 502 | + (second & 2) /= 0, | |
| 503 | + (second & 4) /= 0, | |
| 504 | + (second & 8) /= 0, | |
| 505 | + (second & 16) /= 0, | |
| 506 | + (second & 32) /= 0, | |
| 507 | + (second & 64) /= 0, | |
| 508 | + (second & 128) /= 0 | |
| 509 | + ). | |
| 510 | + | |
| 511 | +type ServerStatus: | |
| 512 | + server_stat | |
| 513 | + ( | |
| 514 | + Bool gaga | |
| 515 | + ). | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | +define ServerStatus | |
| 520 | + to_server_stat | |
| 521 | + ( | |
| 522 | + Word8 first, | |
| 523 | + Word8 second | |
| 524 | + ) = | |
| 525 | + server_stat | |
| 526 | + ( | |
| 527 | + false | |
| 528 | + ). | |
| 529 | + | |
| 530 | +to do: Complete type 'ServerStatus' in file 'mysql_client.anubis'. | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | +type Handshake: | |
| 535 | + handshake | |
| 536 | + ( | |
| 537 | + Word8 protocol_version, | |
| 538 | + String server_version, | |
| 539 | + Nat thread_id, | |
| 540 | + ByteArray scramble_buff, | |
| 541 | + ServerCap server_capabilities, | |
| 542 | + Word8 server_language, | |
| 543 | + ServerStatus server_status | |
| 544 | + ). | |
| 545 | + | |
| 546 | + | |
| 547 | +define ByteArray | |
| 548 | + put_bytes | |
| 549 | + ( | |
| 550 | + List(Word8) bytes, | |
| 551 | + ByteArray result, | |
| 552 | + Int32 i | |
| 553 | + ) = | |
| 554 | + if bytes is | |
| 555 | + { | |
| 556 | + [ ] then result, | |
| 557 | + [b1 . others] then | |
| 558 | + forget(put(result,i,b1)); | |
| 559 | + put_bytes(others,result,i+1) | |
| 560 | + }. | |
| 561 | + | |
| 562 | + | |
| 563 | +define ByteArray | |
| 564 | + to_byte_array | |
| 565 | + ( | |
| 566 | + List(Word8) bytes | |
| 567 | + ) = | |
| 568 | + with n = length(bytes), | |
| 569 | + result = constant_byte_array(n,0), | |
| 570 | + put_bytes(bytes,result,0). | |
| 571 | + | |
| 572 | + | |
| 573 | +define Maybe(Handshake) | |
| 574 | + read_hanshake | |
| 575 | + ( | |
| 576 | + RWStream s | |
| 577 | + ) = | |
| 578 | + if *s is | |
| 579 | + { | |
| 580 | + failure then failure, | |
| 581 | + success(protocol_version) then if read_null_terminated_string(s) is | |
| 582 | + { | |
| 583 | + failure then failure, | |
| 584 | + success(server_version) then if read_n_bytes(s,4) is | |
| 585 | + { | |
| 586 | + failure then failure, | |
| 587 | + success(thread_id_bytes) then if read_n_bytes(s,8) is | |
| 588 | + { | |
| 589 | + failure then failure, | |
| 590 | + success(scramble1_bytes) then if *s is | |
| 591 | + { | |
| 592 | + failure then failure, | |
| 593 | + success(filler1) then if read_2_bytes(s) is | |
| 594 | + { | |
| 595 | + failure then failure, | |
| 596 | + success(server_cap_bytes) then if *s is | |
| 597 | + { | |
| 598 | + failure then failure, | |
| 599 | + success(server_lang) then if read_2_bytes(s) is | |
| 600 | + { | |
| 601 | + failure then failure, | |
| 602 | + success(server_stat_bytes) then if read_n_bytes(s,13) is | |
| 603 | + { | |
| 604 | + failure then failure, | |
| 605 | + success(filler2) then if read_n_bytes(s,13) is | |
| 606 | + { | |
| 607 | + failure then failure, | |
| 608 | + success(scramble2_bytes) then | |
| 609 | + success(handshake | |
| 610 | + ( | |
| 611 | + protocol_version, | |
| 612 | + server_version, | |
| 613 | + to_Nat(thread_id_bytes), | |
| 614 | + to_byte_array(scramble1_bytes+scramble2_bytes), | |
| 615 | + if server_cap_bytes is (b1,b2) then to_server_cap(b1,b2), | |
| 616 | + server_lang, | |
| 617 | + if server_stat_bytes is (b1,b2) then to_server_stat(b1,b2) | |
| 618 | + )) | |
| 619 | + } | |
| 620 | + } | |
| 621 | + } | |
| 622 | + } | |
| 623 | + } | |
| 624 | + } | |
| 625 | + } | |
| 626 | + } | |
| 627 | + } | |
| 628 | + }. | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + *** [] Client Authentication Packet. | |
| 637 | + | |
| 638 | + From client to server during initial handshake. | |
| 639 | + | |
| 640 | + VERSION 4.0 | |
| 641 | + Bytes Name | |
| 642 | + ----- ---- | |
| 643 | + 2 client_flags | |
| 644 | + 3 max_packet_size | |
| 645 | + n (Null-Terminated String) user | |
| 646 | + 8 scramble_buff | |
| 647 | + 1 (filler) always 0x00 | |
| 648 | + | |
| 649 | + VERSION 4.1 | |
| 650 | + Bytes Name | |
| 651 | + ----- ---- | |
| 652 | + 4 client_flags | |
| 653 | + 4 max_packet_size | |
| 654 | + 1 charset_number | |
| 655 | + 23 (filler) always 0x00... | |
| 656 | + n (Null-Terminated String) user | |
| 657 | + n (Length Coded Binary) scramble_buff (1 + x bytes) | |
| 658 | + 1 (filler) always 0x00 | |
| 659 | + n (Null-Terminated String) databasename | |
| 660 | + | |
| 661 | + client_flags: CLIENT_xxx options. The list of possible flag | |
| 662 | + values is in the description of the Handshake | |
| 663 | + Initialisation Packet, for server_capabilities. | |
| 664 | + For some of the bits, the server passed "what | |
| 665 | + it's capable of". The client leaves some of the | |
| 666 | + bits on, adds others, and passes back to the server. | |
| 667 | + One important flag is: whether compression is desired. | |
| 668 | + | |
| 669 | + max_packet_size: the maximum number of bytes in a packet for the client | |
| 670 | + | |
| 671 | + charset_number: in the same domain as the server_language field that | |
| 672 | + the server passes in the Handshake Initialization packet. | |
| 673 | + | |
| 674 | + user: identification | |
| 675 | + | |
| 676 | + scramble_buff: the password, after encrypting using the scramble_buff | |
| 677 | + contents passed by the server (see "Password functions" | |
| 678 | + section elsewhere in this document) | |
| 679 | + if length is zero, no password was given | |
| 680 | + | |
| 681 | + databasename: name of schema to use initially | |
| 682 | + | |
| 683 | + | |
| 684 | + The scramble_buff and databasename fields are optional. | |
| 685 | + | |
| 686 | + Alternative terms: "Client authentication packet" is sometimes called "client auth | |
| 687 | + response" or "client auth packet" or "login packet". "Scramble_buff" is sometimes | |
| 688 | + called "crypted password". | |
| 689 | + | |
| 690 | + | |
| 691 | + Example Client Authentication Packet | |
| 692 | + Hexadecimal ASCII | |
| 693 | + ----------- ----- | |
| 694 | + client_flags 85 a6 03 00 .... | |
| 695 | + max_packet_size 00 00 00 01 .... | |
| 696 | + charset_number 08 . | |
| 697 | + (filler) 00 00 00 00 00 00 00 00 ........ | |
| 698 | + 00 00 00 00 00 00 00 00 ........ | |
| 699 | + 00 00 00 00 00 00 00 ....... | |
| 700 | + user 70 67 75 6c 75 74 7a 61 pgulutza | |
| 701 | + 6e 00 n. | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + *** [] Password functions. | |
| 709 | + | |
| 710 | + The Server Initialization Packet and the Client Authentication Packet both have an | |
| 711 | + 8-byte field, scramble_buff. The value in this field is used for password | |
| 712 | + authentication. It works thus: | |
| 713 | + | |
| 714 | + The server sends a random string to the client, in scramble_buff. | |
| 715 | + The client encrypts the scramble_buff value using the password that the user | |
| 716 | + enters. This happens in sql/password.c:scramble() function. | |
| 717 | + The client sends the encrypted scramble_buff value to the server. | |
| 718 | + The server encrypts the original random string using a value in the mysql | |
| 719 | + database, mysql.user.Password. | |
| 720 | + The server compares its encrypted random string to what the client sent | |
| 721 | + in scramble_buff. | |
| 722 | + If they are the same, the password is okay. | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + *** [] Command Packet. | |
| 731 | + | |
| 732 | + From client to server whenever the client wants the server to do something. | |
| 733 | + | |
| 734 | + Bytes Name | |
| 735 | + ----- ---- | |
| 736 | + 1 command | |
| 737 | + n arg | |
| 738 | + | |
| 739 | + command: The most common value is 03 COM_QUERY, because | |
| 740 | + INSERT UPDATE DELETE SELECT etc. have this code. | |
| 741 | + The possible values at time of writing (taken | |
| 742 | + from /include/mysql_com.h for enum_server_command) are: | |
| 743 | + | |
| 744 | + # Name Associated client function | |
| 745 | + - ---- -------------------------- | |
| 746 | + | |
| 747 | + 0x00 COM_SLEEP (none, this is an internal thread state) | |
| 748 | + 0x01 COM_QUIT mysql_close | |
| 749 | + 0x02 COM_INIT_DB mysql_select_db | |
| 750 | + 0x03 COM_QUERY mysql_real_query | |
| 751 | + 0x04 COM_FIELD_LIST mysql_list_fields | |
| 752 | + 0x05 COM_CREATE_DB mysql_create_db (deprecated) | |
| 753 | + 0x06 COM_DROP_DB mysql_drop_db (deprecated) | |
| 754 | + 0x07 COM_REFRESH mysql_refresh | |
| 755 | + 0x08 COM_SHUTDOWN mysql_shutdown | |
| 756 | + 0x09 COM_STATISTICS mysql_stat | |
| 757 | + 0x0a COM_PROCESS_INFO mysql_list_processes | |
| 758 | + 0x0b COM_CONNECT (none, this is an internal thread state) | |
| 759 | + 0x0c COM_PROCESS_KILL mysql_kill | |
| 760 | + 0x0d COM_DEBUG mysql_dump_debug_info | |
| 761 | + 0x0e COM_PING mysql_ping | |
| 762 | + 0x0f COM_TIME (none, this is an internal thread state) | |
| 763 | + 0x10 COM_DELAYED_INSERT (none, this is an internal thread state) | |
| 764 | + 0x11 COM_CHANGE_USER mysql_change_user | |
| 765 | + 0x12 COM_BINLOG_DUMP (used by slave server / mysqlbinlog) | |
| 766 | + 0x13 COM_TABLE_DUMP (used by slave server to get master table) | |
| 767 | + 0x14 COM_CONNECT_OUT (used by slave to log connection to master) | |
| 768 | + 0x15 COM_REGISTER_SLAVE (used by slave to register to master) | |
| 769 | + 0x16 COM_STMT_PREPARE mysql_stmt_prepare | |
| 770 | + 0x17 COM_STMT_EXECUTE mysql_stmt_execute | |
| 771 | + 0x18 COM_STMT_SEND_LONG_DATA mysql_stmt_send_long_data | |
| 772 | + 0x19 COM_STMT_CLOSE mysql_stmt_close | |
| 773 | + 0x1a COM_STMT_RESET mysql_stmt_reset | |
| 774 | + 0x1b COM_SET_OPTION mysql_set_server_option | |
| 775 | + 0x1c COM_STMT_FETCH mysql_stmt_fetch | |
| 776 | + | |
| 777 | + arg: The text of the command is just the way the user typed it, there is no processing | |
| 778 | + by the client (except removal of the final ';'). | |
| 779 | + This field is not a null-terminated string; however, | |
| 780 | + the size can be calculated from the packet size, | |
| 781 | + and the MySQL client appends '\0' when receiving. | |
| 782 | + | |
| 783 | + | |
| 784 | + The command byte is stored in the thd structure for the MySQL worker threads and is | |
| 785 | + shown in the Command column for SHOW PROCESSLIST. An inactive thread gets 0x00 | |
| 786 | + (Sleep). The dedicated thread to execute INSERT DELAYED gets 0x10. | |
| 787 | + | |
| 788 | + The replication requests (0x12 .. 0x15) cannot be send from regular clients, only from | |
| 789 | + another server or from the mysqlbinlog program. | |
| 790 | + | |
| 791 | + | |
| 792 | + Example Command Packet | |
| 793 | + | |
| 794 | + Hexadecimal ASCII | |
| 795 | + ----------- ----- | |
| 796 | + command 02 . | |
| 797 | + arg 74 65 73 74 test | |
| 798 | + | |
| 799 | + In the example, the value 02 in the command field stands for COM_INIT_DB. This is the | |
| 800 | + packet that the client puts together for "use test;". | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + *** [] Types Of Result Packets. | |
| 809 | + | |
| 810 | + A "result packet" is a packet that goes from the server to the client in response to a | |
| 811 | + Client Authentication Packet or Command Packet. To distinguish between the types of | |
| 812 | + result packets, a client must look at the first byte in the packet. We will call this | |
| 813 | + byte "field_count" in the description of each individual package, although it goes by | |
| 814 | + several names. | |
| 815 | + | |
| 816 | + Type Of Result Packet Hexadecimal Value Of First Byte (field_count) | |
| 817 | + --------------------- --------------------------------------------- | |
| 818 | + | |
| 819 | + OK Packet 00 | |
| 820 | + Error Packet ff | |
| 821 | + Result Set Packet 1-250 (first byte of Length-Coded Binary) | |
| 822 | + Field Packet 1-250 ("") | |
| 823 | + Row Data Packet 1-250 ("") | |
| 824 | + EOF Packet fe | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + *** [] OK Packet. | |
| 833 | + | |
| 834 | + From server to client in response to command, if no error and no result set. | |
| 835 | + | |
| 836 | + VERSION 4.0 | |
| 837 | + Bytes Name | |
| 838 | + ----- ---- | |
| 839 | + 1 (Length Coded Binary) field_count, always = 0 | |
| 840 | + 1-9 (Length Coded Binary) affected_rows | |
| 841 | + 1-9 (Length Coded Binary) insert_id | |
| 842 | + 2 server_status | |
| 843 | + n (until end of packet) message | |
| 844 | + | |
| 845 | + VERSION 4.1 | |
| 846 | + Bytes Name | |
| 847 | + ----- ---- | |
| 848 | + 1 (Length Coded Binary) field_count, always = 0 | |
| 849 | + 1-9 (Length Coded Binary) affected_rows | |
| 850 | + 1-9 (Length Coded Binary) insert_id | |
| 851 | + 2 server_status | |
| 852 | + 2 warning_count | |
| 853 | + n (until end of packet) message | |
| 854 | + | |
| 855 | + field_count: always = 0 | |
| 856 | + | |
| 857 | + affected_rows: = number of rows affected by INSERT/UPDATE/DELETE | |
| 858 | + | |
| 859 | + insert_id: If the statement generated any AUTO_INCREMENT number, | |
| 860 | + it is returned here. Otherwise this field contains 0. | |
| 861 | + Note: when using for example a multiple row INSERT the | |
| 862 | + insert_id will be from the first row inserted, not from | |
| 863 | + last. | |
| 864 | + | |
| 865 | + server_status: = The client can use this to check if the | |
| 866 | + command was inside a transaction. | |
| 867 | + | |
| 868 | + warning_count: number of warnings | |
| 869 | + | |
| 870 | + message: For example, after a multi-line INSERT, message might be | |
| 871 | + "Records: 3 Duplicates: 0 Warnings: 0" | |
| 872 | + | |
| 873 | + | |
| 874 | + The message field is optional. | |
| 875 | + | |
| 876 | + Alternative terms: OK Packet is also known as "okay packet" or "ok packet" or | |
| 877 | + "OK-Packet". field_count is also known as "number of rows" or "marker for ok | |
| 878 | + packet". message is also known as "Messagetext". OK Packets (and result set packets) | |
| 879 | + are also called "Result packets". | |
| 880 | + | |
| 881 | + | |
| 882 | + Example OK Packet | |
| 883 | + | |
| 884 | + Hexadecimal ASCII | |
| 885 | + ----------- ----- | |
| 886 | + field_count 00 . | |
| 887 | + affected_rows 01 . | |
| 888 | + insert_id 00 . | |
| 889 | + server_status 02 00 .. | |
| 890 | + warning_count 00 00 .. | |
| 891 | + | |
| 892 | + In the example, the optional message field is missing (the client can determine this by | |
| 893 | + examining the packet length). This is a packet that the server returns after a | |
| 894 | + successful INSERT of a single row that contains no auto_increment columns. | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + *** [] Error Packet. | |
| 903 | + | |
| 904 | + From server to client in response to command, if error. | |
| 905 | + | |
| 906 | + VERSION 4.0 | |
| 907 | + Bytes Name | |
| 908 | + ----- ---- | |
| 909 | + 1 field_count, always = 0xff | |
| 910 | + 2 errno | |
| 911 | + n message | |
| 912 | + | |
| 913 | + VERSION 4.1 | |
| 914 | + Bytes Name | |
| 915 | + ----- ---- | |
| 916 | + 1 field_count, always = 0xff | |
| 917 | + 2 errno | |
| 918 | + 1 (sqlstate marker), always '#' | |
| 919 | + 5 sqlstate (5 characters) | |
| 920 | + n message | |
| 921 | + | |
| 922 | + field_count: Always 0xff (255 decimal). | |
| 923 | + | |
| 924 | + errno: The possible values are listed in the manual, and in | |
| 925 | + the MySQL source code file /include/mysqld_error.h. | |
| 926 | + | |
| 927 | + sqlstate marker: This is always '#'. It is necessary for distinguishing | |
| 928 | + version-4.1 messages. | |
| 929 | + | |
| 930 | + sqlstate: The server translates errno values to sqlstate values | |
| 931 | + with a function named mysql_errno_to_sqlstate(). The | |
| 932 | + possible values are listed in the manual, and in the | |
| 933 | + MySQL source code file /include/sql_state.h. | |
| 934 | + | |
| 935 | + message: The error message is a string which ends at the end of | |
| 936 | + the packet, that is, its length can be determined from | |
| 937 | + the packet header. The MySQL client (in the my_net_read() | |
| 938 | + function) always adds '\0' to a packet, so the message | |
| 939 | + may appear to be a Null-Terminated String. | |
| 940 | + Expect the message to be between 0 and 512 bytes long. | |
| 941 | + | |
| 942 | + | |
| 943 | + Alternative terms: field_count is also known as "Status code" or "Error Packet | |
| 944 | + marker". errno is also known as "Error Number" or "Error Code". | |
| 945 | + | |
| 946 | + | |
| 947 | + Example of Error Packet | |
| 948 | + | |
| 949 | + Hexadecimal ASCII | |
| 950 | + ----------- ----- | |
| 951 | + field_count ff . | |
| 952 | + errno 1b 04 .. | |
| 953 | + (sqlstate marker) 23 # | |
| 954 | + sqlstate 34 32 53 30 32 42S02 | |
| 955 | + message 55 63 6b 6e 6f 77 6e 20 Unknown | |
| 956 | + 74 61 62 6c 6c 65 20 27 table ' | |
| 957 | + 71 27 q' | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + *** [] Result Set Header Packet. | |
| 966 | + | |
| 967 | + From server to client after command, if no error and result set -- that is, if the | |
| 968 | + command was a query which returned a result set. | |
| 969 | + | |
| 970 | + The Result Set Header Packet is the first of several, possibly many, packets that the | |
| 971 | + server sends for result sets. The order of packets for a result set is: | |
| 972 | + | |
| 973 | + (Result Set Header Packet) the number of columns | |
| 974 | + (Field Packets) column descriptors | |
| 975 | + (EOF Packet) marker: end of Field Packets | |
| 976 | + (Row Data Packets) row contents | |
| 977 | + (End Packet) marker: end of Data Packets | |
| 978 | + | |
| 979 | + Bytes Name | |
| 980 | + ----- ---- | |
| 981 | + 1-9 (Length-Coded-Binary) field_count | |
| 982 | + 1-9 (Length-Coded-Binary) extra | |
| 983 | + | |
| 984 | + field_count: See the section "Types Of Result Packets" | |
| 985 | + to see how one can distinguish the | |
| 986 | + first byte of field_count from the first | |
| 987 | + byte of an OK Packet, or other packet types. | |
| 988 | + | |
| 989 | + extra: For example, SHOW COLUMNS uses this to send | |
| 990 | + the number of rows in the table. | |
| 991 | + | |
| 992 | + | |
| 993 | + The "extra" field is optional and never appears for ordinary result sets. | |
| 994 | + | |
| 995 | + Alternative terms: a Result Set Packet is also called "a result packet for a command | |
| 996 | + returning rows" or "a field description packet". | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + Example of Result Set Header Packet | |
| 1002 | + | |
| 1003 | + Hexadecimal ASCII | |
| 1004 | + ----------- ----- | |
| 1005 | + field_count 03 . | |
| 1006 | + | |
| 1007 | + In the example, we se what the packet would contain after "SELECT * FROM t7" if table | |
| 1008 | + t7 has 3 columns. | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + *** [] Field Packet. | |
| 1017 | + | |
| 1018 | + From Server To Client, part of Result Set Packets. One for each column in the result | |
| 1019 | + set. Thus, if the value of field_columns in the Result Set Header Packet is 3, then the | |
| 1020 | + Field Packet occurs 3 times. | |
| 1021 | + | |
| 1022 | + VERSION 4.0 | |
| 1023 | + Bytes Name | |
| 1024 | + ----- ---- | |
| 1025 | + n (Length Coded String) table | |
| 1026 | + n (Length Coded String) name | |
| 1027 | + 4 (Length Coded Binary) length | |
| 1028 | + 2 (Length Coded Binary) type | |
| 1029 | + 2 (Length Coded Binary) flags | |
| 1030 | + 1 decimals | |
| 1031 | + n (Length Coded Binary) default | |
| 1032 | + | |
| 1033 | + VERSION 4.1 | |
| 1034 | + Bytes Name | |
| 1035 | + ----- ---- | |
| 1036 | + n (Length Coded String) catalog | |
| 1037 | + n (Length Coded String) db | |
| 1038 | + n (Length Coded String) table | |
| 1039 | + n (Length Coded String) org_table | |
| 1040 | + n (Length Coded String) name | |
| 1041 | + n (Length Coded String) org_name | |
| 1042 | + 1 (filler) | |
| 1043 | + 2 charsetnr | |
| 1044 | + 4 length | |
| 1045 | + 1 type | |
| 1046 | + 2 flags | |
| 1047 | + 1 decimals | |
| 1048 | + 2 (filler), always 0x00 | |
| 1049 | + n (Length Coded Binary) default | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + In practice, since identifiers are almost always 250 bytes or shorter, the Length Coded | |
| 1053 | + Strings look like: (1 byte for length of data) (data) | |
| 1054 | + | |
| 1055 | + catalog: Catalog. For 4.1, 5.0 and 5.1 the value is "def". | |
| 1056 | + db: Database identifier, also known as schema name. | |
| 1057 | + table: Table identifier, after AS clause (if any). | |
| 1058 | + org_table: Original table identifier, before AS clause (if any). | |
| 1059 | + name: Column identifier, after AS clause (if any). | |
| 1060 | + org_name: Column identifier, before AS clause (if any). | |
| 1061 | + charsetnr: Character set number. | |
| 1062 | + length: Length of column, according to the definition. | |
| 1063 | + Also known as "display length". The value given | |
| 1064 | + here may be larger than the actual length, for | |
| 1065 | + example an instance of a VARCHAR(2) column may | |
| 1066 | + have only 1 character in it. | |
| 1067 | + type: The code for the column's data type. Also known as | |
| 1068 | + "enum_field_type". The possible values at time of | |
| 1069 | + writing (taken from include/mysql_com.h), in hexadecimal: | |
| 1070 | + 0x00 FIELD_TYPE_DECIMAL | |
| 1071 | + 0x01 FIELD_TYPE_TINY | |
| 1072 | + 0x02 FIELD_TYPE_SHORT | |
| 1073 | + 0x03 FIELD_TYPE_LONG | |
| 1074 | + 0x04 FIELD_TYPE_FLOAT | |
| 1075 | + 0x05 FIELD_TYPE_DOUBLE | |
| 1076 | + 0x06 FIELD_TYPE_NULL | |
| 1077 | + 0x07 FIELD_TYPE_TIMESTAMP | |
| 1078 | + 0x08 FIELD_TYPE_LONGLONG | |
| 1079 | + 0x09 FIELD_TYPE_INT24 | |
| 1080 | + 0x0a FIELD_TYPE_DATE | |
| 1081 | + 0x0b FIELD_TYPE_TIME | |
| 1082 | + 0x0c FIELD_TYPE_DATETIME | |
| 1083 | + 0x0d FIELD_TYPE_YEAR | |
| 1084 | + 0x0e FIELD_TYPE_NEWDATE | |
| 1085 | + 0x0f FIELD_TYPE_VARCHAR (new in MySQL 5.0) | |
| 1086 | + 0x10 FIELD_TYPE_BIT (new in MySQL 5.0) | |
| 1087 | + 0xf6 FIELD_TYPE_NEWDECIMAL (new in MYSQL 5.0) | |
| 1088 | + 0xf7 FIELD_TYPE_ENUM | |
| 1089 | + 0xf8 FIELD_TYPE_SET | |
| 1090 | + 0xf9 FIELD_TYPE_TINY_BLOB | |
| 1091 | + 0xfa FIELD_TYPE_MEDIUM_BLOB | |
| 1092 | + 0xfb FIELD_TYPE_LONG_BLOB | |
| 1093 | + 0xfc FIELD_TYPE_BLOB | |
| 1094 | + 0xfd FIELD_TYPE_VAR_STRING | |
| 1095 | + 0xfe FIELD_TYPE_STRING | |
| 1096 | + 0xff FIELD_TYPE_GEOMETRY | |
| 1097 | + | |
| 1098 | + flags: The possible flag values at time of | |
| 1099 | + writing (taken from include/mysql_com.h), in hexadecimal: | |
| 1100 | + 0001 NOT_NULL_FLAG | |
| 1101 | + 0002 PRI_KEY_FLAG | |
| 1102 | + 0004 UNIQUE_KEY_FLAG | |
| 1103 | + 0008 MULTIPLE_KEY_FLAG | |
| 1104 | + 0010 BLOB_FLAG | |
| 1105 | + 0020 UNSIGNED_FLAG | |
| 1106 | + 0040 ZEROFILL_FLAG | |
| 1107 | + 0080 BINARY_FLAG | |
| 1108 | + 0100 ENUM_FLAG | |
| 1109 | + 0200 AUTO_INCREMENT_FLAG | |
| 1110 | + 0400 TIMESTAMP_FLAG | |
| 1111 | + 0800 SET_FLAG | |
| 1112 | + | |
| 1113 | + decimals: The number of positions after the decimal | |
| 1114 | + point if the type is DECIMAL or NUMERIC. | |
| 1115 | + Also known as "scale". | |
| 1116 | + default: For table definitions. Doesn't occur for | |
| 1117 | + normal result sets. See mysql_list_fields(). | |
| 1118 | + | |
| 1119 | + Alternative Terms: Field Packets are also called "Header Info Packets" or "field | |
| 1120 | + descriptor packets" (that's a better term but it's rarely used). In non-MySQL contexts | |
| 1121 | + Field Packets are more commonly known as "Result Set Metadata". | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + Example of Field Packet | |
| 1125 | + | |
| 1126 | + Hexadecimal ASCII | |
| 1127 | + ----------- ----- | |
| 1128 | + catalog 03 73 74 64 .std | |
| 1129 | + db 03 64 62 31 .db1 | |
| 1130 | + table 02 54 37 .T7 | |
| 1131 | + org_table 02 74 37 .t7 | |
| 1132 | + name 02 53 31 .S1 | |
| 1133 | + org_name 02 73 31 .s1 | |
| 1134 | + (filler) 0c . | |
| 1135 | + charsetnr 08 00 .. | |
| 1136 | + length 01 00 00 00 .... | |
| 1137 | + type fe . | |
| 1138 | + flags 00 00 .. | |
| 1139 | + decimals 00 . | |
| 1140 | + (filler) 00 00 .. | |
| 1141 | + | |
| 1142 | + In the example, we see what the server returns for "SELECT s1 AS S1 FROM t7 AS T7" | |
| 1143 | + where column s1 is defined as CHAR(1). | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + *** [] EOF Packet. | |
| 1152 | + | |
| 1153 | + From Server To Client, at the end of a series of Field Packets, and at the end of a | |
| 1154 | + series of Data Packets. With prepared statements, EOF Packet can also end parameter | |
| 1155 | + information, which we'll describe later. | |
| 1156 | + | |
| 1157 | + VERSION 4.0 | |
| 1158 | + Bytes Name | |
| 1159 | + ----- ---- | |
| 1160 | + 1 field_count, always = 0xfe | |
| 1161 | + | |
| 1162 | + VERSION 4.1 | |
| 1163 | + Bytes Name | |
| 1164 | + ----- ---- | |
| 1165 | + 1 field_count, always = 0xfe | |
| 1166 | + 2 warning_count | |
| 1167 | + 2 Status Flags | |
| 1168 | + | |
| 1169 | + field_count: The value is always 0xfe (decimal 254). | |
| 1170 | + However ... recall (from the | |
| 1171 | + section "Elements", above) that the value 254 can begin | |
| 1172 | + a Length-Encoded-Binary value which contains an 8-byte | |
| 1173 | + integer. So, to ensure that a packet is really an EOF | |
| 1174 | + Packet: (a) check that first byte in packet = 0xfe, (b) | |
| 1175 | + check that size of packet < 9. | |
| 1176 | + | |
| 1177 | + warning_count: Number of warnings. Sent after all data has been sent | |
| 1178 | + to the client. | |
| 1179 | + | |
| 1180 | + server_status: Contains flags like SERVER_STATUS_MORE_RESULTS. | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + Alternative terms: EOF Packet is also known as "Last Data Packet" or "End Packet". | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + Example of EOF Packet | |
| 1187 | + | |
| 1188 | + Hexadecimal ASCII | |
| 1189 | + ----------- ----- | |
| 1190 | + field_count fe . | |
| 1191 | + warning_count 00 00 .. | |
| 1192 | + server_status 00 00 .. | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + *** [] Row Data Packet. | |
| 1202 | + | |
| 1203 | + From server to client. One packet for each row in the result set. | |
| 1204 | + | |
| 1205 | + Bytes Name | |
| 1206 | + ----- ---- | |
| 1207 | + n (Length Coded String) (column value) | |
| 1208 | + ... | |
| 1209 | + | |
| 1210 | + (column value): The data in the column, as a character string. | |
| 1211 | + If a column is defined as non-character, the | |
| 1212 | + server converts the value into a character | |
| 1213 | + before sending it. Since the value is a Length | |
| 1214 | + Coded String, a NULL can be represented with a | |
| 1215 | + single byte containing 251(see the description | |
| 1216 | + of Length Coded Strings in section "Elements" above). | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + The (column value) fields occur multiple times. All (column value) fields are in one | |
| 1220 | + packet. There is no space between each (column value). | |
| 1221 | + | |
| 1222 | + Alternative Terms: Row Data Packets are also called "Row Packets" or "Data Packets". | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + Example of Row Data Packet | |
| 1226 | + | |
| 1227 | + Hexadecimal ASCII | |
| 1228 | + ----------- ----- | |
| 1229 | + (first column) 01 58 .X | |
| 1230 | + (second column) 02 35 35 .55 | |
| 1231 | + | |
| 1232 | + In the example, we see what the packet contains after a SELECT from a table defined as | |
| 1233 | + "(s1 CHAR, s2 INTEGER)" and containing one row where s1='X' and s2=55. | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + *** [] Row Data Packet: Binary (Tentative Description) | |
| 1244 | + | |
| 1245 | + From server to client, or from client to server (if the client has a prepared | |
| 1246 | + statement, the "result set" packet format is used for transferring parameter | |
| 1247 | + descriptors and parameter data). | |
| 1248 | + | |
| 1249 | + Recall that in the description of Row Data we said that: "If a column is defined as | |
| 1250 | + non-character, the server converts the value into a character before sending it." That | |
| 1251 | + doesn't have to be true. If it isn't true, it's a Row Data Packet: Binary. | |
| 1252 | + | |
| 1253 | + Bytes Name | |
| 1254 | + ----- ---- | |
| 1255 | + 1 Null Bit Map with first two bits = 01 | |
| 1256 | + n (Length Coded String) (column value) | |
| 1257 | + ... | |
| 1258 | + | |
| 1259 | + Bytes Name | |
| 1260 | + ----- ---- | |
| 1261 | + ? Packet Header | |
| 1262 | + 1 Null Bit Map with first two bits = 01 | |
| 1263 | + | |
| 1264 | + Null Bit Map: The most significant 2 bits are reserved. Since | |
| 1265 | + there is always one bit on and one bit off, this can't be | |
| 1266 | + confused with the first byte of an Error Packet (255), the | |
| 1267 | + first byte of a Last Data Packet (254), or the first byte of | |
| 1268 | + an OK Packet (0). | |
| 1269 | + | |
| 1270 | + (column value): The column order and organization are the same as for | |
| 1271 | + conventional Row Data Packets. The difference is that | |
| 1272 | + each column value is sent just as it is stored. It's now up | |
| 1273 | + to the client to convert numbers to strings if that's desirable. | |
| 1274 | + For a description of column storage, see "Physical Attributes Of | |
| 1275 | + Columns" elsewhere in this document. | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + Only non-zero parameters are passed. | |
| 1279 | + | |
| 1280 | + Because no conversion takes place, fixed-length data items are as described in the | |
| 1281 | + "Physical Attributes of Columns" section: one byte for TINYINT, two bytes for FLOAT, | |
| 1282 | + four bytes for FLOAT, etc. Strings will appear as packed-string-length plus string | |
| 1283 | + value. DATETIME, DATE and TIME will be as follows: | |
| 1284 | + | |
| 1285 | + Type Size Comment | |
| 1286 | + ---- ---- ------- | |
| 1287 | + date 1 + 0-11 Length + 2 byte year, 1 byte MMDDHHMMSS, | |
| 1288 | + 4 byte billionth of a second | |
| 1289 | + datetime 1 + 0-11 Length + 2 byte year, 1 byte MMDDHHMMSS, | |
| 1290 | + 4 byte billionth of a second | |
| 1291 | + time 1 + 0-11 Length + sign (0 = pos, 1= neg), 4 byte days, | |
| 1292 | + 1 byte HHMMDD, 4 byte billionth of a second | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + Alternative Terms: Row Data Packet: Binary is also called "Binary result set packet". | |
| 1296 | + | |
| 1297 | + Except for the different way of signalling NULLs, the server/client parameter | |
| 1298 | + interaction here proceeds the say way that the server sends result set data to the | |
| 1299 | + client. Since the data is not sent as a string, the length and meaning depend on the | |
| 1300 | + data type. The client must make appropriate conversions given its knowledge of the data | |
| 1301 | + type. | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + *** [] OK for Prepared Statement Initialization Packet (Tentative Description). | |
| 1312 | + | |
| 1313 | + From server to client, in response to prepared statement initialization packet. | |
| 1314 | + | |
| 1315 | + Bytes Name | |
| 1316 | + ----- ---- | |
| 1317 | + 1 0 - marker for OK packet | |
| 1318 | + 4 statement_handler_id | |
| 1319 | + 2 number of columns in result set | |
| 1320 | + 2 number of parameters in query | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + Alternative terms: statement_handler_id is called "statement handle" or "hstmt" | |
| 1324 | + everywhere but at MySQL. Prepared statement initialization packet is also called | |
| 1325 | + "prepared statement init packet". | |
| 1326 | + | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + *** [] Parameter Packet (Tentative Description). | |
| 1333 | + | |
| 1334 | + From server to client, for prepared statements which contain parameters. | |
| 1335 | + | |
| 1336 | + The Parameter Packets follow a Prepared Statement Initialization Packet which has a | |
| 1337 | + positive value in the parameters field. | |
| 1338 | + | |
| 1339 | + Bytes Name | |
| 1340 | + ----- ---- | |
| 1341 | + 2 type | |
| 1342 | + 2 flags | |
| 1343 | + 1 decimals | |
| 1344 | + 4 length | |
| 1345 | + | |
| 1346 | + type: Same as for type field in a Field Packet. | |
| 1347 | + | |
| 1348 | + flags: Same as for flags field in a Field Packet. | |
| 1349 | + | |
| 1350 | + decimals: Same as for decimals field in a Field Packet. | |
| 1351 | + | |
| 1352 | + length: Same as for length field in a Field Packet. | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + Notice the similarity to a Field Packet. | |
| 1356 | + | |
| 1357 | + The parameter data will be sent in a packet with the same format as Row Data Packet: | |
| 1358 | + Binary. | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + *** [] Long Data Packet (Tentative Description). | |
| 1368 | + | |
| 1369 | + From client to server, for long parameter values. | |
| 1370 | + | |
| 1371 | + Bytes Name | |
| 1372 | + ----- ---- | |
| 1373 | + 4 statement_handler_id | |
| 1374 | + 2 parameter_number | |
| 1375 | + 2 type | |
| 1376 | + n data | |
| 1377 | + | |
| 1378 | + statement_handler_id: ID of statement handler | |
| 1379 | + | |
| 1380 | + parameter_number: Parameter number. | |
| 1381 | + | |
| 1382 | + type: Parameter data type. Not used at time of writing. | |
| 1383 | + | |
| 1384 | + data: Value of parameter, as binary string. The length | |
| 1385 | + of data is implicit from the packet length. | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + This is used by mysql_send_long_data() to set any parameter to a string value. One can | |
| 1389 | + call mysql_send_long_data() multiple times for the same parameter; The server will | |
| 1390 | + concatenate the results to one big string. | |
| 1391 | + | |
| 1392 | + The server will not send an ok or error packet in response to this. If there is an | |
| 1393 | + error (for example the string is too big), one will see the error when calling | |
| 1394 | + "execute". | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + *** [] Execute Packet (Tentative Description). | |
| 1402 | + | |
| 1403 | + From client to server, to execute a prepared statement. | |
| 1404 | + | |
| 1405 | + Bytes Name | |
| 1406 | + ----- ---- | |
| 1407 | + 1 code | |
| 1408 | + 4 statement_id | |
| 1409 | + 1 flags | |
| 1410 | + 4 iteration_count | |
| 1411 | + (param_count+7)/8 null_bit_map | |
| 1412 | + 1 new_parameter_bound_flag | |
| 1413 | + n*2 type of parameters (only if new_params_bound = 1) | |
| 1414 | + | |
| 1415 | + code: always COM_EXECUTE | |
| 1416 | + | |
| 1417 | + statement_id: statement identifier | |
| 1418 | + | |
| 1419 | + flags: reserved for future use. In MySQL 4.0, always 0. | |
| 1420 | + In MySQL 5.0: | |
| 1421 | + 0: CURSOR_TYPE_NO_CURSOR | |
| 1422 | + 1: CURSOR_TYPE_READ_ONLY | |
| 1423 | + 2: CURSOR_TYPE_FOR_UPDATE | |
| 1424 | + 4: CURSOR_TYPE_SCROLLABLE | |
| 1425 | + | |
| 1426 | + iteration_count: reserved for future use. Currently always 1. | |
| 1427 | + | |
| 1428 | + null_bit_map: A bitmap indicating parameters that are NULL. | |
| 1429 | + Bits are counted from LSB, using as many bytes | |
| 1430 | + as necessary ((param_count+7)/8) | |
| 1431 | + i.e. if the first parameter (parameter 0) is NULL, then | |
| 1432 | + the least significant bit in the first byte will be 1. | |
| 1433 | + | |
| 1434 | + new_parameter_bound_flag: Contains 1 if this is the first time | |
| 1435 | + that "execute" has been called, or if | |
| 1436 | + the parameters have been rebound. | |
| 1437 | + | |
| 1438 | + type: Occurs once for each parameter that is not NULL. | |
| 1439 | + The highest significant bit of this 16-bit value | |
| 1440 | + encodes the unsigned property. The other 15 bits | |
| 1441 | + are reserved for the type (only 8 currently used). | |
| 1442 | + This block is sent when parameters have been rebound | |
| 1443 | + or when a prepared statement is executed for the | |
| 1444 | + first time. | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + The Execute Packet is also known as "COM_EXECUTE Packet". | |
| 1448 | + | |
| 1449 | + In response to an Execute Packet, the server should send back one of: an OK Packet, an | |
| 1450 | + Error Packet, or a series of Result Set Packets in which all the Row Data Packets are | |
| 1451 | + binary. | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + This chapter does not discuss compression, but you should be aware of its existence. | |
| 1455 | + | |
| 1456 | + Compression is of one or more logical packets. The packet_number field that is in each | |
| 1457 | + packet header is an aid for keeping track. | |
| 1458 | + | |
| 1459 | + The opposite of "compressed" is "raw". | |
| 1460 | + | |
| 1461 | + Compression is used if both client and server support zlib compression, and the client | |
| 1462 | + requests compression. | |
| 1463 | + | |
| 1464 | + A compressed packet header is: packet length (3 bytes), packet number (1 byte), and | |
| 1465 | + Uncompressed Packet Length (3 bytes). The Uncompressed Packet Length is the number of | |
| 1466 | + bytes in the original, uncompressed packet. If this is zero then the data is not | |
| 1467 | + compressed. | |
| 1468 | + | |
| 1469 | + When the compressed protocol is in use (that is, when the client has requested it by | |
| 1470 | + setting the flag bit in the Client Authentication Packet and the server has accepted | |
| 1471 | + it), either the client or the server may compress packets. However, compression will | |
| 1472 | + not occur if the compressed length is greater than the original length. Thus, some | |
| 1473 | + packets will be compressed while other packets are not compressed. Retrieved from | |
| 1474 | + "http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol" | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 0 | 1481 | \ No newline at end of file | ... | ... |