Annotation of parser3/src/sql/mysql/parser3mysql.C, revision 1.16
1.1 paf 1: /** @file
1.15 paf 2: Parser MySQL driver.
1.1 paf 3:
1.7 paf 4: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.1 paf 5:
1.7 paf 6: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1 paf 7:
1.16 ! parser 8: $Id: parser3mysql.C,v 1.15 2001/04/26 15:09:08 paf Exp $
1.1 paf 9: */
10:
1.14 paf 11: #include "config_includes.h"
1.4 paf 12:
1.1 paf 13: #include "pa_sql_driver.h"
1.14 paf 14:
15: #define NO_CLIENT_LONG_LONG
1.4 paf 16: #include "mysql.h"
1.14 paf 17: #include "ltdl.h"
1.1 paf 18:
1.9 paf 19: #define MAX_STRING 0x400
1.11 paf 20: #define MAX_NUMBER 20
1.9 paf 21:
1.11 paf 22: #if _MSC_VER
23: # define snprintf _snprintf
24: #endif
25:
26: static char *lsplit(char *string, char delim) {
1.4 paf 27: if(string) {
28: char *v=strchr(string, delim);
29: if(v) {
30: *v=0;
31: return v+1;
32: }
33: }
34: return 0;
35: }
36:
1.6 paf 37: /**
38: MySQL server driver
39:
1.10 paf 40: @todo figure out about memory for errors:
41: static=add multithread locks;
42: dynamic=who should free it up?
1.6 paf 43: */
1.1 paf 44: class MySQL_Driver : public SQL_Driver {
45: public:
46:
1.4 paf 47: MySQL_Driver() : SQL_Driver() {
1.3 paf 48: }
1.1 paf 49:
50: /// get api version
1.6 paf 51: int api_version() { return SQL_DRIVER_API_VERSION; }
1.14 paf 52: /// initialize driver by loading sql dynamic link library
53: const char *initialize(const char *dlopen_file_spec) {
54: return dlink(dlopen_file_spec);
55: }
1.16 ! parser 56: /** connect
! 57: @param used_only_to_connect_url
! 58: format: @b user:pass@host[:port]/database/charset
! 59: 3.23.22b
! 60: Currently the only option for @b character_set_name is cp1251_koi8.
! 61: WARNING: must be used only to connect, for buffer doesn't live long
! 62: */
1.6 paf 63: void connect(
1.16 ! parser 64: char *used_only_to_connect_url,
1.6 paf 65: void **connection ///< output: MYSQL *
1.5 paf 66: ) {
1.16 ! parser 67: char *user=used_only_to_connect_url;
1.4 paf 68: char *host=lsplit(user, '@');
69: char *db=lsplit(host, '/');
70: char *pwd=lsplit(user, ':');
71: char *error_pos=0;
72: char *port_cstr=lsplit(host, ':');
73: int port=port_cstr?strtol(port_cstr, &error_pos, 0):0;
1.9 paf 74: char *charset=lsplit(db, '/');
1.4 paf 75:
76: MYSQL *mysql=mysql_init(NULL);
77: if(!mysql_real_connect(mysql,
78: host, user, pwd, db, port?port:MYSQL_PORT, NULL, 0))
1.11 paf 79: services->_throw(mysql_error(mysql));
1.4 paf 80:
1.9 paf 81: if(charset) {
82: // set charset
83: char statement[MAX_STRING]="set CHARACTER SET "; // cp1251_koi8
84: strncat(statement, charset, MAX_STRING);
85:
86: if(mysql_query(mysql, statement))
1.11 paf 87: services->_throw(mysql_error(mysql));
1.14 paf 88: (*mysql_store_result)(mysql); // throw out the result [don't need but must call]
1.9 paf 89: }
90:
1.6 paf 91: *(MYSQL **)connection=mysql;
1.1 paf 92: }
1.6 paf 93: void disconnect(void *connection) {
94: mysql_close((MYSQL *)connection);
1.1 paf 95: }
1.6 paf 96: void commit(void *connection) {}
97: void rollback(void *connection) {}
1.7 paf 98:
1.8 paf 99: bool ping(void *connection) {
100: return mysql_ping((MYSQL *)connection)==0;
101: }
102:
1.9 paf 103: unsigned int quote(void *connection,
104: char *to, const char *from, unsigned int length) {
105: /*
106: 3.23.22b
107: You must allocate the to buffer to be at least length*2+1 bytes long.
108: (In the worse case, each character may need to be encoded as using two bytes,
109: and you need room for the terminating null byte.)
110:
111: it's already UNTAINT_TIMES_BIGGER
112: */
1.14 paf 113: return (*mysql_escape_string)(to, from, length);
1.9 paf 114: }
1.7 paf 115: void query(void *connection,
1.11 paf 116: const char *astatement, unsigned long offset, unsigned long limit,
1.7 paf 117: unsigned int *column_count, Cell **columns,
118: unsigned long *row_count, Cell ***rows) {
119:
120: MYSQL *mysql=(MYSQL *)connection;
121: MYSQL_RES *res=NULL;
1.8 paf 122:
1.11 paf 123: const char *statement;
124: if(offset || limit) {
125: size_t statement_size=strlen(astatement);
126: char *statement_limited=(char *)services->malloc(
127: statement_size+MAX_NUMBER*2+8/* limit #,#*/+1);
128: char *cur=statement_limited;
129: memcpy(cur, astatement, statement_size); cur+=statement_size;
130: cur+=sprintf(cur, " limit ");
131: if(offset)
1.13 paf 132: cur+=snprintf(cur, MAX_NUMBER+1, "%u,", offset);
1.11 paf 133: if(limit)
1.13 paf 134: cur+=snprintf(cur, MAX_NUMBER, "%u", limit);
1.11 paf 135: statement=statement_limited;
136: } else
137: statement=astatement;
138:
1.7 paf 139: if(mysql_query(mysql, statement))
1.11 paf 140: services->_throw(mysql_error(mysql));
1.7 paf 141: if(!(res=mysql_store_result(mysql)) && mysql_field_count(mysql))
1.11 paf 142: services->_throw(mysql_error(mysql));
1.7 paf 143: if(!res) {
144: // empty result
145: *row_count=0;
146: *column_count=0;
147: return;
148: }
149:
150: *column_count=mysql_num_fields(res);
1.14 paf 151: if(!*column_count) // old client
152: *column_count=mysql_field_count(mysql);
153:
1.11 paf 154: *columns=(Cell *)services->malloc(sizeof(Cell)*(*column_count));
1.7 paf 155:
156: *row_count=(unsigned long)mysql_num_rows(res);
1.11 paf 157: *rows=(Cell **)services->malloc(sizeof(Cell *)*(*row_count));
1.7 paf 158:
159: for(unsigned int i=0; i<(*column_count); i++){
160: MYSQL_FIELD *field=mysql_fetch_field(res);
161: size_t size=strlen(field->name);
162: (*columns)[i].size=size;
1.11 paf 163: (*columns)[i].ptr=services->malloc(size);
1.7 paf 164: memcpy((*columns)[i].ptr, field->name, size);
165: }
166:
167: for(unsigned long r=0; r<(*row_count); r++)
168: if(MYSQL_ROW mysql_row=mysql_fetch_row(res)) { // never false..
169: unsigned long *lengths=mysql_fetch_lengths(res);
170: Cell *row=(Cell *)malloc(sizeof(Cell)*(*column_count));
171: (*rows)[r]=row;
172: for(unsigned int i=0; i<(*column_count); i++){
173: size_t size=(size_t)lengths[i];
174: row[i].size=size;
1.11 paf 175: row[i].ptr=services->malloc(size);
1.7 paf 176: memcpy(row[i].ptr, mysql_row[i], size);
177: }
178: }
179:
180: mysql_free_result(res);
181: }
1.14 paf 182:
183: private: // mysql client library funcs
184:
185: typedef MYSQL* (STDCALL *t_mysql_init)(MYSQL *); t_mysql_init mysql_init;
186:
187: typedef MYSQL_RES* (STDCALL *t_mysql_store_result)(MYSQL *); t_mysql_store_result mysql_store_result;
188:
189: typedef int (STDCALL *t_mysql_query)(MYSQL *, const char *q); t_mysql_query mysql_query;
190:
191: typedef char * (STDCALL *t_mysql_error)(MYSQL *); t_mysql_error mysql_error;
192: static char* STDCALL subst_mysql_error(MYSQL *mysql) { return (mysql)->net.last_error; }
193:
194: typedef MYSQL* (STDCALL *t_mysql_real_connect)(MYSQL *, const char *host,
195: const char *user,
196: const char *passwd,
197: const char *db,
198: unsigned int port,
199: const char *unix_socket,
200: unsigned int clientflag); t_mysql_real_connect mysql_real_connect;
201:
202: typedef void (STDCALL *t_mysql_close)(MYSQL *); t_mysql_close mysql_close;
203:
204: typedef int (STDCALL *t_mysql_ping)(MYSQL *); t_mysql_ping mysql_ping;
205:
206: typedef unsigned long (STDCALL *t_mysql_escape_string)(char *to,const char *from,
207: unsigned long from_length); t_mysql_escape_string mysql_escape_string;
208:
209: typedef void (STDCALL *t_mysql_free_result)(MYSQL_RES *result); t_mysql_free_result mysql_free_result;
210:
211: typedef unsigned long* (STDCALL *t_mysql_fetch_lengths)(MYSQL_RES *result); t_mysql_fetch_lengths mysql_fetch_lengths;
212:
213: typedef MYSQL_ROW (STDCALL *t_mysql_fetch_row)(MYSQL_RES *result); t_mysql_fetch_row mysql_fetch_row;
214:
215: typedef MYSQL_FIELD* (STDCALL *t_mysql_fetch_field)(MYSQL_RES *result); t_mysql_fetch_field mysql_fetch_field;
216:
217: typedef my_ulonglong (STDCALL *t_mysql_num_rows)(MYSQL_RES *); t_mysql_num_rows mysql_num_rows;
218: static my_ulonglong STDCALL subst_mysql_num_rows(MYSQL_RES *res) { return res->row_count; }
219:
220: typedef unsigned int (STDCALL *t_mysql_num_fields)(MYSQL_RES *); t_mysql_num_fields mysql_num_fields;
221: static unsigned int STDCALL subst_mysql_num_fields(MYSQL_RES *res) { return res->field_count; }
222:
223: typedef unsigned int (STDCALL *t_mysql_field_count)(MYSQL *); t_mysql_field_count mysql_field_count;
224: static unsigned int STDCALL subst_mysql_field_count(MYSQL *mysql) { return mysql->field_count; }
225:
226: private: // mysql client library funcs linking
227:
228: const char *dlink(const char *dlopen_file_spec) {
229: lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
230: if (!handle)
231: return "can not open the dynamic link module";
232:
233: #define DSLINK(name, action) \
234: name=(t_##name)lt_dlsym(handle, #name); \
235: if(!name) \
236: action;
237:
238: #define DLINK(name) DSLINK(name, return "function " #name " was not found")
239: #define SLINK(name) DSLINK(name, name=subst_##name)
240:
241: DLINK(mysql_init);
242: DLINK(mysql_store_result);
243: DLINK(mysql_query);
244: SLINK(mysql_error);
245: DLINK(mysql_real_connect);
246: DLINK(mysql_close);
247: DLINK(mysql_ping);
248: DLINK(mysql_escape_string);
249: DLINK(mysql_free_result);
250: DLINK(mysql_fetch_lengths);
251: DLINK(mysql_fetch_row);
252: DLINK(mysql_fetch_field);
253: SLINK(mysql_num_rows);
254: SLINK(mysql_num_fields);
255: SLINK(mysql_field_count);
256: return 0;
257: }
258:
1.1 paf 259: };
260:
261: extern "C" SQL_Driver *create() {
262: return new MySQL_Driver();
263: }
E-mail: