~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/db/dbmi_base/table.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
  \file lib/db/dbmi_base/table.c
 
3
  
 
4
  \brief DBMI Library (base) - table management
 
5
  
 
6
  (C) 1999-2009, 2011 by the GRASS Development Team
 
7
  
 
8
  This program is free software under the GNU General Public License
 
9
  (>=v2). Read the file COPYING that comes with GRASS for details.
 
10
  
 
11
  \author Joel Jones (CERL/UIUC), Radim Blazek
 
12
  \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
 
13
*/
 
14
 
1
15
#include <stdlib.h>
 
16
#include <string.h>
2
17
#include <grass/gis.h>
3
18
#include <grass/dbmi.h>
4
19
 
5
20
/*!
6
 
   \fn 
7
 
   \brief 
8
 
   \return 
9
 
   \param 
 
21
  \brief Allocate a table with a specific number of columns
 
22
  
 
23
  \param ncols number of columns which should be allocated
 
24
  
 
25
  \return allocated dbTable
 
26
  \return NULL in case of an error
10
27
 */
11
28
dbTable *db_alloc_table(int ncols)
12
29
{
32
49
}
33
50
 
34
51
/*!
35
 
   \fn 
36
 
   \brief 
37
 
   \return 
38
 
   \param 
39
 
 */
 
52
   \brief Initialize the table to zero
 
53
   
 
54
   \param table pointer to dbTable
 
55
*/
40
56
void db_init_table(dbTable * table)
41
57
{
42
58
    db_zero((void *)table, sizeof(dbTable));
45
61
}
46
62
 
47
63
/*!
48
 
   \fn 
49
 
   \brief 
50
 
   \return 
51
 
   \param 
52
 
 */
 
64
  \brief Free the table
 
65
  
 
66
  \param table pointer to dbTable
 
67
*/
53
68
void db_free_table(dbTable * table)
54
69
{
55
70
    int i;
64
79
}
65
80
 
66
81
/*!
67
 
   \fn 
68
 
   \brief 
69
 
   \return 
70
 
   \param 
71
 
 */
 
82
   \brief Set the name of the table
 
83
 
 
84
   \param table pointer to dbTable
 
85
   \param name The name of the table
 
86
 
 
87
   \return DB_OK on success
 
88
*/
72
89
int db_set_table_name(dbTable * table, const char *name)
73
90
{
74
91
    return db_set_string(&table->tableName, name);
75
92
}
76
93
 
77
94
/*!
78
 
   \fn 
79
 
   \brief 
80
 
   \return 
81
 
   \param 
82
 
 */
 
95
  \brief Get the name of the table
 
96
 
 
97
  \param table pointer to dbTable
 
98
 
 
99
  \return name of the table
 
100
*/
83
101
const char *db_get_table_name(dbTable * table)
84
102
{
85
103
    return db_get_string(&table->tableName);
86
104
}
87
105
 
88
106
/*!
89
 
   \fn 
90
 
   \brief 
91
 
   \return 
92
 
   \param 
 
107
  \brief Set the description of the table
 
108
  
 
109
  \param table pointer to dbTable
 
110
  \param name description of the table
 
111
  
 
112
  \return DB_OK
93
113
 */
94
114
int db_set_table_description(dbTable * table, const char *description)
95
115
{
97
117
}
98
118
 
99
119
/*!
100
 
   \fn 
101
 
   \brief 
102
 
   \return 
103
 
   \param 
104
 
 */
 
120
  \brief Get the description of the table
 
121
 
 
122
  \param table pointer to dbTable
 
123
  
 
124
  \return description of the table
 
125
*/
105
126
const char *db_get_table_description(dbTable * table)
106
127
{
107
128
    return db_get_string(&table->description);
108
129
}
109
130
 
110
131
/*!
111
 
   \fn 
112
 
   \brief 
113
 
   \return 
114
 
   \param 
115
 
 */
 
132
  \brief Return the number of columns of the table
 
133
  
 
134
  \param table pointer to dbTable
 
135
  
 
136
  \return number of columns
 
137
*/
116
138
int db_get_table_number_of_columns(dbTable * table)
117
139
{
118
140
    return table->numColumns;
119
141
}
120
142
 
121
 
/*!
122
 
   \fn 
123
 
   \brief 
124
 
   \return 
125
 
   \param 
126
 
 */
127
143
static void set_all_column_privs(dbTable * table, void (*set_column_priv) ())
128
144
{
129
145
    int col, ncols;
136
152
    }
137
153
}
138
154
 
139
 
/*!
140
 
   \fn 
141
 
   \brief 
142
 
   \return 
143
 
   \param 
144
 
 */
145
155
static int get_all_column_privs(dbTable * table, int (*get_column_priv) ())
146
156
{
147
157
    int priv, col, ncols;
158
168
}
159
169
 
160
170
/*!
161
 
   \fn 
162
 
   \brief 
163
 
   \return 
164
 
   \param 
165
 
 */
 
171
  \brief Grant selection privileges for all columns
 
172
  
 
173
  \param table pointer to dbTable
 
174
*/
166
175
void db_set_table_select_priv_granted(dbTable * table)
167
176
{
168
177
    set_all_column_privs(table, db_set_column_select_priv_granted);
169
178
}
170
179
 
171
180
/*!
172
 
   \fn 
173
 
   \brief 
174
 
   \return 
175
 
   \param 
176
 
 */
 
181
  \brief Set selection privileges not granted for all columns
 
182
  
 
183
  \param table pointer to dbTable
 
184
*/
177
185
void db_set_table_select_priv_not_granted(dbTable * table)
178
186
{
179
187
    set_all_column_privs(table, db_set_column_select_priv_not_granted);
180
188
}
181
189
 
182
190
/*!
183
 
   \fn 
184
 
   \brief 
185
 
   \return 
186
 
   \param 
187
 
 */
 
191
  \brief Get table select privileges
 
192
 
 
193
  \param table pointer to dbTable
 
194
 
 
195
  \return privilages
 
196
*/
188
197
int db_get_table_select_priv(dbTable * table)
189
198
{
190
199
    return get_all_column_privs(table, db_get_column_select_priv);
191
200
}
192
201
 
193
202
/*!
194
 
   \fn 
195
 
   \brief 
196
 
   \return 
197
 
   \param 
198
 
 */
 
203
  \brief Grant update privileges for all columns
 
204
 
 
205
  \param table pointer to dbTable
 
206
*/
199
207
void db_set_table_update_priv_granted(dbTable * table)
200
208
{
201
209
    set_all_column_privs(table, db_set_column_update_priv_granted);
202
210
}
203
211
 
204
212
/*!
205
 
   \fn 
206
 
   \brief 
207
 
   \return 
208
 
   \param 
209
 
 */
 
213
  \brief Set update privileges not granted for all columns
 
214
  
 
215
  \param table pointer to dbTable
 
216
*/
210
217
void db_set_table_update_priv_not_granted(dbTable * table)
211
218
{
212
219
    set_all_column_privs(table, db_set_column_update_priv_not_granted);
213
220
}
214
221
 
215
222
/*!
216
 
   \fn 
217
 
   \brief 
218
 
   \return 
219
 
   \param 
220
 
 */
 
223
  \brief Get table update privileges
 
224
 
 
225
  \param table pointer to dbTable
 
226
 
 
227
  \return privilages
 
228
*/
221
229
int db_get_table_update_priv(dbTable * table)
222
230
{
223
231
    return get_all_column_privs(table, db_get_column_update_priv);
224
232
}
225
233
 
226
234
/*!
227
 
   \fn 
228
 
   \brief 
229
 
   \return 
230
 
   \param 
231
 
 */
 
235
  \brief Grant insert privileges for table
 
236
  
 
237
  \param table pointer to dbTable
 
238
*/
232
239
void db_set_table_insert_priv_granted(dbTable * table)
233
240
{
234
241
    table->priv_insert = DB_GRANTED;
235
242
}
236
243
 
237
244
/*!
238
 
   \fn 
239
 
   \brief 
240
 
   \return 
241
 
   \param 
 
245
   \brief Set insert privileges not granted for table
 
246
   
 
247
   \param table pointer to dbTable
242
248
 */
243
249
void db_set_table_insert_priv_not_granted(dbTable * table)
244
250
{
246
252
}
247
253
 
248
254
/*!
249
 
   \fn 
250
 
   \brief 
251
 
   \return 
252
 
   \param 
253
 
 */
 
255
  \brief Get table insert privileges
 
256
 
 
257
  \param table pointer to dbTable
 
258
 
 
259
  \return prilileges
 
260
*/
254
261
int db_get_table_insert_priv(dbTable * table)
255
262
{
256
263
    return table->priv_insert;
257
264
}
258
265
 
259
266
/*!
260
 
   \fn 
261
 
   \brief 
262
 
   \return 
263
 
   \param 
 
267
  \brief Grant delete privileges for table
 
268
  
 
269
  \param table pointer to dbTable
264
270
 */
265
271
void db_set_table_delete_priv_granted(dbTable * table)
266
272
{
268
274
}
269
275
 
270
276
/*!
271
 
   \fn 
272
 
   \brief 
273
 
   \return 
274
 
   \param 
275
 
 */
 
277
  \brief Set delete privileges not granted for table
 
278
  
 
279
  \param table pointer to dbTable
 
280
*/
276
281
void db_set_table_delete_priv_not_granted(dbTable * table)
277
282
{
278
283
    table->priv_delete = DB_NOT_GRANTED;
279
284
}
280
285
 
281
286
/*!
282
 
   \fn 
283
 
   \brief 
284
 
   \return 
285
 
   \param 
286
 
 */
 
287
  \brief Get table delete privileges
 
288
 
 
289
  \param table pointer to dbTable
 
290
 
 
291
  \return privileges
 
292
*/
287
293
int db_get_table_delete_priv(dbTable * table)
288
294
{
289
295
    return table->priv_delete;
290
296
}
291
297
 
292
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
*/
 
307
dbColumn *db_get_table_column(dbTable * table, int idx)
 
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
*/
 
323
dbColumn *db_get_table_column_by_name(dbTable * table, const char* name)
 
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
*/
 
354
int db_set_table_column(dbTable * table, int idx, dbColumn *column)
 
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
*/
 
372
int db_append_table_column(dbTable * table, dbColumn *column)
 
373
{
 
374
    table->columns = (dbColumn*)db_realloc((void*)table->columns, sizeof(dbColumn)*(table->numColumns + 1));
 
375
    if(table->columns == NULL)
 
376
        return DB_FAILED;
 
377
    db_copy_column(&table->columns[table->numColumns], column);
 
378
    table->numColumns++;
 
379
    return DB_OK;
 
380
}
 
381
 
 
382
/*!
 
383
  \brief Make a new exact copy of an existing table
 
384
  
 
385
  New memory is allocated for the clone, the columns-content will be copied too.
 
386
  
 
387
  \param src Pointer to dbTable
 
388
  
 
389
  \return A new alloacted clone of the given table on success 
 
390
  \return NULL on error
 
391
*/
 
392
dbTable *db_clone_table(dbTable *src)
 
393
{
 
394
    int i, n = db_get_table_number_of_columns(src);
 
395
    dbTable *new = db_alloc_table(n);
 
396
    if(new == NULL)
 
397
        return (new = NULL);
 
398
 
 
399
    db_copy_string(&new->description, &src->description);
 
400
    db_copy_string(&new->tableName, &src->tableName);
 
401
 
 
402
    /* Deep copy the columns */
 
403
    for(i = 0; i < n; i++)
 
404
    {
 
405
        db_copy_column(&new->columns[i], &src->columns[i]);
 
406
    }
 
407
 
 
408
    new->numColumns = n;
 
409
    new->priv_delete = src->priv_delete;
 
410
    new->priv_insert = src->priv_insert;
 
411
 
 
412
    return new;
 
413
}
 
414
 
 
415
/*!
293
416
   \brief Create SQL CREATE sring from table definition
294
 
   \return 
295
 
   \param 
296
 
 */
 
417
 
 
418
   \param table pointer to dbTable
 
419
   \param sql dbString to store the SQL CREATE string
 
420
 
 
421
   \return DB_OK on success
 
422
   \return DB_FAILED on error
 
423
*/
297
424
int db_table_to_sql(dbTable * table, dbString * sql)
298
425
{
299
426
    int col, ncols;