GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
dbmi_client/column.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_client/column.c
3 
4  \brief DBMI Library (client) - column info
5 
6  (C) 1999-2008, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public
9  License (>=v2). Read the file COPYING that comes with GRASS
10  for details.
11 
12  \author Joel Jones (CERL/UIUC), Radim Blazek
13  \author Update by Glynn Clement <glynn gclements.plus.com>
14  \author Martin Landa <landa.martin gmail.com>
15  */
16 
17 #include <stdlib.h>
18 #include <string.h>
19 #include <grass/gis.h>
20 #include <grass/dbmi.h>
21 #include <grass/glocale.h>
22 
23 /*!
24  \brief Get column sqltype
25 
26  See db_sqltype_name().
27 
28  Supported types:
29  - DB_SQL_TYPE_UNKNOWN
30  - DB_SQL_TYPE_CHARACTER
31  - DB_SQL_TYPE_SMALLINT
32  - DB_SQL_TYPE_INTEGER
33  - DB_SQL_TYPE_REAL
34  - DB_SQL_TYPE_DOUBLE_PRECISION
35  - DB_SQL_TYPE_DECIMAL
36  - DB_SQL_TYPE_NUMERIC
37  - DB_SQL_TYPE_DATE
38  - DB_SQL_TYPE_TIME
39  - DB_SQL_TYPE_TIMESTAMP
40  - DB_SQL_TYPE_INTERVAL
41  - DB_SQL_TYPE_TEXT
42  - DB_SQL_TYPE_SERIAL
43 
44  \param driver DB driver
45  \param tab table name
46  \param col column name
47 
48  \return column sqltype
49  \return -1 on error
50  */
51 int db_column_sqltype(dbDriver *driver, const char *tab, const char *col)
52 {
53  dbTable *table;
54  dbString table_name;
55  dbColumn *column;
56  int ncol, cl, type;
57 
58  type = -1;
59 
60  db_init_string(&table_name);
61  db_set_string(&table_name, tab);
62 
63  if (db_describe_table(driver, &table_name, &table) != DB_OK)
64  return -1;
65 
66  db_free_string(&table_name);
67  ncol = db_get_table_number_of_columns(table);
68  for (cl = 0; cl < ncol; cl++) {
69  column = db_get_table_column(table, cl);
70  if (strcmp(db_get_column_name(column), col) == 0) {
71  type = db_get_column_sqltype(column);
72  break;
73  }
74  }
75 
76  db_free_table(table);
77 
78  return type;
79 }
80 
81 /*!
82  \brief Get column ctype
83 
84  See db_sqltype_to_Ctype().
85 
86  Supported types:
87  - DB_C_TYPE_STRING
88  - DB_C_TYPE_INT
89  - DB_C_TYPE_DOUBLE
90  - DB_C_TYPE_DATETIME
91 
92  \param driver DB driver
93  \param tab table name
94  \param col column name
95 
96  \return column Ctype
97  \return -1 on error
98  */
99 int db_column_Ctype(dbDriver *driver, const char *tab, const char *col)
100 {
101  int type;
102 
103  if ((type = db_column_sqltype(driver, tab, col)) >= 0) {
104  type = db_sqltype_to_Ctype(type);
105  return type;
106  }
107 
108  return -1;
109 }
110 
111 /*!
112  \brief Get column structure by table and column name.
113 
114  Column is set to new dbColumn structure or NULL if column was not found
115 
116  \param Driver DB driver
117  \param tname table name
118  \param cname column name
119  \param[out] Column column structure to store within
120 
121  \return DB_OK on success
122  \return DB_FAILED on failure
123  */
124 int db_get_column(dbDriver *Driver, const char *tname, const char *cname,
125  dbColumn **Column)
126 {
127  int i, ncols, ret;
128  dbTable *Table;
129  dbColumn *Col;
130  dbString tabname;
131 
132  db_init_string(&tabname);
133  db_set_string(&tabname, tname);
134 
135  if (db_describe_table(Driver, &tabname, &Table) != DB_OK) {
136  G_warning(_("Unable to describe table <%s>"), tname);
137  return DB_FAILED;
138  }
139 
140  *Column = NULL;
141  ret = DB_FAILED;
142 
143  ncols = db_get_table_number_of_columns(Table);
144  G_debug(3, "ncol = %d", ncols);
145 
146  for (i = 0; i < ncols; i++) {
147  Col = db_get_table_column(Table, i);
148  if (G_strcasecmp(db_get_column_name(Col), cname) == 0) {
149  *Column = db_copy_column(NULL, Col);
150  ret = DB_OK;
151  break;
152  }
153  }
154  db_free_table(Table);
155 
156  return ret;
157 }
#define NULL
Definition: ccmath.h:32
#define DB_FAILED
Definition: dbmi.h:72
#define DB_OK
Definition: dbmi.h:71
int db_column_sqltype(dbDriver *driver, const char *tab, const char *col)
Get column sqltype.
int db_column_Ctype(dbDriver *driver, const char *tab, const char *col)
Get column ctype.
int db_get_column(dbDriver *Driver, const char *tname, const char *cname, dbColumn **Column)
Get column structure by table and column name.
int db_describe_table(dbDriver *, dbString *, dbTable **)
Describe table.
Definition: c_desc_table.c:28
dbColumn * db_get_table_column(dbTable *, int)
Returns column structure for given table and column number.
dbColumn * db_copy_column(dbColumn *, dbColumn *)
Copy a db column from source to destination.
int db_sqltype_to_Ctype(int)
Get C data type based on given SQL data type.
Definition: sqlCtype.c:24
int db_get_column_sqltype(dbColumn *)
Returns column sqltype for column.
void db_free_table(dbTable *)
Free the table.
void db_free_string(dbString *)
Free allocated space for dbString.
Definition: string.c:150
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
void db_init_string(dbString *)
Initialize dbString.
Definition: string.c:25
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
int db_get_table_number_of_columns(dbTable *)
Return the number of columns of the table.
void G_warning(const char *,...) __attribute__((format(printf
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition: strings.c:47
int G_debug(int, const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
Definition: driver.h:21