Annotation of parser3/src/sql/mysql/parser3mysql.C, revision 1.6
1.1 paf 1: /** @file
2: Parser: MySQL driver.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.6 ! paf 8: $Id: parser3mysql.C,v 1.5 2001/04/04 12:54:59 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"
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:
28: char *lsplit(char **string_ref, char delim) {
29: char *result=*string_ref;
30: char *next=lsplit(*string_ref, delim);
31: *string_ref=next;
32: return result;
33: }
1.2 paf 34:
1.6 ! paf 35: /**
! 36: MySQL server driver
! 37:
! 38: @todo
! 39: figure out about memory for errors:
! 40: - static=add multithread locks
! 41: - dynamic=who should free it up?
! 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.5 paf 53: char *url, ///< @b user:pass@host[:port]/database
1.6 ! paf 54: void **connection ///< output: MYSQL *
1.5 paf 55: ) {
1.4 paf 56: char *user=url;
57: char *host=lsplit(user, '@');
58: char *db=lsplit(host, '/');
59: char *pwd=lsplit(user, ':');
60: char *error_pos=0;
61: char *port_cstr=lsplit(host, ':');
62: int port=port_cstr?strtol(port_cstr, &error_pos, 0):0;
63:
64: MYSQL *mysql=mysql_init(NULL);
65: if(!mysql_real_connect(mysql,
66: host, user, pwd, db, port?port:MYSQL_PORT, NULL, 0))
1.6 ! paf 67: fservices->_throw(mysql_error(mysql));
1.4 paf 68:
1.6 ! paf 69: *(MYSQL **)connection=mysql;
1.1 paf 70: }
1.6 ! paf 71: void disconnect(void *connection) {
! 72: mysql_close((MYSQL *)connection);
1.1 paf 73: }
1.6 ! paf 74: void commit(void *connection) {}
! 75: void rollback(void *connection) {}
1.1 paf 76: };
77:
78: extern "C" SQL_Driver *create() {
79: return new MySQL_Driver();
80: }
E-mail: