GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
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 * SPDX-FileCopyrightText: 1999-2008 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Joel Jones (CERL/UIUC), Radim Blazek
10 */
11
12#include <stdlib.h>
13#include <string.h>
14#include <grass/gis.h>
15#include <grass/dbmi.h>
16#include <grass/glocale.h>
17
18/*!
19 \brief Check if table exists
20
21 \param drvname driver name
22 \param dbname database name
23 \param tabname table name
24
25 \return 1 exist
26 \return 0 doesn't exist
27 \return -1 error
28 */
29int db_table_exists(const char *drvname, const char *dbname,
30 const char *tabname)
31{
33 dbString *names;
34 int i, count, found = 0;
35 int full = 0;
36 char buf[1000];
37 char *bufp, *c;
38
39 if (strchr(tabname, '.'))
40 full = 1;
41
43 if (driver == NULL) {
44 G_warning(_("Unable open database <%s> by driver <%s>"), dbname,
45 drvname);
46 return -1;
47 }
48
49 /* The table tabname can be either fully qualified in form table.schema,
50 * or it can be only table name. If the name is fully qualified, compare
51 * whole name, if it is not, compare only table names */
52
53 /* user tables */
54 if (db_list_tables(driver, &names, &count, 0) != DB_OK)
55 return (-1);
56
57 for (i = 0; i < count; i++) {
58 const char *source = db_get_string(&names[i]);
59 if (G_strlcpy(buf, source, sizeof(buf)) >= sizeof(buf)) {
60 G_fatal_error(_("Table name too long: <%s>"), source);
61 }
62 bufp = buf;
63 if (!full && (c = strchr(buf, '.'))) {
64 bufp = c + 1;
65 }
66 G_debug(2, "table = %s -> %s", buf, bufp);
67 if (G_strcasecmp(tabname, bufp) == 0) {
68 found = 1;
69 break;
70 }
71 }
73
74 if (!found) { /* system tables */
75 if (db_list_tables(driver, &names, &count, 1) != DB_OK)
76 return (-1);
77
78 for (i = 0; i < count; i++) {
79 const char *src = db_get_string(&names[i]);
80 if (G_strlcpy(buf, src, sizeof(buf)) >= sizeof(buf)) {
81 G_fatal_error(_("Table name too long: <%s>"), src);
82 }
83 bufp = buf;
84 if (!full && (c = strchr(buf, '.'))) {
85 bufp = c + 1;
86 }
87 if (G_strcasecmp(tabname, bufp) == 0) {
88 found = 1;
89 break;
90 }
91 }
93 }
95
96 return (found);
97}
98
99/*!
100 \brief Get number of rows of table
101
102 \param driver db driver
103 \param sql SQL statement
104
105 \return number of records
106 \return -1
107 */
109{
110 int nrows;
112
114 G_warning(_("Unable to open select cursor: '%s'"), db_get_string(sql));
116 return -1;
117 }
118
119 nrows = db_get_num_rows(&cursor);
121
122 return nrows;
123}
#define NULL
Definition ccmath.h:32
Main header of GRASS DataBase Management Interface.
#define DB_SEQUENTIAL
Definition dbmi.h:121
#define DB_OK
Definition dbmi.h:69
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:23
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
Definition db.c:58
void db_free_string_array(dbString *, int)
Free allocated dbString array.
Definition string.c:161
char * db_get_string(const dbString *)
Get string.
Definition string.c:138
int db_list_tables(dbDriver *, dbString **, int *, int)
List available tables for given connection.
Definition c_list_tabs.c:36
int db_close_cursor(dbCursor *)
Close cursor.
Definition c_close_cur.c:24
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:25
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:45
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:54
#define _(str)
Definition glocale.h:10
int count