Logo Search packages:      
Sourcecode: parser-mysql version File versions  Download package

void MySQL_Driver::connect ( char *  url,
SQL_Driver_services &  services,
void **  connection_ref 
) [inline]

connect

Parameters:
url format: user:pass [:port]|[/unix/socket]/database? charset=value& // transcode by server with command 'SET CHARACTER SET value' ClientCharset=charset& // transcode by parser timeout=3& compress=0& named_pipe=1& autocommit=1& multi_statements=0 // allows more then one statement in one query old_client=1 // simulates 3.xx client. not compatible with multi_statements option 3.x, 4.0 only option for charset is cp1251_koi8. 4.1+ accept not 'cp1251_koi8' but 'cp1251', 'utf8' and much more it is usable for transcoding using sql server
Parameters:
connection_ref  output: Connection*

Definition at line 126 of file parser3mysql.C.

                                                         : Connection*
      ){
            char *user=url;
            char *s=rsplit(user, '@');
            char *host=0;
            char *unix_socket=0;
            if(s && s[0]=='[') { // unix socket
                  unix_socket=1+s;
                  s=lsplit(unix_socket, ']');
            } else { // IP
                  host=s;
            }
            char *db=lsplit(s, '/');
            char *pwd=lsplit(user, ':');
            char *error_pos=0;
            char *port_cstr=lsplit(host, ':');
            int port=port_cstr?strtol(port_cstr, &error_pos, 0):0;
            char *options=lsplit(db, '?');

            char *charset=0;

#ifdef OLD_MYSQL_CLIENT
            int client_flag=0;
#else
            int client_flag=CLIENT_MULTI_RESULTS;
#endif

            Connection& connection=*(Connection *)services.malloc(sizeof(Connection));
            *connection_ref=&connection;
            connection.services=&services;
            connection.handle=mysql_init(NULL);
            connection.client_charset=0;  
            connection.autocommit=true;

            while(options){
                  if(char *key=lsplit(&options, '&')){
                        if(*key){
                              if(char *value=lsplit(key, '=')){
                                    if(strcmp(key, "ClientCharset")==0){ // transcoding with parser
                                          toupper_str(value, value, strlen(value));
                                          connection.client_charset=value;
                                    } else if(strcasecmp(key, "charset")==0){ // transcoding with server
                                          charset=value;
                                    } else if(strcasecmp(key, "timeout")==0){
                                          unsigned int timeout=(unsigned int)atoi(value);
                                          if(mysql_options(connection.handle, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&timeout)!=0)
                                                services._throw(mysql_error(connection.handle));
                                    } else if(strcasecmp(key, "compress")==0){
                                          if(atoi(value))
                                                if(mysql_options(connection.handle, MYSQL_OPT_COMPRESS, 0)!=0)
                                                      services._throw(mysql_error(connection.handle));
                                    } else if(strcasecmp(key, "named_pipe")==0){
                                          if(atoi(value))
                                                if(mysql_options(connection.handle, MYSQL_OPT_NAMED_PIPE, 0)!=0)
                                                      services._throw(mysql_error(connection.handle));
                                    } else if(strcasecmp(key, "autocommit")==0){
                                          if(atoi(value)==0)
                                                connection.autocommit=false;
                                    } else if(strcasecmp(key, "multi_statements")==0){
#ifdef OLD_MYSQL_CLIENT
                                          services._throw("driver was built with old mysql includes so multi_statements option can't be used");
#else
                                          if(!(client_flag==CLIENT_MULTI_RESULTS || client_flag==CLIENT_MULTI_STATEMENTS))
                                                services._throw("you can't specify together options old_client and multi_statements");
                                          if(atoi(value)!=0)
                                                client_flag=CLIENT_MULTI_STATEMENTS;
#endif
                                    } else if(strcasecmp(key, "old_client")==0){
#ifdef OLD_MYSQL_CLIENT
                                          services._throw("driver was built with old mysql includes so old_client option can't be used");
#else
                                          if(!(client_flag==CLIENT_MULTI_RESULTS || client_flag==0))
                                                services._throw("you can't specify together options old_client and multi_statements");
                                          if(atoi(value)!=0)
                                                client_flag=0;
#endif
                                    } else
                                          services._throw("unknown connect option" /*key*/);
                              } else 
                                    services._throw("connect option without =value" /*key*/);
                        }
                  }
            }

            if(!mysql_real_connect(
                              connection.handle, 
                              host, user, pwd, db,
                              port?port:MYSQL_PORT,
                              unix_socket,
                              client_flag
                        )
            ){
                  services._throw(mysql_error(connection.handle));
            }

            if(charset){
                  char statement[MAX_STRING]="SET CHARACTER SET ";
                  strncat(statement, charset, MAX_STRING);
                  _exec(connection, statement);
            }

            if(!connection.autocommit)
                  _exec(connection, "SET AUTOCOMMIT=0");
      }


Generated by  Doxygen 1.6.0   Back to index