File: ul_db_form_query.c

package info (click to toggle)
kamailio 4.2.0-2%2Bdeb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (147 lines) | stat: -rw-r--r-- 4,550 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* sp-ul_db module
 *
 * Copyright (C) 2007 1&1 Internet AG
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio 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
 *
 * Kamailio 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "ul_db_form_query.h"
#include "ul_db_tran.h"
#include "ul_db.h"
#include "p_usrloc_mod.h"

static int db_do_query(ul_db_op_t ul_op, db_func_t * dbf, db1_con_t * dbh, str * table, db_key_t* _k, db_op_t* _o,
                       db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un);

int db_submit_query(ul_db_op_t ul_op, ul_db_handle_t * handle, str * table, db_key_t* _k, db_op_t* _o,
                    db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) {
	int i;
	int working_c[DB_NUM];
	int working_r[DB_NUM];
	int errors = 0;
	int w;

	if(!handle || !table || !table->s) {
		LM_ERR("NULL pointer in parameter.\n");
		return -1;
	}

	if(db_use_transactions) {
		for(i=0; i<DB_NUM; i++) {
			working_c[i] = 0;
			working_r[i] = 0;
		}

		if(ul_db_tran_start(handle, working_r) < 0) {
			LM_ERR("error during starting transaction"
			    " on table %.*s with id %i.\n", table->len, table->s, handle->id);
			w = get_working_sum(working_r, DB_NUM);
			if(db_check_policy(DB_POL_MOD, w, handle->working) < 0) {
				ul_db_tran_rollback(handle, working_r);
				return -1;
			}
		}

		for(i=0; i<DB_NUM; i++) {
			working_c[i] = working_r[i];
			if((handle->db[i].status == DB_ON) && (working_c[i])) {
				if(db_do_query(ul_op, &handle->db[i].dbf, handle->db[i].dbh, table, _k, _o, _v, _uk, _uv, _n, _un) < 0) {
					LM_ERR("error during querying "
					    "table %.*s with id %i on db %i.\n",
					    table->len, table->s, handle->id, i);
					if(db_handle_error(handle, handle->db[i].no) < 0) {
						LM_CRIT("could not handle error on db %i, handle, %i\n",
						    handle->id, handle->db[i].no);
					}
					errors++;
					working_c[i] = 0;
				} else {
					working_r[i] = 0;
				}
			}
		}

		w = get_working_sum(working_c, DB_NUM);
		if(errors > 0) {
			ul_db_tran_rollback(handle, working_r);
			if(db_check_policy(DB_POL_MOD, w, handle->working) < 0) {
				ul_db_tran_rollback(handle, working_c);
				return -1;
			}
		}
		return ul_db_tran_commit(handle, working_c);
	} else {
		for(i=0; i<DB_NUM; i++) {
			if(handle->db[i].status == DB_ON) {
				if(db_do_query(ul_op, &handle->db[i].dbf, handle->db[i].dbh, table, _k, _o, _v, _uk, _uv, _n, _un) < 0) {
					if(db_handle_error(handle, handle->db[i].no) < 0) {
						LM_CRIT("could not handle error on db %i, handle, %i\n",
						    handle->id, handle->db[i].no);
					}
					return -1;
				}
			}
		}
		return 0;
	}
}

static int db_do_query(ul_db_op_t ul_op, db_func_t * dbf, db1_con_t * dbh, str * table, db_key_t* _k, db_op_t* _o,
                       db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) {
	if(dbf->use_table(dbh, table) < 0) {
		LM_ERR("error in use table %.*s.\n", table->len, table->s);
		return -1;
	}
	switch(ul_op) {
			case UL_DB_INS:
			if(dbf->insert(dbh, _k, _v, _n) < 0) {
				LM_ERR("error in inserting into "
				    "table %.*s.\n", table->len, table->s);
				return -1;
			}
			return 0;
			case UL_DB_REPL:
			if(dbf->replace(dbh, _k, _v, _n, _un, 0) < 0) {
				LM_ERR("error in replacing in "
				    "table %.*s.\n", table->len, table->s);
				return -1;
			}
			return 0;
			case UL_DB_INS_UPD:
			if(dbf->insert_update(dbh, _k, _v, _n ) < 0) {
				LM_ERR("error in inserting/updating in "
				    "table %.*s.\n", table->len, table->s);
				return -1;
			}
			return 0;
			case UL_DB_UPD:
			if(dbf->update(dbh, _k, _o, _v, _uk, _uv, _n, _un) < 0) {
				LM_ERR("error in updating "
				    "table %.*s.\n", table->len, table->s);
				return -1;
			}
			return 0;
			case UL_DB_DEL:
			if(dbf->delete(dbh, _k, _o, _v, _n) < 0) {
				LM_ERR("error in deleting from table %.*s.\n", table->len, table->s);
				return -1;
			}
			return 0;
			default: return -1;
	}
	return 0;
}