flatstore.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: flatstore.c 4986 2008-09-24 11:02:42Z henningw $ 
00003  *
00004  * Flatstore module interface
00005  *
00006  * Copyright (C) 2004 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * --------
00026  *  2003-03-11  updated to the new module exports interface (andrei)
00027  *  2003-03-16  flags export parameter added (janakj)
00028  */
00029 
00030 #include <string.h>
00031 #include <ctype.h>
00032 #include "../../mem/mem.h"
00033 #include "../../dprint.h"
00034 #include "flat_pool.h"
00035 #include "flat_con.h"
00036 #include "flatstore_mod.h"
00037 #include "flatstore.h"
00038 
00039 
00040 static int parse_flat_url(const str* url, str* path)
00041 {
00042    if (!url || !url->s || !path) {
00043       LM_ERR("invalid parameter value\n");
00044       return -1;
00045    }
00046    path->s = strchr(url->s, ':') + 1;
00047    path->len = strlen(path->s);
00048    return 0;
00049 }
00050 
00051 
00052 
00053 /*
00054  * Initialize database module
00055  * No function should be called before this
00056  */
00057 db_con_t* flat_db_init(const str* url)
00058 {
00059    db_con_t* res;
00060    str* path;
00061 
00062    if (!url || !url->s) {
00063       LM_ERR("invalid parameter value\n");
00064       return 0;
00065    }
00066 
00067    /* We do not know the name of the table (and the name of the corresponding
00068     * file) at this point, we will simply store the path taken from the url 
00069     * parameter in the table variable, flat_use_table will then pick that 
00070     * value and open the file
00071     */
00072    /* as the table (path) is a substring of the received str, we need to 
00073     * allocate a separate str struct for it -bogdan
00074     */
00075    res = pkg_malloc(sizeof(db_con_t)+sizeof(struct flat_con*)+sizeof(str));
00076    if (!res) {
00077       LM_ERR("no pkg memory left\n");
00078       return 0;
00079    }
00080    memset(res, 0, sizeof(db_con_t) + sizeof(struct flat_con*) + sizeof(str));
00081    path = (str*)(((char*)res) + sizeof(db_con_t) + sizeof(struct flat_con*));
00082 
00083    if (parse_flat_url(url, path) < 0) {
00084       pkg_free(res);
00085       return 0;
00086    }
00087    res->table = path;
00088 
00089    return res;
00090 }
00091 
00092 
00093 /*
00094  * Store name of table that will be used by
00095  * subsequent database functions
00096  */
00097 int flat_use_table(db_con_t* h, const str* t)
00098 {
00099    struct flat_con* con;
00100 
00101    if (!h || !t || !t->s) {
00102       LM_ERR("invalid parameter value\n");
00103       return -1;
00104    }
00105 
00106    if (CON_TABLE(h)->s != t->s) {
00107       if (CON_TAIL(h)) {
00108          /* Decrement the reference count
00109           * of the connection but do not remove
00110           * it from the connection pool
00111           */
00112          con = (struct flat_con*)CON_TAIL(h);
00113          con->ref--;
00114       }
00115 
00116       CON_TAIL(h) = (unsigned long)
00117          flat_get_connection((char*)CON_TABLE(h)->s, (char*)t->s);
00118       if (!CON_TAIL(h)) {
00119          return -1;
00120       }
00121    }
00122    
00123    return 0;
00124 }
00125 
00126 
00127 void flat_db_close(db_con_t* h)
00128 {
00129    struct flat_con* con;
00130 
00131    if (!h) {
00132       LM_ERR("invalid parameter value\n");
00133       return;
00134    }
00135 
00136    con = (struct flat_con*)CON_TAIL(h);
00137 
00138    if (con) {
00139       flat_release_connection(con);
00140    }
00141    pkg_free(h);
00142 }
00143 
00144 
00145 /*
00146  * Insert a row into specified table
00147  * h: structure representing database connection
00148  * k: key names
00149  * v: values of the keys
00150  * n: number of key=value pairs
00151  */
00152 int flat_db_insert(const db_con_t* h, const db_key_t* k, const db_val_t* v,
00153       const int n)
00154 {
00155    FILE* f;
00156    int i;
00157    int l;
00158    char *s, *p;
00159 
00160    if (local_timestamp < *flat_rotate) {
00161       flat_rotate_logs();
00162       local_timestamp = *flat_rotate;
00163    }
00164 
00165    f = CON_FILE(h);
00166    if (!f) {
00167       LM_ERR("uninitialized connection\n");
00168       return -1;
00169    }
00170 
00171    for(i = 0; i < n; i++) {
00172       switch(VAL_TYPE(v + i)) {
00173       case DB_INT:
00174          fprintf(f, "%d", VAL_INT(v + i));
00175          break;
00176 
00177       case DB_BIGINT:
00178          LM_ERR("BIGINT not supported");
00179          return -1;
00180 
00181       case DB_DOUBLE:
00182          fprintf(f, "%f", VAL_DOUBLE(v + i));
00183          break;
00184 
00185       case DB_STRING:
00186          fprintf(f, "%s", VAL_STRING(v + i));
00187          break;
00188 
00189       case DB_STR:
00190          fprintf(f, "%.*s", VAL_STR(v + i).len, VAL_STR(v + i).s);
00191          break;
00192 
00193       case DB_DATETIME:
00194          fprintf(f, "%u", (unsigned int)VAL_TIME(v + i));
00195          break;
00196 
00197       case DB_BLOB:
00198          l = VAL_BLOB(v+i).len;
00199          s = p = VAL_BLOB(v+i).s;
00200          while (l--) {
00201             if ( !(isprint((int)*s) && *s != '\\' && *s != '|')) {
00202                fprintf(f,"%.*s\\x%02X",(int)(s-p),p,(*s & 0xff));
00203                p = s+1;
00204             }
00205             ++s;
00206          }
00207          if (p!=s)
00208             fprintf(f,"%.*s",(int)(s-p),p);
00209          break;
00210 
00211       case DB_BITMAP:
00212          fprintf(f, "%u", VAL_BITMAP(v + i));
00213          break;
00214       }
00215 
00216       if (i < (n - 1)) {
00217          fprintf(f, "%c", *flat_delimiter);
00218       }
00219    }
00220 
00221    fprintf(f, "\n");
00222 
00223    if (flat_flush) {
00224       fflush(f);
00225    }
00226 
00227    return 0;
00228 }

Generated on Wed May 23 06:00:45 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6