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