GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
dbmi_base/column.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/column.c
3 
4  \brief DBMI Library (base) - columns 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 
15 #include <stdlib.h>
16 #include <string.h>
17 #include <grass/gis.h>
18 #include <grass/dbmi.h>
19 
20 /*!
21  \brief Returns column value for given column structure
22 
23  \param column pointer to dbColumn
24 
25  \return pointer to dbValue
26 */
28 {
29  return &column->value;
30 }
31 
32 /*!
33  \brief Returns column default value for given column structure
34 
35  \param column pointer to dbColumn
36 
37  \return pointer to dbValue
38 */
40 {
41  return &column->defaultValue;
42 }
43 
44 /*!
45  \brief Define column sqltype for column
46 
47  The function db_sqltype_name() returns sqltype description.
48 
49  \code
50  #define DB_SQL_TYPE_UNKNOWN 0
51 
52  #define DB_SQL_TYPE_CHARACTER 1
53  #define DB_SQL_TYPE_SMALLINT 2
54  #define DB_SQL_TYPE_INTEGER 3
55  #define DB_SQL_TYPE_REAL 4
56  #define DB_SQL_TYPE_DOUBLE_PRECISION 6
57  #define DB_SQL_TYPE_DECIMAL 7
58  #define DB_SQL_TYPE_NUMERIC 8
59  #define DB_SQL_TYPE_DATE 9
60  #define DB_SQL_TYPE_TIME 10
61  #define DB_SQL_TYPE_TIMESTAMP 11
62  #define DB_SQL_TYPE_INTERVAL 12
63  #define DB_SQL_TYPE_TEXT 13
64 
65  #define DB_SQL_TYPE_SERIAL 21
66  \endcode
67 
68  \param column pointer to dbColumn
69  \param sqltype SQL data type (see list)
70 */
71 void db_set_column_sqltype(dbColumn *column, int sqltype)
72 {
73  column->sqlDataType = sqltype;
74 }
75 
76 /*!
77  \brief Set column host data type
78 
79  \param column pointer to dbColumn
80  \param type data type
81 */
82 void db_set_column_host_type(dbColumn * column, int type)
83 {
84  column->hostDataType = type;
85 }
86 
87 /*!
88  \brief Get column scale
89 
90  \param column pointer to dbColumn
91 
92  \return scale
93 */
95 {
96  return column->scale;
97 }
98 
99 /*!
100  \brief Set column scale
101 
102  \param column pointer to dbColumn
103  \param scale column scale value
104 */
105 void db_set_column_scale(dbColumn * column, int scale)
106 {
107  column->scale = scale;
108 }
109 
110 /*!
111  \brief Get column precision
112 
113  \param column pointer to dbColumn
114 
115  \return precision
116 */
118 {
119  return column->precision;
120 }
121 
122 /*!
123  \brief Set column precision
124 
125  \param column pointer to dbColumn
126  \param precision value
127 */
128 void db_set_column_precision(dbColumn * column, int precision)
129 {
130  column->precision = precision;
131 }
132 
133 /*!
134  \brief Returns column sqltype for column
135 
136  The function db_sqltype_name() returns sqltype description.
137 
138  \param column pointer to dbColumn
139 
140  \return sql data type (see include/dbmi.h)
141 */
143 {
144  return column->sqlDataType;
145 }
146 
147 /*!
148  \brief Get column host type
149 
150  \param column pointer to dbColumn
151 
152  \return data type (see include/dbmi.h)
153 */
155 {
156  return column->hostDataType;
157 }
158 
159 /*!
160  \brief Set default value identificator
161 
162  \param column pointer to dbColumn
163 */
165 {
166  column->hasDefaultValue = 1;
167 }
168 
169 /*!
170  \brief Unset default value identificator
171 
172  \todo Replace by db_unset_column_has_default_value() ?
173 
174  \param column pointer to dbColumn
175 */
177 {
178  column->hasDefaultValue = 0;
179 }
180 
181 /*!
182  \brief Unset default value identificator
183 
184  \param column pointer to dbColumn
185 */
187 {
188  column->hasDefaultValue = 0;
189 }
190 
191 /*!
192  \brief Check if column has defined default value
193 
194  \param column pointer to dbColumn
195 
196  \return 1 if true
197  \return 0 if false
198 */
200 {
201  return (column->hasDefaultValue != 0);
202 }
203 
204 /*!
205  \brief Check if column has defined default value
206 
207  \param column pointer to dbColumn
208 
209  \return 1 if true
210  \return 0 if false
211 */
213 {
214  return (column->hasDefaultValue);
215 }
216 
217 /*!
218  \brief Check if column has defined default value
219 
220  \param column pointer to dbColumn
221 
222  \return 1 if false
223  \return 0 if true
224 */
226 {
227  return (!column->hasDefaultValue);
228 }
229 
230 /*!
231  \brief Set default value to be used
232 
233  \param column pointer to dbColumn
234 */
236 {
237  column->useDefaultValue = 1;
238 }
239 
240 /*!
241  \brief Unset default value to be used
242 
243  \param column pointer to dbColumn
244 */
246 {
247  column->useDefaultValue = 0;
248 }
249 
250 /*!
251  \brief Checks if default value is used
252 
253  \param column pointer to dbColumn
254 
255  \return 1 if true
256  \return 0 if false
257 */
259 {
260  return (column->useDefaultValue != 0);
261 }
262 
263 /*!
264  \brief Set null value to be allowed
265 
266  \param column pointer to dbColumn
267 */
269 {
270  column->nullAllowed = 1;
271 }
272 
273 /*!
274  \brief Unset null value to be allowed
275 
276  \param column pointer to dbColumn
277 */
279 {
280  column->nullAllowed = 0;
281 }
282 
283 /*!
284  \brief Checks if null value is allowed
285 
286  \param column pointer to dbColumn
287 
288  \return 1 if true
289  \return 0 if false
290  */
292 {
293  return (column->nullAllowed != 0);
294 }
295 
296 /*!
297  \brief Get column's length
298 
299  \param column pointer to dbColumn
300 
301  \return length
302 */
304 {
305  return column->dataLen;
306 }
307 
308 /*!
309  \brief Set column's length
310 
311  \param column pointer to dbColumn
312  \param length value
313 */
314 void db_set_column_length(dbColumn * column, int length)
315 {
316  column->dataLen = length;
317 }
318 
319 /*!
320  \brief Set select privileges to be granted
321 
322  \param column pointer to dbColumn
323 */
325 {
326  column->select = DB_GRANTED;
327 }
328 
329 /*!
330  \brief Unset select privileges
331 
332  \param column pointer to dbColumn
333 */
335 {
336  column->select = DB_NOT_GRANTED;
337 }
338 
339 /*!
340  \brief Get select privileges
341 
342  \param column pointer to dbColumn
343 
344  \return privileges
345 */
347 {
348  return column->select;
349 }
350 
351 /*!
352  \brief Set update privileges to be granted
353 
354  \param column pointer to dbColumn
355 */
357 {
358  column->update = DB_GRANTED;
359 }
360 
361 /*!
362  \brief Unset update privileges
363 
364  \param column pointer to dbColumn
365 */
367 {
368  column->update = DB_NOT_GRANTED;
369 }
370 
371 /*!
372  \brief Get update privileges
373 
374  \param column pointer to dbColumn
375 
376  \return privileges
377 */
379 {
380  return column->update;
381 }
382 
383 /*!
384  \brief Initialize dbColumn
385 
386  \param column pointer to dbColumn to be initialized
387 */
388 void db_init_column(dbColumn * column)
389 {
390  db_zero((void *)column, sizeof(dbColumn));
391  db_init_string(&column->columnName);
392  db_init_string(&column->description);
393  db_init_string(&column->value.s);
394  db_init_string(&column->defaultValue.s);
395 }
396 
397 /*!
398  \brief Set column name
399 
400  \param column pointer to dbColumn
401  \param name column name
402 
403  \return DB_OK on success
404  \return error code on failure
405 */
406 int db_set_column_name(dbColumn * column, const char *name)
407 {
408  return db_set_string(&column->columnName, name);
409 }
410 
411 /*!
412  \brief Returns column name for given column
413 
414  \param column pointer to dbColumn
415 
416  \return pointer to string with column name
417 */
418 const char *db_get_column_name(dbColumn * column)
419 {
420  return db_get_string(&column->columnName);
421 }
422 
423 /*!
424  \brief Set column description
425 
426  \param column pointer to dbColumn
427  \param description column's description
428 
429  \return DB_OK on success
430  \return error code on failure
431  */
432 int db_set_column_description(dbColumn * column, const char *description)
433 {
434  return db_set_string(&column->description, description);
435 }
436 
437 /*!
438  \brief Returns column description for given column
439 
440  \param column pointer to dbColumn
441 
442  \return pointer to string with column's description
443 */
444 const char *db_get_column_description(dbColumn * column)
445 {
446  return db_get_string(&column->description);
447 }
448 
449 /*!
450  \brief Frees column structure
451 
452  \param column pointer to dbColumn
453 */
454 void db_free_column(dbColumn * column)
455 {
456  db_free_string(&column->columnName);
457  db_free_string(&column->value.s);
458  /* match init? */
459  db_free_string(&column->description);
460  db_free_string(&column->defaultValue.s);
461 }
462 
463 
464 /*!
465  \brief Copy a db column from source to destination
466 
467  \param src The column to copy from
468  \param dest An allocated column to copy to which will be
469  initialized. In case dest is NULL a new column will be allocated
470  and returned
471  \return The pointer of copied/allocated column
472 */
474 {
475  dbColumn *new = dest;
476 
477  if(new == NULL)
478  new = (dbColumn *) db_calloc(sizeof(dbColumn), 1);
479  else
480  db_init_column(new);
481 
482  db_copy_string(&new->columnName, &src->columnName);
483  db_copy_string(&new->description, &src->description);
484  db_copy_value(&new->defaultValue, &src->defaultValue);
485  db_copy_value(&new->value, &src->value);
486  new->dataLen = src->dataLen;
487  new->hasDefaultValue = src->hasDefaultValue;
488  new->hostDataType = src->hostDataType;
489  new->nullAllowed = src->nullAllowed;
490  new->precision = src->precision;
491  new->scale = src->scale;
492  new->select = src->select;
493  new->sqlDataType = src->sqlDataType;
494  new->update = src->update;
495  new->useDefaultValue = src->useDefaultValue;
496 
497  return new;
498 }
void db_set_column_sqltype(dbColumn *column, int sqltype)
Define column sqltype for column.
int hostDataType
Definition: dbmi.h:207
const char * db_get_column_description(dbColumn *column)
Returns column description for given column.
void db_set_column_precision(dbColumn *column, int precision)
Set column precision.
const char * db_get_column_name(dbColumn *column)
Returns column name for given column.
void db_copy_value(dbValue *, dbValue *)
Copy value.
Definition: value.c:340
void db_init_string(dbString *)
Initialize dbString.
Definition: string.c:25
dbValue value
Definition: dbmi.h:208
void db_set_column_select_priv_granted(dbColumn *column)
Set select privileges to be granted.
void db_set_column_select_priv_not_granted(dbColumn *column)
Unset select privileges.
#define DB_GRANTED
Definition: dbmi.h:128
void db_set_column_has_defined_default_value(dbColumn *column)
Set default value identificator.
dbString s
Definition: dbmi.h:198
void db_zero(void *, int)
Zero allocated space.
int db_get_column_update_priv(dbColumn *column)
Get update privileges.
dbString columnName
Definition: dbmi.h:204
int db_get_column_precision(dbColumn *column)
Get column precision.
void db_init_column(dbColumn *column)
Initialize dbColumn.
char * db_get_string(const dbString *)
Get string.
Definition: string.c:140
dbColumn * db_copy_column(dbColumn *dest, dbColumn *src)
Copy a db column from source to destination.
int db_copy_string(dbString *, const dbString *)
Copy dbString.
Definition: string.c:230
int db_get_column_select_priv(dbColumn *column)
Get select privileges.
int db_get_column_length(dbColumn *column)
Get column&#39;s length.
dbValue * db_get_column_default_value(dbColumn *column)
Returns column default value for given column structure.
char hasDefaultValue
Definition: dbmi.h:213
#define NULL
Definition: ccmath.h:32
void db_unset_column_use_default_value(dbColumn *column)
Unset default value to be used.
int scale
Definition: dbmi.h:211
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
int db_get_column_scale(dbColumn *column)
Get column scale.
int db_set_column_name(dbColumn *column, const char *name)
Set column name.
int db_set_column_description(dbColumn *column, const char *description)
Set column description.
void db_unset_column_null_allowed(dbColumn *column)
Unset null value to be allowed.
int db_test_column_use_default_value(dbColumn *column)
Checks if default value is used.
void db_set_column_use_default_value(dbColumn *column)
Set default value to be used.
int select
Definition: dbmi.h:216
int db_get_column_sqltype(dbColumn *column)
Returns column sqltype for column.
void db_set_column_length(dbColumn *column, int length)
Set column&#39;s length.
dbValue * db_get_column_value(dbColumn *column)
Returns column value for given column structure.
int db_test_column_has_default_value(dbColumn *column)
Check if column has defined default value.
void * db_calloc(int, int)
Allocate memory.
void db_set_column_host_type(dbColumn *column, int type)
Set column host data type.
int db_test_column_has_defined_default_value(dbColumn *column)
Check if column has defined default value.
dbString description
Definition: dbmi.h:205
void db_set_column_scale(dbColumn *column, int scale)
Set column scale.
int dataLen
Definition: dbmi.h:209
void db_set_column_update_priv_granted(dbColumn *column)
Set update privileges to be granted.
int db_test_column_has_undefined_default_value(dbColumn *column)
Check if column has defined default value.
char * dest
Definition: lz4.h:573
dbValue defaultValue
Definition: dbmi.h:215
#define DB_NOT_GRANTED
Definition: dbmi.h:129
void db_unset_column_has_default_value(dbColumn *column)
Unset default value identificator.
void db_set_column_update_priv_not_granted(dbColumn *column)
Unset update privileges.
char useDefaultValue
Definition: dbmi.h:214
int precision
Definition: dbmi.h:210
int db_test_column_null_allowed(dbColumn *column)
Checks if null value is allowed.
const char * name
Definition: named_colr.c:7
void db_set_column_null_allowed(dbColumn *column)
Set null value to be allowed.
void db_set_column_has_undefined_default_value(dbColumn *column)
Unset default value identificator.
int db_get_column_host_type(dbColumn *column)
Get column host type.
int update
Definition: dbmi.h:217
void db_free_column(dbColumn *column)
Frees column structure.
void db_free_string(dbString *)
Free allocated space for dbString.
Definition: string.c:150
int sqlDataType
Definition: dbmi.h:206
char nullAllowed
Definition: dbmi.h:212