| 1 | // tabs = 2 | 
|---|
| 2 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 3 | // | 
|---|
| 4 | //                    MySQL Client API for Borland Delphi (version 4 and above) | 
|---|
| 5 | // | 
|---|
| 6 | //                           Pascal Interface Unit for libmySQL.dll, the | 
|---|
| 7 | //                        Client Library for MySQL AB's SQL Database Server | 
|---|
| 8 | // | 
|---|
| 9 | //                  This is a literal translation of relevant parts of MySQL AB's | 
|---|
| 10 | //                    C header files, mysql.h, mysql_com.h, and mysql_version.h | 
|---|
| 11 | // | 
|---|
| 12 | //                            Copyright (c) 1999-2002 Matthias Fichtner | 
|---|
| 13 | //                           (see license.txt for licensing information) | 
|---|
| 14 | // | 
|---|
| 15 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 16 | //                       See mysql.h for MySQL AB's copyright and GPL notice | 
|---|
| 17 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 18 | // | 
|---|
| 19 | //       17-Aug-1999  mf  Translated mysql.h                             MySQL 3.22.24 | 
|---|
| 20 | //       19-Aug-1999  mf  Corrected some type definitions                MySQL 3.22.24 | 
|---|
| 21 | //       20-Aug-1999  mf  Finished debugging the unit                    MySQL 3.22.24 | 
|---|
| 22 | //       18-Sep-1999  mf  Code maintenance for release 3.22.26a          MySQL 3.22.26a | 
|---|
| 23 | //       22-Oct-1999  mf  Code maintenance for release 3.22.28           MySQL 3.22.28 | 
|---|
| 24 | //       02-Jan-2000  mf  Code maintenance for release 3.22.29           MySQL 3.22.29 | 
|---|
| 25 | //       21-Jan-2000  mf  Code maintenance for release 3.22.30           MySQL 3.22.30 | 
|---|
| 26 | //       07-Feb-2000  mf  Code maintenance for release 3.22.31           MySQL 3.22.31 | 
|---|
| 27 | //       16-Feb-2000  mf  Code maintenance for release 3.22.32           MySQL 3.22.32 | 
|---|
| 28 | //       13-Aug-2000  mf  Code maintenance for release 3.22.34           MySQL 3.22.34 | 
|---|
| 29 | //       14-Aug-2000  mf  Reworked entire unit for first 3.23 release    MySQL 3.23.19-beta | 
|---|
| 30 | //       14-Aug-2000  mf  Added mysql_character_set_name()               MySQL 3.23.22-beta | 
|---|
| 31 | //       11-Sep-2000  mf  Added IS_NUM_FIELD and INTERNAL_NUM_FIELD      MySQL 3.23.24-beta | 
|---|
| 32 | //       08-Oct-2000  mf  Modified TMEM_ROOT, enum_server_command,       MySQL 3.23.25-beta | 
|---|
| 33 | //                        and INTERNAL_NUM_FIELD | 
|---|
| 34 | //       01-Nov-2000  mf  Code maintenance for release 3.23.27           MySQL 3.23.27-beta | 
|---|
| 35 | //       25-Nov-2000  mf  Code maintenance for release 3.23.28           MySQL 3.23.28-gamma | 
|---|
| 36 | //       05-Jan-2001  mf  Code maintenance for release 3.23.30           MySQL 3.23.30-gamma | 
|---|
| 37 | //       19-Jan-2001  mf  Code maintenance for release 3.23.31           MySQL 3.23.31 | 
|---|
| 38 | //       11-Mar-2001  mf  Added functions mysql_real_send_query(),       MySQL 3.23.33 | 
|---|
| 39 | //                        mysql_send_query(), and mysql_reap_query() | 
|---|
| 40 | //       28-Mai-2001  mf  Modified mysql_send_query(), removed           MySQL 3.23.38 | 
|---|
| 41 | //                        mysql_real_send_query(), mysql_reap_query(), | 
|---|
| 42 | //                        added mysql_read_query_result(), and fixed | 
|---|
| 43 | //                        CLIENT_TRANSACTIONS | 
|---|
| 44 | //       07-Aug-2001  mf  Code maintenance for release 3.23.40           MySQL 3.23.40 | 
|---|
| 45 | //       23-Sep-2001  mf  Code maintenance for release 3.23.42           MySQL 3.23.42 | 
|---|
| 46 | //       29-Jan-2002  mf  Added libmysql_load(), libmysql_free(),        MySQL 3.23.47 | 
|---|
| 47 | //                        libmysql_status and LIBMYSQL_ constants | 
|---|
| 48 | //                        for dynamic loading of libmySQL.dll | 
|---|
| 49 | // | 
|---|
| 50 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 51 | // | 
|---|
| 52 | //                   Latest releases of mysql.pas are made available through the | 
|---|
| 53 | //                   distribution site at: http://www.fichtner.net/delphi/mysql/ | 
|---|
| 54 | // | 
|---|
| 55 | //                  Please send questions, bug reports, and suggestions regarding | 
|---|
| 56 | //                  mysql.pas to Matthias Fichtner <mfichtner@fichtner-meyer.com> | 
|---|
| 57 | // | 
|---|
| 58 | //                      See readme.txt for an introduction and documentation. | 
|---|
| 59 | //                    See license.txt for licensing information and disclaimer. | 
|---|
| 60 | // | 
|---|
| 61 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 62 | //                     This unit is provided "as is". Use it at your own risk. | 
|---|
| 63 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 64 |  | 
|---|
| 65 | //{$DEFINE DONT_LOAD_DLL} // Added for TmySQL by Justin P. Yunke on 02/11/2002 | 
|---|
| 66 |  | 
|---|
| 67 | unit mysql; | 
|---|
| 68 |  | 
|---|
| 69 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 70 | INTERFACE | 
|---|
| 71 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 72 |  | 
|---|
| 73 | uses | 
|---|
| 74 | Windows,  // Needed for some type definitions | 
|---|
| 75 | Winsock;  // Needed for some type definitions | 
|---|
| 76 |  | 
|---|
| 77 | // ---------------- | 
|---|
| 78 | // From mysql.h ... | 
|---|
| 79 | // ---------------- | 
|---|
| 80 |  | 
|---|
| 81 | type | 
|---|
| 82 | my_bool = byte; | 
|---|
| 83 | gptr = pChar; | 
|---|
| 84 |  | 
|---|
| 85 | type | 
|---|
| 86 | PUSED_MEM = ^TUSED_MEM;  // struct for once_alloc | 
|---|
| 87 | TUSED_MEM = record | 
|---|
| 88 | next: PUSED_MEM;       // Next block in use | 
|---|
| 89 | left: longword;        // memory left in block | 
|---|
| 90 | size: longword;        // size of block | 
|---|
| 91 | end; | 
|---|
| 92 |  | 
|---|
| 93 | type | 
|---|
| 94 | error_proc = procedure; | 
|---|
| 95 |  | 
|---|
| 96 | type | 
|---|
| 97 | PMEM_ROOT = ^TMEM_ROOT; | 
|---|
| 98 | TMEM_ROOT = record | 
|---|
| 99 | free: PUSED_MEM; | 
|---|
| 100 | used: PUSED_MEM; | 
|---|
| 101 | pre_alloc: PUSED_MEM; | 
|---|
| 102 | min_malloc: longword; | 
|---|
| 103 | block_size: longword; | 
|---|
| 104 | error_handler: error_proc; | 
|---|
| 105 | end; | 
|---|
| 106 |  | 
|---|
| 107 | type | 
|---|
| 108 | my_socket = TSocket; | 
|---|
| 109 |  | 
|---|
| 110 | // -------------------- | 
|---|
| 111 | // From mysql_com.h ... | 
|---|
| 112 | // -------------------- | 
|---|
| 113 |  | 
|---|
| 114 | const | 
|---|
| 115 | NAME_LEN = 64;               // Field/table name length | 
|---|
| 116 | HOSTNAME_LENGTH = 60; | 
|---|
| 117 | USERNAME_LENGTH = 16; | 
|---|
| 118 | SERVER_VERSION_LENGTH = 60; | 
|---|
| 119 |  | 
|---|
| 120 | LOCAL_HOST = 'localhost'; | 
|---|
| 121 | LOCAL_HOST_NAMEDPIPE = '.'; | 
|---|
| 122 |  | 
|---|
| 123 | MYSQL_NAMEDPIPE = 'MySQL'; | 
|---|
| 124 | MYSQL_SERVICENAME = 'MySql'; | 
|---|
| 125 |  | 
|---|
| 126 | type | 
|---|
| 127 | enum_server_command = ( | 
|---|
| 128 | COM_SLEEP, COM_QUIT, COM_INIT_DB, COM_QUERY, | 
|---|
| 129 | COM_FIELD_LIST, COM_CREATE_DB, COM_DROP_DB, COM_REFRESH, | 
|---|
| 130 | COM_SHUTDOWN, COM_STATISTICS, | 
|---|
| 131 | COM_PROCESS_INFO, COM_CONNECT, COM_PROCESS_KILL, | 
|---|
| 132 | COM_DEBUG, COM_PING, COM_TIME, COM_DELAYED_INSERT, | 
|---|
| 133 | COM_CHANGE_USER, COM_BINLOG_DUMP, | 
|---|
| 134 | COM_TABLE_DUMP, COM_CONNECT_OUT | 
|---|
| 135 | ); | 
|---|
| 136 |  | 
|---|
| 137 | const | 
|---|
| 138 | NOT_NULL_FLAG = 1;      // Field can't be NULL | 
|---|
| 139 | PRI_KEY_FLAG = 2;       // Field is part of a primary key | 
|---|
| 140 | UNIQUE_KEY_FLAG = 4;    // Field is part of a unique key | 
|---|
| 141 | MULTIPLE_KEY_FLAG = 8;  // Field is part of a key | 
|---|
| 142 | BLOB_FLAG = 16;         // Field is a blob | 
|---|
| 143 | UNSIGNED_FLAG = 32;     // Field is unsigned | 
|---|
| 144 | ZEROFILL_FLAG = 64;     // Field is zerofill | 
|---|
| 145 | BINARY_FLAG = 128; | 
|---|
| 146 |  | 
|---|
| 147 | // The following are only sent to new clients | 
|---|
| 148 |  | 
|---|
| 149 | ENUM_FLAG = 256;            // field is an enum | 
|---|
| 150 | AUTO_INCREMENT_FLAG = 512;  // field is a autoincrement field | 
|---|
| 151 | TIMESTAMP_FLAG = 1024;      // Field is a timestamp | 
|---|
| 152 | SET_FLAG = 2048;            // field is a set | 
|---|
| 153 | NUM_FLAG = 32768;           // Field is num (for clients) | 
|---|
| 154 | PART_KEY_FLAG = 16384;      // Intern; Part of some key | 
|---|
| 155 | GROUP_FLAG = 32768;         // Intern: Group field | 
|---|
| 156 | UNIQUE_FLAG = 65536;        // Intern: Used by sql_yacc | 
|---|
| 157 |  | 
|---|
| 158 | REFRESH_GRANT = 1;     // Refresh grant tables | 
|---|
| 159 | REFRESH_LOG = 2;       // Start on new log file | 
|---|
| 160 | REFRESH_TABLES = 4;    // close all tables | 
|---|
| 161 | REFRESH_HOSTS = 8;     // Flush host cache | 
|---|
| 162 | REFRESH_STATUS = 16;   // Flush status variables | 
|---|
| 163 | REFRESH_THREADS = 32;  // Flush status variables | 
|---|
| 164 | REFRESH_SLAVE = 64;    // Reset master info and restart slave | 
|---|
| 165 | // thread | 
|---|
| 166 | REFRESH_MASTER = 128;  // Remove all bin logs in the index | 
|---|
| 167 | // and truncate the index | 
|---|
| 168 |  | 
|---|
| 169 | // The following can't be set with mysql_refresh() | 
|---|
| 170 |  | 
|---|
| 171 | REFRESH_READ_LOCK = 16384;  // Lock tables for read | 
|---|
| 172 | REFRESH_FAST = 32768;       // Intern flag | 
|---|
| 173 |  | 
|---|
| 174 | CLIENT_LONG_PASSWORD = 1;      // new more secure passwords | 
|---|
| 175 | CLIENT_FOUND_ROWS = 2;         // Found instead of affected rows | 
|---|
| 176 | CLIENT_LONG_FLAG = 4;          // Get all column flags | 
|---|
| 177 | CLIENT_CONNECT_WITH_DB = 8;    // One can specify db on connect | 
|---|
| 178 | CLIENT_NO_SCHEMA = 16;         // Don't allow database.table.column | 
|---|
| 179 | CLIENT_COMPRESS = 32;          // Can use compression protcol | 
|---|
| 180 | CLIENT_ODBC = 64;              // Odbc client | 
|---|
| 181 | CLIENT_LOCAL_FILES = 128;      // Can use LOAD DATA LOCAL | 
|---|
| 182 | CLIENT_IGNORE_SPACE = 256;     // Ignore spaces before '(' | 
|---|
| 183 | CLIENT_INTERACTIVE = 1024;     // This is an interactive client | 
|---|
| 184 | CLIENT_SSL = 2048;             // Switch to SSL after handshake | 
|---|
| 185 | CLIENT_IGNORE_SIGPIPE = 4096;  // IGNORE sigpipes | 
|---|
| 186 | CLIENT_TRANSACTIONS = 8192;    // Client knows about transactions | 
|---|
| 187 |  | 
|---|
| 188 | SERVER_STATUS_IN_TRANS = 1;    // Transaction has started | 
|---|
| 189 | SERVER_STATUS_AUTOCOMMIT = 2;  // Server in auto_commit mode | 
|---|
| 190 |  | 
|---|
| 191 | MYSQL_ERRMSG_SIZE = 200; | 
|---|
| 192 | NET_READ_TIMEOUT = 30;       // Timeout on read | 
|---|
| 193 | NET_WRITE_TIMEOUT = 60;      // Timeout on write | 
|---|
| 194 | NET_WAIT_TIMEOUT = 8*60*60;  // Wait for new query | 
|---|
| 195 |  | 
|---|
| 196 | type | 
|---|
| 197 | PVio = ^TVio; | 
|---|
| 198 | TVio = record | 
|---|
| 199 | end; | 
|---|
| 200 |  | 
|---|
| 201 | type | 
|---|
| 202 | PNET = ^TNET; | 
|---|
| 203 | TNET = record | 
|---|
| 204 | vio: PVio; | 
|---|
| 205 | fd: my_socket; | 
|---|
| 206 | fcntl: longint; | 
|---|
| 207 | buff, buff_end, write_pos, read_pos: pByte; | 
|---|
| 208 | last_error: array [0..MYSQL_ERRMSG_SIZE - 1] of char; | 
|---|
| 209 | last_errno, max_packet, timeout, pkt_nr: longword; | 
|---|
| 210 | error: byte; | 
|---|
| 211 | return_errno, compress: my_bool; | 
|---|
| 212 | no_send_ok: my_bool;  // needed if we are doing several | 
|---|
| 213 | // queries in one command ( as in LOAD TABLE ... FROM MASTER ), | 
|---|
| 214 | // and do not want to confuse the client with OK at the wrong time | 
|---|
| 215 | remain_in_buf, length, buf_length, where_b: longword; | 
|---|
| 216 | return_status: pLongword; | 
|---|
| 217 | reading_or_writing: byte; | 
|---|
| 218 | save_char: char; | 
|---|
| 219 | end; | 
|---|
| 220 |  | 
|---|
| 221 | const | 
|---|
| 222 | packet_error: longword = $ffffffff; | 
|---|
| 223 |  | 
|---|
| 224 | const | 
|---|
| 225 | FIELD_TYPE_DECIMAL = 0; | 
|---|
| 226 | FIELD_TYPE_TINY = 1; | 
|---|
| 227 | FIELD_TYPE_SHORT = 2; | 
|---|
| 228 | FIELD_TYPE_LONG = 3; | 
|---|
| 229 | FIELD_TYPE_FLOAT = 4; | 
|---|
| 230 | FIELD_TYPE_DOUBLE = 5; | 
|---|
| 231 | FIELD_TYPE_NULL = 6; | 
|---|
| 232 | FIELD_TYPE_TIMESTAMP = 7; | 
|---|
| 233 | FIELD_TYPE_LONGLONG = 8; | 
|---|
| 234 | FIELD_TYPE_INT24 = 9; | 
|---|
| 235 | FIELD_TYPE_DATE = 10; | 
|---|
| 236 | FIELD_TYPE_TIME = 11; | 
|---|
| 237 | FIELD_TYPE_DATETIME = 12; | 
|---|
| 238 | FIELD_TYPE_YEAR = 13; | 
|---|
| 239 | FIELD_TYPE_NEWDATE = 14; | 
|---|
| 240 | FIELD_TYPE_ENUM = 247; | 
|---|
| 241 | FIELD_TYPE_SET = 248; | 
|---|
| 242 | FIELD_TYPE_TINY_BLOB = 249; | 
|---|
| 243 | FIELD_TYPE_MEDIUM_BLOB = 250; | 
|---|
| 244 | FIELD_TYPE_LONG_BLOB = 251; | 
|---|
| 245 | FIELD_TYPE_BLOB = 252; | 
|---|
| 246 | FIELD_TYPE_VAR_STRING = 253; | 
|---|
| 247 | FIELD_TYPE_STRING = 254; | 
|---|
| 248 |  | 
|---|
| 249 | const | 
|---|
| 250 | FIELD_TYPE_CHAR = FIELD_TYPE_TINY;      // For compability | 
|---|
| 251 | FIELD_TYPE_INTERVAL = FIELD_TYPE_ENUM;  // For compability | 
|---|
| 252 |  | 
|---|
| 253 | type | 
|---|
| 254 | enum_field_types = FIELD_TYPE_DECIMAL..FIELD_TYPE_STRING; | 
|---|
| 255 |  | 
|---|
| 256 | // ------------------------ | 
|---|
| 257 | // From mysql_version.h ... | 
|---|
| 258 | // ------------------------ | 
|---|
| 259 |  | 
|---|
| 260 | const | 
|---|
| 261 | PROTOCOL_VERSION = 10; | 
|---|
| 262 | MYSQL_SERVER_VERSION = '3.23.47'; | 
|---|
| 263 | MYSQL_SERVER_SUFFIX = ''; | 
|---|
| 264 | FRM_VER = 6; | 
|---|
| 265 | MYSQL_VERSION_ID = 32347; | 
|---|
| 266 | MYSQL_PORT = 3306; | 
|---|
| 267 | MYSQL_UNIX_ADDR = '/tmp/mysql.sock'; | 
|---|
| 268 |  | 
|---|
| 269 | // ---------------- | 
|---|
| 270 | // From mysql.h ... | 
|---|
| 271 | // ---------------- | 
|---|
| 272 |  | 
|---|
| 273 | function IS_PRI_KEY(n: longword): boolean; | 
|---|
| 274 | function IS_NOT_NULL(n: longword): boolean; | 
|---|
| 275 | function IS_BLOB(n: longword): boolean; | 
|---|
| 276 | function IS_NUM(t: longword): boolean; | 
|---|
| 277 |  | 
|---|
| 278 | type | 
|---|
| 279 | PMYSQL_FIELD = ^TMYSQL_FIELD; | 
|---|
| 280 | TMYSQL_FIELD = record | 
|---|
| 281 | name: pChar;              // Name of column | 
|---|
| 282 | table: pChar;             // Table of column if column was a field | 
|---|
| 283 | def: pChar;               // Default value (set by mysql_list_fields) | 
|---|
| 284 | _type: enum_field_types;  // Type of field. Se mysql_com.h for types | 
|---|
| 285 | length: longword;         // Width of column | 
|---|
| 286 | max_length: longword;     // Max width of selected set | 
|---|
| 287 | flags: longword;          // Div flags | 
|---|
| 288 | decimals: longword;       // Number of decimals in field | 
|---|
| 289 | end; | 
|---|
| 290 |  | 
|---|
| 291 | function IS_NUM_FIELD(f: PMYSQL_FIELD): boolean; | 
|---|
| 292 | function INTERNAL_NUM_FIELD(f: PMYSQL_FIELD): boolean; | 
|---|
| 293 |  | 
|---|
| 294 | type | 
|---|
| 295 | PMYSQL_ROW = ^TMYSQL_ROW;  // return data as array of strings | 
|---|
| 296 | TMYSQL_ROW = array[0..MaxInt div SizeOf(pChar) - 1] of pChar; | 
|---|
| 297 |  | 
|---|
| 298 | type | 
|---|
| 299 | MYSQL_FIELD_OFFSET = longword;  // offset to current field | 
|---|
| 300 |  | 
|---|
| 301 | type | 
|---|
| 302 | my_ulonglong = int64; | 
|---|
| 303 |  | 
|---|
| 304 | const | 
|---|
| 305 | MYSQL_COUNT_ERROR: my_ulonglong = not 0; | 
|---|
| 306 |  | 
|---|
| 307 | type | 
|---|
| 308 | PMYSQL_ROWS = ^TMYSQL_ROWS; | 
|---|
| 309 | TMYSQL_ROWS = record | 
|---|
| 310 | next: PMYSQL_ROWS;  // list of rows | 
|---|
| 311 | data: PMYSQL_ROW; | 
|---|
| 312 | end; | 
|---|
| 313 |  | 
|---|
| 314 | type | 
|---|
| 315 | MYSQL_ROW_OFFSET = PMYSQL_ROWS;  // offset to current row | 
|---|
| 316 |  | 
|---|
| 317 | type | 
|---|
| 318 | PMYSQL_DATA = ^TMYSQL_DATA; | 
|---|
| 319 | TMYSQL_DATA = record | 
|---|
| 320 | rows: my_ulonglong; | 
|---|
| 321 | fields: longword; | 
|---|
| 322 | data: PMYSQL_ROWS; | 
|---|
| 323 | alloc: TMEM_ROOT; | 
|---|
| 324 | end; | 
|---|
| 325 |  | 
|---|
| 326 | type | 
|---|
| 327 | PMYSQL_OPTIONS = ^TMYSQL_OPTIONS; | 
|---|
| 328 | TMYSQL_OPTIONS = record | 
|---|
| 329 | connect_timeout, client_flag: longword; | 
|---|
| 330 | compress, named_pipe: my_bool; | 
|---|
| 331 | port: longword; | 
|---|
| 332 | host, init_command, user, password, unix_socket, db: pChar; | 
|---|
| 333 | my_cnf_file, my_cnf_group, charset_dir, charset_name: pChar; | 
|---|
| 334 | use_ssl: my_bool;   // if to use SSL or not | 
|---|
| 335 | ssl_key: pChar;     // PEM key file | 
|---|
| 336 | ssl_cert: pChar;    // PEM cert file | 
|---|
| 337 | ssl_ca: pChar;      // PEM CA file | 
|---|
| 338 | ssl_capath: pChar;  // PEM directory of CA-s? | 
|---|
| 339 | end; | 
|---|
| 340 |  | 
|---|
| 341 | type | 
|---|
| 342 | mysql_option = ( | 
|---|
| 343 | MYSQL_OPT_CONNECT_TIMEOUT, MYSQL_OPT_COMPRESS, | 
|---|
| 344 | MYSQL_OPT_NAMED_PIPE, MYSQL_INIT_COMMAND, | 
|---|
| 345 | MYSQL_READ_DEFAULT_FILE, MYSQL_READ_DEFAULT_GROUP, | 
|---|
| 346 | MYSQL_SET_CHARSET_DIR, MYSQL_SET_CHARSET_NAME | 
|---|
| 347 | ); | 
|---|
| 348 |  | 
|---|
| 349 | type | 
|---|
| 350 | mysql_status = ( | 
|---|
| 351 | MYSQL_STATUS_READY, MYSQL_STATUS_GET_RESULT, | 
|---|
| 352 | MYSQL_STATUS_USE_RESULT | 
|---|
| 353 | ); | 
|---|
| 354 |  | 
|---|
| 355 | type | 
|---|
| 356 | PMYSQL_FIELDS = ^TMYSQL_FIELDS; | 
|---|
| 357 | TMYSQL_FIELDS = array[0..MaxInt div SizeOf(TMYSQL_FIELD) - 1] of TMYSQL_FIELD; | 
|---|
| 358 |  | 
|---|
| 359 | type | 
|---|
| 360 | PCHARSET_INFO = ^TCHARSET_INFO; | 
|---|
| 361 | TCHARSET_INFO = record | 
|---|
| 362 | // Omitted: Structure not necessarily needed. | 
|---|
| 363 | // Definition of struct charset_info_st can be | 
|---|
| 364 | // found in include/m_ctype.h | 
|---|
| 365 | end; | 
|---|
| 366 |  | 
|---|
| 367 | type | 
|---|
| 368 | PMYSQL = ^TMYSQL; | 
|---|
| 369 | TMYSQL = record | 
|---|
| 370 | net: TNET;                    // Communication parameters | 
|---|
| 371 | connector_fd: gptr;           // ConnectorFd for SSL | 
|---|
| 372 | host, user, passwd, unix_socket, server_version, host_info, info, db: pChar; | 
|---|
| 373 | port, client_flag, server_capabilities: longword; | 
|---|
| 374 | protocol_version: longword; | 
|---|
| 375 | field_count: longword; | 
|---|
| 376 | server_status: longword; | 
|---|
| 377 | thread_id: longword;          // Id for connection in server | 
|---|
| 378 | affected_rows: my_ulonglong; | 
|---|
| 379 | insert_id: my_ulonglong;      // id if insert on table with NEXTNR | 
|---|
| 380 | extra_info: my_ulonglong;     // Used by mysqlshow | 
|---|
| 381 | packet_length: longword; | 
|---|
| 382 | status: mysql_status; | 
|---|
| 383 | fields: PMYSQL_FIELDS; | 
|---|
| 384 | field_alloc: TMEM_ROOT; | 
|---|
| 385 | free_me: my_bool;             // If free in mysql_close | 
|---|
| 386 | reconnect: my_bool;           // set to 1 if automatic reconnect | 
|---|
| 387 | options: TMYSQL_OPTIONS; | 
|---|
| 388 | scramble_buff: array [0..8] of char; | 
|---|
| 389 | charset: PCHARSET_INFO; | 
|---|
| 390 | server_language: longword; | 
|---|
| 391 | end; | 
|---|
| 392 |  | 
|---|
| 393 | type | 
|---|
| 394 | PMYSQL_RES = ^TMYSQL_RES; | 
|---|
| 395 | TMYSQL_RES = record | 
|---|
| 396 | row_count: my_ulonglong; | 
|---|
| 397 | field_count, current_field: longword; | 
|---|
| 398 | fields: PMYSQL_FIELDS; | 
|---|
| 399 | data: PMYSQL_DATA; | 
|---|
| 400 | data_cursor: PMYSQL_ROWS; | 
|---|
| 401 | field_alloc: TMEM_ROOT; | 
|---|
| 402 | row: PMYSQL_ROW;          // If unbuffered read | 
|---|
| 403 | current_row: PMYSQL_ROW;  // buffer to current row | 
|---|
| 404 | lengths: pLongword;       // column lengths of current row | 
|---|
| 405 | handle: PMYSQL;           // for unbuffered reads | 
|---|
| 406 | eof: my_bool;             // Used my mysql_fetch_row | 
|---|
| 407 | end; | 
|---|
| 408 |  | 
|---|
| 409 | // Functions to get information from the MYSQL and MYSQL_RES structures | 
|---|
| 410 | // Should definitely be used if one uses shared libraries | 
|---|
| 411 |  | 
|---|
| 412 | var | 
|---|
| 413 | mysql_num_rows: function(res: PMYSQL_RES): my_ulonglong; stdcall; | 
|---|
| 414 | mysql_num_fields: function(res: PMYSQL_RES): longword; stdcall; | 
|---|
| 415 | mysql_eof: function(res: PMYSQL_RES): my_bool; stdcall; | 
|---|
| 416 | mysql_fetch_field_direct: function(res: PMYSQL_RES; fieldnr: longword): PMYSQL_FIELD; stdcall; | 
|---|
| 417 | mysql_fetch_fields: function(res: PMYSQL_RES): PMYSQL_FIELDS; stdcall; | 
|---|
| 418 | mysql_row_tell: function(res: PMYSQL_RES): PMYSQL_ROWS; stdcall; | 
|---|
| 419 | mysql_field_tell: function(res: PMYSQL_RES): longword; stdcall; | 
|---|
| 420 |  | 
|---|
| 421 | var | 
|---|
| 422 | mysql_field_count: function(_mysql: PMYSQL): longword; stdcall; | 
|---|
| 423 | mysql_affected_rows: function(_mysql: PMYSQL): my_ulonglong; stdcall; | 
|---|
| 424 | mysql_insert_id: function(_mysql: PMYSQL): my_ulonglong; stdcall; | 
|---|
| 425 | mysql_errno: function(_mysql: PMYSQL): longword; stdcall; | 
|---|
| 426 | mysql_error: function(_mysql: PMYSQL): pChar; stdcall; | 
|---|
| 427 | mysql_info: function(_mysql: PMYSQL): pChar; stdcall; | 
|---|
| 428 | mysql_thread_id: function(_mysql: PMYSQL): longword; stdcall; | 
|---|
| 429 | mysql_character_set_name: function(_mysql: PMYSQL): pChar; stdcall; | 
|---|
| 430 |  | 
|---|
| 431 | type | 
|---|
| 432 | PMYSQL_LENGTHS = ^TMYSQL_LENGTHS; | 
|---|
| 433 | TMYSQL_LENGTHS = array[0..MaxInt div SizeOf(longword) - 1] of longword; | 
|---|
| 434 |  | 
|---|
| 435 | type | 
|---|
| 436 | extend_buffer_func = function(void: pointer; _to: pChar; length: pLongword): pChar; | 
|---|
| 437 |  | 
|---|
| 438 | var | 
|---|
| 439 | mysql_init: function(_mysql: PMYSQL): PMYSQL; stdcall; | 
|---|
| 440 | {$IFDEF HAVE_OPENSSL} | 
|---|
| 441 | mysql_ssl_set: function(_mysql: PMYSQL; const key, cert, ca, capath: pChar): longint; stdcall; | 
|---|
| 442 | mysql_ssl_cipher: function(_mysql: PMYSQL): pChar; stdcall; | 
|---|
| 443 | mysql_ssl_clear: function(_mysql: PMYSQL): longint; stdcall; | 
|---|
| 444 | {$ENDIF} // HAVE_OPENSSL | 
|---|
| 445 | mysql_connect: function(_mysql: PMYSQL; const host, user, passwd: pChar): PMYSQL; stdcall; | 
|---|
| 446 | mysql_change_user: function(_mysql: PMYSQL; const user, passwd, db: pChar): my_bool; stdcall; | 
|---|
| 447 | mysql_real_connect: function(_mysql: PMYSQL; const host, user, passwd, db: pChar; port: longword; const unix_socket: pChar; clientflag: longword): PMYSQL; stdcall; | 
|---|
| 448 | mysql_close: procedure(sock: PMYSQL); stdcall; | 
|---|
| 449 | mysql_select_db: function(_mysql: PMYSQL; const db: pChar): longint; stdcall; | 
|---|
| 450 | mysql_query: function(_mysql: PMYSQL; const q: pChar): longint; stdcall; | 
|---|
| 451 | mysql_send_query: function(_mysql: PMYSQL; const q: pChar; length: longword): longint; stdcall; | 
|---|
| 452 | mysql_read_query_result: function(_mysql: PMYSQL): longint; stdcall; | 
|---|
| 453 | mysql_real_query: function(_mysql: PMYSQL; const q: pChar; length: longword): longint; stdcall; | 
|---|
| 454 | mysql_create_db: function(_mysql: PMYSQL; const DB: pChar): longint; stdcall; | 
|---|
| 455 | mysql_drop_db: function(_mysql: PMYSQL; const DB: pChar): longint; stdcall; | 
|---|
| 456 | mysql_shutdown: function(_mysql: PMYSQL): longint; stdcall; | 
|---|
| 457 | mysql_dump_debug_info: function(_mysql: PMYSQL): longint; stdcall; | 
|---|
| 458 | mysql_refresh: function(_mysql: PMYSQL; refresh_options: longword): longint; stdcall; | 
|---|
| 459 | mysql_kill: function(_mysql: PMYSQL; pid: longword): longint; stdcall; | 
|---|
| 460 | mysql_ping: function(_mysql: PMYSQL): longint; stdcall; | 
|---|
| 461 | mysql_stat: function(_mysql: PMYSQL): pChar; stdcall; | 
|---|
| 462 | mysql_get_server_info: function(_mysql: PMYSQL): pChar; stdcall; | 
|---|
| 463 | mysql_get_client_info: function: pChar; stdcall; | 
|---|
| 464 | mysql_get_host_info: function(_mysql: PMYSQL): pChar; stdcall; | 
|---|
| 465 | mysql_get_proto_info: function(_mysql: PMYSQL): longword; stdcall; | 
|---|
| 466 | mysql_list_dbs: function(_mysql: PMYSQL; const wild: pChar): PMYSQL_RES; stdcall; | 
|---|
| 467 | mysql_list_tables: function(_mysql: PMYSQL; const wild: pChar): PMYSQL_RES; stdcall; | 
|---|
| 468 | mysql_list_fields: function(_mysql: PMYSQL; const table, wild: pChar): PMYSQL_RES; stdcall; | 
|---|
| 469 | mysql_list_processes: function(_mysql: PMYSQL): PMYSQL_RES; stdcall; | 
|---|
| 470 | mysql_store_result: function(_mysql: PMYSQL): PMYSQL_RES; stdcall; | 
|---|
| 471 | mysql_use_result: function(_mysql: PMYSQL): PMYSQL_RES; stdcall; | 
|---|
| 472 | mysql_options: function(_mysql: PMYSQL; option: mysql_option; const arg: pChar): longint; stdcall; | 
|---|
| 473 | mysql_free_result: procedure(result: PMYSQL_RES); stdcall; | 
|---|
| 474 | mysql_data_seek: procedure(result: PMYSQL_RES; offset: my_ulonglong); stdcall; | 
|---|
| 475 | mysql_row_seek: function(result: PMYSQL_RES; offset: MYSQL_ROW_OFFSET): MYSQL_ROW_OFFSET; stdcall; | 
|---|
| 476 | mysql_field_seek: function(result: PMYSQL_RES; offset: MYSQL_FIELD_OFFSET): MYSQL_FIELD_OFFSET; stdcall; | 
|---|
| 477 | mysql_fetch_row: function(result: PMYSQL_RES): PMYSQL_ROW; stdcall; | 
|---|
| 478 | mysql_fetch_lengths: function(result: PMYSQL_RES): PMYSQL_LENGTHS; stdcall; | 
|---|
| 479 | mysql_fetch_field: function(result: PMYSQL_RES): PMYSQL_FIELD; stdcall; | 
|---|
| 480 | mysql_escape_string: function(_to: pChar; const from: pChar; from_length: longword): longword; stdcall; | 
|---|
| 481 | mysql_real_escape_string: function(_mysql: PMYSQL; _to: pChar; const from: pChar; length: longword): longword; stdcall; | 
|---|
| 482 | mysql_debug: procedure(const debug: pChar); stdcall; | 
|---|
| 483 | mysql_odbc_escape_string: function(_mysql: PMYSQL; _to: pChar; to_length: longword; const from: pChar; from_length: longword; param: pointer; extend_buffer: extend_buffer_func): pChar; stdcall; | 
|---|
| 484 | myodbc_remove_escape: procedure(_mysql: PMYSQL; name: pChar); stdcall; | 
|---|
| 485 | mysql_thread_safe: function: longword; stdcall; | 
|---|
| 486 |  | 
|---|
| 487 | function mysql_reload(_mysql: PMySQL): longint; | 
|---|
| 488 |  | 
|---|
| 489 | // Status codes for libmySQL.dll | 
|---|
| 490 |  | 
|---|
| 491 | const | 
|---|
| 492 | LIBMYSQL_UNDEFINED = 0;     // libmysql_load() has not yet been called | 
|---|
| 493 | LIBMYSQL_MISSING = 1;       // No suitable DLL could be located | 
|---|
| 494 | LIBMYSQL_INCOMPATIBLE = 2;  // A DLL was found but it is not compatible | 
|---|
| 495 | LIBMYSQL_READY = 3;         // The DLL was loaded successfully | 
|---|
| 496 |  | 
|---|
| 497 | var | 
|---|
| 498 | libmysql_handle: HMODULE = 0; | 
|---|
| 499 | libmysql_status: byte = LIBMYSQL_UNDEFINED; | 
|---|
| 500 |  | 
|---|
| 501 | function libmysql_load(name: pChar): byte; | 
|---|
| 502 | procedure libmysql_free; | 
|---|
| 503 |  | 
|---|
| 504 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 505 | IMPLEMENTATION | 
|---|
| 506 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 507 |  | 
|---|
| 508 | function IS_PRI_KEY(n: longword): boolean; | 
|---|
| 509 | begin | 
|---|
| 510 | Result := (n and PRI_KEY_FLAG) = PRI_KEY_FLAG; | 
|---|
| 511 | end; | 
|---|
| 512 |  | 
|---|
| 513 | function IS_NOT_NULL(n: longword): boolean; | 
|---|
| 514 | begin | 
|---|
| 515 | Result := (n and NOT_NULL_FLAG) = NOT_NULL_FLAG; | 
|---|
| 516 | end; | 
|---|
| 517 |  | 
|---|
| 518 | function IS_BLOB(n: longword): boolean; | 
|---|
| 519 | begin | 
|---|
| 520 | Result := (n and BLOB_FLAG) = BLOB_FLAG; | 
|---|
| 521 | end; | 
|---|
| 522 |  | 
|---|
| 523 | function IS_NUM(t: longword): boolean; | 
|---|
| 524 | begin | 
|---|
| 525 | Result := (t <= FIELD_TYPE_INT24) or (t = FIELD_TYPE_YEAR); | 
|---|
| 526 | end; | 
|---|
| 527 |  | 
|---|
| 528 | function IS_NUM_FIELD(f: PMYSQL_FIELD): boolean; | 
|---|
| 529 | begin | 
|---|
| 530 | Result := (f.flags and NUM_FLAG) = NUM_FLAG; | 
|---|
| 531 | end; | 
|---|
| 532 |  | 
|---|
| 533 | function INTERNAL_NUM_FIELD(f: PMYSQL_FIELD): boolean; | 
|---|
| 534 | begin | 
|---|
| 535 | Result := (((f._type <= FIELD_TYPE_INT24) and ((f._type <> FIELD_TYPE_TIMESTAMP) or (f.length = 14) or (f.length = 8))) or (f._type = FIELD_TYPE_YEAR)); | 
|---|
| 536 | end; | 
|---|
| 537 |  | 
|---|
| 538 | function mysql_reload(_mysql: PMYSQL): longint; | 
|---|
| 539 | begin | 
|---|
| 540 | Result := mysql_refresh(_mysql, REFRESH_GRANT); | 
|---|
| 541 | end; | 
|---|
| 542 |  | 
|---|
| 543 | function libmysql_load(name: pChar): byte; | 
|---|
| 544 |  | 
|---|
| 545 | procedure assign_proc(var proc: FARPROC; name: pChar); | 
|---|
| 546 | begin | 
|---|
| 547 | proc := GetProcAddress(libmysql_handle, name); | 
|---|
| 548 | if proc = nil then libmysql_status := LIBMYSQL_INCOMPATIBLE; | 
|---|
| 549 | end; | 
|---|
| 550 |  | 
|---|
| 551 | begin | 
|---|
| 552 | libmysql_free; | 
|---|
| 553 | if name = nil then name := 'libmysql.dll'; | 
|---|
| 554 | libmysql_handle := LoadLibrary(name); | 
|---|
| 555 | if libmysql_handle = 0 then libmysql_status := LIBMYSQL_MISSING | 
|---|
| 556 | else begin | 
|---|
| 557 | libmysql_status := LIBMYSQL_READY; | 
|---|
| 558 | assign_proc(@mysql_num_rows, 'mysql_num_rows'); | 
|---|
| 559 | assign_proc(@mysql_num_fields, 'mysql_num_fields'); | 
|---|
| 560 | assign_proc(@mysql_eof, 'mysql_eof'); | 
|---|
| 561 | assign_proc(@mysql_fetch_field_direct, 'mysql_fetch_field_direct'); | 
|---|
| 562 | assign_proc(@mysql_fetch_fields, 'mysql_fetch_fields'); | 
|---|
| 563 | assign_proc(@mysql_row_tell, 'mysql_row_tell'); | 
|---|
| 564 | assign_proc(@mysql_field_tell, 'mysql_field_tell'); | 
|---|
| 565 | assign_proc(@mysql_field_count, 'mysql_field_count'); | 
|---|
| 566 | assign_proc(@mysql_affected_rows, 'mysql_affected_rows'); | 
|---|
| 567 | assign_proc(@mysql_insert_id, 'mysql_insert_id'); | 
|---|
| 568 | assign_proc(@mysql_errno, 'mysql_errno'); | 
|---|
| 569 | assign_proc(@mysql_error, 'mysql_error'); | 
|---|
| 570 | assign_proc(@mysql_info, 'mysql_info'); | 
|---|
| 571 | assign_proc(@mysql_thread_id, 'mysql_thread_id'); | 
|---|
| 572 | assign_proc(@mysql_character_set_name, 'mysql_character_set_name'); | 
|---|
| 573 | assign_proc(@mysql_init, 'mysql_init'); | 
|---|
| 574 | {$IFDEF HAVE_OPENSSL} | 
|---|
| 575 | assign_proc(@mysql_ssl_set, 'mysql_ssl_set'); | 
|---|
| 576 | assign_proc(@mysql_ssl_cipher, 'mysql_ssl_cipher'); | 
|---|
| 577 | assign_proc(@mysql_ssl_clear, 'mysql_ssl_clear'); | 
|---|
| 578 | {$ENDIF} // HAVE_OPENSSL | 
|---|
| 579 | assign_proc(@mysql_connect, 'mysql_connect'); | 
|---|
| 580 | assign_proc(@mysql_change_user, 'mysql_change_user'); | 
|---|
| 581 | assign_proc(@mysql_real_connect, 'mysql_real_connect'); | 
|---|
| 582 | assign_proc(@mysql_close, 'mysql_close'); | 
|---|
| 583 | assign_proc(@mysql_select_db, 'mysql_select_db'); | 
|---|
| 584 | assign_proc(@mysql_query, 'mysql_query'); | 
|---|
| 585 | assign_proc(@mysql_send_query, 'mysql_send_query'); | 
|---|
| 586 | assign_proc(@mysql_read_query_result, 'mysql_read_query_result'); | 
|---|
| 587 | assign_proc(@mysql_real_query, 'mysql_real_query'); | 
|---|
| 588 | assign_proc(@mysql_create_db, 'mysql_create_db'); | 
|---|
| 589 | assign_proc(@mysql_drop_db, 'mysql_drop_db'); | 
|---|
| 590 | assign_proc(@mysql_shutdown, 'mysql_shutdown'); | 
|---|
| 591 | assign_proc(@mysql_dump_debug_info, 'mysql_dump_debug_info'); | 
|---|
| 592 | assign_proc(@mysql_refresh, 'mysql_refresh'); | 
|---|
| 593 | assign_proc(@mysql_kill, 'mysql_kill'); | 
|---|
| 594 | assign_proc(@mysql_ping, 'mysql_ping'); | 
|---|
| 595 | assign_proc(@mysql_stat, 'mysql_stat'); | 
|---|
| 596 | assign_proc(@mysql_get_server_info, 'mysql_get_server_info'); | 
|---|
| 597 | assign_proc(@mysql_get_client_info, 'mysql_get_client_info'); | 
|---|
| 598 | assign_proc(@mysql_get_host_info, 'mysql_get_host_info'); | 
|---|
| 599 | assign_proc(@mysql_get_proto_info, 'mysql_get_proto_info'); | 
|---|
| 600 | assign_proc(@mysql_list_dbs, 'mysql_list_dbs'); | 
|---|
| 601 | assign_proc(@mysql_list_tables, 'mysql_list_tables'); | 
|---|
| 602 | assign_proc(@mysql_list_fields, 'mysql_list_fields'); | 
|---|
| 603 | assign_proc(@mysql_list_processes, 'mysql_list_processes'); | 
|---|
| 604 | assign_proc(@mysql_store_result, 'mysql_store_result'); | 
|---|
| 605 | assign_proc(@mysql_use_result, 'mysql_use_result'); | 
|---|
| 606 | assign_proc(@mysql_options, 'mysql_options'); | 
|---|
| 607 | assign_proc(@mysql_free_result, 'mysql_free_result'); | 
|---|
| 608 | assign_proc(@mysql_data_seek, 'mysql_data_seek'); | 
|---|
| 609 | assign_proc(@mysql_row_seek, 'mysql_row_seek'); | 
|---|
| 610 | assign_proc(@mysql_field_seek, 'mysql_field_seek'); | 
|---|
| 611 | assign_proc(@mysql_fetch_row, 'mysql_fetch_row'); | 
|---|
| 612 | assign_proc(@mysql_fetch_lengths, 'mysql_fetch_lengths'); | 
|---|
| 613 | assign_proc(@mysql_fetch_field, 'mysql_fetch_field'); | 
|---|
| 614 | assign_proc(@mysql_escape_string, 'mysql_escape_string'); | 
|---|
| 615 | assign_proc(@mysql_real_escape_string, 'mysql_real_escape_string'); | 
|---|
| 616 | assign_proc(@mysql_debug, 'mysql_debug'); | 
|---|
| 617 | assign_proc(@mysql_odbc_escape_string, 'mysql_odbc_escape_string'); | 
|---|
| 618 | assign_proc(@myodbc_remove_escape, 'myodbc_remove_escape'); | 
|---|
| 619 | assign_proc(@mysql_thread_safe, 'mysql_thread_safe'); | 
|---|
| 620 | end; | 
|---|
| 621 | Result := libmysql_status; | 
|---|
| 622 | end; | 
|---|
| 623 |  | 
|---|
| 624 | procedure libmysql_free; | 
|---|
| 625 | begin | 
|---|
| 626 | if libmysql_handle <> 0 then FreeLibrary(libmysql_handle); | 
|---|
| 627 | libmysql_handle := 0; | 
|---|
| 628 | libmysql_status := LIBMYSQL_UNDEFINED; | 
|---|
| 629 | end; | 
|---|
| 630 |  | 
|---|
| 631 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 632 | INITIALIZATION | 
|---|
| 633 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 634 |  | 
|---|
| 635 | begin | 
|---|
| 636 | {$IFNDEF DONT_LOAD_DLL} | 
|---|
| 637 | libmysql_load(nil); | 
|---|
| 638 | {$ENDIF} // DONT_LOAD_DLL | 
|---|
| 639 | end; | 
|---|
| 640 |  | 
|---|
| 641 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 642 | FINALIZATION | 
|---|
| 643 | // ----------------------------------------------------------------------------------------------- | 
|---|
| 644 |  | 
|---|
| 645 | begin | 
|---|
| 646 | libmysql_free; | 
|---|
| 647 | end; | 
|---|
| 648 |  | 
|---|
| 649 | end. | 
|---|
| 650 |  | 
|---|