📄 mysql.c
字号:
// Copyright (c) 2004 David Muse// See the file COPYING for more information#include <sqlrelay/sqlrclient.h>#include <rudiments/charstring.h>#include <rudiments/rawbuffer.h>#define NEED_DATATYPESTRING 1#define NEED_IS_NUMBER_TYPE_CHAR 1#define NEED_IS_UNSIGNED_TYPE_CHAR 1#define NEED_IS_BINARY_TYPE_CHAR 1#include <datatypes.h>extern "C" {//#define DEBUG_MESSAGES 1#ifdef DEBUG_MESSAGES #define debugFunction() printf("%s:%s():%d: ",__FILE__,__FUNCTION__,__LINE__); fflush(stdout); #define debugPrintf(args) printf(args); fflush(stdout);#else #define debugFunction() /* */ #define debugPrintf(args) /* */#endif#define CR_UNKNOWN_ERROR 2000#define MYSQL_NO_DATA 100#define REFRESH_GRANT 1typedef unsigned long long my_ulonglong;typedef bool my_bool;typedef my_ulonglong * MYSQL_ROW_OFFSET;typedef unsigned int MYSQL_FIELD_OFFSET;enum enum_mysql_set_option { MYSQL_SET_OPTION_UNKNOWN_OPTION };enum mysql_option { MYSQL_OPTION_UNKNOWN_OPTION };// Taken directly from mysql_com.h version 5.0.0-alpha// Back-compatible with all previous versions.enum enum_field_types { MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY, MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG, MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE, MYSQL_TYPE_NULL, MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_LONGLONG,MYSQL_TYPE_INT24, MYSQL_TYPE_DATE, MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME, MYSQL_TYPE_YEAR, MYSQL_TYPE_NEWDATE, MYSQL_TYPE_ENUM=247, MYSQL_TYPE_SET=248, MYSQL_TYPE_TINY_BLOB=249, MYSQL_TYPE_MEDIUM_BLOB=250, MYSQL_TYPE_LONG_BLOB=251, MYSQL_TYPE_BLOB=252, MYSQL_TYPE_VAR_STRING=253, MYSQL_TYPE_STRING=254, MYSQL_TYPE_GEOMETRY=255};#ifdef COMPAT_MYSQL_3// taken directly from mysql.h - 3.23.58struct MYSQL_FIELD { char *name; /* Name of column */ char *table; /* Table of column if column was a field */ char *def; /* Default value (set by mysql_list_fields) */ enum enum_field_types type; /* Type of field. Se mysql_com.h for types */ unsigned int length; /* Width of column */ unsigned int max_length; /* Max width of selected set */ unsigned int flags; /* Div flags */ unsigned int decimals; /* Number of decimals in field */};#endif#ifdef COMPAT_MYSQL_4_0// taken directly from mysql.h - 4.0.17struct MYSQL_FIELD { char *name; /* Name of column */ char *table; /* Table of column if column was a field */ char *org_table; /* Org table name if table was an alias */ char *db; /* Database for table */ char *def; /* Default value (set by mysql_list_fields) */ unsigned long length; /* Width of column */ unsigned long max_length; /* Max width of selected set */ unsigned int flags; /* Div flags */ unsigned int decimals; /* Number of decimals in field */ enum enum_field_types type; /* Type of field. Se mysql_com.h for types */};#endif#if defined(COMPAT_MYSQL_4_1) || defined(COMPAT_MYSQL_5_0)// taken directly from mysql.h - 4.1.1-alpha (5.0.0-alpha is the same)struct MYSQL_FIELD { char *name; /* Name of column */ char *org_name; /* Original column name, if an alias */ char *table; /* Table of column if column was a field */ char *org_table; /* Org table name, if table was an alias */ char *db; /* Database for table */ char *catalog; /* Catalog for table */ char *def; /* Default value (set by mysql_list_fields) */ unsigned long length; /* Width of column */ unsigned long max_length; /* Max width of selected set */ unsigned int name_length; unsigned int org_name_length; unsigned int table_length; unsigned int org_table_length; unsigned int db_length; unsigned int catalog_length; unsigned int def_length; unsigned int flags; /* Div flags */ unsigned int decimals; /* Number of decimals in field */ unsigned int charsetnr; /* Character set */ enum enum_field_types type; /* Type of field. Se mysql_com.h for types */};#endif// taken directly from mysql.h - 5.0struct MYSQL_BIND { unsigned long *length; /* output length pointer */ my_bool *is_null; /* Pointer to null indicators */ char *buffer; /* buffer to get/put data */ enum enum_field_types buffer_type; /* buffer type */ unsigned long buffer_length; /* buffer length, must be set for str/binary */ /* Following are for internal use. Set by mysql_bind_param */ unsigned char *inter_buffer; /* for the current data position */ unsigned long offset; /* offset position for char/binary fetch */ unsigned long internal_length; /* Used if length is 0 */ unsigned int param_number; /* For null count and error messages */ my_bool long_data_used; /* If used with mysql_send_long_data */ my_bool binary_data; /* data buffer is binary */ my_bool null_field; /* NULL data cache flag */ my_bool internal_is_null; /* Used if is_null is 0 */ void (*store_param_func);/*(NET *net, struct MYSQL_BIND *param);*/ void (*fetch_result);/*(struct MYSQL_BIND *, unsigned char **row);*/};// This is the same for all versions of mysql that I've ever seentypedef char **MYSQL_ROW;struct MYSQL_RES { sqlrcursor *sqlrcur; // don't call this errno, some systems have a macro for errno // which will get substituted in for all errno references and give // undesirable results unsigned int errorno; my_ulonglong previousrow; my_ulonglong currentrow; MYSQL_FIELD_OFFSET currentfield; MYSQL_FIELD *fields;};struct MYSQL_STMT { MYSQL_RES *result; MYSQL_BIND *resultbinds;};// FIXME: apps access this directly! so it must be the same size and have// same members as the real struct MYSQLstruct MYSQL { const char *host; unsigned int port; const char *unix_socket; sqlrconnection *sqlrcon; MYSQL_STMT *currentstmt; bool deleteonclose; const char *error; int errorno;};unsigned int mysql_thread_safe();MYSQL *mysql_init(MYSQL *mysql);int mysql_set_server_option(MYSQL *mysql, enum enum_mysql_set_option option);int mysql_options(MYSQL *mysql, enum mysql_option option, const char *arg);int mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert, const char *ca, const char *capath, const char *cipher);MYSQL *mysql_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd);MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag);void mysql_close(MYSQL *mysql);int mysql_ping(MYSQL *mysql);char *mysql_stat(MYSQL *mysql);int mysql_shutdown(MYSQL *mysql);int mysql_reload(MYSQL *mysql);int mysql_refresh(MYSQL *mysql, unsigned int refresh_options);unsigned long mysql_thread_id(MYSQL *mysql);MYSQL_RES *mysql_list_processes(MYSQL *mysql);int mysql_kill(MYSQL *mysql, unsigned long pid);char *mysql_get_client_info();unsigned long mysql_get_client_version();char *mysql_get_host_info(MYSQL *mysql);unsigned int mysql_get_proto_info(MYSQL *mysql);char *mysql_get_server_info(MYSQL *mysql);unsigned long mysql_get_server_version(MYSQL *mysql);my_bool mysql_change_user(MYSQL *mysql, const char *user, const char *password, const char *db);const char *mysql_character_set_name(MYSQL *mysql);void mysql_debug(const char *debug);int mysql_dump_debug_info(MYSQL *mysql);int mysql_create_db(MYSQL *mysql, const char *db);int mysql_select_db(MYSQL *mysql, const char *db);int mysql_drop_db(MYSQL *mysql, const char *db);MYSQL_RES *mysql_list_dbs(MYSQL *mysql, const char *wild);MYSQL_RES *mysql_list_tables(MYSQL *mysql, const char *wild);unsigned long mysql_escape_string(char *to, const char *from, unsigned long length);char *mysql_odbc_escape_string(MYSQL *mysql, char *to, unsigned long to_length, const char *from, unsigned long from_length, void *param, char *(*extend_buffer) (void *, char *to, unsigned long *length));void myodbc_remove_escape(MYSQL *mysql, char *name);int mysql_query(MYSQL *mysql, const char *query);int mysql_send_query(MYSQL *mysql, const char *query, unsigned int length);int mysql_read_query_result(MYSQL *mysql);unsigned long mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length);int mysql_real_query(MYSQL *mysql, const char *query, unsigned long length);char *mysql_info(MYSQL *mysql);my_ulonglong mysql_insert_id(MYSQL *mysql);MYSQL_RES *mysql_store_result(MYSQL *mysql);MYSQL_RES *mysql_use_result(MYSQL *mysql);void mysql_free_result(MYSQL_RES *result);my_bool mysql_more_results(MYSQL *mysql);int mysql_next_result(MYSQL *mysql);MYSQL_RES *mysql_list_fields(MYSQL *mysql, const char *table, const char *wild);unsigned int mysql_num_fields(MYSQL_RES *result);MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result);MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *result);MYSQL_FIELD *mysql_fetch_field_direct(MYSQL_RES *result, unsigned int fieldnr);unsigned long *mysql_fetch_lengths(MYSQL_RES *result);unsigned int mysql_field_count(MYSQL *mysql);MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL_RES *result, MYSQL_FIELD_OFFSET offset);MYSQL_FIELD_OFFSET mysql_field_tell(MYSQL_RES *result);my_ulonglong mysql_num_rows(MYSQL_RES *result);my_ulonglong mysql_affected_rows(MYSQL *mysql);MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset);MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result);void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset);MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);my_bool mysql_eof(MYSQL_RES *result);unsigned int mysql_warning_count(MYSQL *mysql);unsigned int mysql_errno(MYSQL *mysql);const char *mysql_error(MYSQL *mysql);const char *mysql_sqlstate(MYSQL *mysql);my_bool mysql_commit(MYSQL *mysql);my_bool mysql_rollback(MYSQL *mysql);my_bool mysql_autocommit(MYSQL *mysql, my_bool mode);MYSQL_STMT *mysql_prepare(MYSQL *mysql, const char *query, unsigned long length);my_bool mysql_bind_param(MYSQL_STMT *stmt, MYSQL_BIND *bind);my_bool mysql_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind);int mysql_execute(MYSQL_STMT *stmt);unsigned long mysql_param_count(MYSQL_STMT *stmt);MYSQL_RES *mysql_param_result(MYSQL_STMT *stmt);int mysql_fetch(MYSQL_STMT *stmt);int mysql_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind, unsigned int column, unsigned long offset);MYSQL_RES *mysql_get_metadata(MYSQL_STMT *stmt);my_bool mysql_send_long_data(MYSQL_STMT *stmt, unsigned int parameter_number, const char *data, unsigned long length);my_ulonglong mysql_stmt_num_rows(MYSQL_STMT *stmt);my_ulonglong mysql_stmt_affected_rows(MYSQL_STMT *stmt);MYSQL_ROW_OFFSET mysql_stmt_row_seek(MYSQL_STMT *stmt, MYSQL_ROW_OFFSET offset);MYSQL_ROW_OFFSET mysql_stmt_row_tell(MYSQL_STMT *stmt);void mysql_stmt_data_seek(MYSQL_STMT *stmt, my_ulonglong offset);my_bool mysql_stmt_close(MYSQL_STMT *stmt);unsigned int mysql_stmt_errno(MYSQL_STMT *stmt);const char *mysql_stmt_error(MYSQL_STMT *stmt);const char *mysql_stmt_sqlstate(MYSQL_STMT *stmt);int mysql_stmt_store_result(MYSQL_STMT *stmt);my_bool mysql_stmt_free_result(MYSQL_STMT *stmt);my_bool mysql_stmt_reset(MYSQL_STMT *stmt);static int unknownError(MYSQL *mysql);unsigned int mysql_thread_safe() { debugFunction(); return 1;}MYSQL *mysql_init(MYSQL *mysql) { debugFunction(); if (mysql) { rawbuffer::zero(mysql,sizeof(MYSQL)); return mysql; } else { MYSQL *retval=new MYSQL; rawbuffer::zero(retval,sizeof(MYSQL)); return retval; }}int mysql_set_server_option(MYSQL *mysql, enum enum_mysql_set_option option) { debugFunction(); return 0;}int mysql_options(MYSQL *mysql, enum mysql_option option, const char *arg) { debugFunction(); return 0;}int mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert, const char *ca, const char *capath, const char *cipher) { debugFunction(); return 0;}MYSQL *mysql_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd) { debugFunction(); return mysql_real_connect(mysql,host,user,passwd,NULL,9000,NULL,0);}MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag) { debugFunction(); mysql->host=host; mysql->port=port; mysql->unix_socket=unix_socket; mysql->sqlrcon=new sqlrconnection(host,port,unix_socket, user,passwd,0,1); mysql->sqlrcon->copyReferences(); mysql->currentstmt=NULL; return mysql;}void mysql_close(MYSQL *mysql) { debugFunction(); if (mysql) { mysql_stmt_close(mysql->currentstmt); delete mysql->sqlrcon; if (mysql->deleteonclose) { delete mysql; } }}int mysql_ping(MYSQL *mysql) { debugFunction(); return !mysql->sqlrcon->ping();}char *mysql_stat(MYSQL *mysql) { debugFunction(); return "Uptime: 0 Threads: 0 Questions: 0 Slow queries: 0 Opens: 0 Flush tables: 0 Open tables: 0 Queries per second avg: 0.0";}int mysql_shutdown(MYSQL *mysql) { debugFunction(); return unknownError(mysql);}int mysql_refresh(MYSQL *mysql, unsigned int refresh_options) { debugFunction(); return (refresh_options==REFRESH_GRANT)?mysql_reload(mysql):0;}int mysql_reload(MYSQL *mysql) { debugFunction(); if (!charstring::compare(mysql->sqlrcon->identify(),"mysql")) { sqlrcursor sqlrcur(mysql->sqlrcon); return !sqlrcur.sendQuery("FLUSH PRIVILEGES"); } return 0;}unsigned long mysql_thread_id(MYSQL *mysql) { debugFunction(); return 0;}MYSQL_RES *mysql_list_processes(MYSQL *mysql) { debugFunction(); return NULL;}int mysql_kill(MYSQL *mysql, unsigned long pid) { debugFunction(); return unknownError(mysql);}char *mysql_get_client_info() { debugFunction(); // Returns a string that represents the client library version. #ifdef COMPAT_MYSQL_3 return "3.23.58"; #endif #ifdef COMPAT_MYSQL_4_0 return "4.0.17"; #endif #ifdef COMPAT_MYSQL_4_1 return "4.1.1"; #endif #ifdef COMPAT_MYSQL_5_0 return "5.0.0"; #endif}unsigned long mysql_get_client_version() { debugFunction(); // Returns an integer that represents the client library version. // The value has the format XYYZZ where X is the major version, YY is // the release level, and ZZ is the version number within the release // level. For example, a value of 40102 represents a client library // version of 4.1.2. #ifdef COMPAT_MYSQL_3 return 32358; #endif #ifdef COMPAT_MYSQL_4_0 return 40017; #endif #ifdef COMPAT_MYSQL_4_1 return 40101; #endif #ifdef COMPAT_MYSQL_5_0 return 50000; #endif}char *mysql_get_host_info(MYSQL *mysql) { debugFunction(); // Returns a string describing the type of connection in use, // including the server host name. // Should be "host via [unix|inet] socket" return "";}unsigned int mysql_get_proto_info(MYSQL *mysql) { debugFunction(); // Returns the protocol version used by current connection. #ifdef COMPAT_MYSQL_3 return 10; #endif #ifdef COMPAT_MYSQL_4_0 return 12; #endif #ifdef COMPAT_MYSQL_4_1 return 14; #endif #ifdef COMPAT_MYSQL_5_0 return 14; #endif}char *mysql_get_server_info(MYSQL *mysql) { debugFunction(); // Returns a string that represents the server version number. #ifdef COMPAT_MYSQL_3 return "3.23.58"; #endif #ifdef COMPAT_MYSQL_4_0 return "4.0.17"; #endif #ifdef COMPAT_MYSQL_4_1 return "4.1.1"; #endif #ifdef COMPAT_MYSQL_5_0 return "5.0.0"; #endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -