Annotation of parser3/src/sql/mysql/parser3mysql.C, revision 1.7
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.7 ! paf 8: $Id: parser3mysql.C, v 1.6 2001/04/05 08:09:26 paf Exp $
1.1 paf 9: */
10:
1.4 paf 11: #include <stdlib.h>
12:
1.1 paf 13: #include "pa_sql_driver.h"
1.4 paf 14: #include "mysql.h"
1.7 ! paf 15: #include "pa_common.h" //
1.1 paf 16:
1.4 paf 17: char *lsplit(char *string, char delim) {
18: if(string) {
19: char *v=strchr(string, delim);
20: if(v) {
21: *v=0;
22: return v+1;
23: }
24: }
25: return 0;
26: }
27:
1.6 paf 28: /**
29: MySQL server driver
30:
31: @todo
32: figure out about memory for errors:
33: - static=add multithread locks
34: - dynamic=who should free it up?
35: */
1.1 paf 36: class MySQL_Driver : public SQL_Driver {
37: public:
38:
1.4 paf 39: MySQL_Driver() : SQL_Driver() {
1.3 paf 40: }
1.1 paf 41:
42: /// get api version
1.6 paf 43: int api_version() { return SQL_DRIVER_API_VERSION; }
1.4 paf 44: /// connect
1.6 paf 45: void connect(
1.5 paf 46: char *url, ///< @b user:pass@host[:port]/database
1.6 paf 47: void **connection ///< output: MYSQL *
1.5 paf 48: ) {
1.4 paf 49: char *user=url;
50: char *host=lsplit(user, '@');
51: char *db=lsplit(host, '/');
52: char *pwd=lsplit(user, ':');
53: char *error_pos=0;
54: char *port_cstr=lsplit(host, ':');
55: int port=port_cstr?strtol(port_cstr, &error_pos, 0):0;
56:
57: MYSQL *mysql=mysql_init(NULL);
58: if(!mysql_real_connect(mysql,
59: host, user, pwd, db, port?port:MYSQL_PORT, NULL, 0))
1.6 paf 60: fservices->_throw(mysql_error(mysql));
1.4 paf 61:
1.6 paf 62: *(MYSQL **)connection=mysql;
1.1 paf 63: }
1.6 paf 64: void disconnect(void *connection) {
65: mysql_close((MYSQL *)connection);
1.1 paf 66: }
1.6 paf 67: void commit(void *connection) {}
68: void rollback(void *connection) {}
1.7 ! paf 69:
! 70: void query(void *connection,
! 71: const char *statement,
! 72: unsigned int *column_count, Cell **columns,
! 73: unsigned long *row_count, Cell ***rows) {
! 74:
! 75: MYSQL *mysql=(MYSQL *)connection;
! 76: MYSQL_RES *res=NULL;
! 77:
! 78: if(mysql_query(mysql, statement))
! 79: fservices->_throw(mysql_error(mysql));
! 80: if(!(res=mysql_store_result(mysql)) && mysql_field_count(mysql))
! 81: fservices->_throw(mysql_error(mysql));
! 82: if(!res) {
! 83: // empty result
! 84: *row_count=0;
! 85: *column_count=0;
! 86: return;
! 87: }
! 88:
! 89: *column_count=mysql_num_fields(res);
! 90: *columns=(Cell *)fservices->malloc(sizeof(Cell)*(*column_count));
! 91:
! 92: *row_count=(unsigned long)mysql_num_rows(res);
! 93: *rows=(Cell **)fservices->malloc(sizeof(Cell *)*(*row_count));
! 94:
! 95: for(unsigned int i=0; i<(*column_count); i++){
! 96: MYSQL_FIELD *field=mysql_fetch_field(res);
! 97: size_t size=strlen(field->name);
! 98: (*columns)[i].size=size;
! 99: (*columns)[i].ptr=fservices->malloc(size);
! 100: memcpy((*columns)[i].ptr, field->name, size);
! 101: }
! 102:
! 103: for(unsigned long r=0; r<(*row_count); r++)
! 104: if(MYSQL_ROW mysql_row=mysql_fetch_row(res)) { // never false..
! 105: unsigned long *lengths=mysql_fetch_lengths(res);
! 106: Cell *row=(Cell *)malloc(sizeof(Cell)*(*column_count));
! 107: (*rows)[r]=row;
! 108: for(unsigned int i=0; i<(*column_count); i++){
! 109: size_t size=(size_t)lengths[i];
! 110: row[i].size=size;
! 111: row[i].ptr=fservices->malloc(size);
! 112: memcpy(row[i].ptr, mysql_row[i], size);
! 113: }
! 114: }
! 115:
! 116: mysql_free_result(res);
! 117: }
1.1 paf 118: };
119:
120: extern "C" SQL_Driver *create() {
121: return new MySQL_Driver();
122: }
E-mail: