internals.texi 23.7 KB
Newer Older
unknown's avatar
unknown committed
1
\input texinfo  @c -*-texinfo-*-
2
@c Copyright 2002 MySQL AB, TcX AB, Detron HB and Monty Program KB
unknown's avatar
unknown committed
3 4 5
@c
@c %**start of header
@setfilename internals.info
6

unknown's avatar
unknown committed
7 8
@c We want the types in the same index
@synindex cp fn
9

unknown's avatar
unknown committed
10 11 12
@iftex
@afourpaper
@end iftex
13

unknown's avatar
unknown committed
14 15
@c Get version and other info
@include include.texi
16

unknown's avatar
unknown committed
17 18 19 20
@ifclear tex-debug
@c This removes the black squares in the right margin
@finalout
@end ifclear
21

unknown's avatar
unknown committed
22 23
@c Set background for HTML
@set _body_tags BGCOLOR=#FFFFFF TEXT=#000000 LINK=#101090 VLINK=#7030B0
24 25
@settitle @strong{MySQL} Internals Manual for version @value{mysql_version}.
@setchapternewpage odd
unknown's avatar
unknown committed
26
@paragraphindent 0
27

unknown's avatar
unknown committed
28 29 30 31 32 33 34 35 36 37 38 39
@c %**end of header

@ifinfo
@format
START-INFO-DIR-ENTRY
* mysql-internals: (mysql-internals).               @strong{MySQL} internals.
END-INFO-DIR-ENTRY
@end format
@end ifinfo

@titlepage
@sp 10
unknown's avatar
unknown committed
40
@center @titlefont{@strong{MySQL} Internals Manual}
unknown's avatar
unknown committed
41
@sp 10
42 43
@center Copyright @copyright{} 1998-2002 MySQL AB
@page
unknown's avatar
unknown committed
44 45
@end titlepage

46
@node Top, caching, (dir), (dir)
unknown's avatar
unknown committed
47 48 49 50 51 52

@ifinfo
This is a manual about @strong{MySQL} internals.
@end ifinfo

@menu
53 54 55 56 57 58 59
* caching::                     How MySQL Handles Caching
* flush tables::                How MySQL Handles @code{FLUSH TABLES}
* filesort::                    How MySQL Does Sorting (@code{filesort})
* coding guidelines::           Coding Guidelines
* mysys functions::             Functions In The @code{mysys} Library
* DBUG::                        DBUG Tags To Use
* protocol::                    MySQL Client/Server Protocol
unknown's avatar
unknown committed
60 61
@end menu

62 63 64

@node caching, flush tables, Top, Top
@chapter How MySQL Handles Caching
unknown's avatar
unknown committed
65

unknown's avatar
unknown committed
66
@strong{MySQL} has the following caches:
unknown's avatar
unknown committed
67
(Note that the some of the filename have a wrong spelling of cache. :)
unknown's avatar
unknown committed
68

69
@table @strong
unknown's avatar
unknown committed
70

71
@item Key Cache
unknown's avatar
unknown committed
72 73 74
A shared cache for all B-tree index blocks in the different NISAM
files. Uses hashing and reverse linked lists for quick caching of the
last used blocks and quick flushing of changed entries for a specific
unknown's avatar
unknown committed
75
table. (@file{mysys/mf_keycash.c})
unknown's avatar
unknown committed
76

77
@item Record Cache
unknown's avatar
unknown committed
78
This is used for quick scanning of all records in a table.
unknown's avatar
unknown committed
79
(@file{mysys/mf_iocash.c} and @file{isam/_cash.c})
unknown's avatar
unknown committed
80

81
@item Table Cache
unknown's avatar
unknown committed
82
This holds the last used tables. (@file{sql/sql_base.cc})
unknown's avatar
unknown committed
83

84
@item Hostname Cache
unknown's avatar
unknown committed
85
For quick lookup (with reverse name resolving). Is a must when one has a
unknown's avatar
unknown committed
86 87
slow DNS. 
(@file{sql/hostname.cc})
unknown's avatar
unknown committed
88

89
@item Privilege Cache
unknown's avatar
unknown committed
90
To allow quick change between databases the last used privileges are
unknown's avatar
unknown committed
91 92
cached for each user/database combination.
(@file{sql/sql_acl.cc})
unknown's avatar
unknown committed
93

94 95 96
@item Heap Table Cache
Many use of @code{GROUP BY} or @code{DISTINCT} caches all found rows in 
a @code{HEAP} table. (This is a very quick in-memory table with hash index.)
unknown's avatar
unknown committed
97

98 99 100 101 102 103
@item Join Row Cache
For every full join in a @code{SELECT} statement (a full join here means
there were no keys that one could use to find the next table in a list), 
the found rows are cached in a join cache.  One @code{SELECT} query can 
use many join caches in the worst case.
@end table
unknown's avatar
unknown committed
104

105 106 107

@node flush tables, filesort, caching, Top
@chapter How MySQL Handles @code{FLUSH TABLES}
unknown's avatar
unknown committed
108 109

@itemize @bullet
unknown's avatar
unknown committed
110 111

@item
112
Flush tables is handled in @file{sql/sql_base.cc::close_cached_tables()}.
unknown's avatar
unknown committed
113

unknown's avatar
unknown committed
114 115 116
@item
The idea of flush tables is to force all tables to be closed. This
is mainly to ensure that if someone adds a new table outside of
unknown's avatar
unknown committed
117 118 119 120 121
@strong{MySQL} (for example with @code{cp}) all threads will start using 
the new table. This will also ensure that all table changes are flushed 
to disk (but of course not as optimally as simple calling a sync on
all tables)!

unknown's avatar
unknown committed
122
@item
unknown's avatar
unknown committed
123 124
When one does a @code{FLUSH TABLES}, the variable @code{refresh_version} 
will be incremented. Every time a thread releases a table it checks if
unknown's avatar
unknown committed
125
the refresh version of the table (updated at open) is the same as
126 127
the current @code{refresh_version}.  If not it will close it and broadcast
a signal on @code{COND_refresh} (to wait any thread that is waiting for
unknown's avatar
unknown committed
128
all instanses of a table to be closed).
unknown's avatar
unknown committed
129

unknown's avatar
unknown committed
130
@item
unknown's avatar
unknown committed
131 132 133 134 135
The current @code{refresh_version} is also compared to the open 
@code{refresh_version} after a thread gets a lock on a table.  If the 
refresh version is different the thread will free all locks, reopen the
table and try to get the locks again;  This is just to quickly get all 
tables to use the newest version.  This is handled by
136 137
@file{sql/lock.cc::mysql_lock_tables()} and 
@file{sql/sql_base.cc::wait_for_tables()}.
unknown's avatar
unknown committed
138

unknown's avatar
unknown committed
139
@item
unknown's avatar
unknown committed
140 141 142
When all tables has been closed @code{FLUSH TABLES} will return an ok 
to client.

unknown's avatar
unknown committed
143
@item
unknown's avatar
unknown committed
144 145 146 147 148
If the thread that is doing @code{FLUSH TABLES} has a lock on some tables,
it will first close the locked tables, then wait until all other threads
have also closed them, and then reopen them and get the locks.
After this it will give other threads a chance to open the same tables.

unknown's avatar
unknown committed
149 150
@end itemize

151 152
@node filesort, coding guidelines, flush tables, Top
@chapter How MySQL Does Sorting (@code{filesort})
153

unknown's avatar
unknown committed
154 155 156 157 158 159 160 161 162
@itemize @bullet

@item
Read all rows according to key or by table scanning.

@item
Store the sort-key in a buffer (@code{sort_buffer}).

@item
163
When the buffer gets full, run a @code{qsort} on it and store the result
unknown's avatar
unknown committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
in a temporary file.  Save a pointer to the sorted block.

@item
Repeat the above until all rows have been read.

@item
Repeat the following until there is less than @code{MERGEBUFF2} (15) 
blocks left.

@item
Do a multi-merge of up to @code{MERGEBUFF} (7) regions to one block in
another temporary file.  Repeat until all blocks from the first file
are in the second file.

@item
On the last multi-merge, only the pointer to the row (last part of
the sort-key) is written to a result file.

@item
Now the code in @file{sql/records.cc} will be used to read through them
in sorted order by using the row pointers in the result file.
To optimize this, we read in a big block of row pointers, sort these
and then we read the rows in the sorted order into a row buffer
187
(@code{record_buffer}).
unknown's avatar
unknown committed
188 189

@end itemize
190

191 192 193

@node coding guidelines, mysys functions, filesort, Top
@chapter Coding Guidelines
unknown's avatar
unknown committed
194

unknown's avatar
unknown committed
195 196 197 198 199 200
@itemize @bullet

@item
We are using @uref{http://www.bitkeeper.com/, BitKeeper} for source management.

@item
201
You should use the @strong{MySQL} 4.0 source for all developments.
unknown's avatar
unknown committed
202 203 204

@item
If you have any questions about the @strong{MySQL} source, you can post these
205 206
to @email{dev-public@@mysql.com} and we will answer them.  Please
remember to not use this internal email list in public!
unknown's avatar
unknown committed
207 208

@item
209 210
Try to write code in a lot of black boxes that can be reused or use at
least a clean, easy to change interface.
unknown's avatar
unknown committed
211 212 213 214 215

@item
Reuse code;  There is already a lot of algorithms in MySQL for list handling,
queues, dynamic and hashed arrays, sorting, etc. that can be reused.

216 217 218 219 220 221
@item
Use the @code{my_*} functions like @code{my_read()}/@code{my_write()}/
@code{my_malloc()} that you can find in the @code{mysys} library instead 
of the direct system calls;  This will make your code easier to debug and 
more portable.

unknown's avatar
unknown committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235
@item
Try to always write optimized code, so that you don't have to
go back and rewrite it a couple of months later.  It's better to
spend 3 times as much time designing and writing an optimal function than
having to do it all over again later on.

@item
Avoid CPU wasteful code, even where it does not matter, so that
you will not develop sloppy coding habits.

@item
If you can write it in fewer lines, do it (as long as the code will not
be slower or much harder to read).

236 237 238
@item
Don't use two commands on the same line.

unknown's avatar
unknown committed
239 240 241 242
@item
Do not check the same pointer for @code{NULL} more than once.

@item
243
Use long function and variable names in English.  This makes your code
244 245 246
easier to read. 

@item
247 248
Use @code{my_var} as opposed to @code{myVar} or @code{MyVar} (@samp{_} 
rather than dancing SHIFT to seperate words in identifiers).
unknown's avatar
unknown committed
249 250 251 252 253 254

@item
Think assembly - make it easier for the compiler to optimize your code.

@item
Comment your code when you do something that someone else may think
255
is not ``trivial''.
unknown's avatar
unknown committed
256 257

@item
258 259
Use @code{libstring} functions (in the @file{strings} directory)
instead of standard @code{libc} string functions whenever possible.
unknown's avatar
unknown committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

@item
Avoid using @code{malloc()} (its REAL slow);  For memory allocations 
that only need to live for the lifetime of one thread, one should use
@code{sql_alloc()} instead.

@item
Before making big design decisions, please first post a summary of
what you want to do, why you want to do it, and how you plan to do
it.  This way we can easily provide you with feedback and also
easily discuss it thoroughly if some other developer thinks there is better
way to do the same thing!

@item
Class names start with a capital letter.

@item
Structure types are @code{typedef}'ed to an all-caps identifier.

@item
Any @code{#define}'s are in all-caps.

@item
Matching @samp{@{} are in the same column.

285
@item
286 287
Put the @samp{@{} after a @code{switch} on the same line, as this gives 
better overall indentation for the switch statement:
288 289

@example
290
switch (arg) @{
291 292 293
@end example

@item
294 295
In all other cases, @samp{@{} and @samp{@}} should be on their own line, except
if there is nothing inside @samp{@{} and @samp{@}}.
296 297

@item
298
Have a space after @code{if}
299 300

@item
301
Put a space after @samp{,} for function arguments
302

unknown's avatar
unknown committed
303
@item
304
Functions return @samp{0} on success, and non-zero on error, so you can do:
unknown's avatar
unknown committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

@example
if(a() || b() || c()) { error("something went wrong"); }
@end example

@item
Using @code{goto} is okay if not abused.

@item
Avoid default variable initalizations, use @code{LINT_INIT()} if the
compiler complains after making sure that there is really no way
the variable can be used uninitialized.

@item
Do not instantiate a class if you do not have to.

@item
Use pointers rather than array indexing when operating on strings.

@end itemize

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
Suggested mode in emacs:

@example
(load "cc-mode")
(setq c-mode-common-hook '(lambda ()
			    (turn-on-font-lock)
			    (setq comment-column 48)))
(setq c-style-alist
      (cons
       '("MY"
	 (c-basic-offset . 2)
	 (c-comment-only-line-offset . 0)
	 (c-offsets-alist . ((statement-block-intro . +)
			     (knr-argdecl-intro . 0)
			     (substatement-open . 0)
			     (label . -)
			     (statement-cont . +)
			     (arglist-intro . c-lineup-arglist-intro-after-paren)
			     (arglist-close . c-lineup-arglist)
			     ))
	 )
       c-style-alist))
(c-set-style "MY")
(setq c-default-style "MY")
@end example

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

@node mysys functions, DBUG, coding guidelines, Top
@chapter Functions In The @code{mysys} Library

Functions in @code{mysys}: (For flags see @file{my_sys.h})

@table @code
@item int my_copy _A((const char *from, const char *to, myf MyFlags));
Copy file from @code{from} to @code{to}.

@item int my_delete _A((const char *name, myf MyFlags));
Delete file @code{name}.

@item int my_getwd _A((string buf, uint size, myf MyFlags));
@item int my_setwd _A((const char *dir, myf MyFlags));
Get and set working directory.

@item string my_tempnam _A((const char *pfx, myf MyFlags));
Make a unique temporary file name by using dir and adding something after
@code{pfx} to make name unique.  The file name is made by adding a unique
six character string and @code{TMP_EXT} after @code{pfx}.
Returns pointer to @code{malloc()}'ed area for filename. Should be freed by
@code{free()}.

@item File my_open _A((const char *FileName,int Flags,myf MyFlags));
@item File my_create _A((const char *FileName, int CreateFlags, int AccsesFlags, myf MyFlags));
@item int my_close _A((File Filedes, myf MyFlags));
@item uint my_read _A((File Filedes, byte *Buffer, uint Count, myf MyFlags));
@item uint my_write _A((File Filedes, const byte *Buffer, uint Count, myf MyFlags));
@item ulong my_seek _A((File fd,ulong pos,int whence,myf MyFlags));
@item ulong my_tell _A((File fd,myf MyFlags));
Use instead of open, open-with-create-flag, close, read, and write
to get automatic error messages (flag @code{MYF_WME}) and only have
to test for != 0 if error (flag @code{MY_NABP}).

@item int my_rename _A((const char *from, const char *to, myf MyFlags));
Rename file from @code{from} to @code{to}.

@item FILE *my_fopen _A((const char *FileName,int Flags,myf MyFlags));
@item FILE *my_fdopen _A((File Filedes,int Flags,myf MyFlags));
@item int my_fclose _A((FILE *fd,myf MyFlags));
@item uint my_fread _A((FILE *stream,byte *Buffer,uint Count,myf MyFlags));
@item uint my_fwrite _A((FILE *stream,const byte *Buffer,uint Count, myf MyFlags));
@item ulong my_fseek _A((FILE *stream,ulong pos,int whence,myf MyFlags));
@item ulong my_ftell _A((FILE *stream,myf MyFlags));
Same read-interface for streams as for files.

@item gptr _mymalloc _A((uint uSize,const char *sFile,uint uLine, myf MyFlag));
@item gptr _myrealloc _A((string pPtr,uint uSize,const char *sFile,uint uLine, myf MyFlag));
@item void _myfree _A((gptr pPtr,const char *sFile,uint uLine));
@item int _sanity _A((const char *sFile,unsigned int uLine));
@item gptr _myget_copy_of_memory _A((const byte *from,uint length,const char *sFile, uint uLine,myf MyFlag));
@code{malloc(size,myflag)} is mapped to these functions if not compiled 
with @code{-DSAFEMALLOC}.

@item void TERMINATE _A((void));
Writes @code{malloc()} info on @code{stdout} if compiled with 
@code{-DSAFEMALLOC}.

@item int my_chsize _A((File fd, ulong newlength, myf MyFlags));
Change size of file @code{fd} to @code{newlength}.

@item void my_error _D((int nr, myf MyFlags, ...));
Writes message using error number (see @file{mysys/errors.h}) on @code{stdout},
or using curses, if @code{MYSYS_PROGRAM_USES_CURSES()} has been called.

@item void my_message _A((const char *str, myf MyFlags));
Writes @code{str} on @code{stdout}, or using curses, if 
@code{MYSYS_PROGRAM_USES_CURSES()} has been called.

@item void my_init _A((void ));
Start each program (in @code{main()}) with this.

@item void my_end _A((int infoflag));
Gives info about program.
If @code{infoflag & MY_CHECK_ERROR}, prints if some files are left open.
If @code{infoflag & MY_GIVE_INFO}, prints timing info and malloc info
about program.

@item int my_redel _A((const char *from, const char *to, int MyFlags));
Delete @code{from} before rename of @code{to} to @code{from}.  Copies state
from old file to new file. If @code{MY_COPY_TIME} is set, sets old time.

@item int my_copystat _A((const char *from, const char *to, int MyFlags));
Copy state from old file to new file.  If @code{MY_COPY_TIME} is set,
sets old time.

@item string my_filename _A((File fd));
Returns filename of open file.

@item int dirname _A((string to, const char *name));
Copy name of directory from filename.

@item int test_if_hard_path _A((const char *dir_name));
Test if @code{dir_name} is a hard path (starts from root).

@item void convert_dirname _A((string name));
Convert dirname according to system.
In MSDOS, changes all characters to capitals and changes @samp{/} to @samp{\}.

@item string fn_ext _A((const char *name));
Returns pointer to extension in filename.

@item string fn_format _A((string to,const char *name,const char *dsk,const char *form,int flag));
unknown's avatar
unknown committed
456 457 458 459 460 461 462 463 464 465 466 467 468
	format a filename with replace of library and extension and
	converts between different systems.
	params to and name may be identicall
	function dosn't change name if name != to
	Flag may be:	1   force replace filnames library with 'dsk'
			2   force replace extension with 'form' */
			4   force Unpack filename (replace ~ with home)
			8   Pack filename as short as possibly for output to
			    user.
	All open requests should allways use at least:
	"open(fn_format(temp_buffe,name,"","",4),...)" to unpack home and
	convert filename to system-form.

469 470 471 472 473 474 475 476
@item string fn_same _A((string toname, const char *name, int flag));
Copys directory and extension from @code{name} to @code{toname} if neaded.
Copying can be forced by same flags used in @code{fn_format()}.

@item int wild_compare _A((const char *str, const char *wildstr));
Compare if @code{str} matches @code{wildstr}. @code{wildstr} can contain 
@samp{*} and @samp{?} as wildcard characters.
Returns 0 if @code{str} and @code{wildstr} match.
unknown's avatar
unknown committed
477

478 479
@item void get_date _A((string to, int timeflag));
Get current date in a form ready for printing.
unknown's avatar
unknown committed
480

481 482 483
@item void soundex _A((string out_pntr, string in_pntr))
Makes @code{in_pntr} to a 5 char long string.  All words that sound
alike have the same string.
unknown's avatar
unknown committed
484

485 486 487 488
@item int init_key_cache _A((ulong use_mem, ulong leave_this_much_mem));
Use caching of keys in MISAM, PISAM, and ISAM.
@code{KEY_CACHE_SIZE} is a good size.
Remember to lock databases for optimal caching.
unknown's avatar
unknown committed
489

490 491
@item void end_key_cache _A((void));
End key caching.
492 493
@end table 

unknown's avatar
unknown committed
494 495


496 497
@node DBUG, protocol, mysys functions, Top
@chapter DBUG Tags To Use
unknown's avatar
unknown committed
498 499 500

Here is some of the tags we now use:
(We should probably add a couple of new ones)
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

@table @code 
@item enter
Arguments to the function.

@item exit
Results from the function.

@item info
Something that may be interesting.

@item warning
When something doesn't go the usual route or may be wrong.

@item error
When something went wrong.

@item loop
Write in a loop, that is probably only useful when debugging
the loop.  These should normally be deleted when one is
satisfied with the code and it has been in real use for a while.
@end table
unknown's avatar
unknown committed
523 524 525

Some specific to mysqld, because we want to watch these carefully:

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
@table @code
@item trans
Starting/stopping transactions.

@item quit
@code{info} when mysqld is preparing to die.

@item query
Print query.
@end table


@node protocol,  , DBUG, Top
@chapter MySQL Client/Server Protocol

@menu
* raw packet without compression::  
* raw packet with compression::  
* basic packets::               
* communication::               
* fieldtype codes::             
@end menu
unknown's avatar
unknown committed
548

549 550
@node raw packet without compression, raw packet with compression, protocol, protocol
@section Raw Packet Without Compression
unknown's avatar
unknown committed
551

552
@example
553 554 555 556
+-----------------------------------------------+
| Packet Length | Packet no     | Data          |
| 3 Bytes       | 1 Byte        | n Bytes       |
+-----------------------------------------------+
557
@end example
unknown's avatar
unknown committed
558

559 560 561 562 563
@table @asis
@item 3 Byte packet length
The length is calculated with int3store
See include/global.h for details.
The max packetsize can be 16 MB.
unknown's avatar
unknown committed
564

565 566 567 568 569
@item 1 Byte packet no
If no compression is used the first 4 bytes of each packet is the header 
of the packet. The packet number is incremented for each sent packet.
The first packet starts with 0.
@item n Byte data 
unknown's avatar
unknown committed
570

571
@end table
unknown's avatar
unknown committed
572 573

The packet length can be recalculated with:
574 575

@example
unknown's avatar
unknown committed
576
length = byte1 + (256 * byte2) + (256 * 256 * byte3)
577 578
@end example

579 580 581 582 583

@node raw packet with compression, basic packets, raw packet without compression, protocol
@section Raw Packet With Compression

@example
584 585 586 587
+---------------------------------------------------+
| Packet Length | Packet no | Uncomp. Packet Length |
| 3 Bytes       | 1 Byte    | 3 Bytes               |
+---------------------------------------------------+
588
@end example
unknown's avatar
unknown committed
589

590 591 592 593 594 595 596 597 598
@table @asis
@item 3 Byte packet length
The length is calculated with int3store
See include/global.h for details.
The max packetsize can be 16 MB.

@item 1 Byte packet no
@item 3 Byte uncompressed packet length
@end table
unknown's avatar
unknown committed
599

600 601 602
If compression is used the first 7 bytes of each packet
is the header of the packet.

603

604 605 606 607 608 609 610 611
@node basic packets, communication, raw packet with compression, protocol
@section Basic Packets

@menu
* ok packet::                   
* error packet::                
@end menu

612

613 614 615
@node ok packet, error packet, basic packets, basic packets
@subsection OK Packet

616 617
For details, see @file{sql/net_pkg.cc::send_ok()}.

618
@example
619 620 621 622 623 624 625 626 627 628
+-----------------------------------------------+
| Header        | No of Rows    | Affected Rows |
|               | 1 Byte        | 1-8 Byte      |
|-----------------------------------------------|
| ID (last_insert_id)   | Status | Length       |
| 1-8 Byte              | 2 Byte | 1-8 Byte     |
|-----------------------------------------------|
| Messagetext                                   |
| n Byte                                        |
+-----------------------------------------------+
629
@end example
unknown's avatar
unknown committed
630

631 632 633 634 635 636 637 638 639 640
@table @asis
@item Header
@item 1 byte number of rows ? (always 0 ?)
@item 1-8 bytes affected rows
@item 1-8 byte id (last_insert_id) 
@item 2 byte Status (usually 0)
@item If the OK-packege includes a message:
@item 1-8 bytes length of message
@item n bytes messagetext
@end table
unknown's avatar
unknown committed
641

642 643 644 645 646

@node error packet,  , ok packet, basic packets
@subsection Error Packet	

@example
647 648 649 650 651 652 653
+-----------------------------------------------+
| Header        | Status code   | Error no      |
|               | 1 Byte        | 2 Byte        |
|-----------------------------------------------|
| Messagetext                          | 0x00   |
| n Byte                               | 1 Byte |
+-----------------------------------------------+
654 655
@end example

656 657 658 659 660 661 662
@table @asis
@item Header
@item 1 byte status code (0xFF = ERROR)
@item 2 byte error number (is only sent to new 3.23 clients.
@item n byte errortext
@item 1 byte 0x00
@end table
unknown's avatar
unknown committed
663 664


665 666
@node communication, fieldtype codes, basic packets, protocol
@section Communication
unknown's avatar
unknown committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756

> Packet from server to client
< Paket from client tor server

	Login
	------
		> 1. packet	
		Header
		1 byte protocolversion
		n byte serverversion
		1 byte 0x00
		4 byte threadnumber 
		8 byte crypt seed 
		1 byte 0x00
		2 byte CLIENT_xxx options (see include/mysql_com.h
			that is supported by the server
		1 byte number of current server charset
		2 byte server status variables (SERVER_STATUS_xxx flags)
		13 byte 0x00 (not used yet).

		< 2. packet	
		Header
		2 byte CLIENT_xxx options
		3 byte max_allowed_packet for the client
		n byte username
		1 byte 0x00
		8 byte crypted password
		1 byte 0x00
		n byte databasename
		1 byte 0x00 

		> 3. packet	
		OK-packet


	Command
	--------
		< 1. packet	
		Header
		1 byte command type (e.g.0x03 = query)
		n byte query

	Result set (after command)
	--------------------------
		> 2. packet	
		Header
		1-8 byte field_count (packed with net_store_length())
		
		If field_count == 0 (command): 
		1-8 byte affected rows
		1-8 byte insert id
		2 bytes server_status (SERVER_STATUS_xx) 		
		
		If field_count == NULL_LENGTH (251)
		LOAD DATA LOCAL INFILE

		If field_count > 0 Result Set:

		> n packets	
		Header Info
		Column description: 5 data object /column
		(See code in unpack_fields())
		
		Columninfo for each column:
			1 data block table_name
			    1 byte length of block
			    n byte data
			1 data block field_name
			    1 byte length of block...
			    n byte data
			1 data block display length of field
			    1 byte length of block
			    3 bytes display length of filed
			1 data block type field of type (enum_field_types)
			    1 byte length of block
			    1 bytexs field of type
			1 data block flags
			    1 byte length of block
			    2 byte flags for the columns (NOT_NULL_FLAG, ZEROFILL_FLAG....)
			    1 byte decimals

		if table definition:
			1 data block default value

		Actual result (one packet per row):
		4 byte header
		1-8 byte length of data
		n data
		
 
757 758
@node fieldtype codes,  , communication, protocol
@section Fieldtype Codes
unknown's avatar
unknown committed
759

760
@example
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
                display_length  |enum_field_type        |flags
                ----------------------------------------------------
Blob            03 FF FF 00     |01 FC                  |03 90 00 00
Mediumblob      03 FF FF FF     |01 FC                  |03 90 00 00
Tinyblob        03 FF 00 00     |01 FC                  |03 90 00 00
Text            03 FF FF 00     |01 FC                  |03 10 00 00
Mediumtext      03 FF FF FF     |01 FC                  |03 10 00 00
Tinytext        03 FF 00 00     |01 FC                  |03 10 00 00
Integer         03 0B 00 00     |01 03                  |03 03 42 00
Mediumint       03 09 00 00     |01 09                  |03 00 00 00
Smallint        03 06 00 00     |01 02                  |03 00 00 00
Tinyint         03 04 00 00     |01 01                  |03 00 00 00
Varchar         03 XX 00 00     |01 FD                  |03 00 00 00
Enum            03 05 00 00     |01 FE                  |03 00 01 00
Datetime        03 13 00 00     |01 0C                  |03 00 00 00
Timestamp       03 0E 00 00     |01 07                  |03 61 04 00
Time            03 08 00 00     |01 0B                  |03 00 00 00
Date            03 0A 00 00     |01 0A                  |03 00 00 00
779
@end example
unknown's avatar
unknown committed
780

unknown's avatar
unknown committed
781 782 783 784 785 786
@c The Index was empty, and ugly, so I removed it. (jcole, Sep 7, 2000)

@c @node Index
@c @unnumbered Index

@c @printindex fn
unknown's avatar
unknown committed
787 788 789 790 791

@summarycontents
@contents

@bye