all: updated FSF address in GPL text
[sip-router] / modules / p_usrloc / ul_db_tran.c
1 /* sp-ul_db module
2  *
3  * Copyright (C) 2007 1&1 Internet AG
4  *
5  * This file is part of Kamailio, a free SIP server.
6  *
7  * Kamailio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version
11  *
12  * Kamailio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License 
18  * along with this program; if not, write to the Free Software 
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "ul_db_tran.h"
23 #include "ul_db.h"
24 #include "p_usrloc_mod.h"
25
26 static str autocommit_off = str_init("SET AUTOCOMMIT=0");
27 static str start_transaction = str_init("START TRANSACTION");
28 static str commit = str_init("COMMIT");
29 static str autocommit_on = str_init("SET AUTOCOMMIT=1");
30 static str rollback = str_init("ROLLBACK");
31
32 static int submit_tran_start(db_func_t * dbf, db1_con_t * dbh);
33 static int submit_tran_commit(db_func_t * dbf, db1_con_t * dbh);
34 static int submit_tran_rollback(db_func_t * dbf, db1_con_t * dbh);
35
36 int ul_db_tran_start(ul_db_handle_t * handle, int working[]) {
37         int i;
38         int errors = 0;
39         int w = 0;
40
41         if(!handle || !working) {
42                 LM_ERR("NULL pointer in parameter.\n");
43                 return -1;
44         }
45
46         for(i=0; i<DB_NUM; i++) {
47                 if(handle->db[i].status == DB_ON) {
48                         if(submit_tran_start(&handle->db[i].dbf, handle->db[i].dbh) < 0) {
49                                 LM_ERR("error while starting "
50                                     "transaction on id %i, db %i.\n", handle->id, handle->db[i].no);
51                                 if(db_handle_error(handle, handle->db[i].no) < 0) {
52                                         LM_ERR("error during handling error "
53                                             "on id %i on db %i, trying again.\n", handle->id, handle->db[i].no);
54                                         errors++;
55                                 } else {
56                                         if(submit_tran_start(&handle->db[i].dbf, handle->db[i].dbh) < 0) {
57                                                 LM_ERR("error while starting "
58                                                     "transaction on id %i, db %i.\n", handle->id, handle->db[i].no);
59                                                 errors++;
60                                         }
61                                 }
62
63                         } else {
64                                 working[i] = 1;
65                                 w++;
66                         }
67                 }
68         }
69         if((errors > 0) || (w < handle->working)) {
70                 return -1;
71         }
72         return 0;
73 }
74
75 int ul_db_tran_commit(ul_db_handle_t * handle, int working[]) {
76         int i;
77         int errors = 0;
78         int w = 0;
79
80         if(!handle || !working) {
81                 LM_ERR("NULL pointer in parameter.\n");
82                 return -1;
83         }
84
85         for(i=0; i<DB_NUM; i++) {
86                 if((handle->db[i].status == DB_ON) && (working[i])) {
87                         if(submit_tran_commit(&handle->db[i].dbf, handle->db[i].dbh) < 0) {
88                                 LM_ERR("error while committing "
89                                     "transaction on id %i, db %i.\n", handle->id, handle->db[i].no);
90                                 if(db_handle_error(handle, handle->db[i].no) < 0) {
91                                         LM_ERR("error during handling error "
92                                             "on id %i on db %i, trying again.\n", handle->id, handle->db[i].no);
93                                 }
94                                 errors++;
95                         } else {
96                                 w++;
97                         }
98                 }
99         }
100         if((errors > 0) || (w < get_working_sum(working, DB_NUM))) {
101                 return -1;
102         }
103         return 0;
104 }
105
106 int ul_db_tran_rollback(ul_db_handle_t * handle, int working[]) {
107         int i;
108         int errors = 0;
109         int w = 0;
110
111         if(!handle || !working) {
112                 LM_ERR("NULL pointer in parameter.\n");
113                 return -1;
114         }
115
116         for(i=0; i<DB_NUM; i++) {
117                 if((handle->db[i].status == DB_ON) && (working[i])) {
118                         if(submit_tran_rollback(&handle->db[i].dbf, handle->db[i].dbh) < 0) {
119                                 LM_ERR("error while rolling back "
120                                     "transaction on id %i, db %i.\n", handle->id, handle->db[i].no);
121                                 errors++;
122                         } else {
123                                 w++;
124                         }
125                 }
126         }
127         if((errors > 0) || (w < get_working_sum(working, DB_NUM))) {
128                 return -1;
129         }
130         return 0;
131 }
132
133 static int submit_tran_start(db_func_t * dbf, db1_con_t * dbh) {
134         int errors = 0;
135         str tmp;
136         if(dbh) {
137                 if(dbf->raw_query(dbh, &autocommit_off, NULL) < 0) {
138                         LM_ERR("error while turning off "
139                             "autocommit.\n");
140                         errors++;
141                 }
142                 tmp.s  = isolation_level;
143                 tmp.len = strlen(isolation_level);
144                 if(dbf->raw_query(dbh, &tmp, NULL) < 0) {
145                         LM_ERR("error while setting "
146                             "isolation level.\n");
147                         errors++;
148                 }
149                 if(dbf->raw_query(dbh, &start_transaction, NULL) < 0) {
150                         LM_ERR("error while starting "
151                             "transaction.\n");
152                         errors++;
153                 }
154         } else {
155                 LM_ERR("no db handle.\n");
156                 return -1;
157         }
158         if(errors > 0) {
159                 return -1;
160         }
161         return 0;
162 }
163
164 static int submit_tran_commit(db_func_t * dbf, db1_con_t * dbh) {
165         int errors = 0;
166
167         if(dbh) {
168                 if(dbf->raw_query(dbh, &commit, NULL) < 0) {
169                         LM_ERR("error during commit.\n");
170                         errors++;
171                 }
172                 if(dbf->raw_query(dbh, &autocommit_on, NULL) < 0) {
173                         LM_ERR("error while turning "
174                             "on autocommit.\n");
175                         errors++;
176                 }
177         } else {
178                 LM_ERR("no db handle.\n");
179                 return -1;
180         }
181
182         if(errors > 0) {
183                 return -1;
184         }
185         return 0;
186 }
187
188 static int submit_tran_rollback(db_func_t * dbf, db1_con_t * dbh) {
189         int errors = 0;
190
191         if(dbh) {
192                 if(dbf->raw_query(dbh, &rollback, NULL) < 0) {
193                         LM_ERR("error during "
194                             "rollback.\n");
195                         errors++;
196                 }
197                 if(dbf->raw_query(dbh, &autocommit_on, NULL) < 0) {
198                         LM_ERR("error while "
199                             "turning on autocommit.\n");
200                         errors++;
201                 }
202         } else {
203                 LM_ERR("no db handle.\n");
204                 return -1;
205         }
206
207         if(errors > 0) {
208                 return -1;
209         }
210         return 0;
211 }
212
213 int get_working_sum(int working[], int no) {
214         int i;
215         int sum = 0;
216         if(!working) {
217                 return -1;
218         }
219         for(i=0; i<no; i++) {
220                 sum += working[i];
221         }
222         return sum;
223 }