GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dbmi_base/table.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <grass/gis.h>
3 #include <grass/dbmi.h>
4 
11 dbTable *db_alloc_table(int ncols)
12 {
13  dbTable *table;
14  int i;
15 
16  table = (dbTable *) db_malloc(sizeof(dbTable));
17  if (table == NULL)
18  return (table = NULL);
19 
20  db_init_table(table);
21 
22  table->columns = (dbColumn *) db_calloc(sizeof(dbColumn), ncols);
23  if (table->columns == NULL) {
24  db_free(table);
25  return (table = NULL);
26  }
27  table->numColumns = ncols;
28  for (i = 0; i < ncols; i++)
29  db_init_column(&table->columns[i]);
30 
31  return table;
32 }
33 
40 void db_init_table(dbTable * table)
41 {
42  db_zero((void *)table, sizeof(dbTable));
43  db_init_string(&table->tableName);
44  db_init_string(&table->description);
45 }
46 
53 void db_free_table(dbTable * table)
54 {
55  int i;
56 
57  db_free_string(&table->tableName);
58  db_free_string(&table->description);
59  for (i = 0; i < table->numColumns; i++)
60  db_free_column(&table->columns[i]);
61  if (table->columns)
62  db_free(table->columns);
63  db_free(table);
64 }
65 
72 int db_set_table_name(dbTable * table, const char *name)
73 {
74  return db_set_string(&table->tableName, name);
75 }
76 
83 const char *db_get_table_name(dbTable * table)
84 {
85  return db_get_string(&table->tableName);
86 }
87 
94 int db_set_table_description(dbTable * table, const char *description)
95 {
96  return db_set_string(&table->description, description);
97 }
98 
105 const char *db_get_table_description(dbTable * table)
106 {
107  return db_get_string(&table->description);
108 }
109 
116 int db_get_table_number_of_columns(dbTable * table)
117 {
118  return table->numColumns;
119 }
120 
127 static void set_all_column_privs(dbTable * table, void (*set_column_priv) ())
128 {
129  int col, ncols;
130  dbColumn *column;
131 
132  ncols = db_get_table_number_of_columns(table);
133  for (col = 0; col < ncols; col++) {
134  column = db_get_table_column(table, col);
135  set_column_priv(column);
136  }
137 }
138 
145 static int get_all_column_privs(dbTable * table, int (*get_column_priv) ())
146 {
147  int priv, col, ncols;
148  dbColumn *column;
149 
150  ncols = db_get_table_number_of_columns(table);
151  for (col = 0; col < ncols; col++) {
152  column = db_get_table_column(table, col);
153  priv = get_column_priv(column);
154  if (priv != DB_GRANTED)
155  return priv;
156  }
157  return DB_GRANTED;
158 }
159 
166 void db_set_table_select_priv_granted(dbTable * table)
167 {
168  set_all_column_privs(table, db_set_column_select_priv_granted);
169 }
170 
178 {
179  set_all_column_privs(table, db_set_column_select_priv_not_granted);
180 }
181 
188 int db_get_table_select_priv(dbTable * table)
189 {
190  return get_all_column_privs(table, db_get_column_select_priv);
191 }
192 
199 void db_set_table_update_priv_granted(dbTable * table)
200 {
201  set_all_column_privs(table, db_set_column_update_priv_granted);
202 }
203 
211 {
212  set_all_column_privs(table, db_set_column_update_priv_not_granted);
213 }
214 
221 int db_get_table_update_priv(dbTable * table)
222 {
223  return get_all_column_privs(table, db_get_column_update_priv);
224 }
225 
232 void db_set_table_insert_priv_granted(dbTable * table)
233 {
234  table->priv_insert = DB_GRANTED;
235 }
236 
244 {
245  table->priv_insert = DB_NOT_GRANTED;
246 }
247 
254 int db_get_table_insert_priv(dbTable * table)
255 {
256  return table->priv_insert;
257 }
258 
265 void db_set_table_delete_priv_granted(dbTable * table)
266 {
267  table->priv_delete = DB_GRANTED;
268 }
269 
277 {
278  table->priv_delete = DB_NOT_GRANTED;
279 }
280 
287 int db_get_table_delete_priv(dbTable * table)
288 {
289  return table->priv_delete;
290 }
291 
297 int db_table_to_sql(dbTable * table, dbString * sql)
298 {
299  int col, ncols;
300  dbColumn *column;
301  const char *colname;
302  int sqltype, ctype;
303  char buf[500];
304 
305  db_set_string(sql, "create table ");
306  db_append_string(sql, db_get_table_name(table));
307  db_append_string(sql, " ( ");
308 
309  ncols = db_get_table_number_of_columns(table);
310 
311  for (col = 0; col < ncols; col++) {
312  column = db_get_table_column(table, col);
313  colname = db_get_column_name(column);
314  sqltype = db_get_column_sqltype(column);
315 
316  ctype = db_sqltype_to_Ctype(sqltype);
317  G_debug(3, "%s (%s)", colname, db_sqltype_name(sqltype));
318 
319  if (col > 0)
320  db_append_string(sql, ", ");
321  db_append_string(sql, colname);
322  db_append_string(sql, " ");
323  /* Note: I found on Web:
324  * These are the ANSI data types: BIT, CHARACTER, DATE, DECIMAL, DOUBLE PRECISION, FLOAT,
325  * INTEGER, INTERVAL, NUMERIC, REAL, SMALLINT, TIMESTAMP, TIME, VARBIT, VARCHAR, CHAR
326  * ...
327  * Thus, the only data types you can use with the assurance that they will
328  * work everywhere are as follows:
329  * DOUBLE PRECISION, FLOAT, INTEGER, NUMERIC, REAL, SMALLINT, VARCHAR, CHAR */
330  switch (sqltype) {
331  case DB_SQL_TYPE_CHARACTER:
332  sprintf(buf, "varchar(%d)", db_get_column_length(column));
333  db_append_string(sql, buf);
334  break;
335  case DB_SQL_TYPE_TEXT:
336  G_warning("Type TEXT converted to 'VARCHAR(250)'");
337  db_append_string(sql, "varchar(250)");
338  break;
339  case DB_SQL_TYPE_SMALLINT:
340  case DB_SQL_TYPE_INTEGER:
341  db_append_string(sql, "integer");
342  break;
343  case DB_SQL_TYPE_REAL:
344  case DB_SQL_TYPE_DOUBLE_PRECISION:
345  case DB_SQL_TYPE_DECIMAL:
346  case DB_SQL_TYPE_NUMERIC:
347  case DB_SQL_TYPE_INTERVAL:
348  db_append_string(sql, "double precision");
349  break;
350  case DB_SQL_TYPE_DATE:
351  db_append_string(sql, "date");
352  break;
353  case DB_SQL_TYPE_TIME:
354  db_append_string(sql, "time");
355  break;
356  case DB_SQL_TYPE_TIMESTAMP:
357  db_append_string(sql, "datetime");
358  break;
359  default:
360  G_warning("Unknown column type (%s)", colname);
361  return DB_FAILED;
362  }
363  }
364  db_append_string(sql, " )");
365  G_debug(3, "sql statement: %s", db_get_string(sql));
366 
367  return DB_OK;
368 }
dbColumn * db_get_table_column(dbTable *table, int n)
returns column structure for given table and column number
int db_table_to_sql(dbTable *table, dbString *sql)
Create SQL CREATE sring from table definition.
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
const char * db_sqltype_name(int sqltype)
returns sqltype description
Definition: sqltype.c:9
const char * db_get_column_name(dbColumn *column)
returns column name for given column
dbTable * db_alloc_table(int ncols)
int db_set_table_name(dbTable *table, const char *name)
int db_get_table_select_priv(dbTable *table)
void db_set_column_select_priv_granted(dbColumn *column)
void db_set_column_select_priv_not_granted(dbColumn *column)
string name
Definition: render.py:1314
void db_set_table_update_priv_not_granted(dbTable *table)
void db_set_table_update_priv_granted(dbTable *table)
int db_get_column_update_priv(dbColumn *column)
void * db_calloc(int n, int m)
void db_init_column(dbColumn *column)
int db_get_column_select_priv(dbColumn *column)
int db_get_column_length(dbColumn *column)
int db_get_table_update_priv(dbTable *table)
int db_append_string(dbString *x, const char *s)
Definition: string.c:193
int db_sqltype_to_Ctype(int sqltype)
Definition: sqlCtype.c:9
void db_set_table_insert_priv_granted(dbTable *table)
int db_get_column_sqltype(dbColumn *column)
returns column sqltype for column (the function db_sqltype_name() returns sqltype description) ...
const char * db_get_table_description(dbTable *table)
int db_get_table_number_of_columns(dbTable *table)
int db_get_table_delete_priv(dbTable *table)
void * db_malloc(int n)
void db_set_table_insert_priv_not_granted(dbTable *table)
int db_get_table_insert_priv(dbTable *table)
void db_init_table(dbTable *table)
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
void db_set_table_select_priv_granted(dbTable *table)
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
void db_free_table(dbTable *table)
void db_set_column_update_priv_granted(dbColumn *column)
char * db_get_string(dbString *x)
Definition: string.c:131
void db_set_table_delete_priv_granted(dbTable *table)
void db_zero(void *s, int n)
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int db_set_string(dbString *x, const char *s)
Definition: string.c:33
int db_set_table_description(dbTable *table, const char *description)
void db_set_column_update_priv_not_granted(dbColumn *column)
void db_set_table_delete_priv_not_granted(dbTable *table)
void db_set_table_select_priv_not_granted(dbTable *table)
const char * db_get_table_name(dbTable *table)
void db_free_string(dbString *x)
Definition: string.c:142
void db_free_column(dbColumn *column)
frees column structure
void db_init_string(dbString *x)
Definition: string.c:11
void db_free(void *s)