2
\file lib/db/dbmi_base/table.c
4
\brief DBMI Library (base) - table management
6
(C) 1999-2009, 2011 by the GRASS Development Team
8
This program is free software under the GNU General Public License
9
(>=v2). Read the file COPYING that comes with GRASS for details.
11
\author Joel Jones (CERL/UIUC), Radim Blazek
12
\author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
2
17
#include <grass/gis.h>
3
18
#include <grass/dbmi.h>
21
\brief Allocate a table with a specific number of columns
23
\param ncols number of columns which should be allocated
25
\return allocated dbTable
26
\return NULL in case of an error
11
28
dbTable *db_alloc_table(int ncols)
82
\brief Set the name of the table
84
\param table pointer to dbTable
85
\param name The name of the table
87
\return DB_OK on success
72
89
int db_set_table_name(dbTable * table, const char *name)
74
91
return db_set_string(&table->tableName, name);
95
\brief Get the name of the table
97
\param table pointer to dbTable
99
\return name of the table
83
101
const char *db_get_table_name(dbTable * table)
85
103
return db_get_string(&table->tableName);
107
\brief Set the description of the table
109
\param table pointer to dbTable
110
\param name description of the table
94
114
int db_set_table_description(dbTable * table, const char *description)
120
\brief Get the description of the table
122
\param table pointer to dbTable
124
\return description of the table
105
126
const char *db_get_table_description(dbTable * table)
107
128
return db_get_string(&table->description);
132
\brief Return the number of columns of the table
134
\param table pointer to dbTable
136
\return number of columns
116
138
int db_get_table_number_of_columns(dbTable * table)
118
140
return table->numColumns;
127
143
static void set_all_column_privs(dbTable * table, void (*set_column_priv) ())
171
\brief Grant selection privileges for all columns
173
\param table pointer to dbTable
166
175
void db_set_table_select_priv_granted(dbTable * table)
168
177
set_all_column_privs(table, db_set_column_select_priv_granted);
181
\brief Set selection privileges not granted for all columns
183
\param table pointer to dbTable
177
185
void db_set_table_select_priv_not_granted(dbTable * table)
179
187
set_all_column_privs(table, db_set_column_select_priv_not_granted);
191
\brief Get table select privileges
193
\param table pointer to dbTable
188
197
int db_get_table_select_priv(dbTable * table)
190
199
return get_all_column_privs(table, db_get_column_select_priv);
203
\brief Grant update privileges for all columns
205
\param table pointer to dbTable
199
207
void db_set_table_update_priv_granted(dbTable * table)
201
209
set_all_column_privs(table, db_set_column_update_priv_granted);
213
\brief Set update privileges not granted for all columns
215
\param table pointer to dbTable
210
217
void db_set_table_update_priv_not_granted(dbTable * table)
212
219
set_all_column_privs(table, db_set_column_update_priv_not_granted);
223
\brief Get table update privileges
225
\param table pointer to dbTable
221
229
int db_get_table_update_priv(dbTable * table)
223
231
return get_all_column_privs(table, db_get_column_update_priv);
235
\brief Grant insert privileges for table
237
\param table pointer to dbTable
232
239
void db_set_table_insert_priv_granted(dbTable * table)
234
241
table->priv_insert = DB_GRANTED;
245
\brief Set insert privileges not granted for table
247
\param table pointer to dbTable
243
249
void db_set_table_insert_priv_not_granted(dbTable * table)
277
\brief Set delete privileges not granted for table
279
\param table pointer to dbTable
276
281
void db_set_table_delete_priv_not_granted(dbTable * table)
278
283
table->priv_delete = DB_NOT_GRANTED;
287
\brief Get table delete privileges
289
\param table pointer to dbTable
287
293
int db_get_table_delete_priv(dbTable * table)
289
295
return table->priv_delete;
299
\brief Returns column structure for given table and column number
301
\param table pointer to dbTable
302
\param idx column index (starting with '0')
304
\return pointer to dbColumn
305
\return NULL if not found
307
dbColumn *db_get_table_column(dbTable * table, int idx)
309
if (idx < 0 || idx >= table->numColumns)
310
return ((dbColumn *) NULL);
311
return &table->columns[idx];
315
\brief Returns column structure for given table and column name
317
\param table pointer to dbTable
318
\param name the name of the column
320
\return pointer to dbColumn
321
\return NULL if not found
323
dbColumn *db_get_table_column_by_name(dbTable * table, const char* name)
326
int i, columns = table->numColumns;
328
for(i = 0; i < columns; i++ ) {
329
c = db_get_table_column(table, i);
334
if(strcmp(name, db_get_string(&c->columnName)) == 0)
344
\brief Set a specific column for given table and column number
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.
351
\return DB_OK on success
352
\return DB_FAILURE on error
354
int db_set_table_column(dbTable * table, int idx, dbColumn *column)
356
if (idx < 0 || idx >= table->numColumns)
358
db_copy_column(&table->columns[idx], column);
363
\brief Append a specific column to given table
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.
369
\return DB_OK on success
370
\return DB_FAILURE on error
372
int db_append_table_column(dbTable * table, dbColumn *column)
374
table->columns = (dbColumn*)db_realloc((void*)table->columns, sizeof(dbColumn)*(table->numColumns + 1));
375
if(table->columns == NULL)
377
db_copy_column(&table->columns[table->numColumns], column);
383
\brief Make a new exact copy of an existing table
385
New memory is allocated for the clone, the columns-content will be copied too.
387
\param src Pointer to dbTable
389
\return A new alloacted clone of the given table on success
390
\return NULL on error
392
dbTable *db_clone_table(dbTable *src)
394
int i, n = db_get_table_number_of_columns(src);
395
dbTable *new = db_alloc_table(n);
399
db_copy_string(&new->description, &src->description);
400
db_copy_string(&new->tableName, &src->tableName);
402
/* Deep copy the columns */
403
for(i = 0; i < n; i++)
405
db_copy_column(&new->columns[i], &src->columns[i]);
409
new->priv_delete = src->priv_delete;
410
new->priv_insert = src->priv_insert;
293
416
\brief Create SQL CREATE sring from table definition
418
\param table pointer to dbTable
419
\param sql dbString to store the SQL CREATE string
421
\return DB_OK on success
422
\return DB_FAILED on error
297
424
int db_table_to_sql(dbTable * table, dbString * sql)