Added MySQL database interface, minor changes in PostgreSQL.

git-svn-id: http://voip.null.ro/svn/yate@594 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2005-12-09 00:28:56 +00:00
parent fd3a1c95b1
commit 1e7753a150
7 changed files with 489 additions and 20 deletions

View File

@ -0,0 +1,35 @@
[general]
; This section is special - holds settings common to all connections
; priority: int: Handler priority
;priority=100
; Each other section in this file describes a database connection
;[default]
; The section name is used as the database connection name
; timeout: int: Query timeout in milliseconds - will be rounded to seconds
;timeout=10000
; host: string: MySQL server to connect to, defaults to local
;host=
; port: int: Port on which the server is listening for connections
;port=0
; database: string: Name of the database to use in queries
;database=yate
; user: string: Username to use to access the database
;user=mysql
; password: string: Password to authenticate the user to the database server
;password=
; socket: string: UNIX socket to use for local connections
;socket=
; compress: bool: Enable use of compression protocol
;compress=disable

View File

@ -1,4 +1,11 @@
; Each section in this file describes a database connection
[general]
; This section is special - holds settings common to all connections
; priority: int: Handler priority
;priority=100
; Each other section in this file describes a database connection
;[default]
; The section name is used as the database connection name

View File

@ -50,6 +50,9 @@
; - result field to copy in message's textual return value (only for some)
; You can also override the settings from section [default]
; Sample queries below are for PostgreSQL
; Timestamps and intervals are unfortunately non-portable
[user.auth]
; Query and result name for the user.auth message
@ -87,8 +90,8 @@ priority=120
[call.route]
; Query and result name for the routing message
query=SELECT data,(CASE WHEN data IS NULL THEN 'offline' ELSE NULL END) AS error FROM users WHERE username='${called}'
result=data
query=SELECT location,(CASE WHEN location IS NULL THEN 'offline' ELSE NULL END) AS error FROM users WHERE username='${called}'
result=location
priority=120

View File

@ -139,6 +139,17 @@ AC_SUBST(FDSIZE_HACK)
# Checks for optional libraries.
HAVE_RESOLV=no
AC_MSG_CHECKING([for resolver library])
AC_LANG_SAVE
AC_LANG_C
AC_TRY_LINK([#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>],[res_init();],[HAVE_RESOLV=yes],[HAVE_RESOLV=no])
AC_LANG_RESTORE
AC_MSG_RESULT([$HAVE_RESOLV])
AC_SUBST(HAVE_RESOLV)
HAVE_PRI=no
HAVE_PRI_CB=no
AC_ARG_WITH(libpri,AC_HELP_STRING([--with-libpri],[use ISDN PRI if available (default)]),[ac_cv_use_libpri=$withval],[ac_cv_use_libpri=yes])
@ -176,7 +187,39 @@ fi
AC_SUBST(HAVE_PGSQL)
AC_SUBST(PGSQL_INC)
HAVE_MYSQL=no
MYSQL_INC=""
MYSQL_LIB=""
MYSQL_VER=""
AC_ARG_WITH(mysql,AC_HELP_STRING([--with-mysql=DIR],[use MySQL client library from DIR]),[ac_cv_use_mysql=$withval],[ac_cv_use_mysql=yes])
if [[ "x$ac_cv_use_mysql" != "xno" ]]; then
if [[ "x$ac_cv_use_mysql" = "xyes" ]]; then
AC_MSG_CHECKING([for MySQL using mysql_config])
MYSQL_INC=`mysql_config --include 2>/dev/null`
MYSQL_LIB=`mysql_config --libs_r 2>/dev/null`
MYSQL_VER=`mysql_config --version 2>/dev/null`
if [[ "x$MYSQL_INC" != "x" -a "x$MYSQL_LIB" != "x" -a "x$MYSQL_VER" != "x" ]]; then
HAVE_MYSQL=yes
else
MYSQL_INC=""
MYSQL_LIB=""
MYSQL_VER=""
fi
else
AC_MSG_CHECKING([for MySQL in $ac_cv_use_mysql])
if [[ -f "$ac_cv_use_mysql/include/mysql/mysql.h" -a -d "$ac_cv_use_mysql/lib/mysql" ]]; then
MYSQL_INC="-I$ac_cv_use_mysql/include/mysql"
MYSQL_LIB="-L$ac_cv_use_mysql/lib/mysql -lmysqlclient_r -lpthread -lz -lcrypt -lnsl -lm"
MYSQL_VER=`sed -n 's/^.*MYSQL_SERVER_VERSION[[^"]]*"\([[^"]]*\).*$/\1/p' "$ac_cv_use_mysql/include/mysql/mysql_version.h"`
HAVE_MYSQL=yes
fi
fi
AC_MSG_RESULT([$HAVE_MYSQL $MYSQL_VER])
fi
AC_SUBST(HAVE_MYSQL)
AC_SUBST(MYSQL_INC)
AC_SUBST(MYSQL_LIB)
AC_SUBST(MYSQL_VER)
HAVE_ZAP=no
AC_MSG_CHECKING([for Zaptel linux headers])

View File

@ -31,6 +31,15 @@ ifneq (@HAVE_PGSQL@,no)
PROGS := $(PROGS) pgsqldb.yate
endif
ifneq (@HAVE_MYSQL@,no)
PROGS := $(PROGS) mysqldb.yate
endif
ifneq (@HAVE_RESOLV@,no)
# coming soon
# PROGS := $(PROGS) enumroute.yate
endif
ifeq (@HAVE_PRI@_@HAVE_ZAP@,yes_yes)
PROGS := $(PROGS) zapchan.yate
endif
@ -139,6 +148,11 @@ h323chan.yate: LOCALLIBS = @H323_LIB@
pgsqldb.yate: LOCALFLAGS = @PGSQL_INC@
pgsqldb.yate: LOCALLIBS = -lpq
mysqldb.yate: LOCALFLAGS = @MYSQL_INC@
mysqldb.yate: LOCALLIBS = @MYSQL_LIB@
enumroute.yate: LOCALLIBS = -lresolv
iaxchan.yate: @IAX2_DEP@
iaxchan.yate: LOCALLIBS = @IAX2_DEP@
iaxchan.yate: LOCALFLAGS = @IAX2_INC@ @IAX2_LIB@

368
modules/mysqldb.cpp Normal file
View File

@ -0,0 +1,368 @@
/**
* mysqldb.cpp
* This file is part of the YATE Project http://YATE.null.ro
*
* This is the MySQL support from Yate.
*
* Yet Another Telephony Engine - a fully featured software PBX and IVR
* Copyright (C) 2004, 2005 Null Team
*
* This program is free software; you can redistribute it and/or modify
* 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.
*
* This program is distributed in the hope that it will be useful,
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <yatephone.h>
#include <stdio.h>
#include <mysql.h>
#ifndef CLIENT_MULTI_STATEMENTS
#define CLIENT_MULTI_STATEMENTS 0
#define mysql_more_results(x) (0)
#endif
#if (MYSQL_VERSION_ID > 41000)
#define HAVE_MYSQL_410
#else
#define mysql_warning_count(x) (0)
#define mysql_library_init mysql_server_init
#define mysql_library_end mysql_server_end
#endif
using namespace TelEngine;
static ObjList s_conns;
Mutex s_conmutex;
class DbConn : public GenObject
{
public:
DbConn(const NamedList* sect);
~DbConn();
virtual const String& toString() const
{ return m_name; }
bool ok();
int queryDb(const char* query, Message* dest = 0);
bool initDb();
void dropDb();
inline Mutex& mutex()
{ return m_dbmutex; }
void runQueries();
protected:
Mutex m_dbmutex;
private:
bool testDb();
bool startDb();
int queryDbInternal();
String m_name;
unsigned int m_timeout;
MYSQL *m_conn;
String m_host;
String m_user;
String m_pass;
String m_db;
String m_unix;
unsigned int m_port;
bool m_compress;
String m_query;
Message* m_msg;
int m_res;
volatile bool m_go;
};
class DbThread : public Thread
{
public:
inline DbThread(DbConn* conn)
: Thread("mysqldb"), m_conn(conn)
{ }
virtual void run();
virtual void cleanup();
private:
DbConn* m_conn;
};
class MyHandler : public MessageHandler
{
public:
MyHandler(unsigned int prio = 100)
: MessageHandler("database",prio)
{ }
virtual bool received(Message& msg);
};
class MyModule : public Module
{
public:
MyModule();
~MyModule();
protected:
virtual void initialize();
private:
bool m_init;
};
static MyModule module;
DbConn::DbConn(const NamedList* sect)
: m_dbmutex(true), m_name(*sect), m_conn(0), m_msg(0)
{
int tout = sect->getIntValue("timeout",10000);
// round to seconds
m_timeout = (tout + 500) / 1000;
// but make sure it doesn't round to zero unless zero was requested
if (tout && !m_timeout)
m_timeout = 1;
m_host = sect->getValue("host");
m_user = sect->getValue("user","mysql");
m_pass = sect->getValue("password");
m_db = sect->getValue("database","yate");
m_port = sect->getIntValue("port");
m_unix = sect->getValue("socket");
m_compress = sect->getBoolValue("compress");
}
DbConn::~DbConn()
{
s_conns.remove(this,false);
// FIXME: should we try to do it from this thread?
dropDb();
}
// initialize the database connection
bool DbConn::initDb()
{
Lock lock(m_dbmutex);
// allow specifying the raw connection string
Debug(&module,DebugInfo,"Initiating connection for '%s'",m_name.c_str());
m_conn = mysql_init(m_conn);
if (!m_conn) {
Debug(&module,DebugGoOn,"Could not start connection for '%s'",m_name.c_str());
return false;
}
if (m_compress)
mysql_options(m_conn,MYSQL_OPT_COMPRESS,0);
mysql_options(m_conn,MYSQL_OPT_CONNECT_TIMEOUT,(const char*)&m_timeout);
#ifdef MYSQL_OPT_READ_TIMEOUT
mysql_options(m_conn,MYSQL_OPT_READ_TIMEOUT,(const char*)&m_timeout);
#endif
#ifdef MYSQL_OPT_WRITE_TIMEOUT
mysql_options(m_conn,MYSQL_OPT_WRITE_TIMEOUT,(const char*)&m_timeout);
#endif
if (mysql_real_connect(m_conn,m_host,m_user,m_pass,m_db,m_port,m_unix,CLIENT_MULTI_STATEMENTS))
return true;
Debug(&module,DebugWarn,"Connection for '%s' failed: %s",m_name.c_str(),mysql_error(m_conn));
return false;
}
// drop the connection
void DbConn::dropDb()
{
Lock lock(m_dbmutex);
m_res = -1;
m_go = false;
if (!m_conn)
return;
MYSQL* tmp = m_conn;
m_conn = 0;
lock.drop();
mysql_close(tmp);
Debug(&module,DebugInfo,"Database connection '%s' closed",m_name.c_str());
}
// test it the connection is still OK
bool DbConn::testDb()
{
return m_conn && !mysql_ping(m_conn);
}
// perform the query, fill the message with data
// return number of rows, -1 for error
int DbConn::queryDbInternal()
{
if (!testDb())
return -1;
if (mysql_query(m_conn,m_query)) {
Debug(&module,DebugWarn,"Query for '%s' failed: %s",m_name.c_str(),mysql_error(m_conn));
return -1;
}
int total = 0;
unsigned int warns = 0;
unsigned int affected = 0;
do {
MYSQL_RES* res = mysql_store_result(m_conn);
warns += mysql_warning_count(m_conn);
affected += mysql_affected_rows(m_conn);
if (res) {
unsigned int cols = mysql_num_fields(res);
unsigned int rows = mysql_num_rows(res);
Debug(&module,DebugAll,"Got result set %p rows=%u cols=%u",res,rows,cols);
total += rows;
if (m_msg) {
MYSQL_FIELD* fields = mysql_fetch_fields(res);
m_msg->setParam("columns",String(cols));
m_msg->setParam("rows",String(rows));
if (m_msg->getBoolValue("results",true)) {
Array *a = new Array(cols,rows+1);
unsigned int c;
// add field names
for (c = 0; c < cols; c++)
a->set(new String(fields[c].name),c,0);
// and now data row by row
for (unsigned int r = 1; r <= rows; r++) {
MYSQL_ROW row = mysql_fetch_row(res);
if (!row)
break;
for (c = 0; c < cols; c++) {
if (row[c])
a->set(new String(row[c]),c,r);
}
}
m_msg->userData(a);
a->deref();
}
}
mysql_free_result(res);
}
} while (mysql_more_results(m_conn));
if (m_msg) {
m_msg->setParam("affected",String(affected));
if (warns)
m_msg->setParam("warnings",String(warns));
}
return total;
}
void DbConn::runQueries()
{
while (m_conn) {
if (m_go) {
DDebug(&module,DebugAll,"Running query \"%s\" for '%s'",
m_query.c_str(),m_name.c_str());
m_res = queryDbInternal();
m_msg = 0;
m_query.clear();
m_go = false;
}
Thread::yield(true);
}
}
static bool failure(Message* m)
{
if (m)
m->setParam("error","failure");
return false;
}
int DbConn::queryDb(const char* query, Message* dest)
{
if (TelEngine::null(query))
return -1;
DDebug(&module,DebugAll,"Proxying query \"%s\" for '%s'",
query,m_name.c_str());
m_msg = dest;
m_query = query;
m_go = true;
while (m_go)
Thread::yield();
if (m_res < 0)
failure(dest);
return m_res;
}
void DbThread::run()
{
mysql_library_init(0,0,0);
if (m_conn->initDb())
m_conn->runQueries();
}
void DbThread::cleanup()
{
m_conn->dropDb();
mysql_library_end();
mysql_thread_end();
}
static DbConn* findDb(String& account)
{
if (account.null())
return 0;
ObjList* l = s_conns.find(account);
return l ? static_cast<DbConn *>(l->get()): 0;
}
bool MyHandler::received(Message& msg)
{
String tmp(msg.getValue("account"));
if (tmp.null())
return false;
Lock lock(s_conmutex);
DbConn* db= findDb(tmp);
if (!db)
return false;
Lock lo(db->mutex());
lock.drop();
String query(msg.getValue("query"));
db->queryDb(query,&msg);
msg.setParam("dbtype","mysqldb");
return true;
}
MyModule::MyModule()
: Module ("mysqldb","database"),m_init(false)
{
Output("Loaded module MySQL based on %s",mysql_get_client_info());
}
MyModule::~MyModule()
{
s_conns.clear();
Output("Unloaded module MySQL");
}
void MyModule::initialize()
{
if (m_init)
return;
m_init = true;
Output("Initializing module MySQL");
Configuration cfg(Engine::configFile("mysqldb"));
Engine::install(new MyHandler(cfg.getIntValue("general","priority",100)));
unsigned int i;
for (i = 0; i < cfg.sections(); i++) {
NamedList* sec = cfg.getSection(i);
if (!sec || (*sec == "general"))
continue;
DbConn* conn = new DbConn(sec);
DbThread* thr = new DbThread(conn);
if (thr->startup())
s_conns.insert(conn);
else {
delete thr;
conn->destruct();
}
}
}
/* vi: set ts=8 sw=4 sts=4 noet: */

View File

@ -29,7 +29,6 @@
using namespace TelEngine;
static Configuration s_cfg(Engine::configFile("pgsqldb"));
static ObjList s_conns;
Mutex s_conmutex;
@ -48,8 +47,6 @@ public:
{ return m_dbmutex; }
protected:
// virtual bool loadQuery();
String m_query;
Mutex m_dbmutex;
private:
@ -66,8 +63,8 @@ private:
class PgHandler : public MessageHandler
{
public:
PgHandler()
: MessageHandler("database")
PgHandler(unsigned int prio = 100)
: MessageHandler("database",prio)
{ }
virtual bool received(Message& msg);
};
@ -247,10 +244,10 @@ int DbConn::queryDbInternal(const char* query, Message* dest)
dest->setParam("columns",String(columns));
if (dest->getBoolValue("results",true) && !PQbinaryTuples(res)) {
Array *a = new Array(columns,rows+1);
for (int k=0; k<columns; k++) {
for (int k = 0; k < columns; k++) {
String *f= new String(PQfname(res,k));
a->set(f,k,0);
for (int j=0; j<rows; j++) {
for (int j = 0; j < rows; j++) {
// skip over NULL values
if (PQgetisnull(res,j,k))
continue;
@ -316,7 +313,7 @@ static DbConn* findDb(String& account)
{
if (account.null())
return 0;
ObjList *l = s_conns.find(account);
ObjList* l = s_conns.find(account);
return l ? static_cast<DbConn *>(l->get()): 0;
}
@ -326,13 +323,14 @@ bool PgHandler::received(Message& msg)
if (tmp.null())
return false;
Lock lock(s_conmutex);
DbConn *db= findDb(tmp);
DbConn* db= findDb(tmp);
if (!db)
return false;
Lock lo(db->mutex());
lock.drop();
String query(msg.getValue("query"));
db->queryDb(query,&msg);
msg.setParam("dbtype","pgsqldb");
return true;
}
@ -344,7 +342,8 @@ PgModule::PgModule()
PgModule::~PgModule()
{
Output("Unloaded module PostgreSQL");
Output("Unloading module PostgreSQL");
s_conns.clear();
}
void PgModule::initialize()
@ -353,14 +352,14 @@ void PgModule::initialize()
return;
m_init = true;
Output("Initializing module PostgreSQL");
Engine::install(new PgHandler);
Configuration cfg(Engine::configFile("pgsqldb"));
Engine::install(new PgHandler(cfg.getIntValue("general","priority",100)));
unsigned int i;
for (i=0;i<s_cfg.sections();i++)
{
NamedList *sec = s_cfg.getSection(i);
if (!sec)
for (i = 0; i < cfg.sections(); i++) {
NamedList* sec = cfg.getSection(i);
if (!sec || (*sec == "general"))
continue;
DbConn *conn = new DbConn(sec);
DbConn* conn = new DbConn(sec);
conn->initDb();
s_conns.insert(conn);
}