📄 mysql.pas
字号:
TMYSQL_OPTIONS = record
connect_timeout, client_flag: longword;
compress, named_pipe: my_bool;
port: longword;
host, init_command, user, password, unix_socket, db: pChar;
my_cnf_file, my_cnf_group, charset_dir, charset_name: pChar;
use_ssl: my_bool; // if to use SSL or not
ssl_key: pChar; // PEM key file
ssl_cert: pChar; // PEM cert file
ssl_ca: pChar; // PEM CA file
ssl_capath: pChar; // PEM directory of CA-s?
end;
type
mysql_option = (
MYSQL_OPT_CONNECT_TIMEOUT, MYSQL_OPT_COMPRESS,
MYSQL_OPT_NAMED_PIPE, MYSQL_INIT_COMMAND,
MYSQL_READ_DEFAULT_FILE, MYSQL_READ_DEFAULT_GROUP,
MYSQL_SET_CHARSET_DIR, MYSQL_SET_CHARSET_NAME,
MYSQL_OPT_LOCAL_INFILE
);
type
mysql_status = (
MYSQL_STATUS_READY, MYSQL_STATUS_GET_RESULT,
MYSQL_STATUS_USE_RESULT
);
type
PMYSQL_FIELDS = ^TMYSQL_FIELDS;
TMYSQL_FIELDS = array[0..MaxInt div SizeOf(TMYSQL_FIELD) - 1] of TMYSQL_FIELD;
type
PCHARSET_INFO = ^TCHARSET_INFO;
TCHARSET_INFO = record
// Omitted: Structure not necessarily needed.
// Definition of struct charset_info_st can be
// found in include/m_ctype.h
end;
type
PMYSQL = ^TMYSQL;
TMYSQL = record
net: TNET; // Communication parameters
connector_fd: gptr; // ConnectorFd for SSL
host, user, passwd, unix_socket, server_version, host_info, info, db: pChar;
port, client_flag, server_capabilities: longword;
protocol_version: longword;
field_count: longword;
server_status: longword;
thread_id: longword; // Id for connection in server
affected_rows: my_ulonglong;
insert_id: my_ulonglong; // id if insert on table with NEXTNR
extra_info: my_ulonglong; // Used by mysqlshow
packet_length: longword;
status: mysql_status;
fields: PMYSQL_FIELDS;
field_alloc: TMEM_ROOT;
free_me: my_bool; // If free in mysql_close
reconnect: my_bool; // set to 1 if automatic reconnect
options: TMYSQL_OPTIONS;
scramble_buff: array [0..8] of char;
charset: PCHARSET_INFO;
server_language: longword;
end;
type
PMYSQL_RES = ^TMYSQL_RES;
TMYSQL_RES = record
row_count: my_ulonglong;
field_count, current_field: longword;
fields: PMYSQL_FIELDS;
data: PMYSQL_DATA;
data_cursor: PMYSQL_ROWS;
field_alloc: TMEM_ROOT;
row: PMYSQL_ROW; // If unbuffered read
current_row: PMYSQL_ROW; // buffer to current row
lengths: pLongword; // column lengths of current row
handle: PMYSQL; // for unbuffered reads
eof: my_bool; // Used my mysql_fetch_row
end;
// Functions to get information from the MYSQL and MYSQL_RES structures
// Should definitely be used if one uses shared libraries
var
mysql_num_rows: function(res: PMYSQL_RES): my_ulonglong; stdcall;
mysql_num_fields: function(res: PMYSQL_RES): longword; stdcall;
mysql_eof: function(res: PMYSQL_RES): my_bool; stdcall;
mysql_fetch_field_direct: function(res: PMYSQL_RES; fieldnr: longword): PMYSQL_FIELD; stdcall;
mysql_fetch_fields: function(res: PMYSQL_RES): PMYSQL_FIELDS; stdcall;
mysql_row_tell: function(res: PMYSQL_RES): PMYSQL_ROWS; stdcall;
mysql_field_tell: function(res: PMYSQL_RES): longword; stdcall;
var
mysql_field_count: function(_mysql: PMYSQL): longword; stdcall;
mysql_affected_rows: function(_mysql: PMYSQL): my_ulonglong; stdcall;
mysql_insert_id: function(_mysql: PMYSQL): my_ulonglong; stdcall;
mysql_errno: function(_mysql: PMYSQL): longword; stdcall;
mysql_error: function(_mysql: PMYSQL): pChar; stdcall;
mysql_info: function(_mysql: PMYSQL): pChar; stdcall;
mysql_thread_id: function(_mysql: PMYSQL): longword; stdcall;
mysql_character_set_name: function(_mysql: PMYSQL): pChar; stdcall;
type
PMYSQL_LENGTHS = ^TMYSQL_LENGTHS;
TMYSQL_LENGTHS = array[0..MaxInt div SizeOf(longword) - 1] of longword;
type
extend_buffer_func = function(void: pointer; _to: pChar; length: pLongword): pChar;
var
mysql_init: function(_mysql: PMYSQL): PMYSQL; stdcall;
{$IFDEF HAVE_OPENSSL}
mysql_ssl_set: function(_mysql: PMYSQL; const key, cert, ca, capath: pChar): longint; stdcall;
mysql_ssl_cipher: function(_mysql: PMYSQL): pChar; stdcall;
mysql_ssl_clear: function(_mysql: PMYSQL): longint; stdcall;
{$ENDIF} // HAVE_OPENSSL
mysql_connect: function(_mysql: PMYSQL; const host, user, passwd: pChar): PMYSQL; stdcall;
mysql_change_user: function(_mysql: PMYSQL; const user, passwd, db: pChar): my_bool; stdcall;
mysql_real_connect: function(_mysql: PMYSQL; const host, user, passwd, db: pChar; port: longword; const unix_socket: pChar; clientflag: longword): PMYSQL; stdcall;
mysql_close: procedure(sock: PMYSQL); stdcall;
mysql_select_db: function(_mysql: PMYSQL; const db: pChar): longint; stdcall;
mysql_query: function(_mysql: PMYSQL; const q: pChar): longint; stdcall;
mysql_send_query: function(_mysql: PMYSQL; const q: pChar; length: longword): longint; stdcall;
mysql_read_query_result: function(_mysql: PMYSQL): longint; stdcall;
mysql_real_query: function(_mysql: PMYSQL; const q: pChar; length: longword): longint; stdcall;
mysql_create_db: function(_mysql: PMYSQL; const DB: pChar): longint; stdcall;
mysql_drop_db: function(_mysql: PMYSQL; const DB: pChar): longint; stdcall;
mysql_shutdown: function(_mysql: PMYSQL): longint; stdcall;
mysql_dump_debug_info: function(_mysql: PMYSQL): longint; stdcall;
mysql_refresh: function(_mysql: PMYSQL; refresh_options: longword): longint; stdcall;
mysql_kill: function(_mysql: PMYSQL; pid: longword): longint; stdcall;
mysql_ping: function(_mysql: PMYSQL): longint; stdcall;
mysql_stat: function(_mysql: PMYSQL): pChar; stdcall;
mysql_get_server_info: function(_mysql: PMYSQL): pChar; stdcall;
mysql_get_client_info: function: pChar; stdcall;
mysql_get_host_info: function(_mysql: PMYSQL): pChar; stdcall;
mysql_get_proto_info: function(_mysql: PMYSQL): longword; stdcall;
mysql_list_dbs: function(_mysql: PMYSQL; const wild: pChar): PMYSQL_RES; stdcall;
mysql_list_tables: function(_mysql: PMYSQL; const wild: pChar): PMYSQL_RES; stdcall;
mysql_list_fields: function(_mysql: PMYSQL; const table, wild: pChar): PMYSQL_RES; stdcall;
mysql_list_processes: function(_mysql: PMYSQL): PMYSQL_RES; stdcall;
mysql_store_result: function(_mysql: PMYSQL): PMYSQL_RES; stdcall;
mysql_use_result: function(_mysql: PMYSQL): PMYSQL_RES; stdcall;
mysql_options: function(_mysql: PMYSQL; option: mysql_option; const arg: pChar): longint; stdcall;
mysql_free_result: procedure(result: PMYSQL_RES); stdcall;
mysql_data_seek: procedure(result: PMYSQL_RES; offset: my_ulonglong); stdcall;
mysql_row_seek: function(result: PMYSQL_RES; offset: MYSQL_ROW_OFFSET): MYSQL_ROW_OFFSET; stdcall;
mysql_field_seek: function(result: PMYSQL_RES; offset: MYSQL_FIELD_OFFSET): MYSQL_FIELD_OFFSET; stdcall;
mysql_fetch_row: function(result: PMYSQL_RES): PMYSQL_ROW; stdcall;
mysql_fetch_lengths: function(result: PMYSQL_RES): PMYSQL_LENGTHS; stdcall;
mysql_fetch_field: function(result: PMYSQL_RES): PMYSQL_FIELD; stdcall;
mysql_escape_string: function(_to: pChar; const from: pChar; from_length: longword): longword; stdcall;
mysql_real_escape_string: function(_mysql: PMYSQL; _to: pChar; const from: pChar; length: longword): longword; stdcall;
mysql_debug: procedure(const debug: pChar); stdcall;
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;
myodbc_remove_escape: procedure(_mysql: PMYSQL; name: pChar); stdcall;
mysql_thread_safe: function: longword; stdcall;
function mysql_reload(_mysql: PMySQL): longint;
// Status codes for libmySQL.dll
const
LIBMYSQL_UNDEFINED = 0; // libmysql_load() has not yet been called
LIBMYSQL_MISSING = 1; // No suitable DLL could be located
LIBMYSQL_INCOMPATIBLE = 2; // A DLL was found but it is not compatible
LIBMYSQL_READY = 3; // The DLL was loaded successfully
var
libmysql_handle: HMODULE = 0;
libmysql_status: byte = LIBMYSQL_UNDEFINED;
function libmysql_load(name: pChar): byte;
procedure libmysql_free;
// -----------------------------------------------------------------------------------------------
IMPLEMENTATION
// -----------------------------------------------------------------------------------------------
function IS_PRI_KEY(n: longword): boolean;
begin
Result := (n and PRI_KEY_FLAG) = PRI_KEY_FLAG;
end;
function IS_NOT_NULL(n: longword): boolean;
begin
Result := (n and NOT_NULL_FLAG) = NOT_NULL_FLAG;
end;
function IS_BLOB(n: longword): boolean;
begin
Result := (n and BLOB_FLAG) = BLOB_FLAG;
end;
function IS_NUM(t: longword): boolean;
begin
Result := (t <= FIELD_TYPE_INT24) or (t = FIELD_TYPE_YEAR);
end;
function IS_NUM_FIELD(f: PMYSQL_FIELD): boolean;
begin
Result := (f.flags and NUM_FLAG) = NUM_FLAG;
end;
function INTERNAL_NUM_FIELD(f: PMYSQL_FIELD): boolean;
begin
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));
end;
function mysql_reload(_mysql: PMYSQL): longint;
begin
Result := mysql_refresh(_mysql, REFRESH_GRANT);
end;
function libmysql_load(name: pChar): byte;
procedure assign_proc(var proc: FARPROC; name: pChar);
begin
proc := GetProcAddress(libmysql_handle, name);
if proc = nil then libmysql_status := LIBMYSQL_INCOMPATIBLE;
end;
begin
libmysql_free;
if name = nil then name := 'libmysql.dll';
libmysql_handle := LoadLibrary(name);
if libmysql_handle = 0 then libmysql_status := LIBMYSQL_MISSING
else begin
libmysql_status := LIBMYSQL_READY;
assign_proc(@mysql_num_rows, 'mysql_num_rows');
assign_proc(@mysql_num_fields, 'mysql_num_fields');
assign_proc(@mysql_eof, 'mysql_eof');
assign_proc(@mysql_fetch_field_direct, 'mysql_fetch_field_direct');
assign_proc(@mysql_fetch_fields, 'mysql_fetch_fields');
assign_proc(@mysql_row_tell, 'mysql_row_tell');
assign_proc(@mysql_field_tell, 'mysql_field_tell');
assign_proc(@mysql_field_count, 'mysql_field_count');
assign_proc(@mysql_affected_rows, 'mysql_affected_rows');
assign_proc(@mysql_insert_id, 'mysql_insert_id');
assign_proc(@mysql_errno, 'mysql_errno');
assign_proc(@mysql_error, 'mysql_error');
assign_proc(@mysql_info, 'mysql_info');
assign_proc(@mysql_thread_id, 'mysql_thread_id');
assign_proc(@mysql_character_set_name, 'mysql_character_set_name');
assign_proc(@mysql_init, 'mysql_init');
{$IFDEF HAVE_OPENSSL}
assign_proc(@mysql_ssl_set, 'mysql_ssl_set');
assign_proc(@mysql_ssl_cipher, 'mysql_ssl_cipher');
assign_proc(@mysql_ssl_clear, 'mysql_ssl_clear');
{$ENDIF} // HAVE_OPENSSL
assign_proc(@mysql_connect, 'mysql_connect');
assign_proc(@mysql_change_user, 'mysql_change_user');
assign_proc(@mysql_real_connect, 'mysql_real_connect');
assign_proc(@mysql_close, 'mysql_close');
assign_proc(@mysql_select_db, 'mysql_select_db');
assign_proc(@mysql_query, 'mysql_query');
assign_proc(@mysql_send_query, 'mysql_send_query');
assign_proc(@mysql_read_query_result, 'mysql_read_query_result');
assign_proc(@mysql_real_query, 'mysql_real_query');
assign_proc(@mysql_create_db, 'mysql_create_db');
assign_proc(@mysql_drop_db, 'mysql_drop_db');
assign_proc(@mysql_shutdown, 'mysql_shutdown');
assign_proc(@mysql_dump_debug_info, 'mysql_dump_debug_info');
assign_proc(@mysql_refresh, 'mysql_refresh');
assign_proc(@mysql_kill, 'mysql_kill');
assign_proc(@mysql_ping, 'mysql_ping');
assign_proc(@mysql_stat, 'mysql_stat');
assign_proc(@mysql_get_server_info, 'mysql_get_server_info');
assign_proc(@mysql_get_client_info, 'mysql_get_client_info');
assign_proc(@mysql_get_host_info, 'mysql_get_host_info');
assign_proc(@mysql_get_proto_info, 'mysql_get_proto_info');
assign_proc(@mysql_list_dbs, 'mysql_list_dbs');
assign_proc(@mysql_list_tables, 'mysql_list_tables');
assign_proc(@mysql_list_fields, 'mysql_list_fields');
assign_proc(@mysql_list_processes, 'mysql_list_processes');
assign_proc(@mysql_store_result, 'mysql_store_result');
assign_proc(@mysql_use_result, 'mysql_use_result');
assign_proc(@mysql_options, 'mysql_options');
assign_proc(@mysql_free_result, 'mysql_free_result');
assign_proc(@mysql_data_seek, 'mysql_data_seek');
assign_proc(@mysql_row_seek, 'mysql_row_seek');
assign_proc(@mysql_field_seek, 'mysql_field_seek');
assign_proc(@mysql_fetch_row, 'mysql_fetch_row');
assign_proc(@mysql_fetch_lengths, 'mysql_fetch_lengths');
assign_proc(@mysql_fetch_field, 'mysql_fetch_field');
assign_proc(@mysql_escape_string, 'mysql_escape_string');
assign_proc(@mysql_real_escape_string, 'mysql_real_escape_string');
assign_proc(@mysql_debug, 'mysql_debug');
assign_proc(@mysql_odbc_escape_string, 'mysql_odbc_escape_string');
assign_proc(@myodbc_remove_escape, 'myodbc_remove_escape');
assign_proc(@mysql_thread_safe, 'mysql_thread_safe');
end;
Result := libmysql_status;
end;
procedure libmysql_free;
begin
if libmysql_handle <> 0 then FreeLibrary(libmysql_handle);
libmysql_handle := 0;
libmysql_status := LIBMYSQL_UNDEFINED;
end;
// -----------------------------------------------------------------------------------------------
INITIALIZATION
// -----------------------------------------------------------------------------------------------
begin
{$IFNDEF DONT_LOAD_DLL}
libmysql_load(nil);
{$ENDIF} // DONT_LOAD_DLL
end;
// -----------------------------------------------------------------------------------------------
FINALIZATION
// -----------------------------------------------------------------------------------------------
begin
libmysql_free;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -