-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
table.c
495 lines (397 loc) · 11.2 KB
/
table.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*!
\file lib/db/dbmi_base/table.c
\brief DBMI Library (base) - table management
(C) 1999-2009, 2011 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
\author Joel Jones (CERL/UIUC), Radim Blazek
\author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
*/
#include <stdlib.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
/*!
\brief Allocate a table with a specific number of columns
\param ncols number of columns which should be allocated
\return allocated dbTable
\return NULL in case of an error
*/
dbTable *db_alloc_table(int ncols)
{
dbTable *table;
int i;
table = (dbTable *) db_malloc(sizeof(dbTable));
if (table == NULL)
return (table = NULL);
db_init_table(table);
table->columns = (dbColumn *) db_calloc(sizeof(dbColumn), ncols);
if (table->columns == NULL) {
db_free(table);
return (table = NULL);
}
table->numColumns = ncols;
for (i = 0; i < ncols; i++)
db_init_column(&table->columns[i]);
return table;
}
/*!
\brief Initialize the table to zero
\param table pointer to dbTable
*/
void db_init_table(dbTable * table)
{
db_zero((void *)table, sizeof(dbTable));
db_init_string(&table->tableName);
db_init_string(&table->description);
}
/*!
\brief Free the table
\param table pointer to dbTable
*/
void db_free_table(dbTable * table)
{
int i;
db_free_string(&table->tableName);
db_free_string(&table->description);
for (i = 0; i < table->numColumns; i++)
db_free_column(&table->columns[i]);
if (table->columns)
db_free(table->columns);
db_free(table);
}
/*!
\brief Set the name of the table
\param table pointer to dbTable
\param name The name of the table
\return DB_OK on success
*/
int db_set_table_name(dbTable * table, const char *name)
{
return db_set_string(&table->tableName, name);
}
/*!
\brief Get the name of the table
\param table pointer to dbTable
\return name of the table
*/
const char *db_get_table_name(dbTable * table)
{
return db_get_string(&table->tableName);
}
/*!
\brief Set the description of the table
\param table pointer to dbTable
\param name description of the table
\return DB_OK
*/
int db_set_table_description(dbTable * table, const char *description)
{
return db_set_string(&table->description, description);
}
/*!
\brief Get the description of the table
\param table pointer to dbTable
\return description of the table
*/
const char *db_get_table_description(dbTable * table)
{
return db_get_string(&table->description);
}
/*!
\brief Return the number of columns of the table
\param table pointer to dbTable
\return number of columns
*/
int db_get_table_number_of_columns(dbTable * table)
{
return table->numColumns;
}
static void set_all_column_privs(dbTable * table, void (*set_column_priv) ())
{
int col, ncols;
dbColumn *column;
ncols = db_get_table_number_of_columns(table);
for (col = 0; col < ncols; col++) {
column = db_get_table_column(table, col);
set_column_priv(column);
}
}
static int get_all_column_privs(dbTable * table, int (*get_column_priv) ())
{
int priv, col, ncols;
dbColumn *column;
ncols = db_get_table_number_of_columns(table);
for (col = 0; col < ncols; col++) {
column = db_get_table_column(table, col);
priv = get_column_priv(column);
if (priv != DB_GRANTED)
return priv;
}
return DB_GRANTED;
}
/*!
\brief Grant selection privileges for all columns
\param table pointer to dbTable
*/
void db_set_table_select_priv_granted(dbTable * table)
{
set_all_column_privs(table, db_set_column_select_priv_granted);
}
/*!
\brief Set selection privileges not granted for all columns
\param table pointer to dbTable
*/
void db_set_table_select_priv_not_granted(dbTable * table)
{
set_all_column_privs(table, db_set_column_select_priv_not_granted);
}
/*!
\brief Get table select privileges
\param table pointer to dbTable
\return privilages
*/
int db_get_table_select_priv(dbTable * table)
{
return get_all_column_privs(table, db_get_column_select_priv);
}
/*!
\brief Grant update privileges for all columns
\param table pointer to dbTable
*/
void db_set_table_update_priv_granted(dbTable * table)
{
set_all_column_privs(table, db_set_column_update_priv_granted);
}
/*!
\brief Set update privileges not granted for all columns
\param table pointer to dbTable
*/
void db_set_table_update_priv_not_granted(dbTable * table)
{
set_all_column_privs(table, db_set_column_update_priv_not_granted);
}
/*!
\brief Get table update privileges
\param table pointer to dbTable
\return privilages
*/
int db_get_table_update_priv(dbTable * table)
{
return get_all_column_privs(table, db_get_column_update_priv);
}
/*!
\brief Grant insert privileges for table
\param table pointer to dbTable
*/
void db_set_table_insert_priv_granted(dbTable * table)
{
table->priv_insert = DB_GRANTED;
}
/*!
\brief Set insert privileges not granted for table
\param table pointer to dbTable
*/
void db_set_table_insert_priv_not_granted(dbTable * table)
{
table->priv_insert = DB_NOT_GRANTED;
}
/*!
\brief Get table insert privileges
\param table pointer to dbTable
\return prilileges
*/
int db_get_table_insert_priv(dbTable * table)
{
return table->priv_insert;
}
/*!
\brief Grant delete privileges for table
\param table pointer to dbTable
*/
void db_set_table_delete_priv_granted(dbTable * table)
{
table->priv_delete = DB_GRANTED;
}
/*!
\brief Set delete privileges not granted for table
\param table pointer to dbTable
*/
void db_set_table_delete_priv_not_granted(dbTable * table)
{
table->priv_delete = DB_NOT_GRANTED;
}
/*!
\brief Get table delete privileges
\param table pointer to dbTable
\return privileges
*/
int db_get_table_delete_priv(dbTable * table)
{
return table->priv_delete;
}
/*!
\brief Returns column structure for given table and column number
\param table pointer to dbTable
\param idx column index (starting with '0')
\return pointer to dbColumn
\return NULL if not found
*/
dbColumn *db_get_table_column(dbTable * table, int idx)
{
if (idx < 0 || idx >= table->numColumns)
return ((dbColumn *) NULL);
return &table->columns[idx];
}
/*!
\brief Returns column structure for given table and column name
\param table pointer to dbTable
\param name the name of the column
\return pointer to dbColumn
\return NULL if not found
*/
dbColumn *db_get_table_column_by_name(dbTable * table, const char* name)
{
dbColumn *c = NULL;
int i, columns = table->numColumns;
for(i = 0; i < columns; i++ ) {
c = db_get_table_column(table, i);
if(c == NULL)
return c;
if(strcmp(name, db_get_string(&c->columnName)) == 0)
break;
c = NULL;
}
return c;
}
/*!
\brief Set a specific column for given table and column number
\param table Pointer to dbTable
\param idx Column index (starting with '0'). The index must be in range.
\param column Pointer to a dbColumn to insert.
A copy of the column stored, so the original column can be deleted.
\return DB_OK on success
\return DB_FAILURE on error
*/
int db_set_table_column(dbTable * table, int idx, dbColumn *column)
{
if (idx < 0 || idx >= table->numColumns)
return DB_FAILED;
db_copy_column(&table->columns[idx], column);
return DB_OK;
}
/*!
\brief Append a specific column to given table
\param table Pointer to dbTable
\param column Pointer to a dbColumn to append.
A copy of the column is stored, so the original column can be deleted.
\return DB_OK on success
\return DB_FAILURE on error
*/
int db_append_table_column(dbTable * table, dbColumn *column)
{
table->columns = (dbColumn*)db_realloc((void*)table->columns, sizeof(dbColumn)*(table->numColumns + 1));
if(table->columns == NULL)
return DB_FAILED;
db_copy_column(&table->columns[table->numColumns], column);
table->numColumns++;
return DB_OK;
}
/*!
\brief Make a new exact copy of an existing table
New memory is allocated for the clone, the columns-content will be copied too.
\param src Pointer to dbTable
\return A new alloacted clone of the given table on success
\return NULL on error
*/
dbTable *db_clone_table(dbTable *src)
{
int i, n = db_get_table_number_of_columns(src);
dbTable *new = db_alloc_table(n);
if(new == NULL)
return (new = NULL);
db_copy_string(&new->description, &src->description);
db_copy_string(&new->tableName, &src->tableName);
/* Deep copy the columns */
for(i = 0; i < n; i++)
{
db_copy_column(&new->columns[i], &src->columns[i]);
}
new->numColumns = n;
new->priv_delete = src->priv_delete;
new->priv_insert = src->priv_insert;
return new;
}
/*!
\brief Create SQL CREATE sring from table definition
\param table pointer to dbTable
\param sql dbString to store the SQL CREATE string
\return DB_OK on success
\return DB_FAILED on error
*/
int db_table_to_sql(dbTable * table, dbString * sql)
{
int col, ncols;
dbColumn *column;
const char *colname;
int sqltype, ctype;
char buf[500];
db_set_string(sql, "create table ");
db_append_string(sql, db_get_table_name(table));
db_append_string(sql, " ( ");
ncols = db_get_table_number_of_columns(table);
for (col = 0; col < ncols; col++) {
column = db_get_table_column(table, col);
colname = db_get_column_name(column);
sqltype = db_get_column_sqltype(column);
ctype = db_sqltype_to_Ctype(sqltype);
G_debug(3, "%s (%s)", colname, db_sqltype_name(sqltype));
if (col > 0)
db_append_string(sql, ", ");
db_append_string(sql, colname);
db_append_string(sql, " ");
/* Note: I found on Web:
* These are the ANSI data types: BIT, CHARACTER, DATE, DECIMAL, DOUBLE PRECISION, FLOAT,
* INTEGER, INTERVAL, NUMERIC, REAL, SMALLINT, TIMESTAMP, TIME, VARBIT, VARCHAR, CHAR
* ...
* Thus, the only data types you can use with the assurance that they will
* work everywhere are as follows:
* DOUBLE PRECISION, FLOAT, INTEGER, NUMERIC, REAL, SMALLINT, VARCHAR, CHAR */
switch (sqltype) {
case DB_SQL_TYPE_CHARACTER:
sprintf(buf, "varchar(%d)", db_get_column_length(column));
db_append_string(sql, buf);
break;
case DB_SQL_TYPE_TEXT:
G_warning("Type TEXT converted to 'VARCHAR(250)'");
db_append_string(sql, "varchar(250)");
break;
case DB_SQL_TYPE_SMALLINT:
case DB_SQL_TYPE_INTEGER:
db_append_string(sql, "integer");
break;
case DB_SQL_TYPE_REAL:
case DB_SQL_TYPE_DOUBLE_PRECISION:
case DB_SQL_TYPE_DECIMAL:
case DB_SQL_TYPE_NUMERIC:
case DB_SQL_TYPE_INTERVAL:
db_append_string(sql, "double precision");
break;
case DB_SQL_TYPE_DATE:
db_append_string(sql, "date");
break;
case DB_SQL_TYPE_TIME:
db_append_string(sql, "time");
break;
case DB_SQL_TYPE_TIMESTAMP:
db_append_string(sql, "datetime");
break;
default:
G_warning("Unknown column type (%s)", colname);
return DB_FAILED;
}
}
db_append_string(sql, " )");
G_debug(3, "sql statement: %s", db_get_string(sql));
return DB_OK;
}