modules_k/db_unixodbc/con.c
e3649ef3
 /* 
  * $Id$
  *
  * UNIXODBC module
  *
  * Copyright (C) 2005-2006 Marco Lorrai
4c7d0961
  * Copyright (C) 2008 1&1 Internet AG
e3649ef3
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
e3649ef3
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
e3649ef3
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
e3649ef3
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  *
  * History:
  * --------
f4d9af07
  *  2005-12-01  initial commit (chgen)
  *  2006-01-10  UID (username) and PWD (password) attributes added to 
  *              connection string (bogdan)
8e0bd7a1
  *  2006-05-05  extract_error passes back last error state on return (sgupta)
f4d9af07
  */
e3649ef3
 
5408633b
 #include "con.h"
e3649ef3
 #include "../../mem/mem.h"
 #include "../../dprint.h"
 #include "../../ut.h"
 #include <time.h>
 
f4d9af07
 #define DSN_ATTR  "DSN="
 #define DSN_ATTR_LEN  (sizeof(DSN_ATTR)-1)
 #define UID_ATTR  "UID="
 #define UID_ATTR_LEN  (sizeof(UID_ATTR)-1)
 #define PWD_ATTR  "PWD="
 #define PWD_ATTR_LEN  (sizeof(PWD_ATTR)-1)
 
696836d9
 
1ea49e9a
 char *db_unixodbc_build_conn_str(const struct db_id* id, char *buf)
f4d9af07
 {
 	int len, ld, lu, lp;
 	char *p;
 
32a5196d
 	if (!buf) return 0;
 
f4d9af07
 	ld = id->database?strlen(id->database):0;
 	lu = id->username?strlen(id->username):0;
 	lp = id->password?strlen(id->password):0;
 
 	len = (ld?(DSN_ATTR_LEN + ld + 1):0)
 		+ (lu?(UID_ATTR_LEN + lu + 1):0)
 		+ PWD_ATTR_LEN + lp + 1;
 
 	if ( len>=MAX_CONN_STR_LEN ){
af26539e
 		LM_ERR("connection string too long! Increase MAX_CONN_STR_LEN"
de7fe5e9
 				" and recompile\n");
f4d9af07
 		return 0;
 	}
 
 	p = buf;
 	if (ld) {
 		memcpy( p , DSN_ATTR, DSN_ATTR_LEN);
 		p += DSN_ATTR_LEN;
 		memcpy( p, id->database, ld);
 		p += ld;
 	}
 	if (lu) {
a153dae9
 		*(p++) = ';';
f4d9af07
 		memcpy( p , UID_ATTR, UID_ATTR_LEN);
 		p += UID_ATTR_LEN;
 		memcpy( p, id->username, lu);
 		p += lu;
 	}
 	if (lp) {
a153dae9
 		*(p++) = ';';
 		memcpy( p , PWD_ATTR, PWD_ATTR_LEN);
 		p += PWD_ATTR_LEN;
f4d9af07
 		memcpy( p, id->password, lp);
 		p += lp;
 	}
 	*(p++) = ';';
 	*p = 0 ; /* make it null terminated */
 
 	return buf;
 }
 
 
e3649ef3
 /*
  * Create a new connection structure,
  * open the UNIXODBC connection and set reference count to 1
  */
1ea49e9a
 struct my_con* db_unixodbc_new_connection(struct db_id* id)
e3649ef3
 {
 	SQLCHAR outstr[1024];
 	SQLSMALLINT outstrlen;
 	int ret;
 	struct my_con* ptr;
32a5196d
 	char conn_str[MAX_CONN_STR_LEN];
e3649ef3
 
 	if (!id)
 	{
de7fe5e9
 		LM_ERR("invalid parameter value\n");
e3649ef3
 		return 0;
 	}
 
 	ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con));
 	if (!ptr)
 	{
de7fe5e9
 		LM_ERR("no more memory left\n");
e3649ef3
 		return 0;
 	}
 
 	memset(ptr, 0, sizeof(struct my_con));
 	ptr->ref = 1;
a153dae9
 	// allocate environment handle
 	ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(ptr->env));
 	if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
 	{
 		LM_ERR("could not alloc a SQL handle\n");
 		if (ptr) pkg_free(ptr);
 		return 0;
 	}
 	// set the environment
 	ret = SQLSetEnvAttr(ptr->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
 	if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
 	{
 		LM_ERR("could not set the environment\n");
 		goto err1;
 	}
 	// allocate connection handle
 	ret = SQLAllocHandle(SQL_HANDLE_DBC, ptr->env, &(ptr->dbc));
 	if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
 	{
 		LM_ERR("could not alloc a connection handle %d\n", ret);
 		goto err1;
 	}
f4d9af07
 
1ea49e9a
 	if (!db_unixodbc_build_conn_str(id, conn_str)) {
de7fe5e9
 		LM_ERR("failed to build connection string\n");
a153dae9
 		goto err2;
f4d9af07
 	}
af26539e
 
 	LM_DBG("opening connection: unixodbc://xxxx:xxxx@%s/%s\n", ZSW(id->host),
 		ZSW(id->database));
 
a153dae9
 	ret = SQLDriverConnect(ptr->dbc, NULL, (SQLCHAR*)conn_str, SQL_NTS,
e3649ef3
 		outstr, sizeof(outstr), &outstrlen,
 		SQL_DRIVER_COMPLETE);
 	if (SQL_SUCCEEDED(ret))
 	{
de7fe5e9
 		LM_DBG("connection succeeded with reply <%s>\n", outstr);
e3649ef3
 		if (ret == SQL_SUCCESS_WITH_INFO)
 		{
de7fe5e9
 			LM_DBG("driver reported the following diagnostics\n");
1ea49e9a
 			db_unixodbc_extract_error("SQLDriverConnect", ptr->dbc, SQL_HANDLE_DBC, NULL);
e3649ef3
 		}
 	}
 	else
 	{
de7fe5e9
 		LM_ERR("failed to connect\n");
1ea49e9a
 		db_unixodbc_extract_error("SQLDriverConnect", ptr->dbc, SQL_HANDLE_DBC, NULL);
a153dae9
 		goto err2;
e3649ef3
 	}
2b8fe569
 
e3649ef3
 	ptr->stmt_handle = NULL;
 
 	ptr->timestamp = time(0);
 	ptr->id = id;
 	return ptr;
 
a153dae9
 err1:
 	SQLFreeHandle(SQL_HANDLE_ENV, &(ptr->env));
 	if (ptr) pkg_free(ptr);
 	return 0;
 
 err2:
 	SQLFreeHandle(SQL_HANDLE_ENV, &(ptr->env));
 	SQLFreeHandle(SQL_HANDLE_DBC, &(ptr->dbc));
e3649ef3
 	if (ptr) pkg_free(ptr);
 	return 0;
 }
 
 /*
  * Close the connection and release memory
  */
1ea49e9a
 void db_unixodbc_free_connection(struct my_con* con)
e3649ef3
 {
 	if (!con) return;
 	SQLFreeHandle(SQL_HANDLE_ENV, con->env);
 	SQLDisconnect(con->dbc);
 	SQLFreeHandle(SQL_HANDLE_DBC, con->dbc);
 	pkg_free(con);
 }
 
8e0bd7a1
 
1ea49e9a
 void db_unixodbc_extract_error(const char *fn, const SQLHANDLE handle, const SQLSMALLINT type, char* stret)
e3649ef3
 {
 	SQLINTEGER   i = 0;
 	SQLINTEGER   native;
 	SQLCHAR  state[ 7 ];
 	SQLCHAR  text[256];
 	SQLSMALLINT  len;
 	SQLRETURN	ret;
 
 	do
 	{
 		ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
 			sizeof(text), &len );
8e0bd7a1
 		if (SQL_SUCCEEDED(ret)) {
de7fe5e9
 			LM_ERR("unixodbc:%s=%s:%ld:%ld:%s\n", fn, state, (long)i, 
8e0bd7a1
 					(long)native, text);
 			if(stret) strcpy( stret, (char*)state );
 		}
e3649ef3
 	}
 	while( ret == SQL_SUCCESS );
 }