File: listtab.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (79 lines) | stat: -rw-r--r-- 1,989 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <grass/dbmi.h>
#include "odbc.h"
#include "globals.h"
#include "proto.h"

int db__driver_list_tables(tlist, tcount, system)
dbString **tlist;
int *tcount;
int system;
{
    cursor *c;
    dbString *list;
    int count = 0;
    SQLCHAR tableName[SQL_MAX_TABLE_NAME_LEN];
    SQLLEN indi, nrow = 0;
    SQLRETURN ret;
    char ttype[50];

    *tlist = NULL;
    *tcount = 0;

    /* allocate cursor */
    c = alloc_cursor();
    if (c == NULL)
        return DB_FAILED;

    /* Execute SQL */
    if (system)
        sprintf(ttype, "SYSTEM TABLE");
    else
        sprintf(ttype, "TABLE, VIEW");

    ret = SQLTables(c->stmt, NULL, 0, NULL, 0, NULL, 0, (SQLCHAR *)ttype,
                    sizeof(ttype));

    if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
        db_d_append_error("SQLTables()");
        db_d_report_error();
        return DB_FAILED;
    }

    SQLBindCol(c->stmt, 3, SQL_C_CHAR, tableName, sizeof(tableName), &indi);

    /* Get number of rows */
    /* WARNING: after SQLTables(), SQLRowCount() doesn't sets number of rows
     * to number of tables! ODBC developers said, this is correct. */
    nrow = 0;
    ret = SQLFetch(c->stmt);
    while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
        nrow++;
        ret = SQLFetch(c->stmt);
    }

    list = db_alloc_string_array(nrow);
    if (list == NULL)
        return DB_FAILED;

    /* Get table names */
    /* ret = SQLFetch( c->stmt ); */
    ret = SQLFetchScroll(c->stmt, SQL_FETCH_FIRST, 0);
    while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
        if (indi == SQL_NULL_DATA) {
            if (db_set_string(&list[count], "Unknown") != DB_OK)
                return DB_FAILED;
        }
        else {
            if (db_set_string(&list[count], (char *)tableName) != DB_OK)
                return DB_FAILED;
        }
        count++;
        ret = SQLFetch(c->stmt);
    }

    free_cursor(c);

    *tlist = list;
    *tcount = count;
    return DB_OK;
}