Browse code

db_postgres: small spelling fix: lenght -> length

Henning Westerholt authored on 24/03/2019 18:19:37
Showing 1 changed files
... ...
@@ -138,7 +138,7 @@ static inline int sb_add(struct string_buffer *sb, str *nstr)
138 138
 
139 139
 /** Creates str string from zero terminated string without copying.
140 140
  * This function initializes members of a temporary str structure
141
- * with the pointer and lenght of the string from s parameter.
141
+ * with the pointer and length of the string from s parameter.
142 142
  *
143 143
  * @param str A pointer to temporary str structure.
144 144
  * @param s   A zero terminated string.
Browse code

db_postgres: use generic PKG_MEM_ERROR helper defines, fix missing error log

Henning Westerholt authored on 26/01/2019 17:46:40
Showing 1 changed files
... ...
@@ -118,7 +118,7 @@ static inline int sb_add(struct string_buffer *sb, str *nstr)
118 118
 							 * sb->increment;
119 119
 		newp = pkg_malloc(new_size);
120 120
 		if(!newp) {
121
-			ERR("postgres: No memory left\n");
121
+			PKG_MEM_ERROR;
122 122
 			return -1;
123 123
 		}
124 124
 		if(sb->s) {
Browse code

db_postgres: clang format of the code

Daniel-Constantin Mierla authored on 11/10/2017 16:23:19
Showing 1 changed files
... ...
@@ -42,7 +42,8 @@
42 42
 #include <string.h>
43 43
 
44 44
 
45
-enum {
45
+enum
46
+{
46 47
 	STR_DELETE,
47 48
 	STR_INSERT,
48 49
 	STR_UPDATE,
... ...
@@ -68,40 +69,30 @@ enum {
68 69
 };
69 70
 
70 71
 
71
-static str strings[] = {
72
-	STR_STATIC_INIT("delete from "),
73
-	STR_STATIC_INIT("insert into "),
74
-	STR_STATIC_INIT("update "),
75
-	STR_STATIC_INIT("select "),
76
-	STR_STATIC_INIT("replace "),
77
-	STR_STATIC_INIT(" set "),
78
-	STR_STATIC_INIT(" where "),
79
-	STR_STATIC_INIT(" is "),
80
-	STR_STATIC_INIT(" and "),
81
-	STR_STATIC_INIT(" or "),
82
-	STR_STATIC_INIT("?"),
83
-	STR_STATIC_INIT("="),
84
-	STR_STATIC_INIT("!="),
85
-	STR_STATIC_INIT("<"),
86
-	STR_STATIC_INIT(">"),
87
-	STR_STATIC_INIT("<="),
88
-	STR_STATIC_INIT(">="),
89
-	STR_STATIC_INIT(") values ("),
90
-	STR_STATIC_INIT(" from "),
91
-	STR_STATIC_INIT("select typname,pg_type.oid from pg_type"),
92
-	STR_STATIC_INIT("select timestamp '2000-01-01 00:00:00' + time '00:00:01'"), 
93
-	STR_STATIC_INIT("\0")
94
-};
72
+static str strings[] = {STR_STATIC_INIT("delete from "),
73
+		STR_STATIC_INIT("insert into "), STR_STATIC_INIT("update "),
74
+		STR_STATIC_INIT("select "), STR_STATIC_INIT("replace "),
75
+		STR_STATIC_INIT(" set "), STR_STATIC_INIT(" where "),
76
+		STR_STATIC_INIT(" is "), STR_STATIC_INIT(" and "),
77
+		STR_STATIC_INIT(" or "), STR_STATIC_INIT("?"), STR_STATIC_INIT("="),
78
+		STR_STATIC_INIT("!="), STR_STATIC_INIT("<"), STR_STATIC_INIT(">"),
79
+		STR_STATIC_INIT("<="), STR_STATIC_INIT(">="),
80
+		STR_STATIC_INIT(") values ("), STR_STATIC_INIT(" from "),
81
+		STR_STATIC_INIT("select typname,pg_type.oid from pg_type"),
82
+		STR_STATIC_INIT(
83
+				"select timestamp '2000-01-01 00:00:00' + time '00:00:01'"),
84
+		STR_STATIC_INIT("\0")};
95 85
 
96 86
 
97 87
 /**
98 88
  * Reallocatable string buffer.
99 89
  */
100
-struct string_buffer {
101
-	char *s;			/**< allocated memory itself */
102
-	int   len;			/**< used memory */
103
-	int   size;			/**< total size of allocated memory */
104
-	int   increment;	/**< increment when realloc is necessary */ 
90
+struct string_buffer
91
+{
92
+	char *s;	   /**< allocated memory itself */
93
+	int len;	   /**< used memory */
94
+	int size;	  /**< total size of allocated memory */
95
+	int increment; /**< increment when realloc is necessary */
105 96
 };
106 97
 
107 98
 
... ...
@@ -120,16 +111,17 @@ static inline int sb_add(struct string_buffer *sb, str *nstr)
120 111
 	int asize;
121 112
 	char *newp;
122 113
 
123
-	if (rsize > sb->size) {
114
+	if(rsize > sb->size) {
124 115
 		asize = rsize - sb->size;
125
-		new_size = sb->size + (asize / sb->increment
126
-						+ (asize % sb->increment > 0)) * sb->increment;
116
+		new_size = sb->size
117
+				   + (asize / sb->increment + (asize % sb->increment > 0))
118
+							 * sb->increment;
127 119
 		newp = pkg_malloc(new_size);
128
-		if (!newp) {
120
+		if(!newp) {
129 121
 			ERR("postgres: No memory left\n");
130 122
 			return -1;
131 123
 		}
132
-		if (sb->s) {
124
+		if(sb->s) {
133 125
 			memcpy(newp, sb->s, sb->len);
134 126
 			pkg_free(sb->s);
135 127
 		}
... ...
@@ -152,7 +144,7 @@ static inline int sb_add(struct string_buffer *sb, str *nstr)
152 144
  * @param s   A zero terminated string.
153 145
  * @return Pointer to the str structure.
154 146
  */
155
-static inline str* set_str(str *str, const char *s)
147
+static inline str *set_str(str *str, const char *s)
156 148
 {
157 149
 	str->s = (char *)s;
158 150
 	str->len = strlen(s);
... ...
@@ -166,11 +158,11 @@ static inline str* set_str(str *str, const char *s)
166 158
  * @param i Number of the parameter
167 159
  * @retval A pointer to static string with the marker
168 160
  */
169
-static str* get_marker(unsigned int i)
161
+static str *get_marker(unsigned int i)
170 162
 {
171 163
 	static char buf[INT2STR_MAX_LEN + 1];
172 164
 	static str res;
173
-	const char* c;
165
+	const char *c;
174 166
 
175 167
 	buf[0] = '$';
176 168
 	res.s = buf;
... ...
@@ -182,238 +174,298 @@ static str* get_marker(unsigned int i)
182 174
 }
183 175
 
184 176
 
185
-int build_update_sql(str* sql_cmd, db_cmd_t* cmd)
177
+int build_update_sql(str *sql_cmd, db_cmd_t *cmd)
186 178
 {
187
-	struct string_buffer sql_buf = {.s = NULL, .len = 0, 
188
-							  .size = 0, .increment = 128};
189
-	db_fld_t* fld;
179
+	struct string_buffer sql_buf = {
180
+			.s = NULL, .len = 0, .size = 0, .increment = 128};
181
+	db_fld_t *fld;
190 182
 	int i, rv = 0;
191 183
 	str tmpstr;
192 184
 
193 185
 	rv = sb_add(&sql_buf, &strings[STR_UPDATE]); /* "UPDATE " */
194 186
 	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
195
-	rv |= sb_add(&sql_buf, &cmd->table);		 /* table name */
187
+	rv |= sb_add(&sql_buf, &cmd->table); /* table name */
196 188
 	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
197
-	rv |= sb_add(&sql_buf, &strings[STR_SET]);	 /* " SET " */
189
+	rv |= sb_add(&sql_buf, &strings[STR_SET]); /* " SET " */
198 190
 
199 191
 	/* column name-value pairs */
200
-	for(i = 0, fld = cmd->vals; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
192
+	for(i = 0, fld = cmd->vals; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]);
193
+			i++) {
201 194
 		rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
202 195
 		rv |= sb_add(&sql_buf, set_str(&tmpstr, "="));
203 196
 		rv |= sb_add(&sql_buf, &strings[STR_ESC]);
204
-		if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
197
+		if(!DB_FLD_LAST(fld[i + 1]))
198
+			rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
205 199
 	}
206
-	if (rv) goto error;
200
+	if(rv)
201
+		goto error;
207 202
 
208
-	if (!DB_FLD_EMPTY(cmd->match)) {
203
+	if(!DB_FLD_EMPTY(cmd->match)) {
209 204
 		rv |= sb_add(&sql_buf, &strings[STR_WHERE]);
210 205
 
211 206
 		for(i = 0, fld = cmd->match; !DB_FLD_LAST(fld[i]); i++) {
212 207
 			rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
213 208
 
214 209
 			switch(fld[i].op) {
215
-			case DB_EQ:  rv |= sb_add(&sql_buf, &strings[STR_OP_EQ]);  break;
216
-			case DB_NE:  rv |= sb_add(&sql_buf, &strings[STR_OP_NE]);  break;
217
-			case DB_LT:  rv |= sb_add(&sql_buf, &strings[STR_OP_LT]);  break;
218
-			case DB_GT:  rv |= sb_add(&sql_buf, &strings[STR_OP_GT]);  break;
219
-			case DB_LEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_LEQ]); break;
220
-			case DB_GEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_GEQ]); break;
210
+				case DB_EQ:
211
+					rv |= sb_add(&sql_buf, &strings[STR_OP_EQ]);
212
+					break;
213
+				case DB_NE:
214
+					rv |= sb_add(&sql_buf, &strings[STR_OP_NE]);
215
+					break;
216
+				case DB_LT:
217
+					rv |= sb_add(&sql_buf, &strings[STR_OP_LT]);
218
+					break;
219
+				case DB_GT:
220
+					rv |= sb_add(&sql_buf, &strings[STR_OP_GT]);
221
+					break;
222
+				case DB_LEQ:
223
+					rv |= sb_add(&sql_buf, &strings[STR_OP_LEQ]);
224
+					break;
225
+				case DB_GEQ:
226
+					rv |= sb_add(&sql_buf, &strings[STR_OP_GEQ]);
227
+					break;
221 228
 			}
222
-			
229
+
223 230
 			rv |= sb_add(&sql_buf, get_marker(i + 1));
224
-			if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, &strings[STR_AND]);
231
+			if(!DB_FLD_LAST(fld[i + 1]))
232
+				rv |= sb_add(&sql_buf, &strings[STR_AND]);
225 233
 		}
226 234
 	}
227 235
 	rv |= sb_add(&sql_buf, &strings[STR_ZT]);
228
-	if (rv) goto error;
236
+	if(rv)
237
+		goto error;
229 238
 
230 239
 	sql_cmd->s = sql_buf.s;
231 240
 	sql_cmd->len = sql_buf.len;
232 241
 	return 0;
233 242
 
234 243
 error:
235
-	if (sql_buf.s) pkg_free(sql_buf.s);
244
+	if(sql_buf.s)
245
+		pkg_free(sql_buf.s);
236 246
 	return -1;
237 247
 }
238 248
 
239 249
 
240
-int build_insert_sql(str* sql_cmd, db_cmd_t* cmd)
250
+int build_insert_sql(str *sql_cmd, db_cmd_t *cmd)
241 251
 {
242
-	struct string_buffer sql_buf = {.s = NULL, .len = 0, 
243
-									.size = 0, .increment = 128};
244
-	db_fld_t* fld;
252
+	struct string_buffer sql_buf = {
253
+			.s = NULL, .len = 0, .size = 0, .increment = 128};
254
+	db_fld_t *fld;
245 255
 	int i, rv = 0;
246 256
 	str tmpstr;
247 257
 
248 258
 	rv = sb_add(&sql_buf, &strings[STR_INSERT]); /* "INSERT INTO " */
249 259
 	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
250
-	rv |= sb_add(&sql_buf, &cmd->table);		 /* table name */
260
+	rv |= sb_add(&sql_buf, &cmd->table); /* table name */
251 261
 	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\" ("));
252 262
 
253 263
 	/* column names */
254
-	for(i = 0, fld = cmd->vals; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
264
+	for(i = 0, fld = cmd->vals; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]);
265
+			i++) {
255 266
 		rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
256
-		if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
267
+		if(!DB_FLD_LAST(fld[i + 1]))
268
+			rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
257 269
 	}
258
-	if (rv) goto error;
270
+	if(rv)
271
+		goto error;
259 272
 
260 273
 	rv |= sb_add(&sql_buf, &strings[STR_VALUES]);
261 274
 
262
-	for(i = 0, fld = cmd->vals; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
275
+	for(i = 0, fld = cmd->vals; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]);
276
+			i++) {
263 277
 		rv |= sb_add(&sql_buf, get_marker(i + 1));
264
-		if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
278
+		if(!DB_FLD_LAST(fld[i + 1]))
279
+			rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
265 280
 	}
266 281
 	rv |= sb_add(&sql_buf, set_str(&tmpstr, ")"));
267 282
 	rv |= sb_add(&sql_buf, &strings[STR_ZT]);
268
-	if (rv) goto error;
269
-				 
283
+	if(rv)
284
+		goto error;
285
+
270 286
 	sql_cmd->s = sql_buf.s;
271 287
 	sql_cmd->len = sql_buf.len;
272 288
 	return 0;
273 289
 
274 290
 error:
275
-	if (sql_buf.s) pkg_free(sql_buf.s);
291
+	if(sql_buf.s)
292
+		pkg_free(sql_buf.s);
276 293
 	return -1;
277 294
 }
278 295
 
279 296
 
280
-int build_delete_sql(str* sql_cmd, db_cmd_t* cmd)
297
+int build_delete_sql(str *sql_cmd, db_cmd_t *cmd)
281 298
 {
282
-	struct string_buffer sql_buf = {.s = NULL, .len = 0, 
283
-									.size = 0, .increment = 128};
284
-	db_fld_t* fld;
299
+	struct string_buffer sql_buf = {
300
+			.s = NULL, .len = 0, .size = 0, .increment = 128};
301
+	db_fld_t *fld;
285 302
 	int i, rv = 0;
286 303
 	str tmpstr;
287 304
 
288 305
 	rv = sb_add(&sql_buf, &strings[STR_DELETE]); /* "DELETE FROM " */
289 306
 	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
290
-	rv |= sb_add(&sql_buf, &cmd->table);		 /* table name */
307
+	rv |= sb_add(&sql_buf, &cmd->table); /* table name */
291 308
 	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
292 309
 
293
-	if (!DB_FLD_EMPTY(cmd->match)) {
310
+	if(!DB_FLD_EMPTY(cmd->match)) {
294 311
 		rv |= sb_add(&sql_buf, &strings[STR_WHERE]);
295 312
 
296 313
 		for(i = 0, fld = cmd->match; !DB_FLD_LAST(fld[i]); i++) {
297 314
 			rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
298 315
 
299 316
 			switch(fld[i].op) {
300
-			case DB_EQ:  rv |= sb_add(&sql_buf, &strings[STR_OP_EQ]);  break;
301
-			case DB_NE:  rv |= sb_add(&sql_buf, &strings[STR_OP_NE]);  break;
302
-			case DB_LT:  rv |= sb_add(&sql_buf, &strings[STR_OP_LT]);  break;
303
-			case DB_GT:  rv |= sb_add(&sql_buf, &strings[STR_OP_GT]);  break;
304
-			case DB_LEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_LEQ]); break;
305
-			case DB_GEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_GEQ]); break;
317
+				case DB_EQ:
318
+					rv |= sb_add(&sql_buf, &strings[STR_OP_EQ]);
319
+					break;
320
+				case DB_NE:
321
+					rv |= sb_add(&sql_buf, &strings[STR_OP_NE]);
322
+					break;
323
+				case DB_LT:
324
+					rv |= sb_add(&sql_buf, &strings[STR_OP_LT]);
325
+					break;
326
+				case DB_GT:
327
+					rv |= sb_add(&sql_buf, &strings[STR_OP_GT]);
328
+					break;
329
+				case DB_LEQ:
330
+					rv |= sb_add(&sql_buf, &strings[STR_OP_LEQ]);
331
+					break;
332
+				case DB_GEQ:
333
+					rv |= sb_add(&sql_buf, &strings[STR_OP_GEQ]);
334
+					break;
306 335
 			}
307
-			
336
+
308 337
 			rv |= sb_add(&sql_buf, get_marker(i + 1));
309
-			if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, &strings[STR_AND]);
338
+			if(!DB_FLD_LAST(fld[i + 1]))
339
+				rv |= sb_add(&sql_buf, &strings[STR_AND]);
310 340
 		}
311 341
 	}
312 342
 	rv |= sb_add(&sql_buf, &strings[STR_ZT]);
313
-	if (rv) goto error;
343
+	if(rv)
344
+		goto error;
314 345
 
315 346
 	sql_cmd->s = sql_buf.s;
316 347
 	sql_cmd->len = sql_buf.len;
317 348
 	return 0;
318 349
 
319 350
 error:
320
-	if (sql_buf.s) pkg_free(sql_buf.s);
351
+	if(sql_buf.s)
352
+		pkg_free(sql_buf.s);
321 353
 	return -1;
322 354
 }
323 355
 
324 356
 
325
-int build_select_sql(str* sql_cmd, db_cmd_t* cmd)
357
+int build_select_sql(str *sql_cmd, db_cmd_t *cmd)
326 358
 {
327
-	struct string_buffer sql_buf = {.s = NULL, .len = 0, 
328
-									.size = 0, .increment = 128};
329
-	db_fld_t* fld;
359
+	struct string_buffer sql_buf = {
360
+			.s = NULL, .len = 0, .size = 0, .increment = 128};
361
+	db_fld_t *fld;
330 362
 	int i, rv = 0;
331 363
 	str tmpstr;
332 364
 
333 365
 	rv = sb_add(&sql_buf, &strings[STR_SELECT]); /* "SELECT " */
334 366
 
335
-	if (DB_FLD_EMPTY(cmd->result)) {
367
+	if(DB_FLD_EMPTY(cmd->result)) {
336 368
 		rv |= sb_add(&sql_buf, set_str(&tmpstr, "*"));
337 369
 	} else {
338 370
 		for(i = 0, fld = cmd->result; !DB_FLD_LAST(fld[i]); i++) {
339 371
 			rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
340
-			if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
372
+			if(!DB_FLD_LAST(fld[i + 1]))
373
+				rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
341 374
 		}
342 375
 	}
343 376
 
344
-	rv |= sb_add(&sql_buf, &strings[STR_FROM]);  /* " FROM " */
377
+	rv |= sb_add(&sql_buf, &strings[STR_FROM]); /* " FROM " */
345 378
 	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
346
-	rv |= sb_add(&sql_buf, &cmd->table);		 /* table name */
379
+	rv |= sb_add(&sql_buf, &cmd->table); /* table name */
347 380
 	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
348 381
 
349
-	if (!DB_FLD_EMPTY(cmd->match)) {
382
+	if(!DB_FLD_EMPTY(cmd->match)) {
350 383
 		rv |= sb_add(&sql_buf, &strings[STR_WHERE]);
351 384
 
352 385
 		for(i = 0, fld = cmd->match; !DB_FLD_LAST(fld[i]); i++) {
353 386
 			rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
354 387
 
355 388
 			switch(fld[i].op) {
356
-			case DB_EQ:  rv |= sb_add(&sql_buf, &strings[STR_OP_EQ]);  break;
357
-			case DB_NE:  rv |= sb_add(&sql_buf, &strings[STR_OP_NE]);  break;
358
-			case DB_LT:  rv |= sb_add(&sql_buf, &strings[STR_OP_LT]);  break;
359
-			case DB_GT:  rv |= sb_add(&sql_buf, &strings[STR_OP_GT]);  break;
360
-			case DB_LEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_LEQ]); break;
361
-			case DB_GEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_GEQ]); break;
389
+				case DB_EQ:
390
+					rv |= sb_add(&sql_buf, &strings[STR_OP_EQ]);
391
+					break;
392
+				case DB_NE:
393
+					rv |= sb_add(&sql_buf, &strings[STR_OP_NE]);
394
+					break;
395
+				case DB_LT:
396
+					rv |= sb_add(&sql_buf, &strings[STR_OP_LT]);
397
+					break;
398
+				case DB_GT:
399
+					rv |= sb_add(&sql_buf, &strings[STR_OP_GT]);
400
+					break;
401
+				case DB_LEQ:
402
+					rv |= sb_add(&sql_buf, &strings[STR_OP_LEQ]);
403
+					break;
404
+				case DB_GEQ:
405
+					rv |= sb_add(&sql_buf, &strings[STR_OP_GEQ]);
406
+					break;
362 407
 			}
363
-			
408
+
364 409
 			rv |= sb_add(&sql_buf, get_marker(i + 1));
365
-			if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, &strings[STR_AND]);
410
+			if(!DB_FLD_LAST(fld[i + 1]))
411
+				rv |= sb_add(&sql_buf, &strings[STR_AND]);
366 412
 		}
367 413
 	}
368 414
 	rv |= sb_add(&sql_buf, &strings[STR_ZT]);
369
-	if (rv) goto error;
415
+	if(rv)
416
+		goto error;
370 417
 
371 418
 	sql_cmd->s = sql_buf.s;
372 419
 	sql_cmd->len = sql_buf.len;
373 420
 	return 0;
374 421
 
375 422
 error:
376
-	if (sql_buf.s) pkg_free(sql_buf.s);
423
+	if(sql_buf.s)
424
+		pkg_free(sql_buf.s);
377 425
 	return -1;
378 426
 }
379 427
 
380 428
 
381
-int build_select_oid_sql(str* sql_cmd)
429
+int build_select_oid_sql(str *sql_cmd)
382 430
 {
383
-	struct string_buffer sql_buf = {.s = NULL, .len = 0, 
384
-									.size = 0, .increment = 128};
431
+	struct string_buffer sql_buf = {
432
+			.s = NULL, .len = 0, .size = 0, .increment = 128};
385 433
 	int rv = 0;
386
-	
434
+
387 435
 	rv = sb_add(&sql_buf, &strings[STR_OID]);
388 436
 	rv |= sb_add(&sql_buf, &strings[STR_ZT]);
389
-	if (rv) goto error;
437
+	if(rv)
438
+		goto error;
390 439
 
391 440
 	sql_cmd->s = sql_buf.s;
392 441
 	sql_cmd->len = sql_buf.len;
393 442
 	return 0;
394 443
 
395
- error:
396
-	if (sql_buf.s) pkg_free(sql_buf.s);
444
+error:
445
+	if(sql_buf.s)
446
+		pkg_free(sql_buf.s);
397 447
 	return -1;
398 448
 }
399 449
 
400 450
 
401
-int build_timestamp_format_sql(str* sql_cmd)
451
+int build_timestamp_format_sql(str *sql_cmd)
402 452
 {
403
-	struct string_buffer sql_buf = {.s = NULL, .len = 0, 
404
-									.size = 0, .increment = 128};
453
+	struct string_buffer sql_buf = {
454
+			.s = NULL, .len = 0, .size = 0, .increment = 128};
405 455
 	int rv = 0;
406
-	
456
+
407 457
 	rv = sb_add(&sql_buf, &strings[STR_TIMESTAMP]);
408 458
 	rv |= sb_add(&sql_buf, &strings[STR_ZT]);
409
-	if (rv) goto error;
459
+	if(rv)
460
+		goto error;
410 461
 
411 462
 	sql_cmd->s = sql_buf.s;
412 463
 	sql_cmd->len = sql_buf.len;
413 464
 	return 0;
414 465
 
415
- error:
416
-	if (sql_buf.s) pkg_free(sql_buf.s);
466
+error:
467
+	if(sql_buf.s)
468
+		pkg_free(sql_buf.s);
417 469
 	return -1;
418 470
 }
419 471
 
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -35,9 +35,9 @@
35 35
 
36 36
 #include "../../lib/srdb2/db_cmd.h"
37 37
 #include "../../lib/srdb2/db_fld.h"
38
-#include "../../mem/mem.h"
39
-#include "../../dprint.h"
40
-#include "../../ut.h"
38
+#include "../../core/mem/mem.h"
39
+#include "../../core/dprint.h"
40
+#include "../../core/ut.h"
41 41
 
42 42
 #include <string.h>
43 43
 
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,420 @@
1
+/* 
2
+ * PostgreSQL Database Driver for Kamailio
3
+ *
4
+ * Portions Copyright (C) 2001-2003 FhG FOKUS
5
+ * Copyright (C) 2003 August.Net Services, LLC
6
+ * Portions Copyright (C) 2005-2008 iptelorg GmbH
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify it under the
11
+ * terms of the GNU General Public License as published by the Free Software
12
+ * Foundation; either version 2 of the License, or (at your option) any later
13
+ * version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful, but WITHOUT ANY
16
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18
+ * details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License along
21
+ * with this program; if not, write to the Free Software Foundation, Inc., 59
22
+ * Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
+ */
24
+
25
+/** \addtogroup postgres
26
+ * @{ 
27
+ */
28
+
29
+/** \file
30
+ * Implementation of various functions that assemble SQL query strings for
31
+ * PostgreSQL.
32
+ */
33
+
34
+#include "pg_sql.h"
35
+
36
+#include "../../lib/srdb2/db_cmd.h"
37
+#include "../../lib/srdb2/db_fld.h"
38
+#include "../../mem/mem.h"
39
+#include "../../dprint.h"
40
+#include "../../ut.h"
41
+
42
+#include <string.h>
43
+
44
+
45
+enum {
46
+	STR_DELETE,
47
+	STR_INSERT,
48
+	STR_UPDATE,
49
+	STR_SELECT,
50
+	STR_REPLACE,
51
+	STR_SET,
52
+	STR_WHERE,
53
+	STR_IS,
54
+	STR_AND,
55
+	STR_OR,
56
+	STR_ESC,
57
+	STR_OP_EQ,
58
+	STR_OP_NE,
59
+	STR_OP_LT,
60
+	STR_OP_GT,
61
+	STR_OP_LEQ,
62
+	STR_OP_GEQ,
63
+	STR_VALUES,
64
+	STR_FROM,
65
+	STR_OID,
66
+	STR_TIMESTAMP,
67
+	STR_ZT
68
+};
69
+
70
+
71
+static str strings[] = {
72
+	STR_STATIC_INIT("delete from "),
73
+	STR_STATIC_INIT("insert into "),
74
+	STR_STATIC_INIT("update "),
75
+	STR_STATIC_INIT("select "),
76
+	STR_STATIC_INIT("replace "),
77
+	STR_STATIC_INIT(" set "),
78
+	STR_STATIC_INIT(" where "),
79
+	STR_STATIC_INIT(" is "),
80
+	STR_STATIC_INIT(" and "),
81
+	STR_STATIC_INIT(" or "),
82
+	STR_STATIC_INIT("?"),
83
+	STR_STATIC_INIT("="),
84
+	STR_STATIC_INIT("!="),
85
+	STR_STATIC_INIT("<"),
86
+	STR_STATIC_INIT(">"),
87
+	STR_STATIC_INIT("<="),
88
+	STR_STATIC_INIT(">="),
89
+	STR_STATIC_INIT(") values ("),
90
+	STR_STATIC_INIT(" from "),
91
+	STR_STATIC_INIT("select typname,pg_type.oid from pg_type"),
92
+	STR_STATIC_INIT("select timestamp '2000-01-01 00:00:00' + time '00:00:01'"), 
93
+	STR_STATIC_INIT("\0")
94
+};
95
+
96
+
97
+/**
98
+ * Reallocatable string buffer.
99
+ */
100
+struct string_buffer {
101
+	char *s;			/**< allocated memory itself */
102
+	int   len;			/**< used memory */
103
+	int   size;			/**< total size of allocated memory */
104
+	int   increment;	/**< increment when realloc is necessary */ 
105
+};
106
+
107
+
108
+/** Appends string to string buffer.
109
+ * This function appends string to dynamically created string buffer,
110
+ * the buffer is automatically extended if there is not enough room
111
+ * in the buffer. The buffer is allocated using pkg_malloc.
112
+ * @param sb    string buffer
113
+ * @param nstr  string to add
114
+ * @return      0 if OK, -1 if failed
115
+ */
116
+static inline int sb_add(struct string_buffer *sb, str *nstr)
117
+{
118
+	int new_size = 0;
119
+	int rsize = sb->len + nstr->len;
120
+	int asize;
121
+	char *newp;
122
+
123
+	if (rsize > sb->size) {
124
+		asize = rsize - sb->size;
125
+		new_size = sb->size + (asize / sb->increment
126
+						+ (asize % sb->increment > 0)) * sb->increment;
127
+		newp = pkg_malloc(new_size);
128
+		if (!newp) {
129
+			ERR("postgres: No memory left\n");
130
+			return -1;
131
+		}
132
+		if (sb->s) {
133
+			memcpy(newp, sb->s, sb->len);
134
+			pkg_free(sb->s);
135
+		}
136
+		sb->s = newp;
137
+		sb->size = new_size;
138
+	}
139
+	if(sb->s) {
140
+		memcpy(sb->s + sb->len, nstr->s, nstr->len);
141
+		sb->len += nstr->len;
142
+	}
143
+	return 0;
144
+}
145
+
146
+
147
+/** Creates str string from zero terminated string without copying.
148
+ * This function initializes members of a temporary str structure
149
+ * with the pointer and lenght of the string from s parameter.
150
+ *
151
+ * @param str A pointer to temporary str structure.
152
+ * @param s   A zero terminated string.
153
+ * @return Pointer to the str structure.
154
+ */
155
+static inline str* set_str(str *str, const char *s)
156
+{
157
+	str->s = (char *)s;
158
+	str->len = strlen(s);
159
+	return str;
160
+}
161
+
162
+
163
+/** Returns a parameter marker for PostgreSQL with number i
164
+ * The function builds a parameter marker for use in
165
+ * PostgreSQL SQL queries, such as $1, $2, etc.
166
+ * @param i Number of the parameter
167
+ * @retval A pointer to static string with the marker
168
+ */
169
+static str* get_marker(unsigned int i)
170
+{
171
+	static char buf[INT2STR_MAX_LEN + 1];
172
+	static str res;
173
+	const char* c;
174
+
175
+	buf[0] = '$';
176
+	res.s = buf;
177
+
178
+	c = int2str(i, &res.len);
179
+	memcpy(res.s + 1, c, res.len);
180
+	res.len++;
181
+	return &res;
182
+}
183
+
184
+
185
+int build_update_sql(str* sql_cmd, db_cmd_t* cmd)
186
+{
187
+	struct string_buffer sql_buf = {.s = NULL, .len = 0, 
188
+							  .size = 0, .increment = 128};
189
+	db_fld_t* fld;
190
+	int i, rv = 0;
191
+	str tmpstr;
192
+
193
+	rv = sb_add(&sql_buf, &strings[STR_UPDATE]); /* "UPDATE " */
194
+	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
195
+	rv |= sb_add(&sql_buf, &cmd->table);		 /* table name */
196
+	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
197
+	rv |= sb_add(&sql_buf, &strings[STR_SET]);	 /* " SET " */
198
+
199
+	/* column name-value pairs */
200
+	for(i = 0, fld = cmd->vals; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
201
+		rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
202
+		rv |= sb_add(&sql_buf, set_str(&tmpstr, "="));
203
+		rv |= sb_add(&sql_buf, &strings[STR_ESC]);
204
+		if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
205
+	}
206
+	if (rv) goto error;
207
+
208
+	if (!DB_FLD_EMPTY(cmd->match)) {
209
+		rv |= sb_add(&sql_buf, &strings[STR_WHERE]);
210
+
211
+		for(i = 0, fld = cmd->match; !DB_FLD_LAST(fld[i]); i++) {
212
+			rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
213
+
214
+			switch(fld[i].op) {
215
+			case DB_EQ:  rv |= sb_add(&sql_buf, &strings[STR_OP_EQ]);  break;
216
+			case DB_NE:  rv |= sb_add(&sql_buf, &strings[STR_OP_NE]);  break;
217
+			case DB_LT:  rv |= sb_add(&sql_buf, &strings[STR_OP_LT]);  break;
218
+			case DB_GT:  rv |= sb_add(&sql_buf, &strings[STR_OP_GT]);  break;
219
+			case DB_LEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_LEQ]); break;
220
+			case DB_GEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_GEQ]); break;
221
+			}
222
+			
223
+			rv |= sb_add(&sql_buf, get_marker(i + 1));
224
+			if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, &strings[STR_AND]);
225
+		}
226
+	}
227
+	rv |= sb_add(&sql_buf, &strings[STR_ZT]);
228
+	if (rv) goto error;
229
+
230
+	sql_cmd->s = sql_buf.s;
231
+	sql_cmd->len = sql_buf.len;
232
+	return 0;
233
+
234
+error:
235
+	if (sql_buf.s) pkg_free(sql_buf.s);
236
+	return -1;
237
+}
238
+
239
+
240
+int build_insert_sql(str* sql_cmd, db_cmd_t* cmd)
241
+{
242
+	struct string_buffer sql_buf = {.s = NULL, .len = 0, 
243
+									.size = 0, .increment = 128};
244
+	db_fld_t* fld;
245
+	int i, rv = 0;
246
+	str tmpstr;
247
+
248
+	rv = sb_add(&sql_buf, &strings[STR_INSERT]); /* "INSERT INTO " */
249
+	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
250
+	rv |= sb_add(&sql_buf, &cmd->table);		 /* table name */
251
+	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\" ("));
252
+
253
+	/* column names */
254
+	for(i = 0, fld = cmd->vals; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
255
+		rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
256
+		if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
257
+	}
258
+	if (rv) goto error;
259
+
260
+	rv |= sb_add(&sql_buf, &strings[STR_VALUES]);
261
+
262
+	for(i = 0, fld = cmd->vals; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
263
+		rv |= sb_add(&sql_buf, get_marker(i + 1));
264
+		if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
265
+	}
266
+	rv |= sb_add(&sql_buf, set_str(&tmpstr, ")"));
267
+	rv |= sb_add(&sql_buf, &strings[STR_ZT]);
268
+	if (rv) goto error;
269
+				 
270
+	sql_cmd->s = sql_buf.s;
271
+	sql_cmd->len = sql_buf.len;
272
+	return 0;
273
+
274
+error:
275
+	if (sql_buf.s) pkg_free(sql_buf.s);
276
+	return -1;
277
+}
278
+
279
+
280
+int build_delete_sql(str* sql_cmd, db_cmd_t* cmd)
281
+{
282
+	struct string_buffer sql_buf = {.s = NULL, .len = 0, 
283
+									.size = 0, .increment = 128};
284
+	db_fld_t* fld;
285
+	int i, rv = 0;
286
+	str tmpstr;
287
+
288
+	rv = sb_add(&sql_buf, &strings[STR_DELETE]); /* "DELETE FROM " */
289
+	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
290
+	rv |= sb_add(&sql_buf, &cmd->table);		 /* table name */
291
+	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
292
+
293
+	if (!DB_FLD_EMPTY(cmd->match)) {
294
+		rv |= sb_add(&sql_buf, &strings[STR_WHERE]);
295
+
296
+		for(i = 0, fld = cmd->match; !DB_FLD_LAST(fld[i]); i++) {
297
+			rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
298
+
299
+			switch(fld[i].op) {
300
+			case DB_EQ:  rv |= sb_add(&sql_buf, &strings[STR_OP_EQ]);  break;
301
+			case DB_NE:  rv |= sb_add(&sql_buf, &strings[STR_OP_NE]);  break;
302
+			case DB_LT:  rv |= sb_add(&sql_buf, &strings[STR_OP_LT]);  break;
303
+			case DB_GT:  rv |= sb_add(&sql_buf, &strings[STR_OP_GT]);  break;
304
+			case DB_LEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_LEQ]); break;
305
+			case DB_GEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_GEQ]); break;
306
+			}
307
+			
308
+			rv |= sb_add(&sql_buf, get_marker(i + 1));
309
+			if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, &strings[STR_AND]);
310
+		}
311
+	}
312
+	rv |= sb_add(&sql_buf, &strings[STR_ZT]);
313
+	if (rv) goto error;
314
+
315
+	sql_cmd->s = sql_buf.s;
316
+	sql_cmd->len = sql_buf.len;
317
+	return 0;
318
+
319
+error:
320
+	if (sql_buf.s) pkg_free(sql_buf.s);
321
+	return -1;
322
+}
323
+
324
+
325
+int build_select_sql(str* sql_cmd, db_cmd_t* cmd)
326
+{
327
+	struct string_buffer sql_buf = {.s = NULL, .len = 0, 
328
+									.size = 0, .increment = 128};
329
+	db_fld_t* fld;
330
+	int i, rv = 0;
331
+	str tmpstr;
332
+
333
+	rv = sb_add(&sql_buf, &strings[STR_SELECT]); /* "SELECT " */
334
+
335
+	if (DB_FLD_EMPTY(cmd->result)) {
336
+		rv |= sb_add(&sql_buf, set_str(&tmpstr, "*"));
337
+	} else {
338
+		for(i = 0, fld = cmd->result; !DB_FLD_LAST(fld[i]); i++) {
339
+			rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
340
+			if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, set_str(&tmpstr, ","));
341
+		}
342
+	}
343
+
344
+	rv |= sb_add(&sql_buf, &strings[STR_FROM]);  /* " FROM " */
345
+	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
346
+	rv |= sb_add(&sql_buf, &cmd->table);		 /* table name */
347
+	rv |= sb_add(&sql_buf, set_str(&tmpstr, "\""));
348
+
349
+	if (!DB_FLD_EMPTY(cmd->match)) {
350
+		rv |= sb_add(&sql_buf, &strings[STR_WHERE]);
351
+
352
+		for(i = 0, fld = cmd->match; !DB_FLD_LAST(fld[i]); i++) {
353
+			rv |= sb_add(&sql_buf, set_str(&tmpstr, fld[i].name));
354
+
355
+			switch(fld[i].op) {
356
+			case DB_EQ:  rv |= sb_add(&sql_buf, &strings[STR_OP_EQ]);  break;
357
+			case DB_NE:  rv |= sb_add(&sql_buf, &strings[STR_OP_NE]);  break;
358
+			case DB_LT:  rv |= sb_add(&sql_buf, &strings[STR_OP_LT]);  break;
359
+			case DB_GT:  rv |= sb_add(&sql_buf, &strings[STR_OP_GT]);  break;
360
+			case DB_LEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_LEQ]); break;
361
+			case DB_GEQ: rv |= sb_add(&sql_buf, &strings[STR_OP_GEQ]); break;
362
+			}
363
+			
364
+			rv |= sb_add(&sql_buf, get_marker(i + 1));
365
+			if (!DB_FLD_LAST(fld[i + 1])) rv |= sb_add(&sql_buf, &strings[STR_AND]);
366
+		}
367
+	}
368
+	rv |= sb_add(&sql_buf, &strings[STR_ZT]);
369
+	if (rv) goto error;
370
+
371
+	sql_cmd->s = sql_buf.s;