db_unixodbc/val.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: val.c 5519 2009-01-26 18:08:46Z henningw $ 
00003  *
00004  * UNIXODBC module
00005  *
00006  * Copyright (C) 2005-2006 Marco Lorrai
00007  * Copyright (C) 2008 1&1 Internet AG
00008  *
00009  * This file is part of Kamailio, a free SIP server.
00010  *
00011  * Kamailio is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version
00015  *
00016  * Kamailio is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License 
00022  * along with this program; if not, write to the Free Software 
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  *
00026  * History:
00027  * --------
00028  *  2005-12-01  initial commit (chgen)
00029  */
00030 
00031 
00032 #include "../../dprint.h"
00033 #include "../../strcommon.h"
00034 #include "../../db/db_ut.h"
00035 #include "db_unixodbc.h"
00036 #include "val.h"
00037 #include "con.h"
00038 
00039 /*
00040  * Used when converting the query to a result
00041  */
00042 int db_unixodbc_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
00043       const unsigned int _cpy)
00044 {
00045    /* db_unixodbc uses the NULL string for NULL SQL values */
00046    if (_v && _s && !strcmp(_s, "NULL")) {
00047       LM_DBG("converting NULL value");
00048       static str dummy_string = {"", 0};
00049       memset(_v, 0, sizeof(db_val_t));
00050          /* Initialize the string pointers to a dummy empty
00051           * string so that we do not crash when the NULL flag
00052           * is set but the module does not check it properly
00053           */
00054       VAL_STRING(_v) = dummy_string.s;
00055       VAL_STR(_v) = dummy_string;
00056       VAL_BLOB(_v) = dummy_string;
00057       VAL_TYPE(_v) = _t;
00058       VAL_NULL(_v) = 1;
00059       return 0;
00060    } else {
00061       return db_str2val(_t, _v, _s, _l, _cpy);
00062    }
00063 }
00064 
00065 /*
00066  * Used when converting a result from the query
00067  */
00068 int db_unixodbc_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
00069 {
00070    int l, tmp;
00071    char* old_s;
00072 
00073    /* db_unixodbc uses a custom escape function */
00074    tmp = db_val2str(_c, _v, _s, _len);
00075    if (tmp < 1)
00076       return tmp;
00077 
00078    switch(VAL_TYPE(_v))
00079    {
00080       case DB_STRING:
00081          l = strlen(VAL_STRING(_v));
00082          if (*_len < (l * 2 + 3))
00083          {
00084             LM_ERR("destination buffer too short\n");
00085             return -6;
00086          }
00087          else
00088          {
00089             old_s = _s;
00090             *_s++ = '\'';
00091             if(use_escape_common)
00092             {
00093                _s += escape_common(_s, (char*)VAL_STRING(_v), l);
00094             } else {
00095                memcpy(_s, VAL_STRING(_v), l);
00096                _s += l;
00097             }
00098             *_s++ = '\'';
00099             *_s = '\0'; /* FIXME */
00100             *_len = _s - old_s;
00101             return 0;
00102          }
00103          break;
00104 
00105       case DB_STR:
00106          l = VAL_STR(_v).len;
00107          if (*_len < (l * 2 + 3))
00108          {
00109             LM_ERR("destination buffer too short\n");
00110             return -7;
00111          }
00112          else
00113          {
00114             old_s = _s;
00115             *_s++ = '\'';
00116             if(use_escape_common)
00117             {
00118                _s += escape_common(_s, VAL_STR(_v).s, l);
00119             } else {
00120                memcpy(_s, VAL_STR(_v).s, l);
00121                _s += l;
00122             }
00123             *_s++ = '\'';
00124             *_s = '\0'; /* FIXME */
00125             *_len = _s - old_s;
00126             return 0;
00127          }
00128          break;
00129 
00130       case DB_BLOB:
00131          l = VAL_BLOB(_v).len;
00132          if (*_len < (l * 2 + 3))
00133          {
00134             LM_ERR("destination buffer too short\n");
00135             return -9;
00136          }
00137          else
00138          {
00139             old_s = _s;
00140             *_s++ = '\'';
00141             if(use_escape_common)
00142             {
00143                _s += escape_common(_s, VAL_BLOB(_v).s, l);
00144             } else {
00145                memcpy(_s, VAL_BLOB(_v).s, l);
00146                _s += l;
00147             }
00148             *_s++ = '\'';
00149             *_s = '\0'; /* FIXME */
00150             *_len = _s - old_s;
00151             return 0;
00152          }
00153          break;
00154 
00155       default:
00156          LM_DBG("unknown data type\n");
00157          return -10;
00158    }
00159 }

Generated on Tue May 22 14:00:32 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6