Annotation of parser3/src/sql/mysql/parser3mysql.C, revision 1.12
1.1 paf 1: /** @file
2: Parser: MySQL driver.
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.12 ! paf 8: $Id: parser3mysql.C,v 1.11 2001/04/05 20:01:27 paf Exp $
1.1 paf 9: */
10:
1.4 paf 11: #include <stdlib.h>
1.10 paf 12: #include <string.h>
1.11 paf 13: #include <stdio.h>
1.4 paf 14:
1.1 paf 15: #include "pa_sql_driver.h"
1.4 paf 16: #include "mysql.h"
1.1 paf 17:
1.9 paf 18: #define MAX_STRING 0x400
1.11 paf 19: #define MAX_NUMBER 20
1.9 paf 20:
1.11 paf 21: #if _MSC_VER
22: # define snprintf _snprintf
23: #endif
24:
25: static char *lsplit(char *string, char delim) {
1.4 paf 26: if(string) {
27: char *v=strchr(string, delim);
28: if(v) {
29: *v=0;
30: return v+1;
31: }
32: }
33: return 0;
34: }
35:
1.6 paf 36: /**
37: MySQL server driver
38:
1.10 paf 39: @todo figure out about memory for errors:
40: static=add multithread locks;
41: dynamic=who should free it up?
1.6 paf 42: */
1.1 paf 43: class MySQL_Driver : public SQL_Driver {
44: public:
45:
1.4 paf 46: MySQL_Driver() : SQL_Driver() {
1.3 paf 47: }
1.1 paf 48:
49: /// get api version
1.6 paf 50: int api_version() { return SQL_DRIVER_API_VERSION; }
1.4 paf 51: /// connect
1.6 paf 52: void connect(
1.9 paf 53: char *url, /**< @b user:pass@host[:port]/database/charset
54: 3.23.22b
55: Currently the only option for character_set_name is cp1251_koi8 */
1.6 paf 56: void **connection ///< output: MYSQL *
1.5 paf 57: ) {
1.4 paf 58: char *user=url;
59: char *host=lsplit(user, '@');
60: char *db=lsplit(host, '/');
61: char *pwd=lsplit(user, ':');
62: char *error_pos=0;
63: char *port_cstr=lsplit(host, ':');
64: int port=port_cstr?strtol(port_cstr, &error_pos, 0):0;
1.9 paf 65: char *charset=lsplit(db, '/');
1.4 paf 66:
67: MYSQL *mysql=mysql_init(NULL);
68: if(!mysql_real_connect(mysql,
69: host, user, pwd, db, port?port:MYSQL_PORT, NULL, 0))
1.11 paf 70: services->_throw(mysql_error(mysql));
1.4 paf 71:
1.9 paf 72: if(charset) {
73: // set charset
74: char statement[MAX_STRING]="set CHARACTER SET "; // cp1251_koi8
75: strncat(statement, charset, MAX_STRING);
76:
77: if(mysql_query(mysql, statement))
1.11 paf 78: services->_throw(mysql_error(mysql));
1.9 paf 79: mysql_store_result(mysql); // throw out the result [don't need but must call]
80: }
81:
1.6 paf 82: *(MYSQL **)connection=mysql;
1.1 paf 83: }
1.6 paf 84: void disconnect(void *connection) {
85: mysql_close((MYSQL *)connection);
1.1 paf 86: }
1.6 paf 87: void commit(void *connection) {}
88: void rollback(void *connection) {}
1.7 paf 89:
1.8 paf 90: bool ping(void *connection) {
91: return mysql_ping((MYSQL *)connection)==0;
92: }
93:
1.9 paf 94: unsigned int quote(void *connection,
95: char *to, const char *from, unsigned int length) {
96: /*
97: 3.23.22b
98: You must allocate the to buffer to be at least length*2+1 bytes long.
99: (In the worse case, each character may need to be encoded as using two bytes,
100: and you need room for the terminating null byte.)
101:
102: it's already UNTAINT_TIMES_BIGGER
103: */
104: return mysql_escape_string(to, from, length);
105: }
1.7 paf 106: void query(void *connection,
1.11 paf 107: const char *astatement, unsigned long offset, unsigned long limit,
1.7 paf 108: unsigned int *column_count, Cell **columns,
109: unsigned long *row_count, Cell ***rows) {
110:
111: MYSQL *mysql=(MYSQL *)connection;
112: MYSQL_RES *res=NULL;
1.8 paf 113:
1.11 paf 114: const char *statement;
115: if(offset || limit) {
116: size_t statement_size=strlen(astatement);
117: char *statement_limited=(char *)services->malloc(
118: statement_size+MAX_NUMBER*2+8/* limit #,#*/+1);
119: char *cur=statement_limited;
120: memcpy(cur, astatement, statement_size); cur+=statement_size;
121: cur+=sprintf(cur, " limit ");
122: if(offset)
123: cur+=snprintf(cur, MAX_NUMBER+1, "%lu,", offset);
124: if(limit)
125: cur+=snprintf(cur, MAX_NUMBER, "%lu", limit);
126: statement=statement_limited;
127: } else
128: statement=astatement;
129:
1.7 paf 130: if(mysql_query(mysql, statement))
1.11 paf 131: services->_throw(mysql_error(mysql));
1.7 paf 132: if(!(res=mysql_store_result(mysql)) && mysql_field_count(mysql))
1.11 paf 133: services->_throw(mysql_error(mysql));
1.7 paf 134: if(!res) {
135: // empty result
136: *row_count=0;
137: *column_count=0;
138: return;
139: }
140:
141: *column_count=mysql_num_fields(res);
1.11 paf 142: *columns=(Cell *)services->malloc(sizeof(Cell)*(*column_count));
1.7 paf 143:
144: *row_count=(unsigned long)mysql_num_rows(res);
1.11 paf 145: *rows=(Cell **)services->malloc(sizeof(Cell *)*(*row_count));
1.7 paf 146:
147: for(unsigned int i=0; i<(*column_count); i++){
148: MYSQL_FIELD *field=mysql_fetch_field(res);
149: size_t size=strlen(field->name);
150: (*columns)[i].size=size;
1.11 paf 151: (*columns)[i].ptr=services->malloc(size);
1.7 paf 152: memcpy((*columns)[i].ptr, field->name, size);
153: }
154:
155: for(unsigned long r=0; r<(*row_count); r++)
156: if(MYSQL_ROW mysql_row=mysql_fetch_row(res)) { // never false..
157: unsigned long *lengths=mysql_fetch_lengths(res);
158: Cell *row=(Cell *)malloc(sizeof(Cell)*(*column_count));
159: (*rows)[r]=row;
160: for(unsigned int i=0; i<(*column_count); i++){
161: size_t size=(size_t)lengths[i];
162: row[i].size=size;
1.11 paf 163: row[i].ptr=services->malloc(size);
1.7 paf 164: memcpy(row[i].ptr, mysql_row[i], size);
165: }
166: }
167:
168: mysql_free_result(res);
169: }
1.1 paf 170: };
171:
172: extern "C" SQL_Driver *create() {
173: return new MySQL_Driver();
174: }
E-mail: