GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 1999-2008, 2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Joel Jones (CERL/UIUC), Radim Blazek
10 \author Update by Glynn Clement <glynn gclements.plus.com>
11 \author Martin Landa <landa.martin gmail.com>
12 */
13
14#include <stdlib.h>
15#include <string.h>
16#include <grass/gis.h>
17#include <grass/dbmi.h>
18#include <grass/glocale.h>
19
20/*!
21 \brief Get column sqltype
22
23 See db_sqltype_name().
24
25 Supported types:
26 - DB_SQL_TYPE_UNKNOWN
27 - DB_SQL_TYPE_CHARACTER
28 - DB_SQL_TYPE_SMALLINT
29 - DB_SQL_TYPE_INTEGER
30 - DB_SQL_TYPE_REAL
31 - DB_SQL_TYPE_DOUBLE_PRECISION
32 - DB_SQL_TYPE_DECIMAL
33 - DB_SQL_TYPE_NUMERIC
34 - DB_SQL_TYPE_DATE
35 - DB_SQL_TYPE_TIME
36 - DB_SQL_TYPE_TIMESTAMP
37 - DB_SQL_TYPE_INTERVAL
38 - DB_SQL_TYPE_TEXT
39 - DB_SQL_TYPE_SERIAL
40
41 \param driver DB driver
42 \param tab table name
43 \param col column name
44
45 \return column sqltype
46 \return -1 on error
47 */
48int db_column_sqltype(dbDriver *driver, const char *tab, const char *col)
49{
50 dbTable *table;
51 dbString table_name;
53 int ncol, cl, type;
54
55 type = -1;
56
57 db_init_string(&table_name);
58 db_set_string(&table_name, tab);
59
60 if (db_describe_table(driver, &table_name, &table) != DB_OK)
61 return -1;
62
63 db_free_string(&table_name);
65 for (cl = 0; cl < ncol; cl++) {
67 if (strcmp(db_get_column_name(column), col) == 0) {
69 break;
70 }
71 }
72
73 db_free_table(table);
74
75 return type;
76}
77
78/*!
79 \brief Get column ctype
80
81 See db_sqltype_to_Ctype().
82
83 Supported types:
84 - DB_C_TYPE_STRING
85 - DB_C_TYPE_INT
86 - DB_C_TYPE_DOUBLE
87 - DB_C_TYPE_DATETIME
88
89 \param driver DB driver
90 \param tab table name
91 \param col column name
92
93 \return column Ctype
94 \return -1 on error
95 */
96int db_column_Ctype(dbDriver *driver, const char *tab, const char *col)
97{
98 int type;
99
100 if ((type = db_column_sqltype(driver, tab, col)) >= 0) {
101 type = db_sqltype_to_Ctype(type);
102 return type;
103 }
104
105 return -1;
106}
107
108/*!
109 \brief Get column structure by table and column name.
110
111 Column is set to new dbColumn structure or NULL if column was not found
112
113 \param Driver DB driver
114 \param tname table name
115 \param cname column name
116 \param[out] Column column structure to store within
117
118 \return DB_OK on success
119 \return DB_FAILED on failure
120 */
121int db_get_column(dbDriver *Driver, const char *tname, const char *cname,
123{
124 int i, ncols, ret;
125 dbTable *Table;
126 dbColumn *Col;
128
131
133 G_warning(_("Unable to describe table <%s>"), tname);
134 return DB_FAILED;
135 }
136
137 *Column = NULL;
138 ret = DB_FAILED;
139
141 G_debug(3, "ncol = %d", ncols);
142
143 for (i = 0; i < ncols; i++) {
144 Col = db_get_table_column(Table, i);
145 if (G_strcasecmp(db_get_column_name(Col), cname) == 0) {
146 *Column = db_copy_column(NULL, Col);
147 ret = DB_OK;
148 break;
149 }
150 }
152
153 return ret;
154}
#define NULL
Definition ccmath.h:32
Main header of GRASS DataBase Management Interface.
#define DB_FAILED
Definition dbmi.h:70
#define DB_OK
Definition dbmi.h:69
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.
dbColumn * db_copy_column(dbColumn *, dbColumn *)
Copy a db column from source to destination.
dbColumn * db_get_table_column(dbTable *, int)
Returns column structure for given table and column number.
int db_sqltype_to_Ctype(int)
Get C data type based on given SQL data type.
Definition sqlCtype.c:22
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:148
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition string.c:39
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
void db_init_string(dbString *)
Initialize dbString.
Definition string.c:23
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:45
int G_debug(int, const char *,...) __attribute__((format(printf
#define _(str)
Definition glocale.h:10