Exemple #1
0
/* note: dbString *a; ...(...,&a...) */
int db__recv_string_array(dbString ** a, int *n)
{
    int i, count;
    int stat;
    dbString *b;

    *n = 0;
    *a = NULL;
    stat = db__recv_int(&count);
    if (stat != DB_OK)
	return stat;
    if (count < 0) {
	db_protocol_error();
	return DB_PROTOCOL_ERR;
    }

    b = db_alloc_string_array(count);
    if (b == NULL)
	return DB_MEMORY_ERR;

    for (i = 0; i < count; i++) {
	stat = db__recv_string(&b[i]);
	if (stat != DB_OK) {
	    db_free_string_array(b, count);
	    return stat;
	}
    }
    *n = count;
    *a = b;

    return DB_OK;
}
Exemple #2
0
/*!
  \brief Allocate index columns

  \param index pointer to dbIndex
  \param ncols number of columns to be allocated

  \return DB_OK
*/
int db_alloc_index_columns(dbIndex * index, int ncols)
{
    index->columnNames = db_alloc_string_array(ncols);
    if (index->columnNames == NULL)
	return db_get_error_code();
    index->numColumns = ncols;

    return DB_OK;
}
Exemple #3
0
int db__driver_list_tables(dbString ** tlist, int *tcount, int system)
{
    int i, nrows;
    dbString *list;
    sqlite3_stmt *statement;
    const char *rest;
    int ret;

    G_debug(3, "db__driver_list_tables(): system = %d", system);
    ret = sqlite3_prepare(sqlite,
			  "select name from sqlite_master where type = 'table' or type = 'view'",
			  -1, &statement, &rest);

    if (ret != SQLITE_OK) {
	db_d_append_error("%s\n%s",
			  _("Unable to list tables:"),
			  (char *)sqlite3_errmsg(sqlite));
	db_d_report_error();
	sqlite3_finalize(statement);
	return DB_FAILED;
    }

    nrows = 0;
    while (sqlite3_step(statement) == SQLITE_ROW) {
	nrows++;
    }
    /* get real result code */
    ret = sqlite3_reset(statement);
    
    if (ret != SQLITE_OK) {
	db_d_append_error("%s\n%s",
			  _("Unable to list tables:"),
			  (char *)sqlite3_errmsg(sqlite));
	db_d_report_error();
	sqlite3_finalize(statement);
	return DB_FAILED;
    }

    G_debug(3, "nrows = %d", nrows);

    list = db_alloc_string_array(nrows);

    if (list == NULL) {
	db_d_append_error(_("Unable to db_alloc_string_array()"));
	db_d_report_error();
	sqlite3_finalize(statement);
	return DB_FAILED;
    }

    i = 0;
    while (sqlite3_step(statement) == SQLITE_ROW) {
	G_debug(3, "table: %s", sqlite3_column_text(statement, 0));
	db_set_string(&list[i], (char *)sqlite3_column_text(statement, 0));
	i++;
    }

    sqlite3_finalize(statement);

    *tlist = list;
    *tcount = nrows;

    return DB_OK;
}
Exemple #4
0
int db__driver_list_tables(dbString ** tlist, int *tcount, int system)
{
    int i, j, nrows, trows, vrows, ncols, tablecol, tschemacol, viewcol,
	vschemacol;
    dbString *list;
    PGresult *rest, *resv;
    char buf[DB_SQL_MAX];

    *tlist = NULL;
    *tcount = 0;


    /* Get table names */
    sprintf(buf, "SELECT * FROM pg_tables WHERE schemaname %s "
            " ('pg_catalog', 'information_schema') ORDER BY tablename", system ? "IN" : "NOT IN");
    G_debug(2, "SQL: %s", buf);
    
    rest = PQexec(pg_conn, buf);
    if (!rest || PQresultStatus(rest) != PGRES_TUPLES_OK) {
	db_d_append_error("%s\n%s",
			  _("Unable to select table names."),
			  PQerrorMessage(pg_conn));
	db_d_report_error();
	PQclear(rest);
	return DB_FAILED;
    }

    /* Find table and schema col */
    ncols = PQnfields(rest);
    tschemacol = -1;
    for (i = 0; i < ncols; i++) {
	if (strcmp(PQfname(rest, i), "tablename") == 0)
	    tablecol = i;

	if (strcmp(PQfname(rest, i), "schemaname") == 0)
	    tschemacol = i;
    }


    /* Get view names */
    sprintf(buf, "SELECT * FROM pg_views WHERE schemaname %s "
            " ('pg_catalog', 'information_schema') ORDER BY viewname", system ? "IN" : "NOT IN");
    G_debug(2, "SQL: %s", buf);
    
    resv = PQexec(pg_conn, buf);
    if (!resv || PQresultStatus(resv) != PGRES_TUPLES_OK) {
	db_d_append_error("%s\n%s",
			  _("Unable to select view names."),
			  PQerrorMessage(pg_conn));
	db_d_report_error();
	PQclear(resv);
	return DB_FAILED;
    }

    /* Find viewname and schema col */
    ncols = PQnfields(resv);
    vschemacol = -1;
    for (i = 0; i < ncols; i++) {
	if (strcmp(PQfname(resv, i), "viewname") == 0)
	    viewcol = i;

	if (strcmp(PQfname(resv, i), "schemaname") == 0)
	    vschemacol = i;
    }



    trows = PQntuples(rest);
    vrows = PQntuples(resv);
    nrows = trows + vrows;

    list = db_alloc_string_array(nrows);

    if (list == NULL) {
	db_d_append_error(_("Out of memory"));
	db_d_report_error();
	return DB_FAILED;
    }

    for (i = 0; i < trows; i++) {
	if (tschemacol >= 0) {
	    sprintf(buf, "%s.%s", (char *)PQgetvalue(rest, i, tschemacol),
		    (char *)PQgetvalue(rest, i, tablecol));
	}
	else {
	    sprintf(buf, "%s", (char *)PQgetvalue(rest, i, tablecol));
	}
	db_set_string(&list[i], buf);
    }

    PQclear(rest);


    for (j = 0; j < vrows; j++) {
	if (vschemacol >= 0) {
	    sprintf(buf, "%s.%s", (char *)PQgetvalue(resv, j, vschemacol),
		    (char *)PQgetvalue(resv, j, viewcol));
	}
	else {
	    sprintf(buf, "%s", (char *)PQgetvalue(resv, j, viewcol));
	}
	db_set_string(&list[i], buf);
	i++;
    }

    PQclear(resv);


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