[GRASS-SVN] r35481 - in grass/trunk/db: db.columns db.connect
db.copy db.createdb db.databases db.describe db.drivers
db.dropdb db.droptable db.execute db.select db.tables
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Jan 18 05:26:26 EST 2009
Author: martinl
Date: 2009-01-18 05:26:26 -0500 (Sun, 18 Jan 2009)
New Revision: 35481
Added:
grass/trunk/db/db.columns/main.c
grass/trunk/db/db.connect/main.c
grass/trunk/db/db.copy/main.c
grass/trunk/db/db.createdb/main.c
grass/trunk/db/db.databases/main.c
grass/trunk/db/db.describe/main.c
grass/trunk/db/db.drivers/main.c
grass/trunk/db/db.dropdb/main.c
grass/trunk/db/db.droptable/main.c
grass/trunk/db/db.execute/main.c
grass/trunk/db/db.select/main.c
grass/trunk/db/db.tables/main.c
Removed:
grass/trunk/db/db.columns/columns.c
grass/trunk/db/db.connect/connect.c
grass/trunk/db/db.copy/copy.c
grass/trunk/db/db.createdb/createdb.c
grass/trunk/db/db.databases/databases.c
grass/trunk/db/db.describe/describe.c
grass/trunk/db/db.drivers/drivers.c
grass/trunk/db/db.dropdb/dropdb.c
grass/trunk/db/db.droptable/droptable.c
grass/trunk/db/db.execute/execute.c
grass/trunk/db/db.select/select.c
grass/trunk/db/db.tables/tables.c
Log:
db modules: rename source files to 'main.c'
Deleted: grass/trunk/db/db.columns/columns.c
===================================================================
--- grass/trunk/db/db.columns/columns.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.columns/columns.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,101 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.columns
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
- * PURPOSE: list the column names for a table
- * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <grass/gis.h>
-#include <grass/dbmi.h>
-#include <grass/codes.h>
-#include <stdlib.h>
-#include <grass/glocale.h>
-
-
-struct
-{
- char *driver, *database, *table;
-} parms;
-
-
-/* function prototypes */
-static void parse_command_line(int, char **);
-
-
-int main(int argc, char **argv)
-{
- dbDriver *driver;
- dbHandle handle;
- dbTable *table;
- dbString table_name;
- int col, ncols;
-
- parse_command_line(argc, argv);
-
- driver = db_start_driver(parms.driver);
- if (driver == NULL)
- G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
-
- db_init_handle(&handle);
- db_set_handle(&handle, parms.database, NULL);
- if (db_open_database(driver, &handle) != DB_OK)
- exit(EXIT_FAILURE);
-
- db_init_string(&table_name);
- db_set_string(&table_name, parms.table);
- if (db_describe_table(driver, &table_name, &table) != DB_OK)
- exit(EXIT_FAILURE);
-
- db_close_database(driver);
- db_shutdown_driver(driver);
-
- ncols = db_get_table_number_of_columns(table);
- for (col = 0; col < ncols; col++)
- fprintf(stdout, "%s\n",
- db_get_column_name(db_get_table_column(table, col)));
-
- exit(EXIT_SUCCESS);
-}
-
-
-static void parse_command_line(int argc, char **argv)
-{
- struct Option *driver, *database, *table;
- struct GModule *module;
- const char *drv, *db;
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- table = G_define_standard_option(G_OPT_DB_TABLE);
- table->required = YES;
-
- driver = G_define_standard_option(G_OPT_DB_DRIVER);
- driver->options = db_list_drivers();
- if ((drv = db_get_default_driver_name()))
- driver->answer = (char *) drv;
-
- database = G_define_standard_option(G_OPT_DB_DATABASE);
- if ((db = db_get_default_database_name()))
- database->answer = (char *) db;
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, attribute table");
- module->description = _("List all columns for a given table.");
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
- parms.driver = driver->answer;
- parms.database = database->answer;
- parms.table = table->answer;
-}
Copied: grass/trunk/db/db.columns/main.c (from rev 35438, grass/trunk/db/db.columns/columns.c)
===================================================================
--- grass/trunk/db/db.columns/main.c (rev 0)
+++ grass/trunk/db/db.columns/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,101 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.columns
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
+ * PURPOSE: list the column names for a table
+ * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/codes.h>
+#include <stdlib.h>
+#include <grass/glocale.h>
+
+
+struct
+{
+ char *driver, *database, *table;
+} parms;
+
+
+/* function prototypes */
+static void parse_command_line(int, char **);
+
+
+int main(int argc, char **argv)
+{
+ dbDriver *driver;
+ dbHandle handle;
+ dbTable *table;
+ dbString table_name;
+ int col, ncols;
+
+ parse_command_line(argc, argv);
+
+ driver = db_start_driver(parms.driver);
+ if (driver == NULL)
+ G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+
+ db_init_handle(&handle);
+ db_set_handle(&handle, parms.database, NULL);
+ if (db_open_database(driver, &handle) != DB_OK)
+ exit(EXIT_FAILURE);
+
+ db_init_string(&table_name);
+ db_set_string(&table_name, parms.table);
+ if (db_describe_table(driver, &table_name, &table) != DB_OK)
+ exit(EXIT_FAILURE);
+
+ db_close_database(driver);
+ db_shutdown_driver(driver);
+
+ ncols = db_get_table_number_of_columns(table);
+ for (col = 0; col < ncols; col++)
+ fprintf(stdout, "%s\n",
+ db_get_column_name(db_get_table_column(table, col)));
+
+ exit(EXIT_SUCCESS);
+}
+
+
+static void parse_command_line(int argc, char **argv)
+{
+ struct Option *driver, *database, *table;
+ struct GModule *module;
+ const char *drv, *db;
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ table = G_define_standard_option(G_OPT_DB_TABLE);
+ table->required = YES;
+
+ driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ driver->options = db_list_drivers();
+ if ((drv = db_get_default_driver_name()))
+ driver->answer = (char *) drv;
+
+ database = G_define_standard_option(G_OPT_DB_DATABASE);
+ if ((db = db_get_default_database_name()))
+ database->answer = (char *) db;
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, attribute table");
+ module->description = _("List all columns for a given table.");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ parms.driver = driver->answer;
+ parms.database = database->answer;
+ parms.table = table->answer;
+}
Deleted: grass/trunk/db/db.connect/connect.c
===================================================================
--- grass/trunk/db/db.connect/connect.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.connect/connect.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,174 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.connect
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Alex Shevlakov <sixote yahoo.com>,
- * Glynn Clements <glynn gclements.plus.com>,
- * Markus Neteler <neteler itc.it>,
- * Hamish Bowman <hamish_b yahoo com>
- * PURPOSE: set parameters for connection to database
- * COPYRIGHT: (C) 2002-2008 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/dbmi.h>
-#include <grass/codes.h>
-#include <grass/glocale.h>
-
-/* database for DBF can be written with variables:
- * database=$GISDBASE/$LOCATION_NAME/$MAPSET/dbf
- */
-
-int main(int argc, char *argv[])
-{
- dbConnection conn;
- struct Flag *print, *check_set_default;
-
- /* struct Option *driver, *database, *user, *password, *keycol; */
- struct Option *driver, *database, *schema, *group;
- struct GModule *module;
-
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, attribute table");
- module->description =
- _("Prints/sets general DB connection for current mapset and exits.");
-
-
- print = G_define_flag();
- print->key = 'p';
- print->description = _("Print current connection parameters and exit");
-
- check_set_default = G_define_flag();
- check_set_default->key = 'c';
- check_set_default->description =
- _("Check connection parameters, set if uninitialized, and exit");
-
- driver = G_define_standard_option(G_OPT_DB_DRIVER);
- driver->options = db_list_drivers();
- driver->answer = (char *) db_get_default_driver_name();
-
- database = G_define_standard_option(G_OPT_DB_DATABASE);
- database->answer = (char *) db_get_default_database_name();
-
- schema = G_define_option();
- schema->key = "schema";
- schema->type = TYPE_STRING;
- schema->required = NO;
- schema->multiple = NO;
- schema->answer = (char *) db_get_default_schema_name();
- schema->label = _("Database schema");
- schema->description = _("Do not use this option if schemas "
- "are not supported by driver/database server");
-
- group = G_define_option();
- group->key = "group";
- group->type = TYPE_STRING;
- group->required = NO;
- group->multiple = NO;
- group->answer = (char*) db_get_default_group_name();
- group->description = _("Default group of database users to which "
- "select privilege is granted");
-
- /* commented due to new mechanism:
- user = G_define_option() ;
- user->key = "user" ;
- user->type = TYPE_STRING ;
- user->required = NO ;
- user->multiple = NO ;
- user->description= "User:" ;
-
- password = G_define_option() ;
- password->key = "password" ;
- password->type = TYPE_STRING ;
- password->required = NO ;
- password->multiple = NO ;
- password->description= "Password:" ;
- */
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
-
- if (print->answer) {
- /* get and print connection */
- if (db_get_connection(&conn) == DB_OK) {
- fprintf(stdout, "driver:%s\n",
- conn.driverName ? conn.driverName : "");
- fprintf(stdout, "database:%s\n",
- conn.databaseName ? conn.databaseName : "");
- fprintf(stdout, "schema:%s\n",
- conn.schemaName ? conn.schemaName : "");
- fprintf(stdout, "group:%s\n", conn.group ? conn.group : "");
- }
- else
- G_fatal_error(_("Database connection not defined. "
- "Run db.connect."));
-
- exit(EXIT_SUCCESS);
- }
-
-
- if (check_set_default->answer) {
- /* check connection and set to system-wide default in required */
- /*
- * TODO: improve db_{get,set}_connection() to not return DB_OK on error
- * (thus currently there is no point in checking for that here)
- */
- db_get_connection(&conn);
-
- if (!conn.driverName && !conn.databaseName) {
-
- db_set_default_connection();
- db_get_connection(&conn);
-
- G_message(_("Default driver / database set to:\n"
- "driver: %s\ndatabase: %s"), conn.driverName,
- conn.databaseName);
- }
- /* they must be a matched pair, so if one is set but not the other
- then give up and let the user figure it out */
- else if (!conn.driverName) {
- G_fatal_error(_("Default driver is not set"));
- }
- else if (!conn.databaseName) {
- G_fatal_error(_("Default database is not set"));
- }
-
- /* connection either already existed or now exists */
- exit(EXIT_SUCCESS);
- }
-
-
- /* set connection */
- db_get_connection(&conn); /* read current */
-
- if (driver->answer)
- conn.driverName = driver->answer;
-
- if (database->answer)
- conn.databaseName = database->answer;
-
- if (schema->answer)
- conn.schemaName = schema->answer;
-
- if (group->answer)
- conn.group = group->answer;
-
- db_set_connection(&conn);
-
-
- exit(EXIT_SUCCESS);
-}
Copied: grass/trunk/db/db.connect/main.c (from rev 35438, grass/trunk/db/db.connect/connect.c)
===================================================================
--- grass/trunk/db/db.connect/main.c (rev 0)
+++ grass/trunk/db/db.connect/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,174 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.connect
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Alex Shevlakov <sixote yahoo.com>,
+ * Glynn Clements <glynn gclements.plus.com>,
+ * Markus Neteler <neteler itc.it>,
+ * Hamish Bowman <hamish_b yahoo com>
+ * PURPOSE: set parameters for connection to database
+ * COPYRIGHT: (C) 2002-2008 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/codes.h>
+#include <grass/glocale.h>
+
+/* database for DBF can be written with variables:
+ * database=$GISDBASE/$LOCATION_NAME/$MAPSET/dbf
+ */
+
+int main(int argc, char *argv[])
+{
+ dbConnection conn;
+ struct Flag *print, *check_set_default;
+
+ /* struct Option *driver, *database, *user, *password, *keycol; */
+ struct Option *driver, *database, *schema, *group;
+ struct GModule *module;
+
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, attribute table");
+ module->description =
+ _("Prints/sets general DB connection for current mapset and exits.");
+
+
+ print = G_define_flag();
+ print->key = 'p';
+ print->description = _("Print current connection parameters and exit");
+
+ check_set_default = G_define_flag();
+ check_set_default->key = 'c';
+ check_set_default->description =
+ _("Check connection parameters, set if uninitialized, and exit");
+
+ driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ driver->options = db_list_drivers();
+ driver->answer = (char *) db_get_default_driver_name();
+
+ database = G_define_standard_option(G_OPT_DB_DATABASE);
+ database->answer = (char *) db_get_default_database_name();
+
+ schema = G_define_option();
+ schema->key = "schema";
+ schema->type = TYPE_STRING;
+ schema->required = NO;
+ schema->multiple = NO;
+ schema->answer = (char *) db_get_default_schema_name();
+ schema->label = _("Database schema");
+ schema->description = _("Do not use this option if schemas "
+ "are not supported by driver/database server");
+
+ group = G_define_option();
+ group->key = "group";
+ group->type = TYPE_STRING;
+ group->required = NO;
+ group->multiple = NO;
+ group->answer = (char*) db_get_default_group_name();
+ group->description = _("Default group of database users to which "
+ "select privilege is granted");
+
+ /* commented due to new mechanism:
+ user = G_define_option() ;
+ user->key = "user" ;
+ user->type = TYPE_STRING ;
+ user->required = NO ;
+ user->multiple = NO ;
+ user->description= "User:" ;
+
+ password = G_define_option() ;
+ password->key = "password" ;
+ password->type = TYPE_STRING ;
+ password->required = NO ;
+ password->multiple = NO ;
+ password->description= "Password:" ;
+ */
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+
+ if (print->answer) {
+ /* get and print connection */
+ if (db_get_connection(&conn) == DB_OK) {
+ fprintf(stdout, "driver:%s\n",
+ conn.driverName ? conn.driverName : "");
+ fprintf(stdout, "database:%s\n",
+ conn.databaseName ? conn.databaseName : "");
+ fprintf(stdout, "schema:%s\n",
+ conn.schemaName ? conn.schemaName : "");
+ fprintf(stdout, "group:%s\n", conn.group ? conn.group : "");
+ }
+ else
+ G_fatal_error(_("Database connection not defined. "
+ "Run db.connect."));
+
+ exit(EXIT_SUCCESS);
+ }
+
+
+ if (check_set_default->answer) {
+ /* check connection and set to system-wide default in required */
+ /*
+ * TODO: improve db_{get,set}_connection() to not return DB_OK on error
+ * (thus currently there is no point in checking for that here)
+ */
+ db_get_connection(&conn);
+
+ if (!conn.driverName && !conn.databaseName) {
+
+ db_set_default_connection();
+ db_get_connection(&conn);
+
+ G_message(_("Default driver / database set to:\n"
+ "driver: %s\ndatabase: %s"), conn.driverName,
+ conn.databaseName);
+ }
+ /* they must be a matched pair, so if one is set but not the other
+ then give up and let the user figure it out */
+ else if (!conn.driverName) {
+ G_fatal_error(_("Default driver is not set"));
+ }
+ else if (!conn.databaseName) {
+ G_fatal_error(_("Default database is not set"));
+ }
+
+ /* connection either already existed or now exists */
+ exit(EXIT_SUCCESS);
+ }
+
+
+ /* set connection */
+ db_get_connection(&conn); /* read current */
+
+ if (driver->answer)
+ conn.driverName = driver->answer;
+
+ if (database->answer)
+ conn.databaseName = database->answer;
+
+ if (schema->answer)
+ conn.schemaName = schema->answer;
+
+ if (group->answer)
+ conn.group = group->answer;
+
+ db_set_connection(&conn);
+
+
+ exit(EXIT_SUCCESS);
+}
Deleted: grass/trunk/db/db.copy/copy.c
===================================================================
--- grass/trunk/db/db.copy/copy.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.copy/copy.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,129 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.copy
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
- * PURPOSE: copy a table
- * COPYRIGHT: (C) 2003-2006 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <grass/gis.h>
-#include <grass/dbmi.h>
-#include <grass/codes.h>
-#include <grass/glocale.h>
-
-
-int main(int argc, char **argv)
-{
- int ret;
- struct Option *from_driver, *from_database, *from_table;
- struct Option *to_driver, *to_database, *to_table;
- struct Option *where, *select;
- struct GModule *module;
- const char *drv, *db;
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, attribute table, SQL");
- module->label = _("Copy a table.");
- module->description =
- _("Either 'from_table' (optionally with 'where') can be used "
- "or 'select' option, but not 'from_table' and 'select' at the same time.");
-
- from_driver = G_define_standard_option(G_OPT_DB_DRIVER);
- from_driver->key = "from_driver";
- from_driver->options = db_list_drivers();
- from_driver->description = _("Input driver name");
- if ((drv = db_get_default_driver_name()))
- from_driver->answer = (char *) drv;
-
- from_database = G_define_standard_option(G_OPT_DB_DATABASE);
- from_database->key = "from_database";
- from_database->description = _("Input database name");
- if ((db = db_get_default_database_name()))
- from_database->answer = (char *) db;
-
- from_table = G_define_standard_option(G_OPT_DB_TABLE);
- from_table->key = "from_table";
- from_table->description =
- _("Input table name (only, if 'select' is not used)");
-
- to_driver = G_define_standard_option(G_OPT_DB_DRIVER);
- to_driver->key = "to_driver";
- to_driver->options = db_list_drivers();
- to_driver->required = NO;
- to_driver->description = _("Output driver name");
- if ((drv = db_get_default_driver_name()))
- to_driver->answer = (char *) drv;
-
- to_database = G_define_standard_option(G_OPT_DB_DATABASE);
- to_database->key = "to_database";
- to_database->description = _("Output database name");
- if ((db = db_get_default_database_name()))
- to_database->answer = (char *) db;
-
- to_table = G_define_standard_option(G_OPT_DB_TABLE);
- to_table->key = "to_table";
- to_table->required = YES;
- to_table->description = _("Output table name");
- to_table->gisprompt = "new,dbtable,dbtable";
-
- where = G_define_standard_option(G_OPT_DB_WHERE);
-
- select = G_define_option();
- select->key = "select";
- select->type = TYPE_STRING;
- select->required = NO;
- select->label = _("Full select statement (only, if 'from_table' and 'where' is not used)");
- select->description = _("E.g.: SELECT dedek FROM starobince WHERE obec = 'Frimburg'");
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
- /* Check options and copy tables */
- if (from_table->answer) {
- if (select->answer)
- G_fatal_error(_("Cannot combine 'from_table' and 'select' options"));
-
- if (where->answer) {
- ret =
- db_copy_table_where(from_driver->answer,
- from_database->answer, from_table->answer,
- to_driver->answer, to_database->answer,
- to_table->answer, where->answer);
- }
- else {
- ret =
- db_copy_table(from_driver->answer, from_database->answer,
- from_table->answer, to_driver->answer,
- to_database->answer, to_table->answer);
- }
- }
- else {
- if (!select->answer)
- G_fatal_error(_("Either 'from_table' or 'select' option must be given."));
-
- if (where->answer)
- G_fatal_error(_("Cannot combine 'select' and 'where' options"));
-
- ret =
- db_copy_table_select(from_driver->answer, from_database->answer,
- from_table->answer, to_driver->answer,
- to_database->answer, to_table->answer,
- select->answer);
- }
-
- if (ret == DB_FAILED) {
- G_warning(_("Copy table failed"));
- exit(EXIT_FAILURE);
- }
-
- exit(EXIT_SUCCESS);
-}
Copied: grass/trunk/db/db.copy/main.c (from rev 35438, grass/trunk/db/db.copy/copy.c)
===================================================================
--- grass/trunk/db/db.copy/main.c (rev 0)
+++ grass/trunk/db/db.copy/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,129 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.copy
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
+ * PURPOSE: copy a table
+ * COPYRIGHT: (C) 2003-2006 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/codes.h>
+#include <grass/glocale.h>
+
+
+int main(int argc, char **argv)
+{
+ int ret;
+ struct Option *from_driver, *from_database, *from_table;
+ struct Option *to_driver, *to_database, *to_table;
+ struct Option *where, *select;
+ struct GModule *module;
+ const char *drv, *db;
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, attribute table, SQL");
+ module->label = _("Copy a table.");
+ module->description =
+ _("Either 'from_table' (optionally with 'where') can be used "
+ "or 'select' option, but not 'from_table' and 'select' at the same time.");
+
+ from_driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ from_driver->key = "from_driver";
+ from_driver->options = db_list_drivers();
+ from_driver->description = _("Input driver name");
+ if ((drv = db_get_default_driver_name()))
+ from_driver->answer = (char *) drv;
+
+ from_database = G_define_standard_option(G_OPT_DB_DATABASE);
+ from_database->key = "from_database";
+ from_database->description = _("Input database name");
+ if ((db = db_get_default_database_name()))
+ from_database->answer = (char *) db;
+
+ from_table = G_define_standard_option(G_OPT_DB_TABLE);
+ from_table->key = "from_table";
+ from_table->description =
+ _("Input table name (only, if 'select' is not used)");
+
+ to_driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ to_driver->key = "to_driver";
+ to_driver->options = db_list_drivers();
+ to_driver->required = NO;
+ to_driver->description = _("Output driver name");
+ if ((drv = db_get_default_driver_name()))
+ to_driver->answer = (char *) drv;
+
+ to_database = G_define_standard_option(G_OPT_DB_DATABASE);
+ to_database->key = "to_database";
+ to_database->description = _("Output database name");
+ if ((db = db_get_default_database_name()))
+ to_database->answer = (char *) db;
+
+ to_table = G_define_standard_option(G_OPT_DB_TABLE);
+ to_table->key = "to_table";
+ to_table->required = YES;
+ to_table->description = _("Output table name");
+ to_table->gisprompt = "new,dbtable,dbtable";
+
+ where = G_define_standard_option(G_OPT_DB_WHERE);
+
+ select = G_define_option();
+ select->key = "select";
+ select->type = TYPE_STRING;
+ select->required = NO;
+ select->label = _("Full select statement (only, if 'from_table' and 'where' is not used)");
+ select->description = _("E.g.: SELECT dedek FROM starobince WHERE obec = 'Frimburg'");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ /* Check options and copy tables */
+ if (from_table->answer) {
+ if (select->answer)
+ G_fatal_error(_("Cannot combine 'from_table' and 'select' options"));
+
+ if (where->answer) {
+ ret =
+ db_copy_table_where(from_driver->answer,
+ from_database->answer, from_table->answer,
+ to_driver->answer, to_database->answer,
+ to_table->answer, where->answer);
+ }
+ else {
+ ret =
+ db_copy_table(from_driver->answer, from_database->answer,
+ from_table->answer, to_driver->answer,
+ to_database->answer, to_table->answer);
+ }
+ }
+ else {
+ if (!select->answer)
+ G_fatal_error(_("Either 'from_table' or 'select' option must be given."));
+
+ if (where->answer)
+ G_fatal_error(_("Cannot combine 'select' and 'where' options"));
+
+ ret =
+ db_copy_table_select(from_driver->answer, from_database->answer,
+ from_table->answer, to_driver->answer,
+ to_database->answer, to_table->answer,
+ select->answer);
+ }
+
+ if (ret == DB_FAILED) {
+ G_warning(_("Copy table failed"));
+ exit(EXIT_FAILURE);
+ }
+
+ exit(EXIT_SUCCESS);
+}
Deleted: grass/trunk/db/db.createdb/createdb.c
===================================================================
--- grass/trunk/db/db.createdb/createdb.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.createdb/createdb.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,79 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.createdb
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
- * PURPOSE: create a new empty database
- * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <grass/dbmi.h>
-#include <grass/gis.h>
-#include <grass/codes.h>
-#include <grass/glocale.h>
-
-
-struct
-{
- char *driver, *database;
-} parms;
-
-
-/* function prototypes */
-static void parse_command_line(int, char **);
-
-
-int main(int argc, char **argv)
-{
- dbDriver *driver;
- dbHandle handle;
- int stat;
-
- parse_command_line(argc, argv);
-
- driver = db_start_driver(parms.driver);
- if (driver == NULL)
- G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
-
- db_init_handle(&handle);
- db_set_handle(&handle, parms.database, NULL);
- stat = db_create_database(driver, &handle);
- db_shutdown_driver(driver);
-
- exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE);
-}
-
-
-static void parse_command_line(int argc, char **argv)
-{
- struct Option *driver, *database;
- struct GModule *module;
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- driver = G_define_standard_option(G_OPT_DB_DRIVER);
- driver->options = db_list_drivers();
- driver->required = YES;
-
- database = G_define_standard_option(G_OPT_DB_DATABASE);
- database->required = YES;
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, SQL");
- module->description = _("Creates an empty database.");
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
- parms.driver = driver->answer;
- parms.database = database->answer;
-}
Copied: grass/trunk/db/db.createdb/main.c (from rev 35438, grass/trunk/db/db.createdb/createdb.c)
===================================================================
--- grass/trunk/db/db.createdb/main.c (rev 0)
+++ grass/trunk/db/db.createdb/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,79 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.createdb
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
+ * PURPOSE: create a new empty database
+ * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <grass/dbmi.h>
+#include <grass/gis.h>
+#include <grass/codes.h>
+#include <grass/glocale.h>
+
+
+struct
+{
+ char *driver, *database;
+} parms;
+
+
+/* function prototypes */
+static void parse_command_line(int, char **);
+
+
+int main(int argc, char **argv)
+{
+ dbDriver *driver;
+ dbHandle handle;
+ int stat;
+
+ parse_command_line(argc, argv);
+
+ driver = db_start_driver(parms.driver);
+ if (driver == NULL)
+ G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+
+ db_init_handle(&handle);
+ db_set_handle(&handle, parms.database, NULL);
+ stat = db_create_database(driver, &handle);
+ db_shutdown_driver(driver);
+
+ exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+
+
+static void parse_command_line(int argc, char **argv)
+{
+ struct Option *driver, *database;
+ struct GModule *module;
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ driver->options = db_list_drivers();
+ driver->required = YES;
+
+ database = G_define_standard_option(G_OPT_DB_DATABASE);
+ database->required = YES;
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, SQL");
+ module->description = _("Creates an empty database.");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ parms.driver = driver->answer;
+ parms.database = database->answer;
+}
Deleted: grass/trunk/db/db.databases/databases.c
===================================================================
--- grass/trunk/db/db.databases/databases.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.databases/databases.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,99 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.databases
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
- * PURPOSE: lists all databases for a given driver
- * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <grass/dbmi.h>
-#include <grass/gis.h>
-#include <grass/codes.h>
-#include <grass/glocale.h>
-
-
-struct
-{
- char *driver;
- char *location;
-} parms;
-
-
-/* function prototypes */
-static void parse_command_line(int, char **);
-
-
-int main(int argc, char **argv)
-{
- dbDriver *driver;
- dbHandle *handles;
- dbString locations;
- int nlocs = 0;
- int count, i;
-
- db_init_string(&locations);
- parse_command_line(argc, argv);
-
- if (parms.location) {
- db_set_string(&locations, parms.location);
- nlocs = 1;
- }
-
- driver = db_start_driver(parms.driver);
- if (driver == NULL)
- G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
-
- if (db_list_databases(driver, &locations, nlocs, &handles, &count) !=
- DB_OK)
- G_fatal_error(_("Unable to list databases"));
-
- db_shutdown_driver(driver);
-
- for (i = 0; i < count; i++) {
- fprintf(stdout, "%s", db_get_handle_dbname(&handles[i]));
- fprintf(stdout, "\n");
- }
-
- exit(EXIT_SUCCESS);
-}
-
-
-static void parse_command_line(int argc, char **argv)
-{
- struct Option *driver, *location;
- struct GModule *module;
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- driver = G_define_standard_option(G_OPT_DB_DRIVER);
- driver->options = db_list_drivers();
-
- location = G_define_option();
- location->key = "location";
- location->type = TYPE_STRING;
- location->required = NO;
- location->multiple = YES;
- location->description = _("Location name");
-
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, SQL");
- module->description =
- _("List all databases for a given driver and location.");
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
- parms.driver = driver->answer;
- parms.location = location->answer;
-}
Copied: grass/trunk/db/db.databases/main.c (from rev 35438, grass/trunk/db/db.databases/databases.c)
===================================================================
--- grass/trunk/db/db.databases/main.c (rev 0)
+++ grass/trunk/db/db.databases/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,99 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.databases
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
+ * PURPOSE: lists all databases for a given driver
+ * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <grass/dbmi.h>
+#include <grass/gis.h>
+#include <grass/codes.h>
+#include <grass/glocale.h>
+
+
+struct
+{
+ char *driver;
+ char *location;
+} parms;
+
+
+/* function prototypes */
+static void parse_command_line(int, char **);
+
+
+int main(int argc, char **argv)
+{
+ dbDriver *driver;
+ dbHandle *handles;
+ dbString locations;
+ int nlocs = 0;
+ int count, i;
+
+ db_init_string(&locations);
+ parse_command_line(argc, argv);
+
+ if (parms.location) {
+ db_set_string(&locations, parms.location);
+ nlocs = 1;
+ }
+
+ driver = db_start_driver(parms.driver);
+ if (driver == NULL)
+ G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+
+ if (db_list_databases(driver, &locations, nlocs, &handles, &count) !=
+ DB_OK)
+ G_fatal_error(_("Unable to list databases"));
+
+ db_shutdown_driver(driver);
+
+ for (i = 0; i < count; i++) {
+ fprintf(stdout, "%s", db_get_handle_dbname(&handles[i]));
+ fprintf(stdout, "\n");
+ }
+
+ exit(EXIT_SUCCESS);
+}
+
+
+static void parse_command_line(int argc, char **argv)
+{
+ struct Option *driver, *location;
+ struct GModule *module;
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ driver->options = db_list_drivers();
+
+ location = G_define_option();
+ location->key = "location";
+ location->type = TYPE_STRING;
+ location->required = NO;
+ location->multiple = YES;
+ location->description = _("Location name");
+
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, SQL");
+ module->description =
+ _("List all databases for a given driver and location.");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ parms.driver = driver->answer;
+ parms.location = location->answer;
+}
Deleted: grass/trunk/db/db.describe/describe.c
===================================================================
--- grass/trunk/db/db.describe/describe.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.describe/describe.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,135 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.describe
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Glynn Clements <glynn gclements.plus.com>,
- * Markus Neteler <neteler itc.it>,
- * Stephan Holl
- * PURPOSE: Displays table information
- * COPYRIGHT: (C) 2002-2008 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/dbmi.h>
-#include <grass/codes.h>
-#include <grass/glocale.h>
-#include "local_proto.h"
-
-
-struct
-{
- char *driver, *database, *table;
- int printcolnames;
-} parms;
-
-
-/* function prototypes */
-static void parse_command_line(int, char **);
-
-
-int main(int argc, char **argv)
-{
- dbDriver *driver;
- dbHandle handle;
- dbTable *table;
- dbString table_name;
- int col, ncols, nrows;
- dbColumn *column;
- char buf[1024];
- dbString stmt;
-
- parse_command_line(argc, argv);
- driver = db_start_driver(parms.driver);
- if (driver == NULL)
- G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
-
- db_init_handle(&handle);
- db_set_handle(&handle, parms.database, NULL);
- if (db_open_database(driver, &handle) != DB_OK)
- G_fatal_error(_("Unable to open database <%s>"), parms.database);
-
- db_init_string(&table_name);
- db_set_string(&table_name, parms.table);
-
- if (db_describe_table(driver, &table_name, &table) != DB_OK)
- G_fatal_error(_("Unable to describe table <%s>"), table_name);
-
- if (!parms.printcolnames)
- print_table_definition(driver, table);
- else {
- ncols = db_get_table_number_of_columns(table);
-
- db_init_string(&stmt);
- sprintf(buf, "select * from %s", db_get_table_name(table));
- db_set_string(&stmt, buf);
- nrows = db_get_table_number_of_rows(driver, &stmt);
- fprintf(stdout, "ncols: %d\n", ncols);
- fprintf(stdout, "nrows: %d\n", nrows);
- for (col = 0; col < ncols; col++) {
- column = db_get_table_column(table, col);
- fprintf(stdout, "Column %d: %s:%s:%d\n", (col + 1),
- db_get_column_name(column),
- db_sqltype_name(db_get_column_sqltype(column)),
- db_get_column_length(column));
- }
- }
-
- db_close_database(driver);
- db_shutdown_driver(driver);
-
- exit(EXIT_SUCCESS);
-}
-
-
-static void parse_command_line(int argc, char **argv)
-{
- struct Option *driver, *database, *table;
- struct Flag *cols, *tdesc;
- struct GModule *module;
- const char *drv, *db;
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- cols = G_define_flag();
- cols->key = 'c';
- cols->description = _("Print column names only instead "
- "of full column descriptions");
-
- tdesc = G_define_flag();
- tdesc->key = 't';
- tdesc->description = _("Print table structure");
-
- table = G_define_standard_option(G_OPT_DB_TABLE);
- table->required = YES;
-
- driver = G_define_standard_option(G_OPT_DB_DRIVER);
- driver->options = db_list_drivers();
- if ((drv = db_get_default_driver_name()))
- driver->answer = (char *) drv;
-
- database = G_define_standard_option(G_OPT_DB_DATABASE);
- if ((db = db_get_default_database_name()))
- database->answer = (char *) db;
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, attribute table");
- module->description = _("Describes a table in detail.");
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
- parms.driver = driver->answer;
- parms.database = database->answer;
- parms.table = table->answer;
- parms.printcolnames = cols->answer;
-}
Copied: grass/trunk/db/db.describe/main.c (from rev 35438, grass/trunk/db/db.describe/describe.c)
===================================================================
--- grass/trunk/db/db.describe/main.c (rev 0)
+++ grass/trunk/db/db.describe/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,135 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.describe
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Glynn Clements <glynn gclements.plus.com>,
+ * Markus Neteler <neteler itc.it>,
+ * Stephan Holl
+ * PURPOSE: Displays table information
+ * COPYRIGHT: (C) 2002-2008 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/codes.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+
+struct
+{
+ char *driver, *database, *table;
+ int printcolnames;
+} parms;
+
+
+/* function prototypes */
+static void parse_command_line(int, char **);
+
+
+int main(int argc, char **argv)
+{
+ dbDriver *driver;
+ dbHandle handle;
+ dbTable *table;
+ dbString table_name;
+ int col, ncols, nrows;
+ dbColumn *column;
+ char buf[1024];
+ dbString stmt;
+
+ parse_command_line(argc, argv);
+ driver = db_start_driver(parms.driver);
+ if (driver == NULL)
+ G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+
+ db_init_handle(&handle);
+ db_set_handle(&handle, parms.database, NULL);
+ if (db_open_database(driver, &handle) != DB_OK)
+ G_fatal_error(_("Unable to open database <%s>"), parms.database);
+
+ db_init_string(&table_name);
+ db_set_string(&table_name, parms.table);
+
+ if (db_describe_table(driver, &table_name, &table) != DB_OK)
+ G_fatal_error(_("Unable to describe table <%s>"), table_name);
+
+ if (!parms.printcolnames)
+ print_table_definition(driver, table);
+ else {
+ ncols = db_get_table_number_of_columns(table);
+
+ db_init_string(&stmt);
+ sprintf(buf, "select * from %s", db_get_table_name(table));
+ db_set_string(&stmt, buf);
+ nrows = db_get_table_number_of_rows(driver, &stmt);
+ fprintf(stdout, "ncols: %d\n", ncols);
+ fprintf(stdout, "nrows: %d\n", nrows);
+ for (col = 0; col < ncols; col++) {
+ column = db_get_table_column(table, col);
+ fprintf(stdout, "Column %d: %s:%s:%d\n", (col + 1),
+ db_get_column_name(column),
+ db_sqltype_name(db_get_column_sqltype(column)),
+ db_get_column_length(column));
+ }
+ }
+
+ db_close_database(driver);
+ db_shutdown_driver(driver);
+
+ exit(EXIT_SUCCESS);
+}
+
+
+static void parse_command_line(int argc, char **argv)
+{
+ struct Option *driver, *database, *table;
+ struct Flag *cols, *tdesc;
+ struct GModule *module;
+ const char *drv, *db;
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ cols = G_define_flag();
+ cols->key = 'c';
+ cols->description = _("Print column names only instead "
+ "of full column descriptions");
+
+ tdesc = G_define_flag();
+ tdesc->key = 't';
+ tdesc->description = _("Print table structure");
+
+ table = G_define_standard_option(G_OPT_DB_TABLE);
+ table->required = YES;
+
+ driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ driver->options = db_list_drivers();
+ if ((drv = db_get_default_driver_name()))
+ driver->answer = (char *) drv;
+
+ database = G_define_standard_option(G_OPT_DB_DATABASE);
+ if ((db = db_get_default_database_name()))
+ database->answer = (char *) db;
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, attribute table");
+ module->description = _("Describes a table in detail.");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ parms.driver = driver->answer;
+ parms.database = database->answer;
+ parms.table = table->answer;
+ parms.printcolnames = cols->answer;
+}
Deleted: grass/trunk/db/db.drivers/drivers.c
===================================================================
--- grass/trunk/db/db.drivers/drivers.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.drivers/drivers.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,81 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.drivers
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>, Stephan Holl
- * PURPOSE: lists all database drivers
- * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <grass/codes.h>
-#include <grass/dbmi.h>
-#include <grass/codes.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-
-struct
-{
- int f;
-} parms;
-
-/* function prototypes */
-static void parse_command_line(int, char **);
-
-
-int main(int argc, char **argv)
-{
- dbDbmscap *list, *p;
-
- parse_command_line(argc, argv);
-
- list = db_read_dbmscap();
- if (list == NULL) {
- G_message(_("Error trying to read dbmscap file\n"));
- exit(EXIT_FAILURE);
- }
-
- for (p = list; p; p = p->next) {
- fprintf(stdout, "%s", p->driverName);
- if (parms.f)
- fprintf(stdout, ":%s", p->comment);
- fprintf(stdout, "\n");
- }
-
- exit(EXIT_SUCCESS);
-}
-
-
-static void parse_command_line(int argc, char **argv)
-{
- struct Flag *full, *print;
- struct GModule *module;
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- full = G_define_flag();
- full->key = 'f';
- full->description = _("Full output");
-
- print = G_define_flag();
- print->key = 'p';
- print->description = _("print drivers and exit");
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, attribute table");
- module->description = _("List all database drivers.");
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
- parms.f = full->answer;
-}
Copied: grass/trunk/db/db.drivers/main.c (from rev 35438, grass/trunk/db/db.drivers/drivers.c)
===================================================================
--- grass/trunk/db/db.drivers/main.c (rev 0)
+++ grass/trunk/db/db.drivers/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,81 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.drivers
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>, Stephan Holl
+ * PURPOSE: lists all database drivers
+ * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <grass/codes.h>
+#include <grass/dbmi.h>
+#include <grass/codes.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+
+struct
+{
+ int f;
+} parms;
+
+/* function prototypes */
+static void parse_command_line(int, char **);
+
+
+int main(int argc, char **argv)
+{
+ dbDbmscap *list, *p;
+
+ parse_command_line(argc, argv);
+
+ list = db_read_dbmscap();
+ if (list == NULL) {
+ G_message(_("Error trying to read dbmscap file\n"));
+ exit(EXIT_FAILURE);
+ }
+
+ for (p = list; p; p = p->next) {
+ fprintf(stdout, "%s", p->driverName);
+ if (parms.f)
+ fprintf(stdout, ":%s", p->comment);
+ fprintf(stdout, "\n");
+ }
+
+ exit(EXIT_SUCCESS);
+}
+
+
+static void parse_command_line(int argc, char **argv)
+{
+ struct Flag *full, *print;
+ struct GModule *module;
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ full = G_define_flag();
+ full->key = 'f';
+ full->description = _("Full output");
+
+ print = G_define_flag();
+ print->key = 'p';
+ print->description = _("print drivers and exit");
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, attribute table");
+ module->description = _("List all database drivers.");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ parms.f = full->answer;
+}
Deleted: grass/trunk/db/db.dropdb/dropdb.c
===================================================================
--- grass/trunk/db/db.dropdb/dropdb.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.dropdb/dropdb.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,79 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.dropdb
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>, Stephan Holl
- * PURPOSE: removes an existing database
- * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <grass/dbmi.h>
-#include <grass/gis.h>
-#include <grass/codes.h>
-#include <grass/glocale.h>
-
-
-struct
-{
- char *driver, *database;
-} parms;
-
-
-/* function prototypes */
-static void parse_command_line(int, char **);
-
-
-int main(int argc, char **argv)
-{
- dbDriver *driver;
- dbHandle handle;
- int stat;
-
- parse_command_line(argc, argv);
-
- driver = db_start_driver(parms.driver);
- if (driver == NULL)
- G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
-
- db_init_handle(&handle);
- db_set_handle(&handle, parms.database, NULL);
- stat = db_delete_database(driver, &handle);
- db_shutdown_driver(driver);
-
- exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE);
-}
-
-
-static void parse_command_line(int argc, char **argv)
-{
- struct Option *driver, *database;
- struct GModule *module;
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- driver = G_define_standard_option(G_OPT_DB_DRIVER);
- driver->options = db_list_drivers();
- driver->required = YES;
-
- database = G_define_standard_option(G_OPT_DB_DATABASE);
- database->required = YES;
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, SQL");
- module->description = _("Removes a database.");
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
- parms.driver = driver->answer;
- parms.database = database->answer;
-}
Copied: grass/trunk/db/db.dropdb/main.c (from rev 35438, grass/trunk/db/db.dropdb/dropdb.c)
===================================================================
--- grass/trunk/db/db.dropdb/main.c (rev 0)
+++ grass/trunk/db/db.dropdb/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,79 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.dropdb
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>, Stephan Holl
+ * PURPOSE: removes an existing database
+ * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <grass/dbmi.h>
+#include <grass/gis.h>
+#include <grass/codes.h>
+#include <grass/glocale.h>
+
+
+struct
+{
+ char *driver, *database;
+} parms;
+
+
+/* function prototypes */
+static void parse_command_line(int, char **);
+
+
+int main(int argc, char **argv)
+{
+ dbDriver *driver;
+ dbHandle handle;
+ int stat;
+
+ parse_command_line(argc, argv);
+
+ driver = db_start_driver(parms.driver);
+ if (driver == NULL)
+ G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+
+ db_init_handle(&handle);
+ db_set_handle(&handle, parms.database, NULL);
+ stat = db_delete_database(driver, &handle);
+ db_shutdown_driver(driver);
+
+ exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+
+
+static void parse_command_line(int argc, char **argv)
+{
+ struct Option *driver, *database;
+ struct GModule *module;
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ driver->options = db_list_drivers();
+ driver->required = YES;
+
+ database = G_define_standard_option(G_OPT_DB_DATABASE);
+ database->required = YES;
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, SQL");
+ module->description = _("Removes a database.");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ parms.driver = driver->answer;
+ parms.database = database->answer;
+}
Deleted: grass/trunk/db/db.droptable/droptable.c
===================================================================
--- grass/trunk/db/db.droptable/droptable.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.droptable/droptable.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,87 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.droptable
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>, Stephan Holl
- * PURPOSE: removes an existing database table
- * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <grass/dbmi.h>
-#include <grass/gis.h>
-#include <grass/codes.h>
-#include <grass/glocale.h>
-
-
-struct
-{
- char *driver, *database, *table;
-} parms;
-
-
-/* function prototypes */
-static void parse_command_line(int, char **);
-
-
-int main(int argc, char **argv)
-{
- dbDriver *driver;
- dbHandle handle;
- dbString table;
- int stat;
-
- parse_command_line(argc, argv);
-
- driver = db_start_driver(parms.driver);
- if (driver == NULL)
- G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
-
- db_init_handle(&handle);
- db_set_handle(&handle, parms.database, NULL);
-
- db_init_string(&table);
- db_set_string(&table, parms.table);
- stat = db_open_database(driver, &handle);
- if (stat == DB_OK)
- stat = db_drop_table(driver, &table);
- db_shutdown_driver(driver);
-
- exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE);
-}
-
-
-static void parse_command_line(int argc, char **argv)
-{
- struct Option *driver, *database, *table;
- struct GModule *module;
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- table = G_define_standard_option(G_OPT_DB_DRIVER);
- table->required = YES;
-
- driver = G_define_standard_option(G_OPT_DB_DRIVER);
- driver->options = db_list_drivers();
-
- database = G_define_standard_option(G_OPT_DB_DATABASE);
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, SQL");
- module->description = _("Removes a table from database.");
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
- parms.driver = driver->answer;
- parms.database = database->answer;
- parms.table = table->answer;
-}
Copied: grass/trunk/db/db.droptable/main.c (from rev 35438, grass/trunk/db/db.droptable/droptable.c)
===================================================================
--- grass/trunk/db/db.droptable/main.c (rev 0)
+++ grass/trunk/db/db.droptable/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,87 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.droptable
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>, Stephan Holl
+ * PURPOSE: removes an existing database table
+ * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <grass/dbmi.h>
+#include <grass/gis.h>
+#include <grass/codes.h>
+#include <grass/glocale.h>
+
+
+struct
+{
+ char *driver, *database, *table;
+} parms;
+
+
+/* function prototypes */
+static void parse_command_line(int, char **);
+
+
+int main(int argc, char **argv)
+{
+ dbDriver *driver;
+ dbHandle handle;
+ dbString table;
+ int stat;
+
+ parse_command_line(argc, argv);
+
+ driver = db_start_driver(parms.driver);
+ if (driver == NULL)
+ G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+
+ db_init_handle(&handle);
+ db_set_handle(&handle, parms.database, NULL);
+
+ db_init_string(&table);
+ db_set_string(&table, parms.table);
+ stat = db_open_database(driver, &handle);
+ if (stat == DB_OK)
+ stat = db_drop_table(driver, &table);
+ db_shutdown_driver(driver);
+
+ exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+
+
+static void parse_command_line(int argc, char **argv)
+{
+ struct Option *driver, *database, *table;
+ struct GModule *module;
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ table = G_define_standard_option(G_OPT_DB_DRIVER);
+ table->required = YES;
+
+ driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ driver->options = db_list_drivers();
+
+ database = G_define_standard_option(G_OPT_DB_DATABASE);
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, SQL");
+ module->description = _("Removes a table from database.");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ parms.driver = driver->answer;
+ parms.database = database->answer;
+ parms.table = table->answer;
+}
Deleted: grass/trunk/db/db.execute/execute.c
===================================================================
--- grass/trunk/db/db.execute/execute.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.execute/execute.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,171 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.execute
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Huidae Cho <grass4u gmail.com>, Glynn Clements <glynn gclements.plus.com>, Hamish Bowman <hamish_nospam yahoo.com>, Markus Neteler <neteler itc.it>, Stephan Holl
- * PURPOSE: process one non-select sql statement
- * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/dbmi.h>
-#include <grass/codes.h>
-#include <grass/glocale.h>
-
-
-struct
-{
- char *driver, *database, *input;
- int i;
-} parms;
-
-
-/* function prototypes */
-static void parse_command_line(int, char **);
-static int get_stmt(FILE *, dbString *);
-static int stmt_is_empty(dbString *);
-
-
-int main(int argc, char **argv)
-{
- dbString stmt;
- dbDriver *driver;
- dbHandle handle;
- int ret;
- FILE *fd;
- int error = 0;
-
- parse_command_line(argc, argv);
-
- if (parms.input) {
- fd = fopen(parms.input, "r");
- if (fd == NULL) {
- perror(parms.input);
- exit(EXIT_FAILURE);
- }
- }
- else
- fd = stdin;
-
- driver = db_start_driver(parms.driver);
- if (driver == NULL) {
- G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
- }
-
- db_init_handle(&handle);
- db_set_handle(&handle, parms.database, NULL);
- if (db_open_database(driver, &handle) != DB_OK)
- G_fatal_error(_("Unable to open database <%s>"), parms.database);
-
- while (get_stmt(fd, &stmt)) {
- if (!stmt_is_empty(&stmt)) {
- G_debug(3, "sql: %s", db_get_string(&stmt));
-
- ret = db_execute_immediate(driver, &stmt);
-
- if (ret != DB_OK) {
- if (parms.i) { /* ignore SQL errors */
- G_warning(_("Error while executing: '%s'"),
- db_get_string(&stmt));
- error++;
- }
- else
- G_fatal_error(_("Error while executing: '%s'"),
- db_get_string(&stmt));
- }
- }
- }
-
- db_close_database(driver);
- db_shutdown_driver(driver);
-
- exit(error ? EXIT_FAILURE : EXIT_SUCCESS);
-}
-
-
-static void parse_command_line(int argc, char **argv)
-{
- struct Option *driver, *database, *input;
- struct Flag *i;
- struct GModule *module;
- const char *drv, *db;
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, attribute table, SQL");
- module->description = _("Executes any SQL statement.");
-
- input = G_define_standard_option(G_OPT_F_INPUT);
- input->required = NO;
- input->description = _("Name of file containing SQL statements");
-
- driver = G_define_standard_option(G_OPT_DB_DRIVER);
- driver->options = db_list_drivers();
- if ((drv = db_get_default_driver_name()))
- driver->answer = (char *) drv;
-
- database = G_define_standard_option(G_OPT_DB_DATABASE);
- if ((db = db_get_default_database_name()))
- database->answer = (char *) db;
-
- i = G_define_flag();
- i->key = 'i';
- i->description = _("Ignore SQL errors and continue");
-
- if (G_parser(argc, argv))
- exit(EXIT_SUCCESS);
-
- parms.driver = driver->answer;
- parms.database = database->answer;
- parms.input = input->answer;
- parms.i = i->answer;
-}
-
-
-static int get_stmt(FILE * fd, dbString * stmt)
-{
- char buf[4000], buf2[4000];
- int len, row = 0;
-
- db_init_string(stmt);
-
- while (fgets(buf, 4000, fd) != NULL) {
- strcpy(buf2, buf);
- G_chop(buf2);
- len = strlen(buf2);
-
- if (buf2[len - 1] == ';') { /* end of statement */
- buf2[len - 1] = 0; /* truncate ';' */
- db_append_string(stmt, buf2); /* append truncated */
- return 1;
- }
- else {
- db_append_string(stmt, buf); /* append not truncated string (\n may be part of value) */
- }
- row++;
- }
-
- if (row > 0)
- return 1;
-
- return 0;
-}
-
-
-static int stmt_is_empty(dbString * stmt)
-{
- char dummy[2];
-
- return (sscanf(db_get_string(stmt), "%1s", dummy) != 1);
-}
Copied: grass/trunk/db/db.execute/main.c (from rev 35480, grass/trunk/db/db.execute/execute.c)
===================================================================
--- grass/trunk/db/db.execute/main.c (rev 0)
+++ grass/trunk/db/db.execute/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,171 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.execute
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Huidae Cho <grass4u gmail.com>, Glynn Clements <glynn gclements.plus.com>, Hamish Bowman <hamish_nospam yahoo.com>, Markus Neteler <neteler itc.it>, Stephan Holl
+ * PURPOSE: process one non-select sql statement
+ * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/codes.h>
+#include <grass/glocale.h>
+
+
+struct
+{
+ char *driver, *database, *input;
+ int i;
+} parms;
+
+
+/* function prototypes */
+static void parse_command_line(int, char **);
+static int get_stmt(FILE *, dbString *);
+static int stmt_is_empty(dbString *);
+
+
+int main(int argc, char **argv)
+{
+ dbString stmt;
+ dbDriver *driver;
+ dbHandle handle;
+ int ret;
+ FILE *fd;
+ int error = 0;
+
+ parse_command_line(argc, argv);
+
+ if (parms.input) {
+ fd = fopen(parms.input, "r");
+ if (fd == NULL) {
+ perror(parms.input);
+ exit(EXIT_FAILURE);
+ }
+ }
+ else
+ fd = stdin;
+
+ driver = db_start_driver(parms.driver);
+ if (driver == NULL) {
+ G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+ }
+
+ db_init_handle(&handle);
+ db_set_handle(&handle, parms.database, NULL);
+ if (db_open_database(driver, &handle) != DB_OK)
+ G_fatal_error(_("Unable to open database <%s>"), parms.database);
+
+ while (get_stmt(fd, &stmt)) {
+ if (!stmt_is_empty(&stmt)) {
+ G_debug(3, "sql: %s", db_get_string(&stmt));
+
+ ret = db_execute_immediate(driver, &stmt);
+
+ if (ret != DB_OK) {
+ if (parms.i) { /* ignore SQL errors */
+ G_warning(_("Error while executing: '%s'"),
+ db_get_string(&stmt));
+ error++;
+ }
+ else
+ G_fatal_error(_("Error while executing: '%s'"),
+ db_get_string(&stmt));
+ }
+ }
+ }
+
+ db_close_database(driver);
+ db_shutdown_driver(driver);
+
+ exit(error ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+
+static void parse_command_line(int argc, char **argv)
+{
+ struct Option *driver, *database, *input;
+ struct Flag *i;
+ struct GModule *module;
+ const char *drv, *db;
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, attribute table, SQL");
+ module->description = _("Executes any SQL statement.");
+
+ input = G_define_standard_option(G_OPT_F_INPUT);
+ input->required = NO;
+ input->description = _("Name of file containing SQL statements");
+
+ driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ driver->options = db_list_drivers();
+ if ((drv = db_get_default_driver_name()))
+ driver->answer = (char *) drv;
+
+ database = G_define_standard_option(G_OPT_DB_DATABASE);
+ if ((db = db_get_default_database_name()))
+ database->answer = (char *) db;
+
+ i = G_define_flag();
+ i->key = 'i';
+ i->description = _("Ignore SQL errors and continue");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_SUCCESS);
+
+ parms.driver = driver->answer;
+ parms.database = database->answer;
+ parms.input = input->answer;
+ parms.i = i->answer;
+}
+
+
+static int get_stmt(FILE * fd, dbString * stmt)
+{
+ char buf[4000], buf2[4000];
+ int len, row = 0;
+
+ db_init_string(stmt);
+
+ while (fgets(buf, 4000, fd) != NULL) {
+ strcpy(buf2, buf);
+ G_chop(buf2);
+ len = strlen(buf2);
+
+ if (buf2[len - 1] == ';') { /* end of statement */
+ buf2[len - 1] = 0; /* truncate ';' */
+ db_append_string(stmt, buf2); /* append truncated */
+ return 1;
+ }
+ else {
+ db_append_string(stmt, buf); /* append not truncated string (\n may be part of value) */
+ }
+ row++;
+ }
+
+ if (row > 0)
+ return 1;
+
+ return 0;
+}
+
+
+static int stmt_is_empty(dbString * stmt)
+{
+ char dummy[2];
+
+ return (sscanf(db_get_string(stmt), "%1s", dummy) != 1);
+}
Copied: grass/trunk/db/db.select/main.c (from rev 35438, grass/trunk/db/db.select/select.c)
===================================================================
--- grass/trunk/db/db.select/main.c (rev 0)
+++ grass/trunk/db/db.select/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,296 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.select
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Huidae Cho <grass4u gmail.com>, Glynn Clements <glynn gclements.plus.com>, Jachym Cepicky <jachym les-ejk.cz>, Markus Neteler <neteler itc.it>, Stephan Holl
+ * PURPOSE: process one sql select statement
+ * COPYRIGHT: (C) 2002-2007 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/codes.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+
+struct
+{
+ char *driver, *database, *table, *sql, *fs, *vs, *nv, *input;
+ int c, d, h, test_only;
+} parms;
+
+
+/* function prototypes */
+static void parse_command_line(int, char **);
+static int sel(dbDriver *, dbString *);
+static int get_stmt(FILE *, dbString *);
+static int stmt_is_empty(dbString *);
+
+
+int main(int argc, char **argv)
+{
+ dbString stmt;
+ dbDriver *driver;
+ dbHandle handle;
+ int stat;
+ FILE *fd;
+
+ parse_command_line(argc, argv);
+
+ if (parms.input) {
+ fd = fopen(parms.input, "r");
+ if (fd == NULL) {
+ perror(parms.input);
+ exit(ERROR);
+ }
+ }
+ else
+ fd = stdin;
+
+ db_init_string(&stmt);
+
+ driver = db_start_driver(parms.driver);
+ if (driver == NULL) {
+ G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+ }
+
+ db_init_handle(&handle);
+ db_set_handle(&handle, parms.database, NULL);
+ if (db_open_database(driver, &handle) != DB_OK)
+ G_fatal_error(_("Unable to open database <%s>"), parms.database);
+
+ if (parms.sql) {
+ db_set_string(&stmt, parms.sql);
+ stat = sel(driver, &stmt);
+ }
+ else if (parms.table) {
+ db_set_string(&stmt, "select * from ");
+ db_append_string(&stmt, parms.table);
+ stat = sel(driver, &stmt);
+ }
+ else { /* read stdin */
+ stat = OK;
+ while (stat == OK && get_stmt(fd, &stmt)) {
+ if (!stmt_is_empty(&stmt))
+ stat = sel(driver, &stmt);
+ }
+ }
+
+ db_close_database(driver);
+ db_shutdown_driver(driver);
+
+ exit(stat);
+}
+
+
+static int sel(dbDriver * driver, dbString * stmt)
+{
+ dbCursor cursor;
+ dbTable *table;
+ dbColumn *column;
+ dbValue *value;
+ dbString value_string;
+ int col, ncols;
+ int more;
+
+ if (db_open_select_cursor(driver, stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
+ return ERROR;
+ if (parms.test_only)
+ return OK;
+
+ table = db_get_cursor_table(&cursor);
+ ncols = db_get_table_number_of_columns(table);
+ if (parms.d) {
+ for (col = 0; col < ncols; col++) {
+ column = db_get_table_column(table, col);
+ print_column_definition(column);
+ }
+
+ return OK;
+ }
+
+ db_init_string(&value_string);
+
+ /* column names if horizontal output */
+ if (parms.h && parms.c) {
+ for (col = 0; col < ncols; col++) {
+ column = db_get_table_column(table, col);
+ if (col)
+ fprintf(stdout, "%s", parms.fs);
+ fprintf(stdout, "%s", db_get_column_name(column));
+ }
+ fprintf(stdout, "\n");
+ }
+
+ /* fetch the data */
+ while (1) {
+ if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
+ return ERROR;
+ if (!more)
+ break;
+
+ for (col = 0; col < ncols; col++) {
+ column = db_get_table_column(table, col);
+ value = db_get_column_value(column);
+ db_convert_column_value_to_string(column, &value_string);
+ if (parms.c && !parms.h)
+ fprintf(stdout, "%s%s", db_get_column_name(column), parms.fs);
+ if (col && parms.h)
+ fprintf(stdout, "%s", parms.fs);
+ if (parms.nv && db_test_value_isnull(value))
+ fprintf(stdout, "%s", parms.nv);
+ else
+ fprintf(stdout, "%s", db_get_string(&value_string));
+ if (!parms.h)
+ fprintf(stdout, "\n");
+ }
+ if (parms.h)
+ fprintf(stdout, "\n");
+ else if (parms.vs)
+ fprintf(stdout, "%s\n", parms.vs);
+ }
+
+ return OK;
+}
+
+
+static void parse_command_line(int argc, char **argv)
+{
+ struct Option *driver, *database, *table, *sql, *fs, *vs, *nv, *input;
+ struct Flag *c, *d, *v, *flag_test;
+ struct GModule *module;
+ const char *drv, *db;
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ table = G_define_standard_option(G_OPT_DB_TABLE);
+
+ database = G_define_standard_option(G_OPT_DB_DATABASE);
+ if ((db = db_get_default_database_name()))
+ database->answer = (char *) db;
+
+ driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ driver->options = db_list_drivers();
+ if ((drv = db_get_default_driver_name()))
+ driver->answer = (char *) drv;
+
+ sql = G_define_option();
+ sql->key = "sql";
+ sql->type = TYPE_STRING;
+ sql->required = NO;
+ sql->label = _("SQL select statement");
+ sql->description =
+ _("For example: 'select * from rybniky where kapri = 'hodne'");
+
+ input = G_define_standard_option(G_OPT_F_INPUT);
+ input->required = NO;
+ input->description = _("Name of file with sql statement");
+
+ fs = G_define_standard_option(G_OPT_F_SEP);
+ fs->description = _("Output field separator");
+ fs->guisection = _("Format");
+
+ vs = G_define_standard_option(G_OPT_F_SEP);
+ vs->key = "vs";
+ vs->description = _("Output vertical record separator");
+ vs->answer = NULL;
+ vs->guisection = _("Format");
+
+ nv = G_define_option();
+ nv->key = "nv";
+ nv->type = TYPE_STRING;
+ nv->required = NO;
+ nv->description = _("Null value indicator");
+ nv->guisection = _("Format");
+
+ c = G_define_flag();
+ c->key = 'c';
+ c->description = _("Do not include column names in output");
+ c->guisection = _("Format");
+
+ d = G_define_flag();
+ d->key = 'd';
+ d->description = _("Describe query only (don't run it)");
+
+ v = G_define_flag();
+ v->key = 'v';
+ v->description = _("Vertical output (instead of horizontal)");
+ v->guisection = _("Format");
+
+ flag_test = G_define_flag();
+ flag_test->key = 't';
+ flag_test->description = _("Only test query, do not execute");
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, attribute table, SQL");
+ module->description = _("Selects data from table.");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_SUCCESS);
+
+ parms.driver = driver->answer;
+ parms.database = database->answer;
+ parms.table = table->answer;
+ parms.sql = sql->answer;
+ parms.fs = fs->answer;
+ parms.vs = vs->answer;
+ parms.nv = nv->answer;
+ parms.input = input->answer;
+ if (!c->answer)
+ parms.c = 1;
+ else
+ parms.c = 0;
+ parms.d = d->answer;
+ if (!v->answer)
+ parms.h = 1;
+ else
+ parms.h = 0;
+ parms.test_only = flag_test->answer;
+
+ if (!parms.fs)
+ parms.fs = "";
+ if (parms.input && *parms.input == 0) {
+ G_usage();
+ exit(EXIT_FAILURE);
+ }
+}
+
+
+static int get_stmt(FILE * fd, dbString * stmt)
+{
+ char buf[1024];
+ int n;
+ static int first = 1;
+
+ db_zero_string(stmt);
+
+ /* this is until get_stmt is smart enough to handle multiple stmts */
+ if (!first)
+ return 0;
+ first = 0;
+
+ while ((n = fread(buf, 1, sizeof(buf) - 1, fd)) > 0) {
+ buf[n] = 0;
+ db_append_string(stmt, buf);
+ }
+
+ return 1;
+}
+
+
+static int stmt_is_empty(dbString * stmt)
+{
+ char dummy[2];
+
+ return (sscanf(db_get_string(stmt), "%1s", dummy) != 1);
+}
Deleted: grass/trunk/db/db.select/select.c
===================================================================
--- grass/trunk/db/db.select/select.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.select/select.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,296 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.select
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Huidae Cho <grass4u gmail.com>, Glynn Clements <glynn gclements.plus.com>, Jachym Cepicky <jachym les-ejk.cz>, Markus Neteler <neteler itc.it>, Stephan Holl
- * PURPOSE: process one sql select statement
- * COPYRIGHT: (C) 2002-2007 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <grass/gis.h>
-#include <grass/dbmi.h>
-#include <grass/codes.h>
-#include <grass/glocale.h>
-#include "local_proto.h"
-
-
-struct
-{
- char *driver, *database, *table, *sql, *fs, *vs, *nv, *input;
- int c, d, h, test_only;
-} parms;
-
-
-/* function prototypes */
-static void parse_command_line(int, char **);
-static int sel(dbDriver *, dbString *);
-static int get_stmt(FILE *, dbString *);
-static int stmt_is_empty(dbString *);
-
-
-int main(int argc, char **argv)
-{
- dbString stmt;
- dbDriver *driver;
- dbHandle handle;
- int stat;
- FILE *fd;
-
- parse_command_line(argc, argv);
-
- if (parms.input) {
- fd = fopen(parms.input, "r");
- if (fd == NULL) {
- perror(parms.input);
- exit(ERROR);
- }
- }
- else
- fd = stdin;
-
- db_init_string(&stmt);
-
- driver = db_start_driver(parms.driver);
- if (driver == NULL) {
- G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
- }
-
- db_init_handle(&handle);
- db_set_handle(&handle, parms.database, NULL);
- if (db_open_database(driver, &handle) != DB_OK)
- G_fatal_error(_("Unable to open database <%s>"), parms.database);
-
- if (parms.sql) {
- db_set_string(&stmt, parms.sql);
- stat = sel(driver, &stmt);
- }
- else if (parms.table) {
- db_set_string(&stmt, "select * from ");
- db_append_string(&stmt, parms.table);
- stat = sel(driver, &stmt);
- }
- else { /* read stdin */
- stat = OK;
- while (stat == OK && get_stmt(fd, &stmt)) {
- if (!stmt_is_empty(&stmt))
- stat = sel(driver, &stmt);
- }
- }
-
- db_close_database(driver);
- db_shutdown_driver(driver);
-
- exit(stat);
-}
-
-
-static int sel(dbDriver * driver, dbString * stmt)
-{
- dbCursor cursor;
- dbTable *table;
- dbColumn *column;
- dbValue *value;
- dbString value_string;
- int col, ncols;
- int more;
-
- if (db_open_select_cursor(driver, stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
- return ERROR;
- if (parms.test_only)
- return OK;
-
- table = db_get_cursor_table(&cursor);
- ncols = db_get_table_number_of_columns(table);
- if (parms.d) {
- for (col = 0; col < ncols; col++) {
- column = db_get_table_column(table, col);
- print_column_definition(column);
- }
-
- return OK;
- }
-
- db_init_string(&value_string);
-
- /* column names if horizontal output */
- if (parms.h && parms.c) {
- for (col = 0; col < ncols; col++) {
- column = db_get_table_column(table, col);
- if (col)
- fprintf(stdout, "%s", parms.fs);
- fprintf(stdout, "%s", db_get_column_name(column));
- }
- fprintf(stdout, "\n");
- }
-
- /* fetch the data */
- while (1) {
- if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
- return ERROR;
- if (!more)
- break;
-
- for (col = 0; col < ncols; col++) {
- column = db_get_table_column(table, col);
- value = db_get_column_value(column);
- db_convert_column_value_to_string(column, &value_string);
- if (parms.c && !parms.h)
- fprintf(stdout, "%s%s", db_get_column_name(column), parms.fs);
- if (col && parms.h)
- fprintf(stdout, "%s", parms.fs);
- if (parms.nv && db_test_value_isnull(value))
- fprintf(stdout, "%s", parms.nv);
- else
- fprintf(stdout, "%s", db_get_string(&value_string));
- if (!parms.h)
- fprintf(stdout, "\n");
- }
- if (parms.h)
- fprintf(stdout, "\n");
- else if (parms.vs)
- fprintf(stdout, "%s\n", parms.vs);
- }
-
- return OK;
-}
-
-
-static void parse_command_line(int argc, char **argv)
-{
- struct Option *driver, *database, *table, *sql, *fs, *vs, *nv, *input;
- struct Flag *c, *d, *v, *flag_test;
- struct GModule *module;
- const char *drv, *db;
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- table = G_define_standard_option(G_OPT_DB_TABLE);
-
- database = G_define_standard_option(G_OPT_DB_DATABASE);
- if ((db = db_get_default_database_name()))
- database->answer = (char *) db;
-
- driver = G_define_standard_option(G_OPT_DB_DRIVER);
- driver->options = db_list_drivers();
- if ((drv = db_get_default_driver_name()))
- driver->answer = (char *) drv;
-
- sql = G_define_option();
- sql->key = "sql";
- sql->type = TYPE_STRING;
- sql->required = NO;
- sql->label = _("SQL select statement");
- sql->description =
- _("For example: 'select * from rybniky where kapri = 'hodne'");
-
- input = G_define_standard_option(G_OPT_F_INPUT);
- input->required = NO;
- input->description = _("Name of file with sql statement");
-
- fs = G_define_standard_option(G_OPT_F_SEP);
- fs->description = _("Output field separator");
- fs->guisection = _("Format");
-
- vs = G_define_standard_option(G_OPT_F_SEP);
- vs->key = "vs";
- vs->description = _("Output vertical record separator");
- vs->answer = NULL;
- vs->guisection = _("Format");
-
- nv = G_define_option();
- nv->key = "nv";
- nv->type = TYPE_STRING;
- nv->required = NO;
- nv->description = _("Null value indicator");
- nv->guisection = _("Format");
-
- c = G_define_flag();
- c->key = 'c';
- c->description = _("Do not include column names in output");
- c->guisection = _("Format");
-
- d = G_define_flag();
- d->key = 'd';
- d->description = _("Describe query only (don't run it)");
-
- v = G_define_flag();
- v->key = 'v';
- v->description = _("Vertical output (instead of horizontal)");
- v->guisection = _("Format");
-
- flag_test = G_define_flag();
- flag_test->key = 't';
- flag_test->description = _("Only test query, do not execute");
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, attribute table, SQL");
- module->description = _("Selects data from table.");
-
- if (G_parser(argc, argv))
- exit(EXIT_SUCCESS);
-
- parms.driver = driver->answer;
- parms.database = database->answer;
- parms.table = table->answer;
- parms.sql = sql->answer;
- parms.fs = fs->answer;
- parms.vs = vs->answer;
- parms.nv = nv->answer;
- parms.input = input->answer;
- if (!c->answer)
- parms.c = 1;
- else
- parms.c = 0;
- parms.d = d->answer;
- if (!v->answer)
- parms.h = 1;
- else
- parms.h = 0;
- parms.test_only = flag_test->answer;
-
- if (!parms.fs)
- parms.fs = "";
- if (parms.input && *parms.input == 0) {
- G_usage();
- exit(EXIT_FAILURE);
- }
-}
-
-
-static int get_stmt(FILE * fd, dbString * stmt)
-{
- char buf[1024];
- int n;
- static int first = 1;
-
- db_zero_string(stmt);
-
- /* this is until get_stmt is smart enough to handle multiple stmts */
- if (!first)
- return 0;
- first = 0;
-
- while ((n = fread(buf, 1, sizeof(buf) - 1, fd)) > 0) {
- buf[n] = 0;
- db_append_string(stmt, buf);
- }
-
- return 1;
-}
-
-
-static int stmt_is_empty(dbString * stmt)
-{
- char dummy[2];
-
- return (sscanf(db_get_string(stmt), "%1s", dummy) != 1);
-}
Copied: grass/trunk/db/db.tables/main.c (from rev 35438, grass/trunk/db/db.tables/tables.c)
===================================================================
--- grass/trunk/db/db.tables/main.c (rev 0)
+++ grass/trunk/db/db.tables/main.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -0,0 +1,104 @@
+
+/****************************************************************************
+ *
+ * MODULE: db.tables
+ * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
+ * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>, Stephan Holl
+ * PURPOSE: lists all tables for a given database
+ * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/codes.h>
+#include <grass/glocale.h>
+
+
+struct
+{
+ char *driver, *database;
+ int s;
+} parms;
+
+
+/* function prototypes */
+static void parse_command_line(int, char **);
+
+
+int main(int argc, char **argv)
+{
+ dbDriver *driver;
+ dbHandle handle;
+ dbString *names;
+ int i, count;
+ int system_tables;
+
+ parse_command_line(argc, argv);
+
+ driver = db_start_driver(parms.driver);
+ if (driver == NULL)
+ G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+
+ db_init_handle(&handle);
+ db_set_handle(&handle, parms.database, NULL);
+ if (db_open_database(driver, &handle) != DB_OK)
+ G_fatal_error(_("Unable to open database <%s>"), parms.database);
+
+ system_tables = parms.s;
+ if (db_list_tables(driver, &names, &count, system_tables) != DB_OK)
+ exit(ERROR);
+ for (i = 0; i < count; i++)
+ fprintf(stdout, "%s\n", db_get_string(&names[i]));
+
+ db_close_database(driver);
+ db_shutdown_driver(driver);
+
+ exit(EXIT_SUCCESS);
+}
+
+
+static void parse_command_line(int argc, char **argv)
+{
+ struct Option *driver, *database;
+ struct Flag *p, *s;
+ struct GModule *module;
+ const char *drv, *db;
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ driver = G_define_standard_option(G_OPT_DB_DRIVER);
+ driver->options = db_list_drivers();
+ if ((drv = db_get_default_driver_name()))
+ driver->answer = (char *) drv;
+
+ database = G_define_standard_option(G_OPT_DB_DATABASE);
+ if ((db = db_get_default_database_name()))
+ database->answer = (char *) db;
+
+ p = G_define_flag();
+ p->key = 'p';
+ p->description = _("Print tables and exit");
+
+ s = G_define_flag();
+ s->key = 's';
+ s->description = _("System tables instead of user tables");
+
+ /* Set description */
+ module = G_define_module();
+ module->keywords = _("database, attribute table");
+ module->description = _("Lists all tables for a given database.");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_SUCCESS);
+
+ parms.driver = driver->answer;
+ parms.database = database->answer;
+ parms.s = s->answer;
+}
Deleted: grass/trunk/db/db.tables/tables.c
===================================================================
--- grass/trunk/db/db.tables/tables.c 2009-01-18 07:11:14 UTC (rev 35480)
+++ grass/trunk/db/db.tables/tables.c 2009-01-18 10:26:26 UTC (rev 35481)
@@ -1,104 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: db.tables
- * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
- * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>, Stephan Holl
- * PURPOSE: lists all tables for a given database
- * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <grass/gis.h>
-#include <grass/dbmi.h>
-#include <grass/codes.h>
-#include <grass/glocale.h>
-
-
-struct
-{
- char *driver, *database;
- int s;
-} parms;
-
-
-/* function prototypes */
-static void parse_command_line(int, char **);
-
-
-int main(int argc, char **argv)
-{
- dbDriver *driver;
- dbHandle handle;
- dbString *names;
- int i, count;
- int system_tables;
-
- parse_command_line(argc, argv);
-
- driver = db_start_driver(parms.driver);
- if (driver == NULL)
- G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
-
- db_init_handle(&handle);
- db_set_handle(&handle, parms.database, NULL);
- if (db_open_database(driver, &handle) != DB_OK)
- G_fatal_error(_("Unable to open database <%s>"), parms.database);
-
- system_tables = parms.s;
- if (db_list_tables(driver, &names, &count, system_tables) != DB_OK)
- exit(ERROR);
- for (i = 0; i < count; i++)
- fprintf(stdout, "%s\n", db_get_string(&names[i]));
-
- db_close_database(driver);
- db_shutdown_driver(driver);
-
- exit(EXIT_SUCCESS);
-}
-
-
-static void parse_command_line(int argc, char **argv)
-{
- struct Option *driver, *database;
- struct Flag *p, *s;
- struct GModule *module;
- const char *drv, *db;
-
- /* Initialize the GIS calls */
- G_gisinit(argv[0]);
-
- driver = G_define_standard_option(G_OPT_DB_DRIVER);
- driver->options = db_list_drivers();
- if ((drv = db_get_default_driver_name()))
- driver->answer = (char *) drv;
-
- database = G_define_standard_option(G_OPT_DB_DATABASE);
- if ((db = db_get_default_database_name()))
- database->answer = (char *) db;
-
- p = G_define_flag();
- p->key = 'p';
- p->description = _("Print tables and exit");
-
- s = G_define_flag();
- s->key = 's';
- s->description = _("System tables instead of user tables");
-
- /* Set description */
- module = G_define_module();
- module->keywords = _("database, attribute table");
- module->description = _("Lists all tables for a given database.");
-
- if (G_parser(argc, argv))
- exit(EXIT_SUCCESS);
-
- parms.driver = driver->answer;
- parms.database = database->answer;
- parms.s = s->answer;
-}
More information about the grass-commit
mailing list