GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
dbmi_base/table.c
Go to the documentation of this file.
1/*!
2 \file lib/db/dbmi_base/table.c
3
4 \brief DBMI Library (base) - table management
5
6 SPDX-FileCopyrightText: 1999-2009, 2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Joel Jones (CERL/UIUC), Radim Blazek
10 \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
11 */
12
13#include <stdlib.h>
14#include <string.h>
15#include <grass/gis.h>
16#include <grass/dbmi.h>
17
18/*!
19 \brief Allocate a table with a specific number of columns
20
21 \param ncols number of columns which should be allocated
22
23 \return allocated dbTable
24 \return NULL in case of an error
25 */
27{
28 dbTable *table;
29 int i;
30
31 table = (dbTable *)db_malloc(sizeof(dbTable));
32 if (table == NULL)
33 return (table = NULL);
34
35 db_init_table(table);
36
37 table->columns = (dbColumn *)db_calloc(sizeof(dbColumn), ncols);
38 if (table->columns == NULL) {
39 db_free(table);
40 return (table = NULL);
41 }
42 table->numColumns = ncols;
43 for (i = 0; i < ncols; i++)
44 db_init_column(&table->columns[i]);
45
46 return table;
47}
48
49/*!
50 \brief Initialize the table to zero
51
52 \param table pointer to dbTable
53 */
55{
56 db_zero((void *)table, sizeof(dbTable));
59}
60
61/*!
62 \brief Free the table
63
64 \param table pointer to dbTable
65 */
67{
68 int i;
69
72 for (i = 0; i < table->numColumns; i++)
73 db_free_column(&table->columns[i]);
74 if (table->columns)
75 db_free(table->columns);
76 db_free(table);
77}
78
79/*!
80 \brief Set the name of the table
81
82 \param table pointer to dbTable
83 \param name The name of the table
84
85 \return DB_OK on success
86 */
87int db_set_table_name(dbTable *table, const char *name)
88{
89 return db_set_string(&table->tableName, name);
90}
91
92/*!
93 \brief Get the name of the table
94
95 \param table pointer to dbTable
96
97 \return name of the table
98 */
99const char *db_get_table_name(dbTable *table)
100{
101 return db_get_string(&table->tableName);
102}
103
104/*!
105 \brief Set the description of the table
106
107 \param table pointer to dbTable
108 \param description description of the table
109
110 \return DB_OK
111 */
112int db_set_table_description(dbTable *table, const char *description)
113{
114 return db_set_string(&table->description, description);
115}
116
117/*!
118 \brief Get the description of the table
119
120 \param table pointer to dbTable
121
122 \return description of the table
123 */
125{
126 return db_get_string(&table->description);
127}
128
129/*!
130 \brief Return the number of columns of the table
131
132 \param table pointer to dbTable
133
134 \return number of columns
135 */
137{
138 return table->numColumns;
139}
140
141static void set_all_column_privs(dbTable *table,
142 void (*set_column_priv)(dbColumn *))
143{
144 int col, ncols;
146
147 ncols = db_get_table_number_of_columns(table);
148 for (col = 0; col < ncols; col++) {
151 }
152}
153
154static int get_all_column_privs(dbTable *table,
155 int (*get_column_priv)(dbColumn *))
156{
157 int priv, col, ncols;
159
160 ncols = db_get_table_number_of_columns(table);
161 for (col = 0; col < ncols; col++) {
164 if (priv != DB_GRANTED)
165 return priv;
166 }
167 return DB_GRANTED;
168}
169
170/*!
171 \brief Grant selection privileges for all columns
172
173 \param table pointer to dbTable
174 */
176{
177 set_all_column_privs(table, db_set_column_select_priv_granted);
178}
179
180/*!
181 \brief Set selection privileges not granted for all columns
182
183 \param table pointer to dbTable
184 */
186{
187 set_all_column_privs(table, db_set_column_select_priv_not_granted);
188}
189
190/*!
191 \brief Get table select privileges
192
193 \param table pointer to dbTable
194
195 \return privileges
196 */
198{
199 return get_all_column_privs(table, db_get_column_select_priv);
200}
201
202/*!
203 \brief Grant update privileges for all columns
204
205 \param table pointer to dbTable
206 */
208{
209 set_all_column_privs(table, db_set_column_update_priv_granted);
210}
211
212/*!
213 \brief Set update privileges not granted for all columns
214
215 \param table pointer to dbTable
216 */
218{
219 set_all_column_privs(table, db_set_column_update_priv_not_granted);
220}
221
222/*!
223 \brief Get table update privileges
224
225 \param table pointer to dbTable
226
227 \return privileges
228 */
230{
231 return get_all_column_privs(table, db_get_column_update_priv);
232}
233
234/*!
235 \brief Grant insert privileges for table
236
237 \param table pointer to dbTable
238 */
243
244/*!
245 \brief Set insert privileges not granted for table
246
247 \param table pointer to dbTable
248 */
253
254/*!
255 \brief Get table insert privileges
256
257 \param table pointer to dbTable
258
259 \return prilileges
260 */
262{
263 return table->priv_insert;
264}
265
266/*!
267 \brief Grant delete privileges for table
268
269 \param table pointer to dbTable
270 */
275
276/*!
277 \brief Set delete privileges not granted for table
278
279 \param table pointer to dbTable
280 */
285
286/*!
287 \brief Get table delete privileges
288
289 \param table pointer to dbTable
290
291 \return privileges
292 */
294{
295 return table->priv_delete;
296}
297
298/*!
299 \brief Returns column structure for given table and column number
300
301 \param table pointer to dbTable
302 \param idx column index (starting with '0')
303
304 \return pointer to dbColumn
305 \return NULL if not found
306 */
308{
309 if (idx < 0 || idx >= table->numColumns)
310 return ((dbColumn *)NULL);
311 return &table->columns[idx];
312}
313
314/*!
315 \brief Returns column structure for given table and column name
316
317 \param table pointer to dbTable
318 \param name the name of the column
319
320 \return pointer to dbColumn
321 \return NULL if not found
322 */
324{
325 dbColumn *c = NULL;
326 int i, columns = table->numColumns;
327
328 for (i = 0; i < columns; i++) {
329 c = db_get_table_column(table, i);
330
331 if (c == NULL)
332 return c;
333
334 if (strcmp(name, db_get_string(&c->columnName)) == 0)
335 break;
336
337 c = NULL;
338 }
339
340 return c;
341}
342
343/*!
344 \brief Set a specific column for given table and column number
345
346 \param table Pointer to dbTable
347 \param idx Column index (starting with '0'). The index must be in range.
348 \param column Pointer to a dbColumn to insert.
349 A copy of the column stored, so the original column can be deleted.
350
351 \return DB_OK on success
352 \return DB_FAILURE on error
353 */
355{
356 if (idx < 0 || idx >= table->numColumns)
357 return DB_FAILED;
358 db_copy_column(&table->columns[idx], column);
359 return DB_OK;
360}
361
362/*!
363 \brief Append a specific column to given table
364
365 \param table Pointer to dbTable
366 \param column Pointer to a dbColumn to append.
367 A copy of the column is stored, so the original column can be deleted.
368
369 \return DB_OK on success
370 \return DB_FAILURE on error
371 */
373{
374 table->columns = (dbColumn *)db_realloc(
375 (void *)table->columns, sizeof(dbColumn) * (table->numColumns + 1));
376 if (table->columns == NULL)
377 return DB_FAILED;
378 db_copy_column(&table->columns[table->numColumns], column);
379 table->numColumns++;
380 return DB_OK;
381}
382
383/*!
384 \brief Make a new exact copy of an existing table
385
386 New memory is allocated for the clone, the columns-content will be copied
387 too.
388
389 \param src Pointer to dbTable
390
391 \return A new alloacted clone of the given table on success
392 \return NULL on error
393 */
395{
396 int i, n = db_get_table_number_of_columns(src);
397 dbTable *new = db_alloc_table(n);
398
399 if (new == NULL)
400 return (new = NULL);
401
402 db_copy_string(&new->description, &src->description);
403 db_copy_string(&new->tableName, &src->tableName);
404
405 /* Deep copy the columns */
406 for (i = 0; i < n; i++) {
407 db_copy_column(&new->columns[i], &src->columns[i]);
408 }
409
410 new->numColumns = n;
411 new->priv_delete = src->priv_delete;
412 new->priv_insert = src->priv_insert;
413
414 return new;
415}
416
417/*!
418 \brief Create SQL CREATE string from table definition
419
420 \param table pointer to dbTable
421 \param sql dbString to store the SQL CREATE string
422
423 \return DB_OK on success
424 \return DB_FAILED on error
425 */
427{
428 int col, ncols;
430 const char *colname;
431 int sqltype;
432 char buf[500];
433
434 db_set_string(sql, "create table ");
436 db_append_string(sql, " ( ");
437
438 ncols = db_get_table_number_of_columns(table);
439
440 for (col = 0; col < ncols; col++) {
444
445 G_debug(3, "%s (%s)", colname, db_sqltype_name(sqltype));
446
447 if (col > 0)
448 db_append_string(sql, ", ");
450 db_append_string(sql, " ");
451 /* Note: I found on Web:
452 * These are the ANSI data types: BIT, CHARACTER, DATE, DECIMAL, DOUBLE
453 * PRECISION, FLOAT, INTEGER, INTERVAL, NUMERIC, REAL, SMALLINT,
454 * TIMESTAMP, TIME, VARBIT, VARCHAR, CHAR
455 * ...
456 * Thus, the only data types you can use with the assurance that they
457 * will work everywhere are as follows: DOUBLE PRECISION, FLOAT,
458 * INTEGER, NUMERIC, REAL, SMALLINT, VARCHAR, CHAR */
459 switch (sqltype) {
461 snprintf(buf, sizeof(buf), "varchar(%d)",
463 db_append_string(sql, buf);
464 break;
465 case DB_SQL_TYPE_TEXT:
466 G_warning("Type TEXT converted to 'VARCHAR(250)'");
467 db_append_string(sql, "varchar(250)");
468 break;
471 db_append_string(sql, "integer");
472 break;
473 case DB_SQL_TYPE_REAL:
478 db_append_string(sql, "double precision");
479 break;
480 case DB_SQL_TYPE_DATE:
481 db_append_string(sql, "date");
482 break;
483 case DB_SQL_TYPE_TIME:
484 db_append_string(sql, "time");
485 break;
487 db_append_string(sql, "datetime");
488 break;
489 default:
490 G_warning("Unknown column type (%s)", colname);
491 return DB_FAILED;
492 }
493 }
494 db_append_string(sql, " )");
495 G_debug(3, "sql statement: %s", db_get_string(sql));
496
497 return DB_OK;
498}
int columns
Definition calc.c:11
#define NULL
Definition ccmath.h:32
Main header of GRASS DataBase Management Interface.
#define DB_SQL_TYPE_TIME
Definition dbmi.h:87
#define DB_SQL_TYPE_TEXT
Definition dbmi.h:90
#define DB_SQL_TYPE_INTEGER
Definition dbmi.h:81
struct _db_column dbColumn
#define DB_NOT_GRANTED
Definition dbmi.h:127
#define DB_SQL_TYPE_SMALLINT
Definition dbmi.h:80
#define DB_SQL_TYPE_NUMERIC
Definition dbmi.h:85
#define DB_SQL_TYPE_INTERVAL
Definition dbmi.h:89
#define DB_GRANTED
Definition dbmi.h:126
#define DB_SQL_TYPE_REAL
Definition dbmi.h:82
#define DB_FAILED
Definition dbmi.h:70
#define DB_SQL_TYPE_DOUBLE_PRECISION
Definition dbmi.h:83
#define DB_SQL_TYPE_DECIMAL
Definition dbmi.h:84
#define DB_SQL_TYPE_DATE
Definition dbmi.h:86
#define DB_OK
Definition dbmi.h:69
#define DB_SQL_TYPE_CHARACTER
Definition dbmi.h:79
#define DB_SQL_TYPE_TIMESTAMP
Definition dbmi.h:88
dbTable * db_alloc_table(int ncols)
Allocate a table with a specific number of columns.
const char * db_get_table_name(dbTable *table)
Get the name of the table.
void db_set_table_update_priv_not_granted(dbTable *table)
Set update privileges not granted for all columns.
int db_table_to_sql(dbTable *table, dbString *sql)
Create SQL CREATE string from table definition.
int db_get_table_number_of_columns(dbTable *table)
Return the number of columns of the table.
int db_set_table_name(dbTable *table, const char *name)
Set the name of the table.
int db_get_table_insert_priv(dbTable *table)
Get table insert privileges.
const char * db_get_table_description(dbTable *table)
Get the description of the table.
dbColumn * db_get_table_column_by_name(dbTable *table, const char *name)
Returns column structure for given table and column name.
int db_get_table_select_priv(dbTable *table)
Get table select privileges.
int db_get_table_update_priv(dbTable *table)
Get table update privileges.
void db_set_table_update_priv_granted(dbTable *table)
Grant update privileges for all columns.
void db_set_table_delete_priv_not_granted(dbTable *table)
Set delete privileges not granted for table.
dbTable * db_clone_table(dbTable *src)
Make a new exact copy of an existing table.
void db_set_table_delete_priv_granted(dbTable *table)
Grant delete privileges for table.
void db_set_table_insert_priv_not_granted(dbTable *table)
Set insert privileges not granted for table.
void db_free_table(dbTable *table)
Free the table.
int db_set_table_description(dbTable *table, const char *description)
Set the description of the table.
void db_set_table_insert_priv_granted(dbTable *table)
Grant insert privileges for table.
int db_get_table_delete_priv(dbTable *table)
Get table delete privileges.
dbColumn * db_get_table_column(dbTable *table, int idx)
Returns column structure for given table and column number.
void db_set_table_select_priv_not_granted(dbTable *table)
Set selection privileges not granted for all columns.
void db_set_table_select_priv_granted(dbTable *table)
Grant selection privileges for all columns.
int db_set_table_column(dbTable *table, int idx, dbColumn *column)
Set a specific column for given table and column number.
void db_init_table(dbTable *table)
Initialize the table to zero.
int db_append_table_column(dbTable *table, dbColumn *column)
Append a specific column to given table.
int db_get_column_select_priv(dbColumn *)
Get select privileges.
void db_set_column_update_priv_not_granted(dbColumn *)
Unset update privileges.
int db_copy_string(dbString *, const dbString *)
Copy dbString.
Definition string.c:228
dbColumn * db_copy_column(dbColumn *, dbColumn *)
Copy a db column from source to destination.
int db_get_column_length(dbColumn *)
Get column's length.
void db_set_column_select_priv_not_granted(dbColumn *)
Unset select privileges.
int db_get_column_sqltype(dbColumn *)
Returns column sqltype for column.
void * db_realloc(void *, int)
Reallocate memory.
void db_free_string(dbString *)
Free allocated space for dbString.
Definition string.c:148
void * db_calloc(int, int)
Allocate memory.
char * db_get_string(const dbString *)
Get string.
Definition string.c:138
void db_set_column_select_priv_granted(dbColumn *)
Set select privileges to be granted.
void db_set_column_update_priv_granted(dbColumn *)
Set update privileges to be granted.
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition string.c:39
void db_init_column(dbColumn *)
Initialize dbColumn.
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
void db_free(void *)
Free allocated memory.
void db_free_column(dbColumn *)
Frees column structure.
void db_zero(void *, int)
Zero allocated space.
void db_init_string(dbString *)
Initialize dbString.
Definition string.c:23
const char * db_sqltype_name(int)
Get SQL data type description.
Definition sqltype.c:23
int db_append_string(dbString *, const char *)
Append string to dbString.
Definition string.c:203
int db_get_column_update_priv(dbColumn *)
Get update privileges.
void * db_malloc(int)
Allocate memory.
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
const char * name
Definition named_colr.c:6
dbString columnName
Definition dbmi.h:194
dbString tableName
Definition dbmi.h:211
int priv_insert
Definition dbmi.h:215
int priv_delete
Definition dbmi.h:216
int numColumns
Definition dbmi.h:213
dbString description
Definition dbmi.h:212
dbColumn * columns
Definition dbmi.h:214