GRASS 8 Programmer's Manual 8.6.0dev(2026)-ddeab64dbf
Loading...
Searching...
No Matches
dbmi_client/table.c
Go to the documentation of this file.
1/*!
2 * \file db/dbmi_client/table.c
3 *
4 * \brief DBMI Library (client) - table management
5 *
6 * (C) 1999-2008 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 */
14
15#include <stdlib.h>
16#include <string.h>
17#include <grass/gis.h>
18#include <grass/dbmi.h>
19#include <grass/glocale.h>
20
21/*!
22 \brief Check if table exists
23
24 \param drvname driver name
25 \param dbname database name
26 \param tabname table name
27
28 \return 1 exist
29 \return 0 doesn't exist
30 \return -1 error
31 */
32int db_table_exists(const char *drvname, const char *dbname,
33 const char *tabname)
34{
36 dbString *names;
37 int i, count, found = 0;
38 int full = 0;
39 char buf[1000];
40 char *bufp, *c;
41
42 if (strchr(tabname, '.'))
43 full = 1;
44
46 if (driver == NULL) {
47 G_warning(_("Unable open database <%s> by driver <%s>"), dbname,
48 drvname);
49 return -1;
50 }
51
52 /* The table tabname can be either fully qualified in form table.schema,
53 * or it can be only table name. If the name is fully qualified, compare
54 * whole name, if it is not, compare only table names */
55
56 /* user tables */
57 if (db_list_tables(driver, &names, &count, 0) != DB_OK)
58 return (-1);
59
60 for (i = 0; i < count; i++) {
61 const char *source = db_get_string(&names[i]);
62 if (G_strlcpy(buf, source, sizeof(buf)) >= sizeof(buf)) {
63 G_fatal_error(_("Table name too long: <%s>"), source);
64 }
65 bufp = buf;
66 if (!full && (c = strchr(buf, '.'))) {
67 bufp = c + 1;
68 }
69 G_debug(2, "table = %s -> %s", buf, bufp);
70 if (G_strcasecmp(tabname, bufp) == 0) {
71 found = 1;
72 break;
73 }
74 }
76
77 if (!found) { /* system tables */
78 if (db_list_tables(driver, &names, &count, 1) != DB_OK)
79 return (-1);
80
81 for (i = 0; i < count; i++) {
82 const char *src = db_get_string(&names[i]);
83 if (G_strlcpy(buf, src, sizeof(buf)) >= sizeof(buf)) {
84 G_fatal_error(_("Table name too long: <%s>"), src);
85 }
86 bufp = buf;
87 if (!full && (c = strchr(buf, '.'))) {
88 bufp = c + 1;
89 }
90 if (G_strcasecmp(tabname, bufp) == 0) {
91 found = 1;
92 break;
93 }
94 }
96 }
98
99 return (found);
100}
101
102/*!
103 \brief Get number of rows of table
104
105 \param driver db driver
106 \param sql SQL statement
107
108 \return number of records
109 \return -1
110 */
112{
113 int nrows;
115
117 G_warning(_("Unable to open select cursor: '%s'"), db_get_string(sql));
119 return -1;
120 }
121
122 nrows = db_get_num_rows(&cursor);
124
125 return nrows;
126}
#define NULL
Definition ccmath.h:32
Main header of GRASS DataBase Management Interface.
#define DB_SEQUENTIAL
Definition dbmi.h:123
#define DB_OK
Definition dbmi.h:71
int db_table_exists(const char *drvname, const char *dbname, const char *tabname)
Check if table exists.
int db_get_table_number_of_rows(dbDriver *driver, dbString *sql)
Get number of rows of table.
int db_get_num_rows(dbCursor *)
Get number of selected rows.
Definition c_rows.c:26
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
Definition db.c:61
void db_free_string_array(dbString *, int)
Free allocated dbString array.
Definition string.c:163
char * db_get_string(const dbString *)
Get string.
Definition string.c:140
int db_list_tables(dbDriver *, dbString **, int *, int)
List available tables for given connection.
Definition c_list_tabs.c:39
int db_close_cursor(dbCursor *)
Close cursor.
Definition c_close_cur.c:27
int db_open_select_cursor(dbDriver *, dbString *, dbCursor *, int)
Open select cursor.
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
Definition db.c:28
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
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
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
Definition strlcpy.c:52
#define _(str)
Definition glocale.h:10
int count