00001 {*********************************************************}
00002 { }
00003 { Zeos Database Objects }
00004 { Delphi plain interface to libmysql.dll }
00005 { Version 5.0 }
00006 { }
00007 { Originally written by Sergey Seroukhov }
00008 { }
00009 { Thanks to : }
00010 { Pascal Data Objects Library }
00011 { }
00012 { Copyright (c) 2006 John Marino, www.synsport.com }
00013 { }
00014 {*********************************************************}
00015
00016 {@********************************************************}
00017 { Copyright (c) 1999-2006 Zeos Development Group }
00018 { }
00019 { License Agreement: }
00020 { }
00021 { This library is distributed in the hope that it will be }
00022 { useful, but WITHOUT ANY WARRANTY; without even the }
00023 { implied warranty of MERCHANTABILITY or FITNESS FOR }
00024 { A PARTICULAR PURPOSE. See the GNU Lesser General }
00025 { Public License for more details. }
00026 { }
00027 { The source code of the ZEOS Libraries and packages are }
00028 { distributed under the Library GNU General Public }
00029 { License (see the file COPYING / COPYING.ZEOS) }
00030 { with the following modification: }
00031 { As a special exception, the copyright holders of this }
00032 { library give you permission to link this library with }
00033 { independent modules to produce an executable, }
00034 { regardless of the license terms of these independent }
00035 { modules, and to copy and distribute the resulting }
00036 { executable under terms of your choice, provided that }
00037 { you also meet, for each linked independent module, }
00038 { the terms and conditions of the license of that module. }
00039 { An independent module is a module which is not derived }
00040 { from or based on this library. If you modify this }
00041 { library, you may extend this exception to your version }
00042 { of the library, but you are not obligated to do so. }
00043 { If you do not wish to do so, delete this exception }
00044 { statement from your version. }
00045 { }
00046 { }
00047 { The project web site is located on: }
00048 { http:
00049 { http:
00050 { svn:
00051 { }
00052 { http:
00053 { http:
00054 { }
00055 { }
00056 { }
00057 { Zeos Development Group. }
00058 {********************************************************@}
00059
00060 unit ZPlainMySql5;
00061
00062 interface
00063
00064 {$I ZPlain.inc}
00065 {$DEFINE MYSQL_5_API}
00066
00067 {$J+}
00068 {$Z4}
00069 {$IFNDEF VER130BELOW}
00070 {$A8}
00071 {$ENDIF}
00072
00073 uses Classes, ZPlainLoader, ZCompatibility, ZPlainMySqlConstants;
00074
00075 { ***************** Plain API Constants definition **************** }
00076
00077 const
00078 WINDOWS_50_DLL_LOCATION = 'libmysql50.dll';
00079 WINDOWS_51_DLL_LOCATION = 'libmysql51.dll';
00080 WINDOWS_50_DLL_LOCATION_EMBEDDED = 'libmysqld50.dll';
00081 WINDOWS_51_DLL_LOCATION_EMBEDDED = 'libmysqld51.dll';
00082 LINUX1_DLL_LOCATION = 'libmysqlclient.so.15';
00083
00084 { General Declarations }
00085
00086
00087
00088 { Enum Field Types }
00089 FIELD_TYPE_DECIMAL = 0;
00090 FIELD_TYPE_TINY = 1;
00091 FIELD_TYPE_SHORT = 2;
00092 FIELD_TYPE_LONG = 3;
00093 FIELD_TYPE_FLOAT = 4;
00094 FIELD_TYPE_DOUBLE = 5;
00095 FIELD_TYPE_NULL = 6;
00096 FIELD_TYPE_TIMESTAMP = 7;
00097 FIELD_TYPE_LONGLONG = 8;
00098 FIELD_TYPE_INT24 = 9;
00099 FIELD_TYPE_DATE = 10;
00100 FIELD_TYPE_TIME = 11;
00101 FIELD_TYPE_DATETIME = 12;
00102 FIELD_TYPE_YEAR = 13;
00103 FIELD_TYPE_NEWDATE = 14;
00104 FIELD_TYPE_VARCHAR = 15;
00105 FIELD_TYPE_BIT = 16;
00106 FIELD_TYPE_NEWDECIMAL = 246;
00107 FIELD_TYPE_ENUM = 247;
00108 FIELD_TYPE_SET = 248;
00109 FIELD_TYPE_TINY_BLOB = 249;
00110 FIELD_TYPE_MEDIUM_BLOB = 250;
00111 FIELD_TYPE_LONG_BLOB = 251;
00112 FIELD_TYPE_BLOB = 252;
00113 FIELD_TYPE_VAR_STRING = 253;
00114 FIELD_TYPE_STRING = 254;
00115 FIELD_TYPE_GEOMETRY = 255;
00116
00117 { For Compatibility }
00118 FIELD_TYPE_CHAR = FIELD_TYPE_TINY;
00119 FIELD_TYPE_INTERVAL = FIELD_TYPE_ENUM;
00120
00121 MAX_MYSQL_MANAGER_ERR = 256;
00122 MAX_MYSQL_MANAGER_MSG = 256;
00123
00124 MANAGER_OK = 200;
00125 MANAGER_INFO = 250;
00126 MANAGER_ACCESS = 401;
00127 MANAGER_CLIENT_ERR = 450;
00128 MANAGER_INTERNAL_ERR = 500;
00129
00130 { ****************** Plain API Types definition ***************** }
00131
00132 type
00133 TClientCapabilities = (
00134 CLIENT_LONG_PASSWORD,
00135 CLIENT_FOUND_ROWS,
00136 CLIENT_LONG_FLAG,
00137 CLIENT_CONNECT_WITH_DB,
00138 CLIENT_NO_SCHEMA,
00139 CLIENT_COMPRESS,
00140 CLIENT_ODBC,
00141 CLIENT_LOCAL_FILES,
00142 CLIENT_IGNORE_SPACE
00143 );
00144
00145 TSetClientCapabilities = set of TClientCapabilities;
00146
00147 TRefreshOptions = (
00148 _REFRESH_GRANT,
00149 _REFRESH_LOG,
00150 _REFRESH_TABLES,
00151 _REFRESH_HOSTS,
00152 _REFRESH_FAST
00153 );
00154 TSetRefreshOptions = set of TRefreshOptions;
00155
00156 TMySqlStatus = (
00157 MYSQL_STATUS_READY,
00158 MYSQL_STATUS_GET_RESULT,
00159 MYSQL_STATUS_USE_RESULT
00160 );
00161
00162 TMySqlOption = (
00163 MYSQL_OPT_CONNECT_TIMEOUT,
00164 MYSQL_OPT_COMPRESS,
00165 MYSQL_OPT_NAMED_PIPE,
00166 MYSQL_INIT_COMMAND,
00167 MYSQL_READ_DEFAULT_FILE,
00168 MYSQL_READ_DEFAULT_GROUP,
00169 MYSQL_SET_CHARSET_DIR,
00170 MYSQL_SET_CHARSET_NAME,
00171 MYSQL_OPT_LOCAL_INFILE,
00172 MYSQL_OPT_PROTOCOL,
00173 MYSQL_SHARED_MEMORY_BASE_NAME,
00174 MYSQL_OPT_READ_TIMEOUT,
00175 MYSQL_OPT_WRITE_TIMEOUT,
00176 MYSQL_OPT_USE_RESULT,
00177 MYSQL_OPT_USE_REMOTE_CONNECTION,
00178 MYSQL_OPT_USE_EMBEDDED_CONNECTION,
00179 MYSQL_OPT_GUESS_CONNECTION,
00180 MYSQL_SET_CLIENT_IP,
00181 MYSQL_SECURE_AUTH
00182 );
00183
00184 TMySqlRplType = (
00185 MYSQL_RPL_MASTER,
00186 MYSQL_RPL_SLAVE,
00187 MYSQL_RPL_ADMIN
00188 );
00189
00190 TMySqlServerCommand = (
00191 COM_SLEEP,
00192 COM_QUIT,
00193 COM_INIT_DB,
00194 COM_QUERY,
00195 COM_FIELD_LIST,
00196 COM_CREATE_DB,
00197 COM_DROP_DB,
00198 COM_REFRESH,
00199 COM_SHUTDOWN,
00200 COM_STATISTICS,
00201 COM_PROCESS_INFO,
00202 COM_CONNECT,
00203 COM_PROCESS_KILL,
00204 COM_DEBUG,
00205 COM_PING,
00206 COM_TIME,
00207 COM_DELAYED_INSERT,
00208 COM_CHANGE_USER,
00209 COM_BINLOG_DUMP,
00210 COM_TABLE_DUMP,
00211 COM_CONNECT_OUT,
00212 COM_REGISTER_SLAVE,
00213 COM_PREPARE,
00214 COM_EXECUTE,
00215 COM_LONG_DATA,
00216 COM_CLOSE_STMT,
00217 COM_RESET_STMT,
00218 COM_SET_OPTION,
00219 COM_END
00220 );
00221
00222 PUSED_MEM=^USED_MEM;
00223 USED_MEM = packed record
00224 next: PUSED_MEM;
00225 left: Integer;
00226 size: Integer;
00227 end;
00228
00229 PERR_PROC = ^ERR_PROC;
00230 ERR_PROC = procedure;
00231
00232 PMEM_ROOT = ^MEM_ROOT;
00233 MEM_ROOT = packed record
00234 free: PUSED_MEM;
00235 used: PUSED_MEM;
00236 pre_alloc: PUSED_MEM;
00237 min_malloc: Integer;
00238 block_size: Integer;
00239 block_num: Integer;
00240 first_block_usage: Integer;
00241 error_handler: PERR_PROC;
00242 end;
00243
00244 NET = record
00245 vio: Pointer;
00246 buff: PChar;
00247 buff_end: PChar;
00248 write_pos: PChar;
00249 read_pos: PChar;
00250 fd: Integer;
00251 max_packet: Cardinal;
00252 max_packet_size: Cardinal;
00253 pkt_nr: Cardinal;
00254 compress_pkt_nr: Cardinal;
00255 write_timeout: Cardinal;
00256 read_timeout: Cardinal;
00257 retry_count: Cardinal;
00258 fcntl: Integer;
00259 compress: Byte;
00260 remain_in_buf: LongInt;
00261 length: LongInt;
00262 buf_length: LongInt;
00263 where_b: LongInt;
00264 return_status: Pointer;
00265 reading_or_writing: Char;
00266 save_char: Char;
00267 no_send_ok: Byte;
00268 last_error: array[1..MYSQL_ERRMSG_SIZE] of Char;
00269 sqlstate: array[1..SQLSTATE_LENGTH + 1] of Char;
00270 last_errno: Cardinal;
00271 error: Char;
00272 query_cache_query: Pointer;
00273 report_error: Byte;
00274 return_errno: Byte;
00275 end;
00276
00277 PMYSQL_FIELD = ^MYSQL_FIELD;
00278 MYSQL_FIELD = record
00279 name: PChar;
00280 org_name: PChar;
00281 table: PChar;
00282 org_table: PChar;
00283 db: PChar;
00284 catalog: PChar;
00285 def: PChar;
00286 length: LongInt;
00287 max_length: LongInt;
00288 name_length: Cardinal;
00289 org_name_length: Cardinal;
00290 table_length: Cardinal;
00291 org_table_length: Cardinal;
00292 db_length: Cardinal;
00293 catalog_length: Cardinal;
00294 def_length: Cardinal;
00295 flags: Cardinal;
00296 decimals: Cardinal;
00297 charsetnr: Cardinal;
00298 _type: Cardinal;
00299 end;
00300
00301 MYSQL_FIELD_OFFSET = Cardinal;
00302
00303 MYSQL_ROW = array[00..$ff] of PChar;
00304 PMYSQL_ROW = ^MYSQL_ROW;
00305
00306 PMYSQL_ROWS = ^MYSQL_ROWS;
00307 MYSQL_ROWS = record
00308 next: PMYSQL_ROWS;
00309 data: PMYSQL_ROW;
00310 end;
00311
00312 MYSQL_ROW_OFFSET = PMYSQL_ROWS;
00313
00314 MYSQL_DATA = record
00315 Rows: Int64;
00316 Fields: Cardinal;
00317 Data: PMYSQL_ROWS;
00318 Alloc: MEM_ROOT;
00319 end;
00320 PMYSQL_DATA = ^MYSQL_DATA;
00321
00322 PMYSQL_OPTIONS = ^_MYSQL_OPTIONS;
00323 _MYSQL_OPTIONS = record
00324 connect_timeout: Cardinal;
00325 read_timeout: Cardinal;
00326 write_timeout: Cardinal;
00327 port: Cardinal;
00328 protocol: Cardinal;
00329 client_flag: LongInt;
00330 host: PChar;
00331 user: PChar;
00332 password: PChar;
00333 unix_socket: PChar;
00334 db: PChar;
00335 init_commands: Pointer;
00336 my_cnf_file: PChar;
00337 my_cnf_group: PChar;
00338 charset_dir: PChar;
00339 charset_name: PChar;
00340 ssl_key: PChar;
00341 ssl_cert: PChar;
00342 ssl_ca: PChar;
00343 ssl_capath: PChar;
00344 ssl_cipher: PChar;
00345 shared_memory_base_name: PChar;
00346 max_allowed_packet: LongInt;
00347 use_ssl: Byte;
00348 compress: Byte;
00349 named_pipe: Byte;
00350 rpl_probe: Byte;
00351 rpl_parse: Byte;
00352 no_master_reads: Byte;
00353 separate_thread: Byte;
00354 methods_to_use: TMySqlOption;
00355 client_ip: PChar;
00356 secure_auth: Byte;
00357 local_infile_init: Pointer;
00358 local_infile_read: Pointer;
00359 local_infile_end: Pointer;
00360 local_infile_error: Pointer;
00361 local_infile_userdata: Pointer;
00362 end;
00363
00364 PMY_CHARSET_INFO = ^MY_CHARSET_INFO;
00365 MY_CHARSET_INFO = record
00366 number: Cardinal;
00367 state: Cardinal;
00368 csname: PChar;
00369 name: PChar;
00370 comment: PChar;
00371 dir: PChar;
00372 mbminlen: Cardinal;
00373 mbmaxlen: Cardinal;
00374 end;
00375
00376 PMYSQL_METHODS = ^MYSQL_METHODS;
00377 PMYSQL = ^MYSQL;
00378
00379 {$IFDEF ENABLE_MYSQL_DEPRECATED}
00380
00381
00382 MYSQL = record
00383 _net: NET;
00384 connector_fd: Pointer;
00385 host: PChar;
00386 user: PChar;
00387 passwd: PChar;
00388 unix_socket: PChar;
00389 server_version: PChar;
00390 host_info: PChar;
00391 info: PChar;
00392 db: PChar;
00393 charset: PChar;
00394 fields: PMYSQL_FIELD;
00395 field_alloc: MEM_ROOT;
00396 affected_rows: Int64;
00397 insert_id: Int64;
00398 extra_info: Int64;
00399 thread_id: LongInt;
00400 packet_length: LongInt;
00401 port: Cardinal;
00402 client_flag: LongInt;
00403 server_capabilities: LongInt;
00404 protocol_version: Cardinal;
00405 field_count: Cardinal;
00406 server_status: Cardinal;
00407 server_language: Cardinal;
00408 warning_count: Cardinal;
00409 options: _mysql_options;
00410 status: TMySqlStatus;
00411 free_me: Byte;
00412 reconnect: Byte;
00413 scramble: array[1..SCRAMBLE_LENGTH+1] of Char;
00414 rpl_pivot: Byte;
00415 master: PMYSQL;
00416 next_slave: PMYSQL;
00417 last_used_slave: PMYSQL;
00418 last_used_con: PMYSQL;
00419 stmts: Pointer;
00420 methods: Pointer;
00421 thd: Pointer;
00422 unbuffered_fetch_owner: PByte;
00423 end;
00424 {$ELSE}
00425 MYSQL = pointer;
00426 {$ENDIF ENABLE_MYSQL_DEPRECATED}
00427
00428 PREP_STMT_STATE=(
00429 MY_ST_UNKNOWN,
00430 MY_ST_PREPARE,
00431 MY_ST_EXECUTE);
00432
00433 PMYSQL_BIND = ^MYSQL_BIND;
00434 MYSQL_BIND = record
00435 length: PLongInt;
00436 is_null: PByte;
00437 buffer: PChar;
00438 buffer_type: Cardinal;
00439 buffer_length: LongInt;
00440 inter_buffer: PByte;
00441 offset: LongInt;
00442 internal_length: LongInt;
00443 param_number: Cardinal;
00444 long_data_used: Byte;
00445 binary_data: Byte;
00446 null_field: Byte;
00447 internal_is_null: Byte;
00448 store_param_func: procedure(_net: NET; param: PMYSQL_BIND);
00449 fetch_result: procedure(param: PMYSQL_BIND; row: PMYSQL_ROW);
00450 end;
00451
00452 PMYSQL_STMT = ^MYSQL_STMT;
00453 MYSQL_STMT = record
00454 handle: PMYSQL;
00455 params: PMYSQL_BIND;
00456 result: PMYSQL_RES;
00457 bind: PMYSQL_BIND;
00458 fields: PMYSQL_FIELD;
00459 list: LIST;
00460 current_row: PByte;
00461 last_fetched_buffer: PByte;
00462 query: PChar;
00463 mem_root: MEM_ROOT;
00464 last_fetched_column: Int64;
00465 stmt_id: LongInt;
00466 last_errno: Cardinal;
00467 param_count: Cardinal;
00468 field_count: Cardinal;
00469 state: PREP_STMT_STATE;
00470 last_error: array[1..MYSQL_ERRMSG_SIZE] of Char;
00471 sqlstate: array[1..SQLSTATE_LENGTH + 1] of Char;
00472 long_alloced: Byte;
00473 send_types_to_server: Byte;
00474 param_buffers: Byte;
00475 res_buffers: Byte;
00476 result_buffered: Byte;
00477 end;
00478
00479 MYSQL_METHODS = record
00480 read_query_result: function(handle: PMYSQL): Byte;
00481 advanced_command: function(handle: PMYSQL; command: TMySqlServerCommand;
00482 header: PChar; header_length: LongInt; const arg: PChar;
00483 arg_length: LongInt; skip_check: Byte): Byte;
00484 read_rows: function( handle: PMYSQL; mysql_fields: PMYSQL_FIELD;
00485 fields: Cardinal): PMYSQL_DATA;
00486 use_result: function(handle: PMYSQL): PMYSQL_RES;
00487 fetch_lengths: procedure(_to: PLongInt; column: MYSQL_ROW;
00488 field_count: Cardinal);
00489 list_fields: function(handle: PMYSQL): PMYSQL_FIELD;
00490 read_prepare_result: function(handle: PMYSQL; stmt: PMYSQL_STMT): Byte;
00491 stmt_execute: function(stmt: PMYSQL_STMT): Integer;
00492 read_binary_rows: function(stmt: PMYSQL_STMT): PMYSQL_DATA;
00493 unbuffered_fetch: function(handle: PMYSQL; row: PMYSQL_ROW): Integer;
00494 free_embedded_thd: procedure(handle: PMYSQL);
00495 read_statisticd: function(handle: PMYSQL): PChar;
00496 end;
00497
00498 TModifyType = (MODIFY_INSERT, MODIFY_UPDATE, MODIFY_DELETE);
00499 TQuoteOptions = (QUOTE_STRIP_CR,QUOTE_STRIP_LF);
00500 TQuoteOptionsSet = set of TQuoteOptions;
00501
00502 PMYSQL_MANAGER = ^MYSQL_MANAGER;
00503 MYSQL_MANAGER = record
00504 _net: NET;
00505 host: PChar;
00506 user: PChar;
00507 passwd: PChar;
00508 port: Cardinal;
00509 free_me: Byte;
00510 eof: Byte;
00511 cmd_status: Integer;
00512 last_errno: Integer;
00513 net_buf: PChar;
00514 net_buf_pos: PChar;
00515 net_data_end: PChar;
00516 net_buf_size: Integer;
00517 last_error: array[1..MAX_MYSQL_MANAGER_ERR] of Char;
00518 end;
00519
00520 { Options for mysql_set_option }
00521 TMySqlSetOption = (
00522 MYSQL_OPTION_MULTI_STATEMENTS_ON,
00523 MYSQL_OPTION_MULTI_STATEMENTS_OFF
00524 );
00525
00526 { ************** Plain API Function types definition ************* }
00527 {$I ZPlainMysqlFunc.inc}
00528
00529 { ************** Collection of Plain API Function types definition ************* }
00530 MYSQL5_API = record
00531 {$I ZPlainMysqlVar.inc}
00532 END;
00533
00534 type
00535 {** Implements a loader for MySQL native library. }
00536 TZMySQLNativeLibraryLoader = class (TZNativeLibraryLoader)
00537 public
00538 api_rec : MYSQL5_API;
00539 destructor Destroy; override;
00540 function Load: Boolean; override;
00541 end;
00542
00543 var
00544
00545 LibraryLoader: TZMySQLNativeLibraryLoader;
00546 LibraryLoaderEmbedded: TZMySQLNativeLibraryLoader;
00547
00548 implementation
00549
00550 { TZMySQLNativeLibraryLoader }
00551
00552 {**
00553 Loads a library module.
00554 @return <code>True</code> if library was successfully loaded.
00555 }
00556 function TZMySQLNativeLibraryLoader.Load: Boolean;
00557 begin
00558 Result := inherited Load;
00559
00560 { ************** Load adresses of API Functions ************* }
00561 {$I ZPlainMysqlRec.inc}
00562 end;
00563
00564 {**
00565 Destroys the library and cleanups the memory.
00566 }
00567 destructor TZMySQLNativeLibraryLoader.Destroy;
00568 begin
00569 if (Loaded) and (@api_rec.mysql_server_end <> nil) then
00570 api_rec.mysql_server_end;
00571 inherited Destroy;
00572 end;
00573
00574 initialization
00575 {$IFNDEF UNIX}
00576 LibraryLoader := TZMySQLNativeLibraryLoader.Create(
00577 [WINDOWS_51_DLL_LOCATION
00578 , WINDOWS_50_DLL_LOCATION
00579 {$IFNDEF MYSQL_STRICT_DLL_LOADING}
00580 , WINDOWS2_DLL_LOCATION
00581 {$ENDIF}
00582 ]);
00583 LibraryLoaderEmbedded := TZMySQLNativeLibraryLoader.Create(
00584 [WINDOWS_51_DLL_LOCATION_EMBEDDED
00585 , WINDOWS_50_DLL_LOCATION_EMBEDDED
00586 {$IFNDEF MYSQL_STRICT_DLL_LOADING}
00587 , WINDOWS2_DLL_LOCATION_EMBEDDED
00588 {$ENDIF}
00589 ]);
00590 {$ELSE}
00591 LibraryLoader := TZMySQLNativeLibraryLoader.Create(
00592 [LINUX1_DLL_LOCATION,LINUX2_DLL_LOCATION]);
00593 LibraryLoaderEmbedded := TZMySQLNativeLibraryLoader.Create(
00594 [LINUX_DLL_LOCATION_EMBEDDED]);
00595 {$ENDIF}
00596 {$UNDEF MYSQL_5_API}
00597 finalization
00598 if Assigned(LibraryLoader) then
00599 LibraryLoader.Free;
00600 if Assigned(LibraryLoaderEmbedded) then
00601 LibraryLoaderEmbedded.Free;
00602 end.