my_con.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "my_con.h"
00032 #include "db_mysql.h"
00033 #include <mysql/mysql_version.h>
00034 #include "../../mem/mem.h"
00035 #include "../../dprint.h"
00036 #include "../../ut.h"
00037
00038
00039
00040
00041
00042
00043 struct my_con* db_mysql_new_connection(const struct db_id* id)
00044 {
00045 struct my_con* ptr;
00046 char *host, *grp;
00047
00048 if (!id) {
00049 LM_ERR("invalid parameter value\n");
00050 return 0;
00051 }
00052
00053 ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con));
00054 if (!ptr) {
00055 LM_ERR("no private memory left\n");
00056 return 0;
00057 }
00058
00059 memset(ptr, 0, sizeof(struct my_con));
00060 ptr->ref = 1;
00061
00062 ptr->con = (MYSQL*)pkg_malloc(sizeof(MYSQL));
00063 if (!ptr->con) {
00064 LM_ERR("no private memory left\n");
00065 goto err;
00066 }
00067
00068 mysql_init(ptr->con);
00069
00070 if (id->host[0] == '[' && (host = strchr(id->host, ']')) != NULL) {
00071 grp = id->host + 1;
00072 *host = '\0';
00073 if (host != id->host + strlen(id->host)-1) {
00074 host += 1;
00075 }
00076 else {
00077
00078
00079 host = NULL;
00080 }
00081
00082
00083 mysql_options(ptr->con, MYSQL_READ_DEFAULT_GROUP, grp);
00084 }
00085 else {
00086 host = id->host;
00087 }
00088
00089 if (id->port) {
00090 LM_DBG("opening connection: mysql://xxxx:xxxx@%s:%d/%s\n", ZSW(host),
00091 id->port, ZSW(id->database));
00092 } else {
00093 LM_DBG("opening connection: mysql://xxxx:xxxx@%s/%s\n", ZSW(host),
00094 ZSW(id->database));
00095 }
00096
00097
00098 mysql_options(ptr->con, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&db_mysql_timeout_interval);
00099 mysql_options(ptr->con, MYSQL_OPT_READ_TIMEOUT, (const char *)&db_mysql_timeout_interval);
00100 mysql_options(ptr->con, MYSQL_OPT_WRITE_TIMEOUT, (const char *)&db_mysql_timeout_interval);
00101
00102 #if (MYSQL_VERSION_ID >= 40100)
00103 if (!mysql_real_connect(ptr->con, host, id->username, id->password,
00104 id->database, id->port, 0, CLIENT_MULTI_STATEMENTS)) {
00105 #else
00106 if (!mysql_real_connect(ptr->con, host, id->username, id->password,
00107 id->database, id->port, 0, 0)) {
00108 #endif
00109 LM_ERR("driver error: %s\n", mysql_error(ptr->con));
00110 mysql_close(ptr->con);
00111 goto err;
00112 }
00113
00114 if (db_mysql_auto_reconnect)
00115 ptr->con->reconnect = 1;
00116 else
00117 ptr->con->reconnect = 0;
00118
00119 LM_DBG("connection type is %s\n", mysql_get_host_info(ptr->con));
00120 LM_DBG("protocol version is %d\n", mysql_get_proto_info(ptr->con));
00121 LM_DBG("server version is %s\n", mysql_get_server_info(ptr->con));
00122
00123 ptr->timestamp = time(0);
00124 ptr->id = (struct db_id*)id;
00125 return ptr;
00126
00127 err:
00128 if (ptr && ptr->con) pkg_free(ptr->con);
00129 if (ptr) pkg_free(ptr);
00130 return 0;
00131 }
00132
00133
00134
00135
00136
00137 void db_mysql_free_connection(struct pool_con* con)
00138 {
00139 if (!con) return;
00140
00141 struct my_con * _c;
00142 _c = (struct my_con*) con;
00143
00144 if (_c->res) mysql_free_result(_c->res);
00145 if (_c->id) free_db_id(_c->id);
00146 if (_c->con) {
00147 mysql_close(_c->con);
00148 pkg_free(_c->con);
00149 }
00150 pkg_free(_c);
00151 }