bdb_uval.c

00001 /* $Id$
00002  *
00003  * Copyright (C) 2006-2007 Sippy Software, Inc. <sales@sippysoft.com>
00004  *
00005  * This file is part of ser, a free SIP server.
00006  *
00007  * ser is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version
00011  *
00012  * For a license to use the ser software under conditions
00013  * other than those described here, or to purchase support for this
00014  * software, please contact iptel.org by e-mail at the following addresses:
00015  *    info@iptel.org
00016  *
00017  * ser is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  *
00026  */
00027 
00028 
00029 #include "bdb.h"
00030 
00031 int bdb_urow_db2bdb(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n, bdb_urow_p *_r)
00032 {
00033         bdb_urow_p      r;
00034         bdb_table_p     t;
00035         bdb_column_p    c;
00036 
00037         int             found, found_key;
00038         int             i, j;
00039         int             c_idx;
00040         bdb_uval_p      v;
00041 
00042         *_r = NULL;
00043 
00044         if (_n <= 0) {
00045 #ifdef BDB_EXTRA_DEBUG
00046                 LOG(L_ERR, "BDB:bdb_urow_db2bdb: no defined keys to be updated\n");
00047 #endif
00048                 return -1;
00049         }
00050 
00051         if ((t = bdb_find_table(CON_TABLE(_h))) == NULL) {
00052 #ifdef BDB_EXTRA_DEBUG
00053                 LOG(L_ERR, "BDB:bdb_urow_db2bdb: no table in use\n");
00054 #endif
00055                 return -1;
00056         };
00057 
00058         /* check for dublicates in update set */
00059         for (i = 1; i < _n; i++) {
00060                 for (j = 0; j < i; j++) {
00061                         if (!strcmp(_k[i], _k[j])) {
00062 #ifdef BDB_EXTRA_DEBUG
00063                                 LOG(L_ERR, "BDB:bdb_urow_db2bdb: dublicates keys in update set: '%s' and '%s'\n", _k[i], _k[j]);
00064 #endif
00065                                 return -1;
00066                         }
00067                 }
00068         }
00069 
00070         /* check if all columns exist */
00071         for (i = 0; i < _n; i++) {
00072                 found = 0;
00073                 /* key column is always first one */
00074                 for (c = t->cols, found_key = 1; c != NULL; c = c->next, found_key = 0) {
00075                         if (!strcmp(_k[i], c->name.s)) {
00076                                 found = 1;
00077                                 break;
00078                         }
00079                 }
00080                 if (found_key == 1) {
00081                         LOG(L_ERR, "BDB:bdb_urow_db2bdb: unable to update primary key value\n");
00082                         return -1;
00083                 }
00084                 if (!found) {
00085                         LOG(L_ERR, "BDB:bdb_urow_db2bdb: column '%s' does not exist\n", _k[i]);
00086                         return -1;
00087                 }
00088         }
00089 
00090         r = pkg_malloc(sizeof(*r));
00091         memset(r, 0, sizeof(*r));
00092 
00093         /* filling data into row */
00094         for (c = t->cols, c_idx = 0; c != NULL; c = c->next, c_idx++) {
00095                 for (i = 0; i < _n; i++) {
00096                         if (!strcmp(_k[i], c->name.s)) {
00097 #ifdef BDB_EXTRA_DEBUG
00098                                 LOG(L_NOTICE, "BDB:bdb_urow_db2bdb: filling column '%.*s', c_idx = %0d\n", c->name.len, c->name.s, c_idx);
00099 #endif
00100                                 v = pkg_malloc(sizeof(*v));
00101                                 memset(v, 0, sizeof(*v));
00102 
00103                                 v->c_idx = c_idx;
00104 
00105                                 bdb_push_ufield(r, v);
00106 
00107                                 if (bdb_ufield_db2bdb(v, &_v[i]) < 0) {
00108                                         bdb_free_urow(r);
00109                                         return -1;
00110                                 };
00111                         }
00112                 }
00113         };
00114 
00115         *_r = r;
00116 
00117         return 0;
00118 };
00119 
00120 
00121 void bdb_free_urow(bdb_urow_p _r)
00122 {
00123         if (_r->fields != NULL) {
00124                 bdb_free_ufield_list(_r->fields);
00125         }
00126 
00127         pkg_free(_r);
00128 };
00129 
00130 
00131 void bdb_free_ufield(bdb_uval_p _v)
00132 {
00133         if (!VAL_NULL(&(_v->v))) {
00134                 if (VAL_TYPE(&(_v->v)) == DB_STR || VAL_TYPE(&(_v->v)) == DB_STRING ||
00135                     VAL_TYPE(&(_v->v)) == DB_BLOB) {
00136                         pkg_free(VAL_STR(&(_v->v)).s);
00137                 }
00138         }
00139         pkg_free(_v);
00140 };
00141 
00142 
00143 void bdb_free_ufield_list(bdb_uval_p _v)
00144 {
00145         bdb_uval_p     curr, next;
00146 
00147         for (curr = _v; curr != NULL;) {
00148                 next = curr->next;
00149                 bdb_free_ufield(curr);
00150                 curr = next;
00151         }
00152 };
00153 
00154 
00155 void bdb_push_ufield(bdb_urow_p _r, bdb_uval_p _v)
00156 {
00157         bdb_uval_p      f;
00158 
00159         if (_r->fields == NULL) {
00160                 _r->fields = _v;
00161                 return;
00162         }
00163         f = _r->fields;
00164         while (f->next != NULL) {
00165                 f = f->next;
00166         }
00167         f->next = _v;
00168 };
00169 
00170 
00171 int bdb_ufield_db2bdb(bdb_uval_p v, db_val_t* _v)
00172 {
00173         char    *s;
00174 
00175         VAL_NULL(&(v->v)) = VAL_NULL(_v);
00176         VAL_TYPE(&(v->v)) = VAL_TYPE(_v);
00177 
00178         if (!VAL_NULL(&(v->v))) {
00179                 switch (VAL_TYPE(_v)) {
00180                 case DB_INT:
00181                         VAL_INT(&(v->v)) = VAL_INT(_v);
00182                         break;
00183                 case DB_FLOAT:
00184                         VAL_FLOAT(&(v->v)) = VAL_FLOAT(_v);
00185                         break;
00186                 case DB_DATETIME:
00187                         VAL_TIME(&(v->v)) = VAL_TIME(_v);
00188                         break;
00189                 case DB_BLOB:
00190                         s = pkg_malloc(VAL_BLOB(_v).len);
00191                         memcpy(s, VAL_BLOB(_v).s, VAL_BLOB(_v).len);
00192                         VAL_BLOB(&(v->v)).s = s;
00193                         VAL_BLOB(&(v->v)).len = VAL_BLOB(_v).len;
00194                         break;
00195                 case DB_DOUBLE:
00196                         VAL_DOUBLE(&(v->v)) = VAL_DOUBLE(_v);
00197                         break;
00198                 case DB_STRING:
00199                         VAL_STR(&(v->v)).len = strlen(VAL_STRING(_v)) + 1;
00200                         s = pkg_malloc(VAL_STR(&(v->v)).len);
00201                         strcpy(s, VAL_STRING(_v));
00202                         VAL_STRING(&(v->v)) = s;
00203                         break;
00204                 case DB_STR:
00205                         s = pkg_malloc(VAL_STR(_v).len);
00206                         memcpy(s, VAL_STR(_v).s, VAL_STR(_v).len);
00207                         VAL_STR(&(v->v)).s = s;
00208                         VAL_STR(&(v->v)).len = VAL_STR(_v).len;
00209                         break;
00210                 case DB_BITMAP:
00211                         VAL_BITMAP(&(v->v)) = VAL_BITMAP(_v);
00212                         break;
00213                 default:
00214                         LOG(L_ERR, "BDB:bdb_ufield_db2bdb: unknown column type: %0X\n", VAL_TYPE(_v));
00215                         return -1;
00216                         break;
00217                 }
00218         }
00219 
00220         return 0;
00221 };
00222 
00223