Переглянути джерело

indent -bad -bap -bbb -br -bli0 -bls -cli0 -ncs -fc1 -hnl -i4 \
-nbbo -nbc -nbfda -nbfde -ncdb -ncdw -nce -nfca -npcs -nprs \
-npsl -nsc -nsob -saf -sai -saw -sbi0 -ss -ts8 -ut


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@32526 15284696-431f-4ddb-bdfa-cd5b030d7da7

Markus Neteler 16 роки тому
батько
коміт
8868d4b686
100 змінених файлів з 4361 додано та 4330 видалено
  1. 34 33
      db/base/columns.c
  2. 76 72
      db/base/connect.c
  3. 97 81
      db/base/copy.c
  4. 25 25
      db/base/createdb.c
  5. 34 32
      db/base/databases.c
  6. 56 57
      db/base/describe.c
  7. 20 19
      db/base/drivers.c
  8. 25 25
      db/base/dropdb.c
  9. 32 32
      db/base/droptable.c
  10. 70 70
      db/base/execute.c
  11. 2 2
      db/base/local_proto.h
  12. 35 36
      db/base/printtab.c
  13. 137 140
      db/base/select.c
  14. 31 31
      db/base/tables.c
  15. 60 53
      db/db.login/main.c
  16. 57 59
      db/drivers/dbf/column.c
  17. 11 12
      db/drivers/dbf/create_table.c
  18. 15 20
      db/drivers/dbf/cursor.c
  19. 2 1
      db/drivers/dbf/db.c
  20. 1 0
      db/drivers/dbf/dbdriver.h
  21. 657 594
      db/drivers/dbf/dbfexe.c
  22. 79 88
      db/drivers/dbf/describe.c
  23. 3 5
      db/drivers/dbf/driver.c
  24. 18 22
      db/drivers/dbf/error.c
  25. 13 17
      db/drivers/dbf/execute.c
  26. 43 49
      db/drivers/dbf/fetch.c
  27. 58 53
      db/drivers/dbf/globals.h
  28. 11 15
      db/drivers/dbf/listtab.c
  29. 9 9
      db/drivers/dbf/main.c
  30. 46 51
      db/drivers/dbf/main_debug.c
  31. 17 19
      db/drivers/dbf/proto.h
  32. 19 23
      db/drivers/dbf/select.c
  33. 5 6
      db/drivers/dbf/str.c
  34. 208 221
      db/drivers/dbf/table.c
  35. 99 100
      db/drivers/mysql/create_table.c
  36. 15 17
      db/drivers/mysql/cursor.c
  37. 107 113
      db/drivers/mysql/db.c
  38. 1 0
      db/drivers/mysql/dbdriver.h
  39. 126 130
      db/drivers/mysql/describe.c
  40. 3 5
      db/drivers/mysql/driver.c
  41. 10 13
      db/drivers/mysql/error.c
  42. 23 25
      db/drivers/mysql/execute.c
  43. 179 172
      db/drivers/mysql/fetch.c
  44. 14 14
      db/drivers/mysql/globals.h
  45. 38 39
      db/drivers/mysql/index.c
  46. 13 15
      db/drivers/mysql/listtab.c
  47. 3 2
      db/drivers/mysql/main.c
  48. 58 62
      db/drivers/mysql/parse.c
  49. 9 9
      db/drivers/mysql/proto.h
  50. 22 21
      db/drivers/mysql/select.c
  51. 16 18
      db/drivers/odbc/connect.c
  52. 18 17
      db/drivers/odbc/create_table.c
  53. 26 31
      db/drivers/odbc/cursor.c
  54. 28 30
      db/drivers/odbc/db.c
  55. 1 0
      db/drivers/odbc/dbdriver.h
  56. 119 117
      db/drivers/odbc/describe.c
  57. 3 4
      db/drivers/odbc/driver.c
  58. 4 4
      db/drivers/odbc/error.c
  59. 17 17
      db/drivers/odbc/execute.c
  60. 88 83
      db/drivers/odbc/fetch.c
  61. 13 12
      db/drivers/odbc/globals.h
  62. 27 32
      db/drivers/odbc/listdb.c
  63. 37 43
      db/drivers/odbc/listtab.c
  64. 3 3
      db/drivers/odbc/main.c
  65. 15 15
      db/drivers/odbc/odbc.h
  66. 7 7
      db/drivers/odbc/proto.h
  67. 29 28
      db/drivers/odbc/select.c
  68. 131 134
      db/drivers/odbc/table.c
  69. 18 20
      db/drivers/ogr/cursor.c
  70. 13 12
      db/drivers/ogr/db.c
  71. 1 0
      db/drivers/ogr/dbdriver.h
  72. 100 88
      db/drivers/ogr/describe.c
  73. 3 4
      db/drivers/ogr/driver.c
  74. 19 22
      db/drivers/ogr/error.c
  75. 78 75
      db/drivers/ogr/fetch.c
  76. 14 14
      db/drivers/ogr/globals.h
  77. 12 12
      db/drivers/ogr/listtab.c
  78. 4 3
      db/drivers/ogr/main.c
  79. 8 7
      db/drivers/ogr/proto.h
  80. 9 9
      db/drivers/ogr/select.c
  81. 84 86
      db/drivers/postgres/create_table.c
  82. 12 16
      db/drivers/postgres/cursor.c
  83. 65 58
      db/drivers/postgres/db.c
  84. 1 0
      db/drivers/postgres/dbdriver.h
  85. 125 114
      db/drivers/postgres/describe.c
  86. 2 5
      db/drivers/postgres/driver.c
  87. 9 13
      db/drivers/postgres/error.c
  88. 20 21
      db/drivers/postgres/execute.c
  89. 122 122
      db/drivers/postgres/fetch.c
  90. 44 43
      db/drivers/postgres/globals.h
  91. 35 36
      db/drivers/postgres/index.c
  92. 20 18
      db/drivers/postgres/listdb.c
  93. 37 31
      db/drivers/postgres/listtab.c
  94. 2 2
      db/drivers/postgres/main.c
  95. 50 46
      db/drivers/postgres/parse.c
  96. 29 30
      db/drivers/postgres/priv.c
  97. 10 10
      db/drivers/postgres/proto.h
  98. 17 18
      db/drivers/postgres/select.c
  99. 90 91
      db/drivers/sqlite/create_table.c
  100. 0 0
      db/drivers/sqlite/cursor.c

+ 34 - 33
db/base/columns.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.columns
  * MODULE:       db.columns
@@ -19,17 +20,17 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 
 
-struct {
-	char *driver, *database, *table;
+struct
+{
+    char *driver, *database, *table;
 } parms;
 } parms;
 
 
 
 
 /* function prototypes */
 /* function prototypes */
-static void parse_command_line (int, char **);
+static void parse_command_line(int, char **);
 
 
 
 
-int
-main (int argc, char **argv)
+int main(int argc, char **argv)
 {
 {
     dbDriver *driver;
     dbDriver *driver;
     dbHandle handle;
     dbHandle handle;
@@ -37,64 +38,64 @@ main (int argc, char **argv)
     dbString table_name;
     dbString table_name;
     int col, ncols;
     int col, ncols;
 
 
-    parse_command_line (argc, argv);
+    parse_command_line(argc, argv);
 
 
     driver = db_start_driver(parms.driver);
     driver = db_start_driver(parms.driver);
     if (driver == NULL)
     if (driver == NULL)
-        G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
-       
-    db_init_handle (&handle);
-    db_set_handle (&handle, parms.database, 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)
     if (db_open_database(driver, &handle) != DB_OK)
-        exit (EXIT_FAILURE);
+	exit(EXIT_FAILURE);
 
 
     db_init_string(&table_name);
     db_init_string(&table_name);
     db_set_string(&table_name, parms.table);
     db_set_string(&table_name, parms.table);
-    if(db_describe_table (driver, &table_name, &table) != DB_OK)
-        exit (EXIT_FAILURE);
+    if (db_describe_table(driver, &table_name, &table) != DB_OK)
+	exit(EXIT_FAILURE);
 
 
     db_close_database(driver);
     db_close_database(driver);
     db_shutdown_driver(driver);
     db_shutdown_driver(driver);
 
 
     ncols = db_get_table_number_of_columns(table);
     ncols = db_get_table_number_of_columns(table);
     for (col = 0; col < ncols; col++)
     for (col = 0; col < ncols; col++)
-	fprintf(stdout, "%s\n", db_get_column_name(db_get_table_column(table, col)));
+	fprintf(stdout, "%s\n",
+		db_get_column_name(db_get_table_column(table, col)));
 
 
-    exit (EXIT_SUCCESS);
+    exit(EXIT_SUCCESS);
 }
 }
 
 
 
 
-static void
-parse_command_line (int argc, char **argv)
+static void parse_command_line(int argc, char **argv)
 {
 {
     struct Option *driver, *database, *table;
     struct Option *driver, *database, *table;
     struct GModule *module;
     struct GModule *module;
     const char *drv, *db;
     const char *drv, *db;
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-        G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
-    table 		= G_define_standard_option(G_OPT_TABLE);
-    table->required 	= YES;
+    table = G_define_standard_option(G_OPT_TABLE);
+    table->required = YES;
 
 
-    driver 		= G_define_standard_option(G_OPT_DRIVER);
-    driver->options     = db_list_drivers();
-    if ( (drv=db_get_default_driver_name()) )
-        driver->answer = drv;
+    driver = G_define_standard_option(G_OPT_DRIVER);
+    driver->options = db_list_drivers();
+    if ((drv = db_get_default_driver_name()))
+	driver->answer = drv;
 
 
-    database 		= G_define_standard_option(G_OPT_DATABASE);
-    if ( (db=db_get_default_database_name()) )
-         database->answer = db;
+    database = G_define_standard_option(G_OPT_DATABASE);
+    if ((db = db_get_default_database_name()))
+	database->answer = db;
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
     module->description = _("List all columns for a given table.");
     module->description = _("List all columns for a given table.");
 
 
-    if(G_parser(argc, argv))
-        exit (EXIT_FAILURE);
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
 
 
-    parms.driver	= driver->answer;
-    parms.database	= database->answer;
-    parms.table		= table->answer;
+    parms.driver = driver->answer;
+    parms.database = database->answer;
+    parms.table = table->answer;
 }
 }

+ 76 - 72
db/base/connect.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.connect
  * MODULE:       db.connect
@@ -26,88 +27,91 @@
  *   database=$GISDBASE/$LOCATION_NAME/$MAPSET/dbf
  *   database=$GISDBASE/$LOCATION_NAME/$MAPSET/dbf
  */
  */
 
 
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
 {
     dbConnection conn;
     dbConnection conn;
     struct Flag *print, *check_set_default;
     struct Flag *print, *check_set_default;
-/*    struct Option *driver, *database, *user, *password, *keycol;*/
+
+    /*    struct Option *driver, *database, *user, *password, *keycol; */
     struct Option *driver, *database, *schema, *group;
     struct Option *driver, *database, *schema, *group;
     struct GModule *module;
     struct GModule *module;
 
 
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
-    module->keywords    = _("database, SQL");
+    module = G_define_module();
+    module->keywords = _("database, SQL");
     module->description =
     module->description =
 	_("Prints/sets general DB connection for current mapset and exits.");
 	_("Prints/sets general DB connection for current mapset and exits.");
 
 
 
 
     print = G_define_flag();
     print = G_define_flag();
-    print->key               = 'p';
-    print->description       = _("Print current connection parameters and exit");
+    print->key = 'p';
+    print->description = _("Print current connection parameters and exit");
 
 
     check_set_default = G_define_flag();
     check_set_default = G_define_flag();
-    check_set_default->key         = 'c';
+    check_set_default->key = 'c';
     check_set_default->description =
     check_set_default->description =
 	_("Check connection parameters, set if uninitialized, and exit");
 	_("Check connection parameters, set if uninitialized, and exit");
 
 
     driver = G_define_standard_option(G_OPT_DRIVER);
     driver = G_define_standard_option(G_OPT_DRIVER);
-    driver->options    = db_list_drivers();
+    driver->options = db_list_drivers();
     driver->answer = db_get_default_driver_name();
     driver->answer = db_get_default_driver_name();
 
 
     database = G_define_standard_option(G_OPT_DATABASE);
     database = G_define_standard_option(G_OPT_DATABASE);
     database->answer = db_get_default_database_name();
     database->answer = db_get_default_database_name();
 
 
-    schema = G_define_option() ;
-    schema->key        = "schema" ;
-    schema->type       = TYPE_STRING ;
-    schema->required   = NO  ;
-    schema->multiple   = NO ;
-    schema->answer     = db_get_default_schema_name();
-    schema->label      = _("Database schema");
+    schema = G_define_option();
+    schema->key = "schema";
+    schema->type = TYPE_STRING;
+    schema->required = NO;
+    schema->multiple = NO;
+    schema->answer = db_get_default_schema_name();
+    schema->label = _("Database schema");
     schema->description = _("Do not use this option if schemas "
     schema->description = _("Do not use this option if schemas "
 			    "are not supported by driver/database server");
 			    "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     = db_get_default_group_name();
+    group = G_define_option();
+    group->key = "group";
+    group->type = TYPE_STRING;
+    group->required = NO;
+    group->multiple = NO;
+    group->answer = db_get_default_group_name();
     group->description = _("Default group of database users to which "
     group->description = _("Default group of database users to which "
 			   "select privilege is granted");
 			   "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))
+    /* 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);
 	exit(EXIT_FAILURE);
 
 
 
 
-    if(print->answer) {
+    if (print->answer) {
 	/* get and print connection */
 	/* 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 : "");    
+	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
 	else
 	    G_fatal_error(_("Database connection not defined. "
 	    G_fatal_error(_("Database connection not defined. "
@@ -117,29 +121,30 @@ main(int argc, char *argv[])
     }
     }
 
 
 
 
-    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)
- */
+    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);
 	db_get_connection(&conn);
 
 
-	if ( !conn.driverName && !conn.databaseName ) {
+	if (!conn.driverName && !conn.databaseName) {
 
 
 	    db_set_default_connection();
 	    db_set_default_connection();
 	    db_get_connection(&conn);
 	    db_get_connection(&conn);
 
 
 	    G_message(_("Default driver / database set to:\n"
 	    G_message(_("Default driver / database set to:\n"
-		"driver: %s\ndatabase: %s"), conn.driverName, conn.databaseName);
+			"driver: %s\ndatabase: %s"), conn.driverName,
+		      conn.databaseName);
 	}
 	}
 	/* they must be a matched pair, so if one is set but not the other
 	/* 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") ); 
+	   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") ); 
+	else if (!conn.databaseName) {
+	    G_fatal_error(_("Default database is not set"));
 	}
 	}
 
 
 	/* connection either already existed or now exists */
 	/* connection either already existed or now exists */
@@ -147,24 +152,23 @@ main(int argc, char *argv[])
     }
     }
 
 
 
 
-    /* set connection*/
-    db_get_connection( &conn );  /* read current */
+    /* set connection */
+    db_get_connection(&conn);	/* read current */
 
 
-    if ( driver->answer )
-    	conn.driverName = driver->answer;
-    		
-    if ( database->answer )
-    	conn.databaseName = database->answer;
+    if (driver->answer)
+	conn.driverName = driver->answer;
 
 
-    if ( schema->answer )
-    	conn.schemaName = schema->answer;
+    if (database->answer)
+	conn.databaseName = database->answer;
 
 
-    if ( group->answer )
-    	conn.group = group->answer;
+    if (schema->answer)
+	conn.schemaName = schema->answer;
 
 
-    db_set_connection( &conn );    
+    if (group->answer)
+	conn.group = group->answer;
+
+    db_set_connection(&conn);
 
 
 
 
     exit(EXIT_SUCCESS);
     exit(EXIT_SUCCESS);
 }
 }
-

+ 97 - 81
db/base/copy.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.copy
  * MODULE:       db.copy
@@ -19,114 +20,129 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 
 
-int
-main (int argc, char **argv)
+int main(int argc, char **argv)
 {
 {
-    int    ret;
+    int ret;
     struct Option *from_driver, *from_database, *from_table;
     struct Option *from_driver, *from_database, *from_table;
     struct Option *to_driver, *to_database, *to_table;
     struct Option *to_driver, *to_database, *to_table;
     struct Option *where, *select;
     struct Option *where, *select;
     struct GModule *module;
     struct GModule *module;
-    const char   *drv, *db;
+    const char *drv, *db;
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
-    module->description = _("Copy a table. Either 'from_table' (optionaly with 'where') can be used "
-			"or 'select' option, but not 'from_table' and 'select' at the same time.");
-
-    from_driver		     = G_define_option();
-    from_driver->key 	     = "from_driver";
-    from_driver->type 	     = TYPE_STRING;
-    from_driver->options     = db_list_drivers();
-    from_driver->required    = NO;
+    module->description =
+	_
+	("Copy a table. Either 'from_table' (optionaly with 'where') can be used "
+	 "or 'select' option, but not 'from_table' and 'select' at the same time.");
+
+    from_driver = G_define_option();
+    from_driver->key = "from_driver";
+    from_driver->type = TYPE_STRING;
+    from_driver->options = db_list_drivers();
+    from_driver->required = NO;
     from_driver->description = _("Input driver name");
     from_driver->description = _("Input driver name");
-    if ( (drv=db_get_default_driver_name()) )
-         from_driver->answer = drv;
+    if ((drv = db_get_default_driver_name()))
+	from_driver->answer = drv;
 
 
-    from_database 	       = G_define_option();
-    from_database->key 	       = "from_database";
-    from_database->type        = TYPE_STRING;
-    from_database->required    = NO;
+    from_database = G_define_option();
+    from_database->key = "from_database";
+    from_database->type = TYPE_STRING;
+    from_database->required = NO;
     from_database->description = _("Input database name");
     from_database->description = _("Input database name");
-    if ( (db=db_get_default_database_name()) )
+    if ((db = db_get_default_database_name()))
 	from_database->answer = db;
 	from_database->answer = db;
 
 
-    from_table 		    = G_define_option();
-    from_table->key 	    = "from_table";
-    from_table->type 	    = TYPE_STRING;
-    from_table->required    = NO;
-    from_table->description = _("Input table name (only, if 'select' is not used)");
-
-    to_driver		   = G_define_option();
-    to_driver->key 	   = "to_driver";
-    to_driver->type 	   = TYPE_STRING;
-    to_driver->options     = db_list_drivers();
-    to_driver->required    = NO;
+    from_table = G_define_option();
+    from_table->key = "from_table";
+    from_table->type = TYPE_STRING;
+    from_table->required = NO;
+    from_table->description =
+	_("Input table name (only, if 'select' is not used)");
+
+    to_driver = G_define_option();
+    to_driver->key = "to_driver";
+    to_driver->type = TYPE_STRING;
+    to_driver->options = db_list_drivers();
+    to_driver->required = NO;
     to_driver->description = _("Output driver name");
     to_driver->description = _("Output driver name");
-    if ( (drv=db_get_default_driver_name()) )
-         to_driver->answer = drv;
+    if ((drv = db_get_default_driver_name()))
+	to_driver->answer = drv;
 
 
-    to_database 	     = G_define_option();
-    to_database->key 	     = "to_database";
-    to_database->type        = TYPE_STRING;
-    to_database->required    = NO;
+    to_database = G_define_option();
+    to_database->key = "to_database";
+    to_database->type = TYPE_STRING;
+    to_database->required = NO;
     to_database->description = _("Output database name");
     to_database->description = _("Output database name");
-    if ( (db=db_get_default_database_name()) )
+    if ((db = db_get_default_database_name()))
 	to_database->answer = db;
 	to_database->answer = db;
 
 
-    to_table 		  = G_define_option();
-    to_table->key 	  = "to_table";
-    to_table->type 	  = TYPE_STRING;
-    to_table->required    = YES;
+    to_table = G_define_option();
+    to_table->key = "to_table";
+    to_table->type = TYPE_STRING;
+    to_table->required = YES;
     to_table->description = _("Output table name");
     to_table->description = _("Output table name");
 
 
-    where 		= G_define_option();
-    where->key 	    	= "where";
-    where->type 	= TYPE_STRING;
-    where->required	= NO;
-    where->description	= _("Optional where condition (without WHERE key word), e.g.:\n"
-			  "\t\tobec = 'Liptakov'");
-
-    select 		= G_define_option();
-    select->key 	= "select";
-    select->type 	= TYPE_STRING;
-    select->required	= NO;
-    select->description	= _("Full select statement (only, if 'from_table' and 'where' is not used), e.g.:\n"
-			  "\t\tSELECT dedek FROM starobince WHERE obec = 'Frimburg'");
+    where = G_define_option();
+    where->key = "where";
+    where->type = TYPE_STRING;
+    where->required = NO;
+    where->description =
+	_("Optional where condition (without WHERE key word), e.g.:\n"
+	  "\t\tobec = 'Liptakov'");
+
+    select = G_define_option();
+    select->key = "select";
+    select->type = TYPE_STRING;
+    select->required = NO;
+    select->description =
+	_
+	("Full select statement (only, if 'from_table' and 'where' is not used), e.g.:\n"
+	 "\t\tSELECT dedek FROM starobince WHERE obec = 'Frimburg'");
 
 
     if (G_parser(argc, argv))
     if (G_parser(argc, argv))
-        exit (EXIT_FAILURE);
+	exit(EXIT_FAILURE);
 
 
     /* Check options and copy tables */
     /* 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 );
+    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 {
-       	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 );
+	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);
+    if (ret == DB_FAILED) {
+	G_warning(_("Copy table failed"));
+	exit(EXIT_FAILURE);
     }
     }
 
 
-    exit (EXIT_SUCCESS);
+    exit(EXIT_SUCCESS);
 }
 }

+ 25 - 25
db/base/createdb.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.createdb
  * MODULE:       db.createdb
@@ -19,61 +20,60 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 
 
-struct {
-	char *driver, *database;
+struct
+{
+    char *driver, *database;
 } parms;
 } parms;
 
 
 
 
 /* function prototypes */
 /* function prototypes */
-static void parse_command_line (int, char **);
+static void parse_command_line(int, char **);
 
 
 
 
-int
-main (int argc, char **argv)
+int main(int argc, char **argv)
 {
 {
     dbDriver *driver;
     dbDriver *driver;
     dbHandle handle;
     dbHandle handle;
     int stat;
     int stat;
 
 
-    parse_command_line (argc, argv);
+    parse_command_line(argc, argv);
 
 
-    driver = db_start_driver (parms.driver);
+    driver = db_start_driver(parms.driver);
     if (driver == NULL)
     if (driver == NULL)
-        G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+	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);
+    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);
     exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 }
 
 
 
 
-static void
-parse_command_line(int argc, char **argv)
+static void parse_command_line(int argc, char **argv)
 {
 {
     struct Option *driver, *database;
     struct Option *driver, *database;
     struct GModule *module;
     struct GModule *module;
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
-    driver 		= G_define_standard_option(G_OPT_DRIVER);
-    driver->options     = db_list_drivers();
-    driver->required 	= YES;
+    driver = G_define_standard_option(G_OPT_DRIVER);
+    driver->options = db_list_drivers();
+    driver->required = YES;
 
 
-    database 		= G_define_standard_option(G_OPT_DATABASE);
-    database->required 	= YES;
+    database = G_define_standard_option(G_OPT_DATABASE);
+    database->required = YES;
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
     module->description = _("Creates an empty database.");
     module->description = _("Creates an empty database.");
 
 
-    if(G_parser(argc, argv))
-        exit (EXIT_FAILURE);
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
 
 
-    parms.driver	= driver->answer;
-    parms.database	= database->answer;
+    parms.driver = driver->answer;
+    parms.database = database->answer;
 }
 }

+ 34 - 32
db/base/databases.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.databases
  * MODULE:       db.databases
@@ -19,18 +20,18 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 
 
-struct {
-	char *driver;
-	char *location;
+struct
+{
+    char *driver;
+    char *location;
 } parms;
 } parms;
 
 
 
 
 /* function prototypes */
 /* function prototypes */
-static void parse_command_line (int, char **);
+static void parse_command_line(int, char **);
 
 
 
 
-int
-main (int argc, char **argv)
+int main(int argc, char **argv)
 {
 {
     dbDriver *driver;
     dbDriver *driver;
     dbHandle *handles;
     dbHandle *handles;
@@ -38,22 +39,23 @@ main (int argc, char **argv)
     int nlocs = 0;
     int nlocs = 0;
     int count, i;
     int count, i;
 
 
-    db_init_string ( &locations );
+    db_init_string(&locations);
     parse_command_line(argc, argv);
     parse_command_line(argc, argv);
 
 
-    if ( parms.location ) {
-        db_set_string ( &locations, parms.location );
+    if (parms.location) {
+	db_set_string(&locations, parms.location);
 	nlocs = 1;
 	nlocs = 1;
     }
     }
-    
-    driver = db_start_driver (parms.driver);
+
+    driver = db_start_driver(parms.driver);
     if (driver == NULL)
     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 start driver <%s>"), parms.driver);
+
+    if (db_list_databases(driver, &locations, nlocs, &handles, &count) !=
+	DB_OK)
 	G_fatal_error(_("Unable to list databases"));
 	G_fatal_error(_("Unable to list databases"));
 
 
-    db_shutdown_driver (driver);
+    db_shutdown_driver(driver);
 
 
     for (i = 0; i < count; i++) {
     for (i = 0; i < count; i++) {
 	fprintf(stdout, "%s", db_get_handle_dbname(&handles[i]));
 	fprintf(stdout, "%s", db_get_handle_dbname(&handles[i]));
@@ -64,34 +66,34 @@ main (int argc, char **argv)
 }
 }
 
 
 
 
-static void
-parse_command_line (int argc, char **argv)
+static void parse_command_line(int argc, char **argv)
 {
 {
     struct Option *driver, *location;
     struct Option *driver, *location;
     struct GModule *module;
     struct GModule *module;
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
-    driver 		= G_define_standard_option(G_OPT_DRIVER);
-    driver->options     = db_list_drivers();
+    driver = G_define_standard_option(G_OPT_DRIVER);
+    driver->options = db_list_drivers();
 
 
-    location 		  = G_define_option();
-    location->key 	  = "location";
-    location->type 	  = TYPE_STRING;
-    location->required 	  = NO;
-    location->multiple 	  = YES;
+    location = G_define_option();
+    location->key = "location";
+    location->type = TYPE_STRING;
+    location->required = NO;
+    location->multiple = YES;
     location->description = _("Location name");
     location->description = _("Location name");
 
 
-    
+
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
-    module->description = _("List all databases for a given driver and location.");
+    module->description =
+	_("List all databases for a given driver and location.");
 
 
-    if(G_parser(argc, argv))
-        exit(EXIT_FAILURE);
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
 
 
-    parms.driver     = driver->answer;
-    parms.location   = location->answer;
+    parms.driver = driver->answer;
+    parms.location = location->answer;
 }
 }

+ 56 - 57
db/base/describe.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.describe
  * MODULE:       db.describe
@@ -23,18 +24,18 @@
 #include "local_proto.h"
 #include "local_proto.h"
 
 
 
 
-struct {
-	char *driver, *database, *table;
-	int printcolnames;
+struct
+{
+    char *driver, *database, *table;
+    int printcolnames;
 } parms;
 } parms;
 
 
 
 
 /* function prototypes */
 /* function prototypes */
-static void parse_command_line (int, char **);
+static void parse_command_line(int, char **);
 
 
 
 
-int
-main (int argc, char **argv)
+int main(int argc, char **argv)
 {
 {
     dbDriver *driver;
     dbDriver *driver;
     dbHandle handle;
     dbHandle handle;
@@ -45,43 +46,42 @@ main (int argc, char **argv)
     char buf[1024];
     char buf[1024];
     dbString stmt;
     dbString stmt;
 
 
-    parse_command_line (argc, argv);
+    parse_command_line(argc, argv);
     driver = db_start_driver(parms.driver);
     driver = db_start_driver(parms.driver);
     if (driver == NULL)
     if (driver == NULL)
-        G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+	G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
 
 
-    db_init_handle (&handle);
-    db_set_handle (&handle, parms.database, NULL );
+    db_init_handle(&handle);
+    db_set_handle(&handle, parms.database, NULL);
     if (db_open_database(driver, &handle) != DB_OK)
     if (db_open_database(driver, &handle) != DB_OK)
-	G_fatal_error (_("Unable to open database <%s>"),
-		       parms.database);
+	G_fatal_error(_("Unable to open database <%s>"), parms.database);
 
 
     db_init_string(&table_name);
     db_init_string(&table_name);
     db_set_string(&table_name, parms.table);
     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));
-        }
+    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_close_database(driver);
     db_shutdown_driver(driver);
     db_shutdown_driver(driver);
 
 
@@ -89,8 +89,7 @@ main (int argc, char **argv)
 }
 }
 
 
 
 
-static void
-parse_command_line(int argc, char **argv)
+static void parse_command_line(int argc, char **argv)
 {
 {
     struct Option *driver, *database, *table;
     struct Option *driver, *database, *table;
     struct Flag *cols, *tdesc;
     struct Flag *cols, *tdesc;
@@ -98,39 +97,39 @@ parse_command_line(int argc, char **argv)
     const char *drv, *db;
     const char *drv, *db;
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
     cols = G_define_flag();
     cols = G_define_flag();
-    cols->key               = 'c';
-    cols->description       = _("Print column names only instead "
-				"of full column descriptions");
+    cols->key = 'c';
+    cols->description = _("Print column names only instead "
+			  "of full column descriptions");
 
 
     tdesc = G_define_flag();
     tdesc = G_define_flag();
-    tdesc->key               = 't';
-    tdesc->description       = _("Print table structure");
+    tdesc->key = 't';
+    tdesc->description = _("Print table structure");
 
 
-    table 		= G_define_standard_option(G_OPT_TABLE);
-    table->required 	= YES;
+    table = G_define_standard_option(G_OPT_TABLE);
+    table->required = YES;
 
 
-    driver 		= G_define_standard_option(G_OPT_DRIVER);
-    driver->options     = db_list_drivers();
-    if ( (drv=db_get_default_driver_name()) )
-        driver->answer = drv;
+    driver = G_define_standard_option(G_OPT_DRIVER);
+    driver->options = db_list_drivers();
+    if ((drv = db_get_default_driver_name()))
+	driver->answer = drv;
 
 
-    database 		= G_define_standard_option(G_OPT_DATABASE);
-    if ( (db=db_get_default_database_name()) )
-        database->answer = db;
+    database = G_define_standard_option(G_OPT_DATABASE);
+    if ((db = db_get_default_database_name()))
+	database->answer = db;
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
     module->description = _("Describes a table in detail.");
     module->description = _("Describes a table in detail.");
 
 
-    if(G_parser(argc, argv))
-        exit (EXIT_FAILURE);
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
 
 
-    parms.driver	= driver->answer;
-    parms.database	= database->answer;
-    parms.table		= table->answer;
+    parms.driver = driver->answer;
+    parms.database = database->answer;
+    parms.table = table->answer;
     parms.printcolnames = cols->answer;
     parms.printcolnames = cols->answer;
 }
 }

+ 20 - 19
db/base/drivers.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.drivers
  * MODULE:       db.drivers
@@ -20,61 +21,61 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 
 
-struct {
-	int f;
+struct
+{
+    int f;
 } parms;
 } parms;
 
 
 /* function prototypes */
 /* function prototypes */
-static void parse_command_line (int, char **);
+static void parse_command_line(int, char **);
 
 
 
 
-int
-main (int argc, char **argv)
+int main(int argc, char **argv)
 {
 {
     dbDbmscap *list, *p;
     dbDbmscap *list, *p;
 
 
-    parse_command_line (argc, argv);
+    parse_command_line(argc, argv);
 
 
     list = db_read_dbmscap();
     list = db_read_dbmscap();
     if (list == NULL) {
     if (list == NULL) {
-      G_message ( _("Error trying to read dbmscap file\n"));
-      exit (EXIT_FAILURE);
+	G_message(_("Error trying to read dbmscap file\n"));
+	exit(EXIT_FAILURE);
     }
     }
 
 
     for (p = list; p; p = p->next) {
     for (p = list; p; p = p->next) {
-      fprintf(stdout, "%s", p->driverName);
-      if (parms.f) fprintf(stdout, ":%s", p->comment);
-      fprintf(stdout, "\n");
+	fprintf(stdout, "%s", p->driverName);
+	if (parms.f)
+	    fprintf(stdout, ":%s", p->comment);
+	fprintf(stdout, "\n");
     }
     }
 
 
-    exit (EXIT_SUCCESS);
+    exit(EXIT_SUCCESS);
 }
 }
 
 
 
 
-static void
-parse_command_line (int argc, char **argv)
+static void parse_command_line(int argc, char **argv)
 {
 {
     struct Flag *full, *print;
     struct Flag *full, *print;
     struct GModule *module;
     struct GModule *module;
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
     full = G_define_flag();
     full = G_define_flag();
     full->key = 'f';
     full->key = 'f';
     full->description = _("Full output");
     full->description = _("Full output");
 
 
     print = G_define_flag();
     print = G_define_flag();
-    print->key          = 'p';
-    print->description  = _("print drivers and exit");    
+    print->key = 'p';
+    print->description = _("print drivers and exit");
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
     module->description = _("List all database drivers.");
     module->description = _("List all database drivers.");
 
 
     if (G_parser(argc, argv))
     if (G_parser(argc, argv))
-        exit (EXIT_FAILURE);
+	exit(EXIT_FAILURE);
 
 
     parms.f = full->answer;
     parms.f = full->answer;
 }
 }

+ 25 - 25
db/base/dropdb.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.dropdb
  * MODULE:       db.dropdb
@@ -19,61 +20,60 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 
 
-struct {
-	char *driver, *database;
+struct
+{
+    char *driver, *database;
 } parms;
 } parms;
 
 
 
 
 /* function prototypes */
 /* function prototypes */
-static void parse_command_line (int, char **);
+static void parse_command_line(int, char **);
 
 
 
 
-int
-main (int argc, char **argv)
+int main(int argc, char **argv)
 {
 {
     dbDriver *driver;
     dbDriver *driver;
     dbHandle handle;
     dbHandle handle;
     int stat;
     int stat;
 
 
-    parse_command_line (argc, argv);
+    parse_command_line(argc, argv);
 
 
-    driver = db_start_driver (parms.driver); 
+    driver = db_start_driver(parms.driver);
     if (driver == NULL)
     if (driver == NULL)
-        G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
+	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);
+    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);
     exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 }
 
 
 
 
-static void
-parse_command_line (int argc, char **argv)
+static void parse_command_line(int argc, char **argv)
 {
 {
     struct Option *driver, *database;
     struct Option *driver, *database;
     struct GModule *module;
     struct GModule *module;
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
-    driver 		= G_define_standard_option(G_OPT_DRIVER);
-    driver->options     = db_list_drivers();
-    driver->required 	= YES;
+    driver = G_define_standard_option(G_OPT_DRIVER);
+    driver->options = db_list_drivers();
+    driver->required = YES;
 
 
-    database 		= G_define_standard_option(G_OPT_DATABASE);
-    database->required 	= YES;
+    database = G_define_standard_option(G_OPT_DATABASE);
+    database->required = YES;
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
     module->description = _("Removes a database.");
     module->description = _("Removes a database.");
 
 
-    if(G_parser(argc, argv))
-        exit(EXIT_FAILURE);
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
 
 
-    parms.driver	= driver->answer;
-    parms.database	= database->answer;
+    parms.driver = driver->answer;
+    parms.database = database->answer;
 }
 }

+ 32 - 32
db/base/droptable.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.droptable
  * MODULE:       db.droptable
@@ -19,69 +20,68 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 
 
-struct {
-	char *driver, *database, *table;
+struct
+{
+    char *driver, *database, *table;
 } parms;
 } parms;
 
 
 
 
 /* function prototypes */
 /* function prototypes */
-static void parse_command_line (int, char **);
+static void parse_command_line(int, char **);
 
 
 
 
-int
-main (int argc, char **argv)
+int main(int argc, char **argv)
 {
 {
     dbDriver *driver;
     dbDriver *driver;
     dbHandle handle;
     dbHandle handle;
     dbString table;
     dbString table;
     int stat;
     int stat;
 
 
-    parse_command_line (argc, argv);
+    parse_command_line(argc, argv);
 
 
-    driver = db_start_driver (parms.driver);
+    driver = db_start_driver(parms.driver);
     if (driver == NULL)
     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);
+	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);
     exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 }
 
 
 
 
-static void
-parse_command_line (int argc, char **argv)
+static void parse_command_line(int argc, char **argv)
 {
 {
     struct Option *driver, *database, *table;
     struct Option *driver, *database, *table;
     struct GModule *module;
     struct GModule *module;
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
-    table 		= G_define_standard_option(G_OPT_DRIVER);
-    table->required 	= YES;
+    table = G_define_standard_option(G_OPT_DRIVER);
+    table->required = YES;
 
 
-    driver 		= G_define_standard_option(G_OPT_DRIVER);
-    driver->options     = db_list_drivers();
+    driver = G_define_standard_option(G_OPT_DRIVER);
+    driver->options = db_list_drivers();
 
 
-    database 		= G_define_standard_option(G_OPT_DATABASE);
+    database = G_define_standard_option(G_OPT_DATABASE);
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
     module->description = _("Removes a table from database.");
     module->description = _("Removes a table from database.");
 
 
-    if(G_parser(argc, argv))
-        exit(EXIT_FAILURE);
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
 
 
-    parms.driver	= driver->answer;
-    parms.database	= database->answer;
-    parms.table		= table->answer;
+    parms.driver = driver->answer;
+    parms.database = database->answer;
+    parms.table = table->answer;
 }
 }

+ 70 - 70
db/base/execute.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.execute
  * MODULE:       db.execute
@@ -20,20 +21,20 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 
 
-struct {
-	char *driver, *database, *input;
-	int i;
+struct
+{
+    char *driver, *database, *input;
+    int i;
 } parms;
 } parms;
 
 
 
 
 /* function prototypes */
 /* function prototypes */
 static void parse_command_line(int, char **);
 static void parse_command_line(int, char **);
-static int get_stmt (FILE *, dbString *);
-static int stmt_is_empty (dbString *);
+static int get_stmt(FILE *, dbString *);
+static int stmt_is_empty(dbString *);
 
 
 
 
-int
-main (int argc, char **argv)
+int main(int argc, char **argv)
 {
 {
     dbString stmt;
     dbString stmt;
     dbDriver *driver;
     dbDriver *driver;
@@ -42,14 +43,12 @@ main (int argc, char **argv)
     FILE *fd;
     FILE *fd;
     int error = 0;
     int error = 0;
 
 
-    parse_command_line (argc, argv);
+    parse_command_line(argc, argv);
 
 
-    if (parms.input)
-    {
-	fd = fopen (parms.input, "r");
-	if (fd == NULL)
-	{
-	    perror (parms.input);
+    if (parms.input) {
+	fd = fopen(parms.input, "r");
+	if (fd == NULL) {
+	    perror(parms.input);
 	    exit(EXIT_FAILURE);
 	    exit(EXIT_FAILURE);
 	}
 	}
     }
     }
@@ -61,27 +60,26 @@ main (int argc, char **argv)
 	G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
 	G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
     }
     }
 
 
-    db_init_handle (&handle);
-    db_set_handle (&handle, parms.database, NULL);
+    db_init_handle(&handle);
+    db_set_handle(&handle, parms.database, NULL);
     if (db_open_database(driver, &handle) != DB_OK)
     if (db_open_database(driver, &handle) != DB_OK)
 	G_fatal_error(_("Unable to open database <%s>"), parms.database);
 	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));
+    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));
 	    }
 	    }
 	}
 	}
     }
     }
@@ -93,7 +91,7 @@ main (int argc, char **argv)
 }
 }
 
 
 
 
-static void parse_command_line (int argc, char **argv)
+static void parse_command_line(int argc, char **argv)
 {
 {
     struct Option *driver, *database, *input;
     struct Option *driver, *database, *input;
     struct Flag *i;
     struct Flag *i;
@@ -101,76 +99,78 @@ static void parse_command_line (int argc, char **argv)
     const char *drv, *db;
     const char *drv, *db;
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
     module->description = _("Executes any SQL statement.");
     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");
+    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_DRIVER);
-    driver->options     = db_list_drivers();
-    if ( (drv=db_get_default_driver_name()) )
-        driver->answer = drv;
+    driver = G_define_standard_option(G_OPT_DRIVER);
+    driver->options = db_list_drivers();
+    if ((drv = db_get_default_driver_name()))
+	driver->answer = drv;
 
 
-    database 		= G_define_standard_option(G_OPT_DATABASE);
-    if ( (db=db_get_default_database_name()) )
-        database->answer = db;
+    database = G_define_standard_option(G_OPT_DATABASE);
+    if ((db = db_get_default_database_name()))
+	database->answer = db;
 
 
     i = G_define_flag();
     i = G_define_flag();
-    i->key              = 'i';
-    i->description      = _("Ignore SQL errors and continue");
-    
-    if(G_parser(argc, argv))
+    i->key = 'i';
+    i->description = _("Ignore SQL errors and continue");
+
+    if (G_parser(argc, argv))
 	exit(EXIT_SUCCESS);
 	exit(EXIT_SUCCESS);
 
 
-    parms.driver	= driver->answer;
-    parms.database	= database->answer;
-    parms.input		= input->answer;
-    parms.i		= i->answer;
+    parms.driver = driver->answer;
+    parms.database = database->answer;
+    parms.input = input->answer;
+    parms.i = i->answer;
 }
 }
 
 
 
 
-static int get_stmt (FILE *fd, dbString *stmt)
+static int get_stmt(FILE * fd, dbString * stmt)
 {
 {
     char buf[4000], buf2[4000], buf3[7];
     char buf[4000], buf2[4000], buf3[7];
     int len, row = 0;
     int len, row = 0;
 
 
-    db_init_string (stmt);
+    db_init_string(stmt);
 
 
-    while ( fgets (buf, 4000, fd) != NULL ) {
-        strcpy ( buf2, buf );
-        G_chop (buf2);
-        len = strlen (buf2);
+    while (fgets(buf, 4000, fd) != NULL) {
+	strcpy(buf2, buf);
+	G_chop(buf2);
+	len = strlen(buf2);
 
 
 	G_strncpy(buf3, buf2, 6);
 	G_strncpy(buf3, buf2, 6);
-        if (G_strcasecmp(buf3, "select") == 0)
-            G_fatal_error (_("Use db.select for SELECT SQL statements"));
+	if (G_strcasecmp(buf3, "select") == 0)
+	    G_fatal_error(_("Use db.select for SELECT SQL statements"));
 
 
-	len = strlen (buf2);
-	if ( buf2[ len - 1 ] == ';' ) {  /* end of statement */
-	    buf2 [len - 1] = 0;          /* truncate ';' */
-	    db_append_string (stmt, buf2); /* append truncated */
+	len = strlen(buf2);
+	if (buf2[len - 1] == ';') {	/* end of statement */
+	    buf2[len - 1] = 0;	/* truncate ';' */
+	    db_append_string(stmt, buf2);	/* append truncated */
 	    return 1;
 	    return 1;
-	} else {
-	    db_append_string (stmt, buf); /* append not truncated string (\n may be part of value) */
+	}
+	else {
+	    db_append_string(stmt, buf);	/* append not truncated string (\n may be part of value) */
 	}
 	}
 	row++;
 	row++;
     }
     }
 
 
-    if ( row > 0 ) return 1;
+    if (row > 0)
+	return 1;
 
 
     return 0;
     return 0;
 }
 }
 
 
 
 
-static int stmt_is_empty (dbString *stmt)
+static int stmt_is_empty(dbString * stmt)
 {
 {
     char dummy[2];
     char dummy[2];
 
 
-    return (sscanf (db_get_string(stmt), "%1s", dummy) != 1);
+    return (sscanf(db_get_string(stmt), "%1s", dummy) != 1);
 }
 }

+ 2 - 2
db/base/local_proto.h

@@ -1,8 +1,8 @@
 #ifndef __LOCAL_PROTO_H__
 #ifndef __LOCAL_PROTO_H__
 #define __LOCAL_PROTO_H__
 #define __LOCAL_PROTO_H__
 
 
-int print_priv (char *, int);
+int print_priv(char *, int);
 int print_column_definition(dbColumn *);
 int print_column_definition(dbColumn *);
-int print_table_definition (dbDriver *, dbTable *);
+int print_table_definition(dbDriver *, dbTable *);
 
 
 #endif /* __LOCAL_PROTO_H__ */
 #endif /* __LOCAL_PROTO_H__ */

+ 35 - 36
db/base/printtab.c

@@ -3,9 +3,7 @@
 #include "local_proto.h"
 #include "local_proto.h"
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
-int
-print_table_definition (dbDriver *driver, dbTable *table)
-
+int print_table_definition(dbDriver * driver, dbTable * table)
 {
 {
     int ncols, col, nrows;
     int ncols, col, nrows;
     dbColumn *column;
     dbColumn *column;
@@ -14,66 +12,67 @@ print_table_definition (dbDriver *driver, dbTable *table)
 
 
     fprintf(stdout, "table:%s\n", db_get_table_name(table));
     fprintf(stdout, "table:%s\n", db_get_table_name(table));
     fprintf(stdout, "description:%s\n", db_get_table_description(table));
     fprintf(stdout, "description:%s\n", db_get_table_description(table));
-    print_priv ("insert", db_get_table_insert_priv(table));
-    print_priv ("delete", db_get_table_delete_priv(table));
+    print_priv("insert", db_get_table_insert_priv(table));
+    print_priv("delete", db_get_table_delete_priv(table));
 
 
     ncols = db_get_table_number_of_columns(table);
     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);
+    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, "ncols:%d\n", ncols);
     fprintf(stdout, "nrows:%d\n", nrows);
     fprintf(stdout, "nrows:%d\n", nrows);
-    for (col = 0; col < ncols; col++)
-    {
-	column = db_get_table_column (table, col);
+    for (col = 0; col < ncols; col++) {
+	column = db_get_table_column(table, col);
 	fprintf(stdout, "\n");
 	fprintf(stdout, "\n");
-	print_column_definition (column);
+	print_column_definition(column);
     }
     }
-    
+
     return 0;
     return 0;
 }
 }
 
 
-int
-print_column_definition (dbColumn *column)
-
+int print_column_definition(dbColumn * column)
 {
 {
     dbString value_string;
     dbString value_string;
 
 
     fprintf(stdout, "column:%s\n", db_get_column_name(column));
     fprintf(stdout, "column:%s\n", db_get_column_name(column));
     fprintf(stdout, "description:%s\n", db_get_column_description(column));
     fprintf(stdout, "description:%s\n", db_get_column_description(column));
-    fprintf(stdout, "type:%s\n", db_sqltype_name(db_get_column_sqltype(column)));
+    fprintf(stdout, "type:%s\n",
+	    db_sqltype_name(db_get_column_sqltype(column)));
     fprintf(stdout, "len:%d\n", db_get_column_length(column));
     fprintf(stdout, "len:%d\n", db_get_column_length(column));
     fprintf(stdout, "scale:%d\n", db_get_column_scale(column));
     fprintf(stdout, "scale:%d\n", db_get_column_scale(column));
     fprintf(stdout, "precision:%d\n", db_get_column_precision(column));
     fprintf(stdout, "precision:%d\n", db_get_column_precision(column));
     fprintf(stdout, "default:");
     fprintf(stdout, "default:");
-    if (db_test_column_has_default_value(column))
-    {
-      db_init_string(&value_string);
-      db_convert_column_default_value_to_string (column, &value_string);
-      fprintf(stdout, "%s", db_get_string(&value_string));
+    if (db_test_column_has_default_value(column)) {
+	db_init_string(&value_string);
+	db_convert_column_default_value_to_string(column, &value_string);
+	fprintf(stdout, "%s", db_get_string(&value_string));
     }
     }
     fprintf(stdout, "\n");
     fprintf(stdout, "\n");
-    fprintf(stdout, "nullok:%s\n", db_test_column_null_allowed(column) ? "yes" : "no");
-    print_priv ("select", db_get_column_select_priv(column));
-    print_priv ("update", db_get_column_update_priv(column));
-    
+    fprintf(stdout, "nullok:%s\n",
+	    db_test_column_null_allowed(column) ? "yes" : "no");
+    print_priv("select", db_get_column_select_priv(column));
+    print_priv("update", db_get_column_update_priv(column));
+
     return 0;
     return 0;
 }
 }
 
 
-int
-print_priv  (char *label, int  priv)
-
+int print_priv(char *label, int priv)
 {
 {
     fprintf(stdout, "%s:", label);
     fprintf(stdout, "%s:", label);
-    switch (priv)
-    {
-    case DB_GRANTED:     fprintf(stdout, "yes"); break;
-    case DB_NOT_GRANTED: fprintf(stdout, "no"); break;
-    default:             fprintf(stdout, "?"); break;
+    switch (priv) {
+    case DB_GRANTED:
+	fprintf(stdout, "yes");
+	break;
+    case DB_NOT_GRANTED:
+	fprintf(stdout, "no");
+	break;
+    default:
+	fprintf(stdout, "?");
+	break;
     }
     }
     fprintf(stdout, "\n");
     fprintf(stdout, "\n");
-    
+
     return 0;
     return 0;
 }
 }

+ 137 - 140
db/base/select.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.select
  * MODULE:       db.select
@@ -20,21 +21,21 @@
 #include "local_proto.h"
 #include "local_proto.h"
 
 
 
 
-struct {
-	char *driver, *database, *table, *sql, *fs, *vs, *nv, *input;
-	int c,d,h, test_only;
+struct
+{
+    char *driver, *database, *table, *sql, *fs, *vs, *nv, *input;
+    int c, d, h, test_only;
 } parms;
 } parms;
 
 
 
 
 /* function prototypes */
 /* 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 *);
+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)
+int main(int argc, char **argv)
 {
 {
     dbString stmt;
     dbString stmt;
     dbDriver *driver;
     dbDriver *driver;
@@ -42,44 +43,43 @@ main (int argc, char **argv)
     int stat;
     int stat;
     FILE *fd;
     FILE *fd;
 
 
-    parse_command_line (argc, argv);
+    parse_command_line(argc, argv);
 
 
-    if (parms.input)
-    {
-	fd = fopen (parms.input, "r");
-	if (fd == NULL)
-	{
-	    perror (parms.input);
+    if (parms.input) {
+	fd = fopen(parms.input, "r");
+	if (fd == NULL) {
+	    perror(parms.input);
 	    exit(ERROR);
 	    exit(ERROR);
 	}
 	}
     }
     }
     else
     else
 	fd = stdin;
 	fd = stdin;
-       
-    db_init_string ( &stmt );
-    
+
+    db_init_string(&stmt);
+
     driver = db_start_driver(parms.driver);
     driver = db_start_driver(parms.driver);
     if (driver == NULL) {
     if (driver == NULL) {
 	G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
 	G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
     }
     }
 
 
-    db_init_handle (&handle);
-    db_set_handle (&handle, parms.database, NULL);
+    db_init_handle(&handle);
+    db_set_handle(&handle, parms.database, NULL);
     if (db_open_database(driver, &handle) != DB_OK)
     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); 
+	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);
 	stat = sel(driver, &stmt);
-    } else { /* read stdin */
+    }
+    else {			/* read stdin */
 	stat = OK;
 	stat = OK;
-	while(stat == OK && get_stmt (fd, &stmt))
-	{
-	    if(!stmt_is_empty(&stmt))
+	while (stat == OK && get_stmt(fd, &stmt)) {
+	    if (!stmt_is_empty(&stmt))
 		stat = sel(driver, &stmt);
 		stat = sel(driver, &stmt);
 	}
 	}
     }
     }
@@ -91,8 +91,7 @@ main (int argc, char **argv)
 }
 }
 
 
 
 
-static int
-sel (dbDriver *driver, dbString *stmt)
+static int sel(dbDriver * driver, dbString * stmt)
 {
 {
     dbCursor cursor;
     dbCursor cursor;
     dbTable *table;
     dbTable *table;
@@ -103,16 +102,14 @@ sel (dbDriver *driver, dbString *stmt)
     int more;
     int more;
 
 
     if (db_open_select_cursor(driver, stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
     if (db_open_select_cursor(driver, stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
-        return ERROR;
+	return ERROR;
     if (parms.test_only)
     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++)
-	{
+	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);
 	    column = db_get_table_column(table, col);
 	    print_column_definition(column);
 	    print_column_definition(column);
 	}
 	}
@@ -120,175 +117,175 @@ sel (dbDriver *driver, dbString *stmt)
 	return OK;
 	return OK;
     }
     }
 
 
-    db_init_string (&value_string);
+    db_init_string(&value_string);
 
 
     /* column names if horizontal output */
     /* column names if horizontal output */
-    if (parms.h && parms.c)
-    {
-	for (col = 0; col < ncols; col++)
-	{
+    if (parms.h && parms.c) {
+	for (col = 0; col < ncols; col++) {
 	    column = db_get_table_column(table, col);
 	    column = db_get_table_column(table, col);
-	    if (col) fprintf (stdout, "%s", parms.fs);
-	    fprintf (stdout, "%s", db_get_column_name (column));
+	    if (col)
+		fprintf(stdout, "%s", parms.fs);
+	    fprintf(stdout, "%s", db_get_column_name(column));
 	}
 	}
-	fprintf (stdout, "\n");
+	fprintf(stdout, "\n");
     }
     }
 
 
     /* fetch the data */
     /* fetch the data */
-    while(1)
-    {
-	if(db_fetch (&cursor, DB_NEXT, &more) != DB_OK)
+    while (1) {
+	if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
 	    return ERROR;
 	    return ERROR;
 	if (!more)
 	if (!more)
 	    break;
 	    break;
 
 
-	for (col = 0; col < ncols; col++)
-	{
+	for (col = 0; col < ncols; col++) {
 	    column = db_get_table_column(table, col);
 	    column = db_get_table_column(table, col);
-	    value  = db_get_column_value(column);
-	    db_convert_column_value_to_string (column, &value_string);
+	    value = db_get_column_value(column);
+	    db_convert_column_value_to_string(column, &value_string);
 	    if (parms.c && !parms.h)
 	    if (parms.c && !parms.h)
-		fprintf (stdout, "%s%s", db_get_column_name (column), parms.fs);
+		fprintf(stdout, "%s%s", db_get_column_name(column), parms.fs);
 	    if (col && parms.h)
 	    if (col && parms.h)
-		fprintf (stdout, "%s", parms.fs);
-	    if(parms.nv && db_test_value_isnull(value))
-		fprintf (stdout, "%s", parms.nv);
+		fprintf(stdout, "%s", parms.fs);
+	    if (parms.nv && db_test_value_isnull(value))
+		fprintf(stdout, "%s", parms.nv);
 	    else
 	    else
-		fprintf (stdout, "%s", db_get_string (&value_string));
+		fprintf(stdout, "%s", db_get_string(&value_string));
 	    if (!parms.h)
 	    if (!parms.h)
-		fprintf (stdout, "\n");
+		fprintf(stdout, "\n");
 	}
 	}
 	if (parms.h)
 	if (parms.h)
-	    fprintf (stdout, "\n");
+	    fprintf(stdout, "\n");
 	else if (parms.vs)
 	else if (parms.vs)
-	    fprintf (stdout, "%s\n", parms.vs);
+	    fprintf(stdout, "%s\n", parms.vs);
     }
     }
 
 
     return OK;
     return OK;
 }
 }
 
 
 
 
-static void
-parse_command_line (int argc, char **argv)
+static void parse_command_line(int argc, char **argv)
 {
 {
     struct Option *driver, *database, *table, *sql, *fs, *vs, *nv, *input;
     struct Option *driver, *database, *table, *sql, *fs, *vs, *nv, *input;
-    struct Flag *c,*d,*v, *flag_test;
+    struct Flag *c, *d, *v, *flag_test;
     struct GModule *module;
     struct GModule *module;
     const char *drv, *db;
     const char *drv, *db;
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
-    table 		= G_define_standard_option(G_OPT_TABLE);
+    table = G_define_standard_option(G_OPT_TABLE);
 
 
-    database 		= G_define_standard_option(G_OPT_DATABASE);
-    if ( (db=db_get_default_database_name()) )
-        database->answer = db;
+    database = G_define_standard_option(G_OPT_DATABASE);
+    if ((db = db_get_default_database_name()))
+	database->answer = db;
 
 
-    driver 		= G_define_standard_option(G_OPT_DRIVER);
-    driver->options     = db_list_drivers();
-    if ( (drv=db_get_default_driver_name()) )
-        driver->answer = drv;
+    driver = G_define_standard_option(G_OPT_DRIVER);
+    driver->options = db_list_drivers();
+    if ((drv = db_get_default_driver_name()))
+	driver->answer = 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'");
+    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'");
 
 
-    fs 			= G_define_standard_option(G_OPT_F_SEP);
-    fs->description 	= _("Output field separator");
+    fs = G_define_standard_option(G_OPT_F_SEP);
+    fs->description = _("Output field separator");
 
 
-    vs 			= G_define_standard_option(G_OPT_F_SEP);
-    vs->key 		= "vs";
-    vs->description 	= _("Output vertical record separator");
-    vs->answer          = NULL;
+    vs = G_define_standard_option(G_OPT_F_SEP);
+    vs->key = "vs";
+    vs->description = _("Output vertical record separator");
+    vs->answer = NULL;
 
 
-    nv 			= G_define_option();
-    nv->key 		= "nv";
-    nv->type 		= TYPE_STRING;
-    nv->required 	= NO;
-    nv->description 	= _("Null value indicator");
+    nv = G_define_option();
+    nv->key = "nv";
+    nv->type = TYPE_STRING;
+    nv->required = NO;
+    nv->description = _("Null value indicator");
 
 
-    input 		= G_define_standard_option(G_OPT_F_INPUT);
-    input->required 	= NO;
-    input->description 	= _("Name of file with sql statement");
+    input = G_define_standard_option(G_OPT_F_INPUT);
+    input->required = NO;
+    input->description = _("Name of file with sql statement");
 
 
-    c			= G_define_flag();
-    c->key		= 'c';
-    c->description	= _("Do not include column names in output");
+    c = G_define_flag();
+    c->key = 'c';
+    c->description = _("Do not include column names in output");
 
 
-    d			= G_define_flag();
-    d->key		= 'd';
-    d->description	= _("Describe query only (don't run it)");
+    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 = G_define_flag();
+    v->key = 'v';
+    v->description = _("Vertical output (instead of horizontal)");
 
 
-    flag_test			= G_define_flag();
-    flag_test->key		= 't';
-    flag_test->description	= _("Only test query, do not execute");
+    flag_test = G_define_flag();
+    flag_test->key = 't';
+    flag_test->description = _("Only test query, do not execute");
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
     module->description = _("Selects data from table.");
     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)
-    {
+    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();
 	G_usage();
 	exit(EXIT_FAILURE);
 	exit(EXIT_FAILURE);
     }
     }
 }
 }
 
 
 
 
-static int
-get_stmt (FILE *fd, dbString *stmt)
+static int get_stmt(FILE * fd, dbString * stmt)
 {
 {
     char buf[1024];
     char buf[1024];
     int n;
     int n;
     static int first = 1;
     static int first = 1;
 
 
-    db_zero_string (stmt);
+    db_zero_string(stmt);
 
 
     /* this is until get_stmt is smart enough to handle multiple stmts */
     /* this is until get_stmt is smart enough to handle multiple stmts */
     if (!first)
     if (!first)
 	return 0;
 	return 0;
     first = 0;
     first = 0;
 
 
-    while ( ( n = fread (buf, 1, sizeof(buf)-1, fd)) > 0)
-    {
+    while ((n = fread(buf, 1, sizeof(buf) - 1, fd)) > 0) {
 	buf[n] = 0;
 	buf[n] = 0;
-	db_append_string (stmt, buf);
+	db_append_string(stmt, buf);
     }
     }
 
 
     return 1;
     return 1;
 }
 }
 
 
 
 
-static int
-stmt_is_empty (dbString *stmt)
+static int stmt_is_empty(dbString * stmt)
 {
 {
     char dummy[2];
     char dummy[2];
 
 
-    return (sscanf (db_get_string(stmt), "%1s", dummy) != 1);
+    return (sscanf(db_get_string(stmt), "%1s", dummy) != 1);
 }
 }

+ 31 - 31
db/base/tables.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.tables
  * MODULE:       db.tables
@@ -19,18 +20,18 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 
 
-struct {
-	char *driver, *database;
-	int s;
+struct
+{
+    char *driver, *database;
+    int s;
 } parms;
 } parms;
 
 
 
 
 /* function prototypes */
 /* function prototypes */
-static void parse_command_line (int, char **);
+static void parse_command_line(int, char **);
 
 
 
 
-int
-main (int argc, char **argv)
+int main(int argc, char **argv)
 {
 {
     dbDriver *driver;
     dbDriver *driver;
     dbHandle handle;
     dbHandle handle;
@@ -38,22 +39,22 @@ main (int argc, char **argv)
     int i, count;
     int i, count;
     int system_tables;
     int system_tables;
 
 
-    parse_command_line (argc, argv);
+    parse_command_line(argc, argv);
 
 
     driver = db_start_driver(parms.driver);
     driver = db_start_driver(parms.driver);
     if (driver == NULL)
     if (driver == NULL)
 	G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
 	G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
 
 
-    db_init_handle (&handle);
-    db_set_handle (&handle, parms.database, NULL);
+    db_init_handle(&handle);
+    db_set_handle(&handle, parms.database, NULL);
     if (db_open_database(driver, &handle) != DB_OK)
     if (db_open_database(driver, &handle) != DB_OK)
-      	G_fatal_error(_("Unable to open database <%s>"), parms.database);
+	G_fatal_error(_("Unable to open database <%s>"), parms.database);
 
 
     system_tables = parms.s;
     system_tables = parms.s;
-    if(db_list_tables (driver, &names, &count, system_tables) != DB_OK)
+    if (db_list_tables(driver, &names, &count, system_tables) != DB_OK)
 	exit(ERROR);
 	exit(ERROR);
     for (i = 0; i < count; i++)
     for (i = 0; i < count; i++)
-	fprintf(stdout, "%s\n", db_get_string (&names[i]));
+	fprintf(stdout, "%s\n", db_get_string(&names[i]));
 
 
     db_close_database(driver);
     db_close_database(driver);
     db_shutdown_driver(driver);
     db_shutdown_driver(driver);
@@ -62,8 +63,7 @@ main (int argc, char **argv)
 }
 }
 
 
 
 
-static void
-parse_command_line (int argc, char **argv)
+static void parse_command_line(int argc, char **argv)
 {
 {
     struct Option *driver, *database;
     struct Option *driver, *database;
     struct Flag *p, *s;
     struct Flag *p, *s;
@@ -71,34 +71,34 @@ parse_command_line (int argc, char **argv)
     const char *drv, *db;
     const char *drv, *db;
 
 
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
-    driver 		= G_define_standard_option(G_OPT_DRIVER);
-    driver->options     = db_list_drivers();
-    if ( (drv=db_get_default_driver_name()) ) 
+    driver = G_define_standard_option(G_OPT_DRIVER);
+    driver->options = db_list_drivers();
+    if ((drv = db_get_default_driver_name()))
 	driver->answer = drv;
 	driver->answer = drv;
 
 
-    database 		= G_define_standard_option(G_OPT_DATABASE);
-    if ( (db=db_get_default_database_name()) ) 
+    database = G_define_standard_option(G_OPT_DATABASE);
+    if ((db = db_get_default_database_name()))
 	database->answer = db;
 	database->answer = db;
 
 
     p = G_define_flag();
     p = G_define_flag();
-    p->key              = 'p';
-    p->description      = _("Print tables and exit");    
+    p->key = 'p';
+    p->description = _("Print tables and exit");
 
 
-    s			= G_define_flag();
-    s->key		= 's';
-    s->description	= _("System tables instead of user tables");
+    s = G_define_flag();
+    s->key = 's';
+    s->description = _("System tables instead of user tables");
 
 
     /* Set description */
     /* Set description */
-    module              = G_define_module();
+    module = G_define_module();
     module->keywords = _("database, SQL");
     module->keywords = _("database, SQL");
     module->description = _("Lists all tables for a given database.");
     module->description = _("Lists all tables for a given database.");
 
 
-    if(G_parser(argc, argv))
-        exit(EXIT_SUCCESS);
+    if (G_parser(argc, argv))
+	exit(EXIT_SUCCESS);
 
 
-    parms.driver	= driver->answer;
-    parms.database	= database->answer;
-    parms.s		= s->answer;
+    parms.driver = driver->answer;
+    parms.database = database->answer;
+    parms.s = s->answer;
 }
 }

+ 60 - 53
db/db.login/main.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       db.login
  * MODULE:       db.login
@@ -25,83 +26,89 @@
 #include <grass/dbmi.h>
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
 {
     struct Option *driver, *database, *user, *password;
     struct Option *driver, *database, *user, *password;
     struct GModule *module;
     struct GModule *module;
+
 #ifdef HAVE_TERMIOS_H
 #ifdef HAVE_TERMIOS_H
     struct termios tios, tios2;
     struct termios tios, tios2;
 #endif
 #endif
     char answer[200];
     char answer[200];
-    
+
     /* Initialize the GIS calls */
     /* Initialize the GIS calls */
-    G_gisinit(argv[0]) ;
+    G_gisinit(argv[0]);
 
 
-    module              = G_define_module();
-    module->keywords    = _("database, SQL");
+    module = G_define_module();
+    module->keywords = _("database, SQL");
     module->description = _("Sets user/password for driver/database.");
     module->description = _("Sets user/password for driver/database.");
-    
-    driver             = G_define_standard_option(G_OPT_DRIVER) ;
-    driver->options    = db_list_drivers();
-    driver->required   = YES;
-    driver->answer     = db_get_default_driver_name();
 
 
-    database             = G_define_standard_option(G_OPT_DATABASE) ;
-    database->required   = YES ;
-    database->answer     = db_get_default_database_name();
+    driver = G_define_standard_option(G_OPT_DRIVER);
+    driver->options = db_list_drivers();
+    driver->required = YES;
+    driver->answer = db_get_default_driver_name();
 
 
-    user             = G_define_option() ;
-    user->key        = "user" ;
-    user->type       = TYPE_STRING ;
-    user->required   = NO  ;
-    user->multiple   = NO ;
-    user->description= _("Username") ;
+    database = G_define_standard_option(G_OPT_DATABASE);
+    database->required = YES;
+    database->answer = db_get_default_database_name();
 
 
-    password             = G_define_option() ;
-    password->key        = "password" ;
-    password->type       = TYPE_STRING ;
-    password->required   = NO ;
-    password->multiple   = NO ;
-    password->description= _("Password") ;
+    user = G_define_option();
+    user->key = "user";
+    user->type = TYPE_STRING;
+    user->required = NO;
+    user->multiple = NO;
+    user->description = _("Username");
 
 
-    if(G_parser(argc, argv))
+    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);
 	exit(EXIT_FAILURE);
 
 
     /* set connection */
     /* set connection */
-    if (!password->answer && isatty(fileno(stdin)) ){
-        for(;;) {
+    if (!password->answer && isatty(fileno(stdin))) {
+	for (;;) {
 #ifdef HAVE_TERMIOS_H
 #ifdef HAVE_TERMIOS_H
-          tcgetattr(STDIN_FILENO, &tios);
-          tios2 = tios;
-          tios2.c_lflag &= ~ECHO;
-          tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios2);
+	    tcgetattr(STDIN_FILENO, &tios);
+	    tios2 = tios;
+	    tios2.c_lflag &= ~ECHO;
+	    tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios2);
 #endif
 #endif
-          do {
-              fprintf (stderr,_("\nEnter database password for connection\n<%s:%s:user=%s>\n"), driver->answer, database->answer, user->answer);
-              fprintf (stderr, _("Hit RETURN to cancel request\n"));
-              fprintf (stderr,">");
-          } while(!G_gets(answer));
+	    do {
+		fprintf(stderr,
+			_
+			("\nEnter database password for connection\n<%s:%s:user=%s>\n"),
+			driver->answer, database->answer, user->answer);
+		fprintf(stderr, _("Hit RETURN to cancel request\n"));
+		fprintf(stderr, ">");
+	    } while (!G_gets(answer));
 #ifdef HAVE_TERMIOS_H
 #ifdef HAVE_TERMIOS_H
-          tcsetattr(STDIN_FILENO, TCSANOW, &tios);
+	    tcsetattr(STDIN_FILENO, TCSANOW, &tios);
 #endif
 #endif
-          G_strip(answer);
-          if(strlen(answer)==0) {
-	     G_message(_("Exiting. Not changing current settings"));
-	     return -1;
-	  } else {
-	     G_message(_("New password set"));
-	     password->answer = G_store(answer);
-	     break;
-	  }
+	    G_strip(answer);
+	    if (strlen(answer) == 0) {
+		G_message(_("Exiting. Not changing current settings"));
+		return -1;
+	    }
+	    else {
+		G_message(_("New password set"));
+		password->answer = G_store(answer);
+		break;
+	    }
 	}
 	}
     }
     }
-    if (  db_set_login ( driver->answer, database->answer, user->answer, password->answer ) == DB_FAILED ) {
-	G_fatal_error ( _("Unable to set user/password") );
+    if (db_set_login
+	(driver->answer, database->answer, user->answer,
+	 password->answer) == DB_FAILED) {
+	G_fatal_error(_("Unable to set user/password"));
     }
     }
 
 
-    if ( password->answer )
-        G_warning ( _("The password was stored in file") );
-	
+    if (password->answer)
+	G_warning(_("The password was stored in file"));
+
     exit(EXIT_SUCCESS);
     exit(EXIT_SUCCESS);
 }
 }

+ 57 - 59
db/drivers/dbf/column.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -20,106 +21,103 @@
 #include <grass/dbmi.h>
 #include <grass/dbmi.h>
 #include <grass/gis.h>
 #include <grass/gis.h>
 #include "globals.h"
 #include "globals.h"
-#include "proto.h" 
+#include "proto.h"
 
 
 /* add column to table */
 /* add column to table */
-int add_column (int tab, int type, char *name, int width, int decimals)
+int add_column(int tab, int type, char *name, int width, int decimals)
 {
 {
     int c;
     int c;
 
 
-    G_debug (3, "add_column(): tab = %d, type = %d, name = %s, width = %d, decimals = %d",
-	                       tab, type, name, width, decimals);
+    G_debug(3,
+	    "add_column(): tab = %d, type = %d, name = %s, width = %d, decimals = %d",
+	    tab, type, name, width, decimals);
 
 
     /* Check if the column exists */
     /* Check if the column exists */
-    for ( c = 0; c < db.tables[tab].ncols; c++ ) {
-	if ( G_strcasecmp( db.tables[tab].cols[c].name, name ) == 0 ) {
-	    append_error( "Column '%s' already exists (duplicate name)\n", name);
+    for (c = 0; c < db.tables[tab].ncols; c++) {
+	if (G_strcasecmp(db.tables[tab].cols[c].name, name) == 0) {
+	    append_error("Column '%s' already exists (duplicate name)\n",
+			 name);
 	    return DB_FAILED;
 	    return DB_FAILED;
 	}
 	}
     }
     }
-	    
-    c = db.tables[tab].ncols; 
-    
-    if ( db.tables[tab].ncols == db.tables[tab].acols )
-      {
-        db.tables[tab].acols += 15; 
-	db.tables[tab].cols = (COLUMN *) G_realloc ( db.tables[tab].cols, db.tables[tab].acols * sizeof (TABLE) ); 
-      }
-    
-    strncpy ( db.tables[tab].cols[c].name, name, DBF_COL_NAME-1 );
-    db.tables[tab].cols[c].name[DBF_COL_NAME-1] = '\0';
-    
-    db.tables[tab].cols[c].type = type; 
-    db.tables[tab].cols[c].width = width; 
-    db.tables[tab].cols[c].decimals = decimals; 
-    
-    db.tables[tab].ncols++; 
-    
+
+    c = db.tables[tab].ncols;
+
+    if (db.tables[tab].ncols == db.tables[tab].acols) {
+	db.tables[tab].acols += 15;
+	db.tables[tab].cols =
+	    (COLUMN *) G_realloc(db.tables[tab].cols,
+				 db.tables[tab].acols * sizeof(TABLE));
+    }
+
+    strncpy(db.tables[tab].cols[c].name, name, DBF_COL_NAME - 1);
+    db.tables[tab].cols[c].name[DBF_COL_NAME - 1] = '\0';
+
+    db.tables[tab].cols[c].type = type;
+    db.tables[tab].cols[c].width = width;
+    db.tables[tab].cols[c].decimals = decimals;
+
+    db.tables[tab].ncols++;
+
     return DB_OK;
     return DB_OK;
 }
 }
 
 
 /* returns column index or -1 */
 /* returns column index or -1 */
-int find_column (int tab, char *col)
+int find_column(int tab, char *col)
 {
 {
     int i;
     int i;
-     
-    for ( i = 0; i < db.tables[tab].ncols; i++ )
-      {
-        if ( G_strcasecmp( db.tables[tab].cols[i].name, col ) == 0 )
-            return (i);
-      } 
+
+    for (i = 0; i < db.tables[tab].ncols; i++) {
+	if (G_strcasecmp(db.tables[tab].cols[i].name, col) == 0)
+	    return (i);
+    }
     return (-1);
     return (-1);
 }
 }
 
 
 /* drop column from table */
 /* drop column from table */
-int drop_column (int tab, char *name)
+int drop_column(int tab, char *name)
 {
 {
     int i, j, c;
     int i, j, c;
-    
-    G_debug (3, "drop_column(): tab = %d, name = %s", tab, name);
+
+    G_debug(3, "drop_column(): tab = %d, name = %s", tab, name);
 
 
     /* Check if the column exists */
     /* Check if the column exists */
-    c = find_column (tab, name);
-    if (c == -1)
-    {
-	append_error( "Column '%s' does not exist\n", name);
+    c = find_column(tab, name);
+    if (c == -1) {
+	append_error("Column '%s' does not exist\n", name);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
+
     db.tables[tab].ncols--;
     db.tables[tab].ncols--;
 
 
-    for (i = c; i < db.tables[tab].ncols; i++)
-    {
-	strcpy (db.tables[tab].cols[i].name, db.tables[tab].cols[i+1].name);
-	db.tables[tab].cols[i].type = db.tables[tab].cols[i+1].type;
-	db.tables[tab].cols[i].width = db.tables[tab].cols[i+1].width;
-	db.tables[tab].cols[i].decimals = db.tables[tab].cols[i+1].decimals; 
+    for (i = c; i < db.tables[tab].ncols; i++) {
+	strcpy(db.tables[tab].cols[i].name, db.tables[tab].cols[i + 1].name);
+	db.tables[tab].cols[i].type = db.tables[tab].cols[i + 1].type;
+	db.tables[tab].cols[i].width = db.tables[tab].cols[i + 1].width;
+	db.tables[tab].cols[i].decimals = db.tables[tab].cols[i + 1].decimals;
     }
     }
 
 
-/* drop column from each row */
-    for (i = 0; i < db.tables[tab].nrows; i++)
-    {
-	for (j = c; j < db.tables[tab].ncols; j++)
-	{
-	    VALUE* dbval_c  = &(db.tables[tab].rows[i].values[j]);
-	    VALUE* dbval_c1 = &(db.tables[tab].rows[i].values[j + 1]);
+    /* drop column from each row */
+    for (i = 0; i < db.tables[tab].nrows; i++) {
+	for (j = c; j < db.tables[tab].ncols; j++) {
+	    VALUE *dbval_c = &(db.tables[tab].rows[i].values[j]);
+	    VALUE *dbval_c1 = &(db.tables[tab].rows[i].values[j + 1]);
 
 
 	    dbval_c->i = dbval_c1->i;
 	    dbval_c->i = dbval_c1->i;
 	    dbval_c->d = dbval_c1->d;
 	    dbval_c->d = dbval_c1->d;
 
 
-	    if (dbval_c1->c != NULL) 
-	    {
+	    if (dbval_c1->c != NULL) {
 		save_string(dbval_c, dbval_c1->c);
 		save_string(dbval_c, dbval_c1->c);
-		G_free ((char*) dbval_c1->c);
+		G_free((char *)dbval_c1->c);
 		dbval_c1->c = NULL;
 		dbval_c1->c = NULL;
 	    }
 	    }
 
 
 	    dbval_c->is_null = dbval_c1->is_null;
 	    dbval_c->is_null = dbval_c1->is_null;
 	}
 	}
 
 
-	db.tables[tab].rows[i].values = 
-	    (VALUE *) G_realloc (db.tables[tab].rows[i].values,
-				 db.tables[tab].ncols * sizeof(VALUE));
+	db.tables[tab].rows[i].values =
+	    (VALUE *) G_realloc(db.tables[tab].rows[i].values,
+				db.tables[tab].ncols * sizeof(VALUE));
     }
     }
     return DB_OK;
     return DB_OK;
 }
 }

+ 11 - 12
db/drivers/dbf/create_table.c

@@ -2,27 +2,26 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_create_table (dbTable *table)
+int db__driver_create_table(dbTable * table)
 {
 {
     dbString sql;
     dbString sql;
     int ret;
     int ret;
-    
-    G_debug (3, "db__driver_create_table()");
 
 
-    db_init_string (&sql);
+    G_debug(3, "db__driver_create_table()");
 
 
-    db_table_to_sql ( table, &sql );
+    db_init_string(&sql);
 
 
-    G_debug (3, " SQL: %s", db_get_string(&sql) );
+    db_table_to_sql(table, &sql);
 
 
-    ret = execute ( db_get_string(&sql), NULL);    
+    G_debug(3, " SQL: %s", db_get_string(&sql));
 
 
-    if ( ret == DB_FAILED ) {
-	append_error("Cannot create table" );
-	report_error( );
+    ret = execute(db_get_string(&sql), NULL);
+
+    if (ret == DB_FAILED) {
+	append_error("Cannot create table");
+	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
+
     return DB_OK;
     return DB_OK;
 }
 }

+ 15 - 20
db/drivers/dbf/cursor.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -19,9 +20,7 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_close_cursor (dbCursor *dbc)
-
+int db__driver_close_cursor(dbCursor * dbc)
 {
 {
     cursor *c;
     cursor *c;
 
 
@@ -37,35 +36,31 @@ db__driver_close_cursor (dbCursor *dbc)
 }
 }
 
 
 
 
-cursor * alloc_cursor()
+cursor *alloc_cursor()
 {
 {
-    cursor     *c;
+    cursor *c;
 
 
     /* allocate the cursor */
     /* allocate the cursor */
     c = (cursor *) db_malloc(sizeof(cursor));
     c = (cursor *) db_malloc(sizeof(cursor));
-    if (c == NULL)
-    {
-        append_error ("cannot alloc new cursor");
-        return c; 
-    } 
-    
+    if (c == NULL) {
+	append_error("cannot alloc new cursor");
+	return c;
+    }
+
     /* tokenize it */
     /* tokenize it */
     c->token = db_new_token(c);
     c->token = db_new_token(c);
-    if (c->token < 0)       
-    {
-	free_cursor (c);
+    if (c->token < 0) {
+	free_cursor(c);
 	c = NULL;
 	c = NULL;
-	append_error ("cannot tokenize new cursor\n");
+	append_error("cannot tokenize new cursor\n");
     }
     }
 
 
     return c;
     return c;
 }
 }
 
 
-void free_cursor( cursor *c)
+void free_cursor(cursor * c)
 {
 {
     db_drop_token(c->token);
     db_drop_token(c->token);
-    sqpFreeStmt( c->st );  
-    G_free (c);  
+    sqpFreeStmt(c->st);
+    G_free(c);
 }
 }
-
-

+ 2 - 1
db/drivers/dbf/db.c

@@ -93,7 +93,8 @@ int db__driver_open_database(dbHandle * handle)
 		report_error();
 		report_error();
 		return DB_FAILED;
 		return DB_FAILED;
 	    }
 	    }
-	} else { /* some other problem */
+	}
+	else {			/* some other problem */
 	    append_error("Cannot open dbf database: %s\n", name);
 	    append_error("Cannot open dbf database: %s\n", name);
 	    report_error();
 	    report_error();
 	    return DB_FAILED;
 	    return DB_FAILED;

+ 1 - 0
db/drivers/dbf/dbdriver.h

@@ -16,6 +16,7 @@ int db__driver_fetch();
 int db__driver_get_num_rows();
 int db__driver_get_num_rows();
 int db__driver_list_tables();
 int db__driver_list_tables();
 int db__driver_open_select_cursor();
 int db__driver_open_select_cursor();
+
 #define	init_dbdriver() do{\
 #define	init_dbdriver() do{\
 db_driver_create_table = db__driver_create_table;\
 db_driver_create_table = db__driver_create_table;\
 db_driver_close_cursor = db__driver_close_cursor;\
 db_driver_close_cursor = db__driver_close_cursor;\

Різницю між файлами не показано, бо вона завелика
+ 657 - 594
db/drivers/dbf/dbfexe.c


+ 79 - 88
db/drivers/dbf/describe.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -20,122 +21,112 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_describe_table  (dbString *table_name, dbTable **table)
-
+int db__driver_describe_table(dbString * table_name, dbTable ** table)
 {
 {
-    int         tab;	
-    char        *name;
-
-    name = db_get_string (table_name);
-    
-    tab = find_table ( name );
-    if ( tab == -1 ) {
-	append_error("Table '%s' doesn't exist", db_get_string(table_name)); 
+    int tab;
+    char *name;
+
+    name = db_get_string(table_name);
+
+    tab = find_table(name);
+    if (tab == -1) {
+	append_error("Table '%s' doesn't exist", db_get_string(table_name));
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    describe_table ( tab, NULL, 0, table );
+    describe_table(tab, NULL, 0, table);
 
 
     return DB_OK;
     return DB_OK;
 }
 }
 
 
 /* scols is array of indexes selected columns or null,
 /* scols is array of indexes selected columns or null,
  * if nscols == 0 => describe all columns */
  * if nscols == 0 => describe all columns */
-int
-describe_table (int tab, int *scols, int nscols, dbTable **table)
+int describe_table(int tab, int *scols, int nscols, dbTable ** table)
 {
 {
-    int         i, col, ncols, dbtype, precision, scale;
-    dbColumn    *column;
-    COLUMN      *dcol;
+    int i, col, ncols, dbtype, precision, scale;
+    dbColumn *column;
+    COLUMN *dcol;
 
 
-    load_table_head ( tab );
+    load_table_head(tab);
     ncols = db.tables[tab].ncols;
     ncols = db.tables[tab].ncols;
 
 
-    if ( nscols > 0 )
-        ncols = nscols;
-    
+    if (nscols > 0)
+	ncols = nscols;
+
     if (!(*table = db_alloc_table(ncols))) {
     if (!(*table = db_alloc_table(ncols))) {
-        return DB_FAILED;
+	return DB_FAILED;
     }
     }
-    
-    for (i=0; i < ncols; i++)
-      {
-        if ( nscols > 0 )
+
+    for (i = 0; i < ncols; i++) {
+	if (nscols > 0)
 	    col = scols[i];
 	    col = scols[i];
-        else
+	else
 	    col = i;
 	    col = i;
 
 
 	dcol = &(db.tables[tab].cols[col]);
 	dcol = &(db.tables[tab].cols[col]);
-        column = db_get_table_column (*table, i);
-	
-        db_set_column_name (column, dcol->name);
-        db_set_column_length (column, dcol->width);
-	db_set_column_host_type (column, dcol->type);
-	
-        switch ( dcol->type )
-          {  
-            case DBF_INT:
-                dbtype = DB_SQL_TYPE_INTEGER;
-                precision = dcol->width - 1;  /* one char for sign */
-                scale = 0;  
-		break;
-            case DBF_DOUBLE:
-                dbtype = DB_SQL_TYPE_DOUBLE_PRECISION;
-                precision = dcol->width - 2;  /* one char for sign second for decimal point */
-                scale = dcol->decimals;  
-                break;
-            case DBF_CHAR:
-                dbtype = DB_SQL_TYPE_CHARACTER;
-                precision = 0;
-                scale = 0; 
-	        break;
-
-            default:
-                dbtype = DB_SQL_TYPE_UNKNOWN;
-                break;
-          }
-
-        db_set_column_sqltype (column, dbtype);
+	column = db_get_table_column(*table, i);
+
+	db_set_column_name(column, dcol->name);
+	db_set_column_length(column, dcol->width);
+	db_set_column_host_type(column, dcol->type);
+
+	switch (dcol->type) {
+	case DBF_INT:
+	    dbtype = DB_SQL_TYPE_INTEGER;
+	    precision = dcol->width - 1;	/* one char for sign */
+	    scale = 0;
+	    break;
+	case DBF_DOUBLE:
+	    dbtype = DB_SQL_TYPE_DOUBLE_PRECISION;
+	    precision = dcol->width - 2;	/* one char for sign second for decimal point */
+	    scale = dcol->decimals;
+	    break;
+	case DBF_CHAR:
+	    dbtype = DB_SQL_TYPE_CHARACTER;
+	    precision = 0;
+	    scale = 0;
+	    break;
+
+	default:
+	    dbtype = DB_SQL_TYPE_UNKNOWN;
+	    break;
+	}
+
+	db_set_column_sqltype(column, dbtype);
 
 
 	/* precision is number of digits */
 	/* precision is number of digits */
-        db_set_column_precision (column, precision);
+	db_set_column_precision(column, precision);
 	/* scale is number of digits to the right of decimal point */
 	/* scale is number of digits to the right of decimal point */
-	db_set_column_scale (column, scale);
-	
-	db_set_column_null_allowed (column);
-        db_set_column_has_undefined_default_value(column);
-        db_unset_column_use_default_value(column);
-	
-        db_set_column_select_priv_granted (column);
-	
-        if ( db.tables[tab].write )
-            db_set_column_update_priv_granted (column);
+	db_set_column_scale(column, scale);
+
+	db_set_column_null_allowed(column);
+	db_set_column_has_undefined_default_value(column);
+	db_unset_column_use_default_value(column);
+
+	db_set_column_select_priv_granted(column);
+
+	if (db.tables[tab].write)
+	    db_set_column_update_priv_granted(column);
 	else
 	else
-            db_set_column_update_priv_not_granted (column); 
+	    db_set_column_update_priv_not_granted(column);
+
+
+    }
 
 
-	
-      }
-    
     /* set the table name */
     /* set the table name */
-    db_set_table_name (*table, db.tables[tab].name);
+    db_set_table_name(*table, db.tables[tab].name);
 
 
     /* set the table description */
     /* set the table description */
     db_set_table_description(*table, "");
     db_set_table_description(*table, "");
-    
-    if ( db.tables[tab].write )
-      {	    
-        db_set_table_delete_priv_granted (*table);
-        db_set_table_insert_priv_granted (*table);
-      } 
-    else 
-      {
-        db_set_table_delete_priv_not_granted (*table);
-        db_set_table_insert_priv_not_granted (*table);
-      }
-	
-    return DB_OK;
-}
-
 
 
+    if (db.tables[tab].write) {
+	db_set_table_delete_priv_granted(*table);
+	db_set_table_insert_priv_granted(*table);
+    }
+    else {
+	db_set_table_delete_priv_not_granted(*table);
+	db_set_table_insert_priv_not_granted(*table);
+    }
 
 
+    return DB_OK;
+}

+ 3 - 5
db/drivers/dbf/driver.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -17,15 +18,12 @@
 #include <grass/dbmi.h>
 #include <grass/dbmi.h>
 #include "globals.h"
 #include "globals.h"
 
 
-int
-db__driver_init  (int argc, char *argv[])
-
+int db__driver_init(int argc, char *argv[])
 {
 {
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_finish()
+int db__driver_finish()
 {
 {
     return DB_OK;
     return DB_OK;
 }
 }

+ 18 - 22
db/drivers/dbf/error.c

@@ -6,44 +6,40 @@
 #include "globals.h"
 #include "globals.h"
 
 
 /* init error message */
 /* init error message */
-void
-init_error ( void )
+void init_error(void)
 {
 {
-    if ( !errMsg ) {
+    if (!errMsg) {
 	errMsg = (dbString *) G_malloc(sizeof(dbString));
 	errMsg = (dbString *) G_malloc(sizeof(dbString));
-        db_init_string (errMsg);
+	db_init_string(errMsg);
     }
     }
 
 
-    db_set_string ( errMsg, "DBMI-DBF driver error:\n");
+    db_set_string(errMsg, "DBMI-DBF driver error:\n");
 }
 }
 
 
 /* append error message */
 /* append error message */
-void
-append_error ( const char *fmt, ...)
+void append_error(const char *fmt, ...)
 {
 {
     FILE *fp = NULL;
     FILE *fp = NULL;
     char *work = NULL;
     char *work = NULL;
     int count = 0;
     int count = 0;
     va_list ap;
     va_list ap;
 
 
-    va_start (ap, fmt);
+    va_start(ap, fmt);
     if ((fp = tmpfile())) {
     if ((fp = tmpfile())) {
-        count = vfprintf (fp, fmt, ap);
-        if (count >= 0 && (work = G_calloc (count+1,1)) ) {
-	    rewind (fp);
-	    fread (work, 1, count, fp);
-	    db_append_string ( errMsg, work);
-	    G_free (work);
-        }
-        fclose (fp);
+	count = vfprintf(fp, fmt, ap);
+	if (count >= 0 && (work = G_calloc(count + 1, 1))) {
+	    rewind(fp);
+	    fread(work, 1, count, fp);
+	    db_append_string(errMsg, work);
+	    G_free(work);
+	}
+	fclose(fp);
     }
     }
-    va_end (ap);
+    va_end(ap);
 }
 }
 
 
-void
-report_error ( void )
+void report_error(void)
 {
 {
-    db_append_string ( errMsg, "\n");
-    db_error ( db_get_string (errMsg) );
+    db_append_string(errMsg, "\n");
+    db_error(db_get_string(errMsg));
 }
 }
-

+ 13 - 17
db/drivers/dbf/execute.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -17,25 +18,20 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_execute_immediate  (dbString *sql)
-
+int db__driver_execute_immediate(dbString * sql)
 {
 {
     char *s;
     char *s;
-    int  ret;
+    int ret;
 
 
-    s = db_get_string (sql);
-    
-    ret = execute ( s, NULL);
-    
-    if ( ret == DB_FAILED )
-      {
-         append_error("Error in db_execute_immediate()");
-         report_error( );
-         return DB_FAILED;
-      }
-    
-    return DB_OK;
-}
+    s = db_get_string(sql);
 
 
+    ret = execute(s, NULL);
 
 
+    if (ret == DB_FAILED) {
+	append_error("Error in db_execute_immediate()");
+	report_error();
+	return DB_FAILED;
+    }
+
+    return DB_OK;
+}

+ 43 - 49
db/drivers/dbf/fetch.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -15,81 +16,77 @@
 *****************************************************************************/
 *****************************************************************************/
 #include <grass/dbmi.h>
 #include <grass/dbmi.h>
 #include "globals.h"
 #include "globals.h"
-#include "proto.h" 
-
-int
-db__driver_fetch (dbCursor *cn, int position, int *more)
+#include "proto.h"
 
 
+int db__driver_fetch(dbCursor * cn, int position, int *more)
 {
 {
-    cursor     *c;
-    dbToken    token;    
-    dbTable    *table;
-    dbColumn   *column;
-    dbValue    *value;
-    int        col, ncols;
-    int        htype, sqltype, ctype;
-    int  dbfrow, dbfcol;
+    cursor *c;
+    dbToken token;
+    dbTable *table;
+    dbColumn *column;
+    dbValue *value;
+    int col, ncols;
+    int htype, sqltype, ctype;
+    int dbfrow, dbfcol;
 
 
     /* get cursor token */
     /* get cursor token */
     token = db_get_cursor_token(cn);
     token = db_get_cursor_token(cn);
 
 
     /* get the cursor by its token */
     /* get the cursor by its token */
-    if (!(c = (cursor *) db_find_token(token))) 
-    {
+    if (!(c = (cursor *) db_find_token(token))) {
 	db_error("cursor not found");
 	db_error("cursor not found");
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     /* fetch on position */
     /* fetch on position */
-    switch (position)
-    { 
+    switch (position) {
     case DB_NEXT:
     case DB_NEXT:
-	c->cur++;    
+	c->cur++;
 	break;
 	break;
     case DB_CURRENT:
     case DB_CURRENT:
 	break;
 	break;
     case DB_PREVIOUS:
     case DB_PREVIOUS:
-	c->cur--;    
-    	break;
+	c->cur--;
+	break;
     case DB_FIRST:
     case DB_FIRST:
-	c->cur = 0;    
+	c->cur = 0;
 	break;
 	break;
     case DB_LAST:
     case DB_LAST:
 	c->cur = c->nrows - 1;
 	c->cur = c->nrows - 1;
 	break;
 	break;
     };
     };
 
 
-    if ( (c->cur >= c->nrows) || (c->cur < 0) )
-    {
+    if ((c->cur >= c->nrows) || (c->cur < 0)) {
 	*more = 0;
 	*more = 0;
 	return DB_OK;
 	return DB_OK;
     }
     }
     *more = 1;
     *more = 1;
 
 
-    
+
     /* get the data out of the descriptor into the table */
     /* get the data out of the descriptor into the table */
     table = db_get_cursor_table(cn);
     table = db_get_cursor_table(cn);
-    ncols = db_get_table_number_of_columns (table);
+    ncols = db_get_table_number_of_columns(table);
     dbfrow = c->set[c->cur];
     dbfrow = c->set[c->cur];
-    for (col = 1; col <= ncols; col++)
-    {
-        dbfcol = c->cols[col-1];
-	column = db_get_table_column (table, col-1);
-	value  = db_get_column_value (column);
-	db_free_string (&value->s);
-	
+    for (col = 1; col <= ncols; col++) {
+	dbfcol = c->cols[col - 1];
+	column = db_get_table_column(table, col - 1);
+	value = db_get_column_value(column);
+	db_free_string(&value->s);
+
 	sqltype = db_get_column_sqltype(column);
 	sqltype = db_get_column_sqltype(column);
-	ctype   = db_sqltype_to_Ctype(sqltype);
-	htype   = db_get_column_host_type(column);
-	
-	if ( db.tables[c->table].rows[dbfrow].values[dbfcol].is_null ) {
-	    db_set_value_null ( value ) ;
-	} else {
-	    db_set_value_not_null ( value ) ;
-	    switch (ctype)
-	    {
+	ctype = db_sqltype_to_Ctype(sqltype);
+	htype = db_get_column_host_type(column);
+
+	if (db.tables[c->table].rows[dbfrow].values[dbfcol].is_null) {
+	    db_set_value_null(value);
+	}
+	else {
+	    db_set_value_not_null(value);
+	    switch (ctype) {
 	    case DB_C_TYPE_STRING:
 	    case DB_C_TYPE_STRING:
-		    db_set_string ( &(value->s), db.tables[c->table].rows[dbfrow].values[dbfcol].c);
+		db_set_string(&(value->s),
+			      db.tables[c->table].rows[dbfrow].values[dbfcol].
+			      c);
 		break;
 		break;
 	    case DB_C_TYPE_INT:
 	    case DB_C_TYPE_INT:
 		value->i = db.tables[c->table].rows[dbfrow].values[dbfcol].i;
 		value->i = db.tables[c->table].rows[dbfrow].values[dbfcol].i;
@@ -103,23 +100,20 @@ db__driver_fetch (dbCursor *cn, int position, int *more)
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_get_num_rows  (dbCursor *cn)
-
+int db__driver_get_num_rows(dbCursor * cn)
 {
 {
-    cursor     *c;
-    dbToken    token;    
+    cursor *c;
+    dbToken token;
 
 
     /* get cursor token */
     /* get cursor token */
     token = db_get_cursor_token(cn);
     token = db_get_cursor_token(cn);
 
 
     /* get the cursor by its token */
     /* get the cursor by its token */
-    if (!(c = (cursor *) db_find_token(token))) 
-    {
+    if (!(c = (cursor *) db_find_token(token))) {
 	db_error("cursor not found");
 	db_error("cursor not found");
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    return ( c->nrows );
+    return (c->nrows);
 
 
 }
 }

+ 58 - 53
db/drivers/dbf/globals.h

@@ -1,82 +1,87 @@
 #include <grass/sqlp.h>
 #include <grass/sqlp.h>
 
 
-#define DBF_COL_NAME 20 /* maximum column name (in fact shouldn't be > 10) */
+#define DBF_COL_NAME 20		/* maximum column name (in fact shouldn't be > 10) */
 
 
 /* 
 /* 
-* DBF API:      http://shapelib.maptools.org/dbf_api.html
-*            or ../../../lib/external/shapelib/shapefil.h
-*
-* DBFFieldType: FTString, FTInteger, FTDouble, FTLogical, FTInvalid
-*                  0          1          2         4         5
-*                DBF_CHAR   DBF_INT   DBF_DOUBLE
-*                  1          2          3
-*/
+ * DBF API:      http://shapelib.maptools.org/dbf_api.html
+ *            or ../../../lib/external/shapelib/shapefil.h
+ *
+ * DBFFieldType: FTString, FTInteger, FTDouble, FTLogical, FTInvalid
+ *                  0          1          2         4         5
+ *                DBF_CHAR   DBF_INT   DBF_DOUBLE
+ *                  1          2          3
+ */
 #define DBF_CHAR   1
 #define DBF_CHAR   1
 #define DBF_INT    2
 #define DBF_INT    2
 #define DBF_DOUBLE 3
 #define DBF_DOUBLE 3
 
 
-typedef struct {
-    char name[DBF_COL_NAME]; 
-    int  type; 
-    int  width; 
-    int  decimals; 
+typedef struct
+{
+    char name[DBF_COL_NAME];
+    int type;
+    int width;
+    int decimals;
 } COLUMN;
 } COLUMN;
 
 
-typedef struct {
-    char  *c; 
-    int    i; 
-    double d; 
-    int    is_null;
+typedef struct
+{
+    char *c;
+    int i;
+    double d;
+    int is_null;
 } VALUE;
 } VALUE;
 
 
-typedef struct {
-    int   alive; 
+typedef struct
+{
+    int alive;
     VALUE *values;
     VALUE *values;
 } ROW;
 } ROW;
 
 
-typedef struct {
-    char  name[1024]; /* table name (without .dbf) */
-    char  file[1024]; /* full path to file (including .dbf) */
-    int   read;       /* TRUE if user has read access to the file */  
-    int   write;      /* TRUE if user has write access to the file */
-    int   alive; 
-    int   described;  /* columns definitions were loaded to heads */
-    int   loaded;     /* data were loaded to rows */
-    int   updated;     
+typedef struct
+{
+    char name[1024];		/* table name (without .dbf) */
+    char file[1024];		/* full path to file (including .dbf) */
+    int read;			/* TRUE if user has read access to the file */
+    int write;			/* TRUE if user has write access to the file */
+    int alive;
+    int described;		/* columns definitions were loaded to heads */
+    int loaded;			/* data were loaded to rows */
+    int updated;
     COLUMN *cols;
     COLUMN *cols;
-    ROW  *rows;
-    int   acols;      /* allocated columns */
-    int   ncols;      /* number of columns */
-    int   arows;      /* allocated rows */
-    int   nrows;      /* number of rows */
+    ROW *rows;
+    int acols;			/* allocated columns */
+    int ncols;			/* number of columns */
+    int arows;			/* allocated rows */
+    int nrows;			/* number of rows */
 } TABLE;
 } TABLE;
 
 
-typedef struct {
-    char   name[1024]; /* db name = full path to db dir */
+typedef struct
+{
+    char name[1024];		/* db name = full path to db dir */
     TABLE *tables;
     TABLE *tables;
-    int    atables;    /* allocated space for tables */
-    int    ntables;    /* number of tables */
+    int atables;		/* allocated space for tables */
+    int ntables;		/* number of tables */
 } DATABASE;
 } DATABASE;
 
 
 /* cursors */
 /* cursors */
-typedef struct {
+typedef struct
+{
     SQLPSTMT *st;
     SQLPSTMT *st;
-    int table;   /* table */
-    int *set;    /* array of indexes to table for selected rows */
-    int nrows;   /* number of rows in set */
-    int cur;     /* position of cursor */
-    int *cols;   /* array of indexes of selected columns */
+    int table;			/* table */
+    int *set;			/* array of indexes to table for selected rows */
+    int nrows;			/* number of rows in set */
+    int cur;			/* position of cursor */
+    int *cols;			/* array of indexes of selected columns */
     int ncols;
     int ncols;
     dbToken token;
     dbToken token;
-    int type;    /* type of cursor: SELECT, UPDATE, INSERT */
-    int *order;  /* array of row indexes (sorted by ORDER BY) */
+    int type;			/* type of cursor: SELECT, UPDATE, INSERT */
+    int *order;			/* array of row indexes (sorted by ORDER BY) */
 } cursor;
 } cursor;
 
 
 #ifdef MAIN
 #ifdef MAIN
-    DATABASE db;
-    dbString *errMsg = NULL;
+DATABASE db;
+dbString *errMsg = NULL;
 #else
 #else
-    extern DATABASE db;
-    extern dbString *errMsg;
-#endif 
-
+extern DATABASE db;
+extern dbString *errMsg;
+#endif

+ 11 - 15
db/drivers/dbf/listtab.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -18,31 +19,26 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_list_tables  (dbString **tlist, int *tcount, int system)
-
+int db__driver_list_tables(dbString ** tlist, int *tcount, int system)
 {
 {
-    dbString 	*list;
-    int 	i;
-    
+    dbString *list;
+    int i;
+
     *tlist = NULL;
     *tlist = NULL;
     *tcount = 0;
     *tcount = 0;
 
 
-    list = db_alloc_string_array( db.ntables );
+    list = db_alloc_string_array(db.ntables);
     if (list == NULL && db.ntables > 0)
     if (list == NULL && db.ntables > 0)
 	return DB_FAILED;
 	return DB_FAILED;
 
 
-    for ( i = 0; i < db.ntables; i++ )  
-      {
-    	if(db_set_string (&list[i], (char *) db.tables[i].name) != DB_OK)
-	  {
+    for (i = 0; i < db.ntables; i++) {
+	if (db_set_string(&list[i], (char *)db.tables[i].name) != DB_OK) {
 	    return DB_FAILED;
 	    return DB_FAILED;
-	  }	
-      }
-    						
+	}
+    }
+
 
 
     *tlist = list;
     *tlist = list;
     *tcount = db.ntables;
     *tcount = db.ntables;
     return DB_OK;
     return DB_OK;
 }
 }
-

+ 9 - 9
db/drivers/dbf/main.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -22,8 +23,7 @@
 #include "proto.h"
 #include "proto.h"
 #include "dbdriver.h"
 #include "dbdriver.h"
 
 
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
 {
     char *name;
     char *name;
 
 
@@ -32,17 +32,17 @@ main(int argc, char *argv[])
 
 
     /* Do not call G_getenv() nor other functions reading GISRC here! It may be that grass variables are
     /* Do not call G_getenv() nor other functions reading GISRC here! It may be that grass variables are
      * not available here, but will be set in db_driver() */
      * not available here, but will be set in db_driver() */
-    
+
     /* Set pointer to driver name */
     /* Set pointer to driver name */
-    name = argv[0] + strlen ( argv[0] );
-    
-    while ( name > argv[0] ) {
-        if ( name[0] == '/' ) {
+    name = argv[0] + strlen(argv[0]);
+
+    while (name > argv[0]) {
+	if (name[0] == '/') {
 	    name++;
 	    name++;
 	    break;
 	    break;
 	}
 	}
 	name--;
 	name--;
     }
     }
-    
-    exit (db_driver (argc, argv));
+
+    exit(db_driver(argc, argv));
 }
 }

+ 46 - 51
db/drivers/dbf/main_debug.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 * just a test for debugging purpose, imitating dbf driver -a.sh.
 * just a test for debugging purpose, imitating dbf driver -a.sh.
 *****************************************************************************/
 *****************************************************************************/
@@ -8,14 +9,13 @@
 #include <grass/gis.h>
 #include <grass/gis.h>
 #include "globals.h"
 #include "globals.h"
 
 
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
 {
 
 
     dbCursor *cursor;
     dbCursor *cursor;
     int stat;
     int stat;
     dbToken token;
     dbToken token;
-    dbString * select;
+    dbString *select;
     int mode = 0;
     int mode = 0;
     dbDriver *driver;
     dbDriver *driver;
     dbHandle handle;
     dbHandle handle;
@@ -24,90 +24,85 @@ main(int argc, char *argv[])
     if (driver == NULL)
     if (driver == NULL)
 	exit(-1);
 	exit(-1);
 
 
-    db_init_handle (&handle);
-    db_set_handle (&handle, "dbf_catalog", NULL);
+    db_init_handle(&handle);
+    db_set_handle(&handle, "dbf_catalog", NULL);
 
 
-    if (db__test_database_open())
-    {
-	db_error ("Multiple open databases not allowed");
+    if (db__test_database_open()) {
+	db_error("Multiple open databases not allowed");
 	return DB_OK;
 	return DB_OK;
     }
     }
 
 
-/* call the procedure */
-    stat = db_driver_open_database (&handle);
+    /* call the procedure */
+    stat = db_driver_open_database(&handle);
 
 
-/* send the return code */
-    if (stat != DB_OK)
-    {
-	db_free_handle (&handle);
+    /* send the return code */
+    if (stat != DB_OK) {
+	db_free_handle(&handle);
 	return DB_OK;
 	return DB_OK;
     }
     }
 
 
-/* record the open in the driver state */
-    db__mark_database_open (
-    	db_get_handle_dbname (&handle),
-    	db_get_handle_dbpath (&handle));
-/* DO NOT free the handle since we saved the pointers to the name,path */
-    
-    select = (dbString *) G_malloc (1024);
+    /* record the open in the driver state */
+    db__mark_database_open(db_get_handle_dbname(&handle),
+			   db_get_handle_dbpath(&handle));
+    /* DO NOT free the handle since we saved the pointers to the name,path */
+
+    select = (dbString *) G_malloc(1024);
 
 
-    db_init_string (select);
+    db_init_string(select);
 
 
     db_set_string(select,
     db_set_string(select,
-"select id, quality, flow from river where (flow = 10) or (flow = 20) or (flow = 30) or (flow = 5) or (flow = 7)");
+		  "select id, quality, flow from river where (flow = 10) or (flow = 20) or (flow = 30) or (flow = 5) or (flow = 7)");
 
 
-/* create a cursor */
-    cursor = (dbCursor *) db_malloc (sizeof(dbCursor));
+    /* create a cursor */
+    cursor = (dbCursor *) db_malloc(sizeof(dbCursor));
     if (cursor == NULL)
     if (cursor == NULL)
 	return db_get_error_code();
 	return db_get_error_code();
-    token = db_new_token ( (dbAddress) cursor);
+    token = db_new_token((dbAddress) cursor);
     if (token < 0)
     if (token < 0)
 	return db_get_error_code();
 	return db_get_error_code();
     db_init_cursor(cursor);
     db_init_cursor(cursor);
     cursor->driver = driver;
     cursor->driver = driver;
 
 
-G_debug(3, "sql is %s", *select);
-G_debug(3, "driver is %s", cursor->driver);
+    G_debug(3, "sql is %s", *select);
+    G_debug(3, "driver is %s", cursor->driver);
 
 
-/* call the procedure */
-    stat = db_driver_open_select_cursor (select, cursor, mode);
-    db_free_string (select);
+    /* call the procedure */
+    stat = db_driver_open_select_cursor(select, cursor, mode);
+    db_free_string(select);
 
 
-/* mark this as a readonly cursor */
-    db_set_cursor_type_readonly (cursor);
+    /* mark this as a readonly cursor */
+    db_set_cursor_type_readonly(cursor);
 
 
-/* add this cursor to the cursors managed by the driver state */
+    /* add this cursor to the cursors managed by the driver state */
     db__add_cursor_to_driver_state(cursor);
     db__add_cursor_to_driver_state(cursor);
-G_debug(3, "db_d_close_database()");
+    G_debug(3, "db_d_close_database()");
 
 
-/* see if a database is open */
-    if (!db__test_database_open())
-    {
-	db_error ("no database is open");
-G_debug(3, "db_d_close_database(): would sent DB_FAILURE");
+    /* see if a database is open */
+    if (!db__test_database_open()) {
+	db_error("no database is open");
+	G_debug(3, "db_d_close_database(): would sent DB_FAILURE");
 	return DB_OK;
 	return DB_OK;
     };
     };
-/* make sure all cursors are closed */
+    /* make sure all cursors are closed */
     db__close_all_cursors();
     db__close_all_cursors();
 
 
-/* call the procedure */
+    /* call the procedure */
     stat = db_driver_close_database();
     stat = db_driver_close_database();
-G_debug(3, "db_d_close_database(): would have stat = %d", stat);
+    G_debug(3, "db_d_close_database(): would have stat = %d", stat);
 
 
-/* send the return code */
-    if (stat != DB_OK)
-    {
-G_debug(3, "db_d_close_database(): would sent DB_FAILURE");
+    /* send the return code */
+    if (stat != DB_OK) {
+	G_debug(3, "db_d_close_database(): would sent DB_FAILURE");
 	return DB_OK;
 	return DB_OK;
     }
     }
-G_debug(3, "db_d_close_database(): would sent DB_OK");
+    G_debug(3, "db_d_close_database(): would sent DB_OK");
 
 
-/* clear the driver state */
-    db__mark_database_closed ();
+    /* clear the driver state */
+    db__mark_database_closed();
     db__init_driver_state();
     db__init_driver_state();
 
 
 
 
-G_debug(3, "main(): ok");
+    G_debug(3, "main(): ok");
     return DB_OK;
     return DB_OK;
 
 
 }
 }

+ 17 - 19
db/drivers/dbf/proto.h

@@ -1,25 +1,23 @@
 /* error.c */
 /* error.c */
-void init_error ( void );
-void append_error ( const char *fmt, ...);
-void report_error ( void );
+void init_error(void);
+void append_error(const char *fmt, ...);
+void report_error(void);
 
 
-int save_string (VALUE *, char *);
+int save_string(VALUE *, char *);
 
 
-cursor * alloc_cursor ();
-void free_cursor ( cursor * );
+cursor *alloc_cursor();
+void free_cursor(cursor *);
 
 
 /* column.c */
 /* column.c */
-int add_column ( int table, int type,  char *name , int width, int decimals );
-int find_column ( int, char * );
-int drop_column ( int, char * );
-
-int add_table ( char *, char *);
-int execute ( char *, cursor *);
-int free_table ( int );
-int find_table ( char * );
-int load_table_head( int );
-int load_table( int );
-int save_table( int );
-int describe_table( int, int *, int, dbTable ** );
-
+int add_column(int table, int type, char *name, int width, int decimals);
+int find_column(int, char *);
+int drop_column(int, char *);
 
 
+int add_table(char *, char *);
+int execute(char *, cursor *);
+int free_table(int);
+int find_table(char *);
+int load_table_head(int);
+int load_table(int);
+int save_table(int);
+int describe_table(int, int *, int, dbTable **);

+ 19 - 23
db/drivers/dbf/select.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -19,43 +20,38 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_open_select_cursor (dbString *sel, dbCursor *dbc, int mode)
-
+int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode)
 {
 {
-    int ret;	
-    cursor      *c;
-    char        *sql;      
-    dbTable     *table;   
-    
+    int ret;
+    cursor *c;
+    char *sql;
+    dbTable *table;
+
     /* allocate cursor */
     /* allocate cursor */
     c = alloc_cursor();
     c = alloc_cursor();
     if (c == NULL)
     if (c == NULL)
 	return DB_FAILED;
 	return DB_FAILED;
 
 
-    db_set_cursor_mode(dbc,mode);
+    db_set_cursor_mode(dbc, mode);
     db_set_cursor_type_readonly(dbc);
     db_set_cursor_type_readonly(dbc);
 
 
     sql = db_get_string(sel);
     sql = db_get_string(sel);
-    
-    ret = execute ( sql, c); 
-		    
-    if ( ret == DB_FAILED )
-      {
-        append_error("Error in db_open_select_cursor()");
-	report_error( );
+
+    ret = execute(sql, c);
+
+    if (ret == DB_FAILED) {
+	append_error("Error in db_open_select_cursor()");
+	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
-      } 
-		
-    describe_table( c->table, c->cols, c->ncols, &table);
-    
+    }
+
+    describe_table(c->table, c->cols, c->ncols, &table);
+
     /* record table with dbCursor */
     /* record table with dbCursor */
     db_set_cursor_table(dbc, table);
     db_set_cursor_table(dbc, table);
-    
+
     /* set dbCursor's token for my cursor */
     /* set dbCursor's token for my cursor */
     db_set_cursor_token(dbc, c->token);
     db_set_cursor_token(dbc, c->token);
 
 
     return DB_OK;
     return DB_OK;
 }
 }
-
-

+ 5 - 6
db/drivers/dbf/str.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -20,16 +21,14 @@
 #include "globals.h"
 #include "globals.h"
 
 
 /* save string to value */
 /* save string to value */
-int save_string ( VALUE *val, char *c )
+int save_string(VALUE * val, char *c)
 {
 {
     int len;
     int len;
 
 
-    len = strlen ( c ) + 1;
-    val->c = (char *) G_realloc (val->c, len);
+    len = strlen(c) + 1;
+    val->c = (char *)G_realloc(val->c, len);
 
 
-    strcpy ( val->c, c );
+    strcpy(val->c, c);
 
 
     return (1);
     return (1);
 }
 }
-    
-

+ 208 - 221
db/drivers/dbf/table.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       DBF driver 
 * MODULE:       DBF driver 
@@ -34,23 +35,23 @@
 #include <grass/shapefil.h>
 #include <grass/shapefil.h>
 #include <grass/gis.h>
 #include <grass/gis.h>
 #include "globals.h"
 #include "globals.h"
-#include "proto.h" 
+#include "proto.h"
 
 
 /* add table to database */
 /* add table to database */
-int add_table (char *table, char *name)
+int add_table(char *table, char *name)
 {
 {
-    G_debug (2, "add_table(): table = %s name = %s", table, name );
-
-    if ( db.atables == db.ntables )
-      {
-        db.atables += 15; 
-	db.tables = (TABLE *) G_realloc ( db.tables, db.atables * sizeof (TABLE) ); 
-      }
-    
-    strcpy ( db.tables[db.ntables].name, table );
-    
-    sprintf ( db.tables[db.ntables].file, "%s/%s", db.name, name );
-    
+    G_debug(2, "add_table(): table = %s name = %s", table, name);
+
+    if (db.atables == db.ntables) {
+	db.atables += 15;
+	db.tables =
+	    (TABLE *) G_realloc(db.tables, db.atables * sizeof(TABLE));
+    }
+
+    strcpy(db.tables[db.ntables].name, table);
+
+    sprintf(db.tables[db.ntables].file, "%s/%s", db.name, name);
+
     db.tables[db.ntables].alive = TRUE;
     db.tables[db.ntables].alive = TRUE;
     db.tables[db.ntables].described = FALSE;
     db.tables[db.ntables].described = FALSE;
     db.tables[db.ntables].loaded = FALSE;
     db.tables[db.ntables].loaded = FALSE;
@@ -62,280 +63,266 @@ int add_table (char *table, char *name)
     db.tables[db.ntables].arows = 0;
     db.tables[db.ntables].arows = 0;
     db.tables[db.ntables].nrows = 0;
     db.tables[db.ntables].nrows = 0;
 
 
-    db.ntables++ ;
-    
+    db.ntables++;
+
     return DB_OK;
     return DB_OK;
 }
 }
 
 
 
 
 /* returns table index or -1 */
 /* returns table index or -1 */
-int find_table (char *table)
+int find_table(char *table)
 {
 {
     int i;
     int i;
 
 
-    G_debug ( 2, "find_table(): table = %s", table );
-	
-    for ( i = 0; i < db.ntables; i++ ) {
-         G_debug ( 2, "  ? %s", db.tables[i].name );
-         if ( G_strcasecmp( db.tables[i].name, table ) == 0 )
-	     return (i);   
-   }
-    
+    G_debug(2, "find_table(): table = %s", table);
+
+    for (i = 0; i < db.ntables; i++) {
+	G_debug(2, "  ? %s", db.tables[i].name);
+	if (G_strcasecmp(db.tables[i].name, table) == 0)
+	    return (i);
+    }
+
     return (-1);
     return (-1);
 }
 }
 
 
-int
-load_table_head( int t)
+int load_table_head(int t)
 {
 {
-    int  i, ncol, dtype, type, width, decimals;
-    DBFHandle   dbf;
+    int i, ncol, dtype, type, width, decimals;
+    DBFHandle dbf;
     char fname[20];
     char fname[20];
 
 
-    G_debug ( 2, "load_table_head(): tab = %d, %s", t, db.tables[t].file);
+    G_debug(2, "load_table_head(): tab = %d, %s", t, db.tables[t].file);
+
+    if (db.tables[t].described == TRUE)	/*already described */
+	return DB_OK;
 
 
-    if ( db.tables[t].described == TRUE ) /*already described */
-        return DB_OK;
-     
-    if ( access( db.tables[t].file, R_OK ) == 0 )
+    if (access(db.tables[t].file, R_OK) == 0)
 	db.tables[t].read = TRUE;
 	db.tables[t].read = TRUE;
     else
     else
-        db.tables[t].read = FALSE;
-    
-    if ( access( db.tables[t].file, W_OK ) == 0 )
+	db.tables[t].read = FALSE;
+
+    if (access(db.tables[t].file, W_OK) == 0)
 	db.tables[t].write = TRUE;
 	db.tables[t].write = TRUE;
     else
     else
-        db.tables[t].write = FALSE;
-    
+	db.tables[t].write = FALSE;
+
     /* load */
     /* load */
-    dbf = DBFOpen( db.tables[t].file, "r" );
-    if( dbf == NULL ) {
+    dbf = DBFOpen(db.tables[t].file, "r");
+    if (dbf == NULL) {
 	append_error("Cannot open dbf file.\n");
 	append_error("Cannot open dbf file.\n");
-        return DB_FAILED;
+	return DB_FAILED;
     }
     }
 
 
     ncol = DBFGetFieldCount(dbf);
     ncol = DBFGetFieldCount(dbf);
-    G_debug ( 2, "  ncols = %d", ncol);
-
-    for( i = 0; i < ncol; i++ )
-      {
-         dtype = DBFGetFieldInfo( dbf, i, fname, &width, &decimals );
-         G_debug ( 2, "  DBFFieldType %d", dtype);
-	 
-	 switch ( dtype )
-	   {
-             case FTString:
-		 type = DBF_CHAR;    
-                 break;
-             case FTInteger:
-		 type = DBF_INT;    
-                 break;
-             case FTDouble:
-		 type = DBF_DOUBLE;    
-                 break;
-	     case FTInvalid:
-	    	 G_warning ("invalid/unsupported DBFFieldType");
-		 break;
-	     default:
-	      	 G_warning ("unknown DBFFieldType");
-	      	 break;
-	   }
-	 
-	 add_column ( t, type, fname, width, decimals);  
-      }
-    
-    DBFClose ( dbf );
+    G_debug(2, "  ncols = %d", ncol);
+
+    for (i = 0; i < ncol; i++) {
+	dtype = DBFGetFieldInfo(dbf, i, fname, &width, &decimals);
+	G_debug(2, "  DBFFieldType %d", dtype);
+
+	switch (dtype) {
+	case FTString:
+	    type = DBF_CHAR;
+	    break;
+	case FTInteger:
+	    type = DBF_INT;
+	    break;
+	case FTDouble:
+	    type = DBF_DOUBLE;
+	    break;
+	case FTInvalid:
+	    G_warning("invalid/unsupported DBFFieldType");
+	    break;
+	default:
+	    G_warning("unknown DBFFieldType");
+	    break;
+	}
+
+	add_column(t, type, fname, width, decimals);
+    }
+
+    DBFClose(dbf);
     db.tables[t].described = TRUE;
     db.tables[t].described = TRUE;
-	
+
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-load_table ( int t)
+int load_table(int t)
 {
 {
-    int  i, j, ncols, nrows, dbfcol;
-    DBFHandle   dbf;
+    int i, j, ncols, nrows, dbfcol;
+    DBFHandle dbf;
     char *buf;
     char *buf;
-    ROW  *rows;
+    ROW *rows;
     VALUE *val;
     VALUE *val;
 
 
-    G_debug ( 2, "load_table(): tab = %d", t);
-    
-    if ( db.tables[t].loaded == TRUE ) /*already loaded */
-        return DB_OK;
-    
-    dbf = DBFOpen( db.tables[t].file, "r" );
-    if( dbf == NULL ) {
+    G_debug(2, "load_table(): tab = %d", t);
+
+    if (db.tables[t].loaded == TRUE)	/*already loaded */
+	return DB_OK;
+
+    dbf = DBFOpen(db.tables[t].file, "r");
+    if (dbf == NULL) {
 	append_error("Cannot open dbf file.\n");
 	append_error("Cannot open dbf file.\n");
-        return DB_FAILED;
+	return DB_FAILED;
     }
     }
 
 
     ncols = db.tables[t].ncols;
     ncols = db.tables[t].ncols;
-    nrows = DBFGetRecordCount( dbf );
+    nrows = DBFGetRecordCount(dbf);
     rows = db.tables[t].rows;
     rows = db.tables[t].rows;
-    rows = (ROW *) G_malloc ( nrows * sizeof(ROW) );
+    rows = (ROW *) G_malloc(nrows * sizeof(ROW));
     db.tables[t].arows = nrows;
     db.tables[t].arows = nrows;
-    
-    G_debug ( 2, "  ncols = %d nrows = %d", ncols, nrows);
-    
-    for( i = 0; i < nrows; i++ )
-      {
-         rows[i].alive = TRUE;
-         rows[i].values = (VALUE *) G_calloc ( ncols, sizeof (VALUE) );
-
-         for( j = 0; j < ncols; j++ )
-           {
-             val = &(rows[i].values[j]);		   
-	     
-	     dbfcol = j;
-
-	     val->is_null = DBFIsAttributeNULL ( dbf, i, dbfcol );
-	     if ( !(val->is_null) ) {
-		 switch ( db.tables[t].cols[j].type )
-		   {
-		     case DBF_INT:    
-			 val->i = DBFReadIntegerAttribute( dbf, i, dbfcol );
-			 break;
-		     case DBF_CHAR:    
-			 buf = (char *) DBFReadStringAttribute( dbf, i, dbfcol );
-			 save_string ( val, buf);
-			 break;
-		     case DBF_DOUBLE:    
-			 val->d = DBFReadDoubleAttribute( dbf, i, dbfcol );
-			 break;
-		   }
-	     }
-           }
-      }
-
-    DBFClose ( dbf );
-    
+
+    G_debug(2, "  ncols = %d nrows = %d", ncols, nrows);
+
+    for (i = 0; i < nrows; i++) {
+	rows[i].alive = TRUE;
+	rows[i].values = (VALUE *) G_calloc(ncols, sizeof(VALUE));
+
+	for (j = 0; j < ncols; j++) {
+	    val = &(rows[i].values[j]);
+
+	    dbfcol = j;
+
+	    val->is_null = DBFIsAttributeNULL(dbf, i, dbfcol);
+	    if (!(val->is_null)) {
+		switch (db.tables[t].cols[j].type) {
+		case DBF_INT:
+		    val->i = DBFReadIntegerAttribute(dbf, i, dbfcol);
+		    break;
+		case DBF_CHAR:
+		    buf = (char *)DBFReadStringAttribute(dbf, i, dbfcol);
+		    save_string(val, buf);
+		    break;
+		case DBF_DOUBLE:
+		    val->d = DBFReadDoubleAttribute(dbf, i, dbfcol);
+		    break;
+		}
+	    }
+	}
+    }
+
+    DBFClose(dbf);
+
     db.tables[t].rows = rows;
     db.tables[t].rows = rows;
     db.tables[t].nrows = nrows;
     db.tables[t].nrows = nrows;
     db.tables[t].loaded = TRUE;
     db.tables[t].loaded = TRUE;
-    
+
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-save_table ( int t)
+int save_table(int t)
 {
 {
-    int  i, j, ncols, nrows, ret, field, rec;
+    int i, j, ncols, nrows, ret, field, rec;
     char name[2000], fname[20], element[100];
     char name[2000], fname[20], element[100];
-    DBFHandle   dbf;
-    ROW  *rows;
+    DBFHandle dbf;
+    ROW *rows;
     VALUE *val;
     VALUE *val;
-    int  dbftype, width, decimals;
+    int dbftype, width, decimals;
 
 
-    G_debug (2, "save_table %d", t);
+    G_debug(2, "save_table %d", t);
 
 
     /* Note: because if driver is killed during the time the table is written, the process
     /* Note: because if driver is killed during the time the table is written, the process
-    *        is not completed and DATA ARE LOST. To minimize this, data are first written
-    *        to temporary file and then this file is renamed to 'database/table.dbf'.
-    *        Hopefully both file are on the same disk/partition */
-    
-    if ( !(db.tables[t].alive) || !(db.tables[t].updated) )
-        return DB_OK;
-    
+     *        is not completed and DATA ARE LOST. To minimize this, data are first written
+     *        to temporary file and then this file is renamed to 'database/table.dbf'.
+     *        Hopefully both file are on the same disk/partition */
+
+    if (!(db.tables[t].alive) || !(db.tables[t].updated))
+	return DB_OK;
+
     /* Construct our temp name because shapelib doesn't like '.' in name */
     /* Construct our temp name because shapelib doesn't like '.' in name */
     G__temp_element(element);
     G__temp_element(element);
-    sprintf (fname, "%d.dbf", getpid()) ;
-    G__file_name (name, element, fname, G_mapset()) ;
-    G_debug (2, "Write table to tempfile: '%s'", name);
-    
-    dbf = DBFCreate( name );
-    if( dbf == NULL )
-        return DB_FAILED;
+    sprintf(fname, "%d.dbf", getpid());
+    G__file_name(name, element, fname, G_mapset());
+    G_debug(2, "Write table to tempfile: '%s'", name);
+
+    dbf = DBFCreate(name);
+    if (dbf == NULL)
+	return DB_FAILED;
 
 
     ncols = db.tables[t].ncols;
     ncols = db.tables[t].ncols;
     rows = db.tables[t].rows;
     rows = db.tables[t].rows;
     nrows = db.tables[t].nrows;
     nrows = db.tables[t].nrows;
 
 
-    for( i = 0; i < ncols; i++ )
-      {
-	switch ( db.tables[t].cols[i].type )
-          {
-            case DBF_INT:
-		dbftype = FTInteger;
-		break;
-            case DBF_CHAR:
-		dbftype = FTString;
-		break;
-            case DBF_DOUBLE:
-		dbftype = FTDouble;
-		break;
-	  }
-	      
-        width = db.tables[t].cols[i].width;
+    for (i = 0; i < ncols; i++) {
+	switch (db.tables[t].cols[i].type) {
+	case DBF_INT:
+	    dbftype = FTInteger;
+	    break;
+	case DBF_CHAR:
+	    dbftype = FTString;
+	    break;
+	case DBF_DOUBLE:
+	    dbftype = FTDouble;
+	    break;
+	}
+
+	width = db.tables[t].cols[i].width;
 	decimals = db.tables[t].cols[i].decimals;
 	decimals = db.tables[t].cols[i].decimals;
-        DBFAddField( dbf, db.tables[t].cols[i].name, dbftype, width, decimals );
+	DBFAddField(dbf, db.tables[t].cols[i].name, dbftype, width, decimals);
 
 
-      }
-    
-    G_debug (2, "Write %d rows", nrows);
+    }
+
+    G_debug(2, "Write %d rows", nrows);
     rec = 0;
     rec = 0;
-    for( i = 0; i < nrows; i++ )
-      {
-         if ( rows[i].alive == FALSE ) continue;
-		 
-         for( j = 0; j < ncols; j++ )
-           {
-	     field = j;
-		 
-             val = &(rows[i].values[j]);		   
-	     if ( val->is_null ) {
-	         DBFWriteNULLAttribute ( dbf, rec, field );
-	     } else { 
-		 switch ( db.tables[t].cols[j].type ) {
-		     case DBF_INT:    
-			 ret = DBFWriteIntegerAttribute( dbf, rec, field, val->i ); 
-			 break;
-		     case DBF_CHAR:    
-			 if ( val->c != NULL )
-			     ret = DBFWriteStringAttribute( dbf, rec, field, val->c ); 
-			 else
-			     ret = DBFWriteStringAttribute( dbf, rec, field, "" ); 
-			 break;
-		     case DBF_DOUBLE:    
-			 ret = DBFWriteDoubleAttribute( dbf, rec, field, val->d ); 
-			 break;
-		 }
-	     }
-           }
-	 rec++;
-      }
-    G_debug (2, "Written %d records", rec);
-
-    DBFClose ( dbf );
+    for (i = 0; i < nrows; i++) {
+	if (rows[i].alive == FALSE)
+	    continue;
+
+	for (j = 0; j < ncols; j++) {
+	    field = j;
+
+	    val = &(rows[i].values[j]);
+	    if (val->is_null) {
+		DBFWriteNULLAttribute(dbf, rec, field);
+	    }
+	    else {
+		switch (db.tables[t].cols[j].type) {
+		case DBF_INT:
+		    ret = DBFWriteIntegerAttribute(dbf, rec, field, val->i);
+		    break;
+		case DBF_CHAR:
+		    if (val->c != NULL)
+			ret =
+			    DBFWriteStringAttribute(dbf, rec, field, val->c);
+		    else
+			ret = DBFWriteStringAttribute(dbf, rec, field, "");
+		    break;
+		case DBF_DOUBLE:
+		    ret = DBFWriteDoubleAttribute(dbf, rec, field, val->d);
+		    break;
+		}
+	    }
+	}
+	rec++;
+    }
+    G_debug(2, "Written %d records", rec);
+
+    DBFClose(dbf);
 
 
     /* Copy */
     /* Copy */
-    if ( G_rename_file ( name, db.tables[t].file ) ) {
-	    append_error( "Cannot move %s\nto %s\n", 
-                          name, db.tables[t].file );
-            return DB_FAILED;
+    if (G_rename_file(name, db.tables[t].file)) {
+	append_error("Cannot move %s\nto %s\n", name, db.tables[t].file);
+	return DB_FAILED;
     };
     };
 
 
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int free_table (int tab)
+int free_table(int tab)
 {
 {
-    int i,j;
-
-    for ( i = 0; i < db.tables[tab].nrows; i++ )
-      {
-	for( j = 0; j < db.tables[tab].ncols; j++ )
-	  {
-            if ( db.tables[tab].cols[j].type == DBF_CHAR && db.tables[tab].rows[i].values[j].c != NULL )
-	      {	    
-                G_free ( db.tables[tab].rows[i].values[j].c );
-	      }
-	  }
-        G_free ( db.tables[tab].rows[i].values );
-      }
-    
-    G_free ( db.tables[tab].rows );
-	      
-    return DB_OK;
-}
+    int i, j;
+
+    for (i = 0; i < db.tables[tab].nrows; i++) {
+	for (j = 0; j < db.tables[tab].ncols; j++) {
+	    if (db.tables[tab].cols[j].type == DBF_CHAR &&
+		db.tables[tab].rows[i].values[j].c != NULL) {
+		G_free(db.tables[tab].rows[i].values[j].c);
+	    }
+	}
+	G_free(db.tables[tab].rows[i].values);
+    }
 
 
+    G_free(db.tables[tab].rows);
 
 
+    return DB_OK;
+}

+ 99 - 100
db/drivers/mysql/create_table.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -15,104 +16,102 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_create_table (dbTable *table)
+int db__driver_create_table(dbTable * table)
 {
 {
     int col, ncols;
     int col, ncols;
-    dbColumn   *column;
+    dbColumn *column;
     char *colname;
     char *colname;
     int sqltype;
     int sqltype;
     char buf[500];
     char buf[500];
     dbString sql;
     dbString sql;
+
     /* dbConnection conn_par; */
     /* dbConnection conn_par; */
-    
-    G_debug (3, "db__driver_create_table()");
+
+    G_debug(3, "db__driver_create_table()");
 
 
     init_error();
     init_error();
 
 
-    db_init_string (&sql);
+    db_init_string(&sql);
 
 
-    db_set_string ( &sql, "CREATE TABLE ");
-    db_append_string ( &sql, db_get_table_name ( table ) );
-    db_append_string ( &sql, " ( ");
+    db_set_string(&sql, "CREATE TABLE ");
+    db_append_string(&sql, db_get_table_name(table));
+    db_append_string(&sql, " ( ");
 
 
     ncols = db_get_table_number_of_columns(table);
     ncols = db_get_table_number_of_columns(table);
 
 
-    for ( col = 0; col < ncols; col++ ) {
-        column = db_get_table_column (table, col);
-	colname = db_get_column_name (column);
-	sqltype = db_get_column_sqltype (column);
-	
-	G_debug ( 3, "%s (%s)", colname, db_sqltype_name(sqltype) );
-
-	if ( col > 0 ) db_append_string ( &sql, ", " );
-	db_append_string ( &sql, colname );
-	db_append_string ( &sql, " " );
-	switch ( sqltype ) 
-        {
-	    case DB_SQL_TYPE_SMALLINT:
-		db_append_string ( &sql, "SMALLINT");
-		break;
-	    case DB_SQL_TYPE_INTEGER:
-		db_append_string ( &sql, "INT");
-		break;
-
-	    case DB_SQL_TYPE_REAL:
-		db_append_string ( &sql, "FLOAT");
-		break;
-
-            /* TODO: better numeric types */
-	    case DB_SQL_TYPE_DOUBLE_PRECISION:
-	    case DB_SQL_TYPE_DECIMAL:
-	    case DB_SQL_TYPE_NUMERIC:
-	    case DB_SQL_TYPE_INTERVAL:
-		db_append_string ( &sql, "DOUBLE");
-		break;
-
-            /* GRASS does not distinguish TIMESTAMP and DATETIME */
-            /*
-            case DB_SQL_TYPE_DATETIME|DB_DATETIME_MASK:
-                db_append_string ( &sql, "DATETIME");
-                break;
-	    */
-            case DB_SQL_TYPE_TIMESTAMP:
-                /* db_append_string ( &sql, "TIMESTAMP"); */
-                db_append_string ( &sql, "DATETIME");
-                break;
-
-            case DB_SQL_TYPE_DATE:
-                db_append_string ( &sql, "DATE");
-                break;
-            case DB_SQL_TYPE_TIME:
-                db_append_string ( &sql, "TIME");
-                break;
-
-	    case DB_SQL_TYPE_CHARACTER:
-                sprintf (buf, "VARCHAR(%d)", 
-			db_get_column_length (column) );
-                db_append_string ( &sql, buf);
-		break;
-	    case DB_SQL_TYPE_TEXT:
-		db_append_string ( &sql, "TEXT");
-                break;
-
- 	    default:
-                G_warning ( "Unknown column type (%s)", colname);
-		return DB_FAILED;
+    for (col = 0; col < ncols; col++) {
+	column = db_get_table_column(table, col);
+	colname = db_get_column_name(column);
+	sqltype = db_get_column_sqltype(column);
+
+	G_debug(3, "%s (%s)", colname, db_sqltype_name(sqltype));
+
+	if (col > 0)
+	    db_append_string(&sql, ", ");
+	db_append_string(&sql, colname);
+	db_append_string(&sql, " ");
+	switch (sqltype) {
+	case DB_SQL_TYPE_SMALLINT:
+	    db_append_string(&sql, "SMALLINT");
+	    break;
+	case DB_SQL_TYPE_INTEGER:
+	    db_append_string(&sql, "INT");
+	    break;
+
+	case DB_SQL_TYPE_REAL:
+	    db_append_string(&sql, "FLOAT");
+	    break;
+
+	    /* TODO: better numeric types */
+	case DB_SQL_TYPE_DOUBLE_PRECISION:
+	case DB_SQL_TYPE_DECIMAL:
+	case DB_SQL_TYPE_NUMERIC:
+	case DB_SQL_TYPE_INTERVAL:
+	    db_append_string(&sql, "DOUBLE");
+	    break;
+
+	    /* GRASS does not distinguish TIMESTAMP and DATETIME */
+	    /*
+	       case DB_SQL_TYPE_DATETIME|DB_DATETIME_MASK:
+	       db_append_string ( &sql, "DATETIME");
+	       break;
+	     */
+	case DB_SQL_TYPE_TIMESTAMP:
+	    /* db_append_string ( &sql, "TIMESTAMP"); */
+	    db_append_string(&sql, "DATETIME");
+	    break;
+
+	case DB_SQL_TYPE_DATE:
+	    db_append_string(&sql, "DATE");
+	    break;
+	case DB_SQL_TYPE_TIME:
+	    db_append_string(&sql, "TIME");
+	    break;
+
+	case DB_SQL_TYPE_CHARACTER:
+	    sprintf(buf, "VARCHAR(%d)", db_get_column_length(column));
+	    db_append_string(&sql, buf);
+	    break;
+	case DB_SQL_TYPE_TEXT:
+	    db_append_string(&sql, "TEXT");
+	    break;
+
+	default:
+	    G_warning("Unknown column type (%s)", colname);
+	    return DB_FAILED;
 	}
 	}
     }
     }
-    db_append_string ( &sql, " )" );
-
-    G_debug (3, " SQL: %s", db_get_string(&sql) );
-    
-    if ( mysql_query ( connection, db_get_string(&sql) ) != 0 )
-    {
-        append_error( "Cannot create table:\n");
-	append_error( db_get_string(&sql) );
-	append_error( "\n" );
-	append_error ( mysql_error(connection) );
+    db_append_string(&sql, " )");
+
+    G_debug(3, " SQL: %s", db_get_string(&sql));
+
+    if (mysql_query(connection, db_get_string(&sql)) != 0) {
+	append_error("Cannot create table:\n");
+	append_error(db_get_string(&sql));
+	append_error("\n");
+	append_error(mysql_error(connection));
 	report_error();
 	report_error();
-	db_free_string ( &sql);
+	db_free_string(&sql);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
@@ -124,26 +123,26 @@ db__driver_create_table (dbTable *table)
      * 2) Only root can grant privileges.
      * 2) Only root can grant privileges.
      */
      */
     /*
     /*
-    db_get_connection(&conn_par);
-
-    if ( conn_par.group ) 
-    {
-	db_set_string ( &sql, "GRANT SELECT ON on " );
-	db_append_string ( &sql, db_get_table_name ( table ) );
-	db_append_string ( &sql, " TO " );
-	db_append_string ( &sql, conn_par.group );
+       db_get_connection(&conn_par);
+
+       if ( conn_par.group ) 
+       {
+       db_set_string ( &sql, "GRANT SELECT ON on " );
+       db_append_string ( &sql, db_get_table_name ( table ) );
+       db_append_string ( &sql, " TO " );
+       db_append_string ( &sql, conn_par.group );
+
+       G_debug (3, " SQL: %s", db_get_string(&sql) );
+
+       if ( mysql_query ( connection, db_get_string(&sql) ) != 0 )
+       {
+       G_warning ( "Cannot grant select on table: \n%s\n%s",
+       db_get_string(&sql), mysql_error(connection) );
+       }
+       }
+     */
 
 
-	G_debug (3, " SQL: %s", db_get_string(&sql) );
+    db_free_string(&sql);
 
 
-	if ( mysql_query ( connection, db_get_string(&sql) ) != 0 )
-	{
-	    G_warning ( "Cannot grant select on table: \n%s\n%s",
-			 db_get_string(&sql), mysql_error(connection) );
-	}
-    }
-    */
-    
-    db_free_string ( &sql);
-    
     return DB_OK;
     return DB_OK;
 }
 }

+ 15 - 17
db/drivers/mysql/cursor.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -17,8 +18,7 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_close_cursor(dbCursor *dbc)
+int db__driver_close_cursor(dbCursor * dbc)
 {
 {
     cursor *c;
     cursor *c;
 
 
@@ -36,24 +36,24 @@ db__driver_close_cursor(dbCursor *dbc)
 }
 }
 
 
 
 
-cursor * alloc_cursor()
+cursor *alloc_cursor()
 {
 {
-    cursor     *c;
+    cursor *c;
 
 
     /* allocate the cursor */
     /* allocate the cursor */
     c = (cursor *) db_malloc(sizeof(cursor));
     c = (cursor *) db_malloc(sizeof(cursor));
     if (c == NULL) {
     if (c == NULL) {
-        append_error( _("Cannot allocate cursor.") );
-        return NULL; 
-    } 
+	append_error(_("Cannot allocate cursor."));
+	return NULL;
+    }
 
 
     c->res = NULL;
     c->res = NULL;
-    
+
     /* tokenize it */
     /* tokenize it */
     c->token = db_new_token(c);
     c->token = db_new_token(c);
     if (c->token < 0) {
     if (c->token < 0) {
-        append_error( _("Cannot ad new token.") );
-        return NULL; 
+	append_error(_("Cannot ad new token."));
+	return NULL;
     }
     }
 
 
     c->cols = NULL;
     c->cols = NULL;
@@ -62,16 +62,14 @@ cursor * alloc_cursor()
     return c;
     return c;
 }
 }
 
 
-void free_cursor( cursor *c)
+void free_cursor(cursor * c)
 {
 {
     db_drop_token(c->token);
     db_drop_token(c->token);
 
 
-    if ( c->res ) 
-    {
-	mysql_free_result ( c->res );
+    if (c->res) {
+	mysql_free_result(c->res);
     }
     }
 
 
-    G_free ( c->cols );
-    G_free(c);  
+    G_free(c->cols);
+    G_free(c);
 }
 }
-

+ 107 - 113
db/drivers/mysql/db.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -18,9 +19,9 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int replace_variables ( char *, char **, char ** );
+int replace_variables(char *, char **, char **);
 
 
-int db__driver_open_database(dbHandle *handle)
+int db__driver_open_database(dbHandle * handle)
 {
 {
     char *name;
     char *name;
     dbConnection default_connection;
     dbConnection default_connection;
@@ -31,96 +32,95 @@ int db__driver_open_database(dbHandle *handle)
     name = db_get_handle_dbname(handle);
     name = db_get_handle_dbname(handle);
 
 
     /* if name is empty use default_connection.databaseName */
     /* if name is empty use default_connection.databaseName */
-    if (strlen(name) == 0) 
+    if (strlen(name) == 0)
 	name = default_connection.databaseName;
 	name = default_connection.databaseName;
 
 
-    G_debug(3, "db_driver_open_database() mysql: database definition = '%s'", name );
+    G_debug(3, "db_driver_open_database() mysql: database definition = '%s'",
+	    name);
 
 
 #ifndef MESQL
 #ifndef MESQL
-  {
-    /* Client version */
-    char *user, *password;
-    CONNPAR connpar;
-
-    if ( parse_conn ( name, &connpar ) == DB_FAILED ) {
-	report_error();
-	return DB_FAILED;
-    }
-    
-    G_debug(3, "host = %s, port = %d, dbname = %s, "
-	       "user = %s, password = %s", 
-	       connpar.host, connpar.port, connpar.dbname, 
-	       connpar.user, connpar.password );
-
-    db_get_login ( "mysql", name, &user, &password );
-
-    connection = mysql_init(NULL);
-    res = mysql_real_connect ( connection, connpar.host, user, password,
-	                       connpar.dbname, connpar.port, NULL, 0);
-
-    G_free ( user );
-    G_free ( password );
-    
-    if ( res == NULL ) 
     {
     {
-	append_error ( _("Cannot connect to MySQL: ") );
-        append_error ( mysql_error(connection) );
-	report_error ();
-	return DB_FAILED;
+	/* Client version */
+	char *user, *password;
+	CONNPAR connpar;
+
+	if (parse_conn(name, &connpar) == DB_FAILED) {
+	    report_error();
+	    return DB_FAILED;
+	}
+
+	G_debug(3, "host = %s, port = %d, dbname = %s, "
+		"user = %s, password = %s",
+		connpar.host, connpar.port, connpar.dbname,
+		connpar.user, connpar.password);
+
+	db_get_login("mysql", name, &user, &password);
+
+	connection = mysql_init(NULL);
+	res = mysql_real_connect(connection, connpar.host, user, password,
+				 connpar.dbname, connpar.port, NULL, 0);
+
+	G_free(user);
+	G_free(password);
+
+	if (res == NULL) {
+	    append_error(_("Cannot connect to MySQL: "));
+	    append_error(mysql_error(connection));
+	    report_error();
+	    return DB_FAILED;
+	}
     }
     }
-  }
 
 
 #else
 #else
 
 
     /* Embedded version */
     /* Embedded version */
-  {
-    char *datadir, *database;
-    char *server_args[4];
-    char *buf;
-
-    if ( !replace_variables ( name, &datadir, &database ) )
-    {
-	append_error ( _("Cannot parse MySQL embedded database name") );
-        append_error ( mysql_error(connection) );
-	report_error ();
-	return DB_FAILED;
-    }
-    
-    server_args[0] = "mesql"; /* this string is not used */
-    G_asprintf ( &buf, "--datadir=%s", datadir ); 
-    server_args[1] = buf;
-    /* With InnoDB it is very slow to close the database */
-    server_args[2] = "--skip-innodb"; /* OK? */
-    /* Without --bootstrap it complains about missing 
-     * mysql.time_zone_leap_second table */
-    server_args[3] = "--bootstrap"; /* OK? */
-
-    if ( mysql_server_init(4, server_args, NULL) )
     {
     {
-	append_error ( _("Cannot initialize MySQL embedded server") );
-        append_error ( mysql_error(connection) );
-	report_error ();
-	free ( datadir );
-	free ( database );
-	return DB_FAILED;
-    }
+	char *datadir, *database;
+	char *server_args[4];
+	char *buf;
+
+	if (!replace_variables(name, &datadir, &database)) {
+	    append_error(_("Cannot parse MySQL embedded database name"));
+	    append_error(mysql_error(connection));
+	    report_error();
+	    return DB_FAILED;
+	}
 
 
-    connection = mysql_init(NULL);
-    mysql_options ( connection, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
+	server_args[0] = "mesql";	/* this string is not used */
+	G_asprintf(&buf, "--datadir=%s", datadir);
+	server_args[1] = buf;
+	/* With InnoDB it is very slow to close the database */
+	server_args[2] = "--skip-innodb";	/* OK? */
+	/* Without --bootstrap it complains about missing 
+	 * mysql.time_zone_leap_second table */
+	server_args[3] = "--bootstrap";	/* OK? */
+
+	if (mysql_server_init(4, server_args, NULL)) {
+	    append_error(_("Cannot initialize MySQL embedded server"));
+	    append_error(mysql_error(connection));
+	    report_error();
+	    free(datadir);
+	    free(database);
+	    return DB_FAILED;
+	}
 
 
-    res = mysql_real_connect ( connection, NULL,NULL,NULL, database, 0,NULL,0);
+	connection = mysql_init(NULL);
+	mysql_options(connection, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
 
 
-    free ( datadir );
-    free ( database );
-    
-    if ( res == NULL ) 
-    {
-	append_error ( _("Cannot connect to MySQL embedded server: ") );
-        append_error ( mysql_error(connection) );
-	report_error ();
-	return DB_FAILED;
+	res =
+	    mysql_real_connect(connection, NULL, NULL, NULL, database, 0,
+			       NULL, 0);
+
+	free(datadir);
+	free(database);
+
+	if (res == NULL) {
+	    append_error(_("Cannot connect to MySQL embedded server: "));
+	    append_error(mysql_error(connection));
+	    report_error();
+	    return DB_FAILED;
+	}
     }
     }
-  }
 
 
 #endif
 #endif
 
 
@@ -130,16 +130,16 @@ int db__driver_open_database(dbHandle *handle)
 int db__driver_close_database()
 int db__driver_close_database()
 {
 {
     init_error();
     init_error();
-    mysql_close ( connection ); /* this will also release connection */
+    mysql_close(connection);	/* this will also release connection */
 
 
 #ifdef MESQL
 #ifdef MESQL
     mysql_server_end();
     mysql_server_end();
 #endif
 #endif
-    
+
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int replace_variables ( char *in, char **datadir, char **database )
+int replace_variables(char *in, char **datadir, char **database)
 {
 {
     *datadir = NULL;
     *datadir = NULL;
     *database = NULL;
     *database = NULL;
@@ -149,46 +149,40 @@ int replace_variables ( char *in, char **datadir, char **database )
     char **tokens;
     char **tokens;
     int no_tokens, n;
     int no_tokens, n;
 
 
-    if ( !strchr(in,'/') ) /* no path */
-    {
-	*datadir = G_store ( "./" );
-	*database = G_store ( in );
-    } 
-    else
-    {
-	tokens = G_tokenize (in, "/");
+    if (!strchr(in, '/')) {	/* no path */
+	*datadir = G_store("./");
+	*database = G_store(in);
+    }
+    else {
+	tokens = G_tokenize(in, "/");
 	no_tokens = G_number_of_tokens(tokens);
 	no_tokens = G_number_of_tokens(tokens);
-    
-	G_debug(3, "no_tokens = %d", no_tokens );
+
+	G_debug(3, "no_tokens = %d", no_tokens);
 
 
 	tmp[0] = '\0';
 	tmp[0] = '\0';
-	for (n = 0; n < no_tokens-1 ; n++)
-	{
-	   if ( n > 0 )
-		strcat ( tmp, "/" );
-
-	   G_debug (3, "tokens[%d] = %s", n, tokens[n] );
-	   if ( tokens[n][0] == '$' )
-	   {
-	       G_strchg(tokens[n],'$', ' ' );
-	       G_chop(tokens[n]);
-	       strcat(tmp, G__getenv(tokens[n]) );
-	       G_debug (3, "   -> %s", G__getenv(tokens[n]) );
-	   }
-	   else
-	   {
-	       strcat (tmp, tokens[n]);
-	   }
+	for (n = 0; n < no_tokens - 1; n++) {
+	    if (n > 0)
+		strcat(tmp, "/");
+
+	    G_debug(3, "tokens[%d] = %s", n, tokens[n]);
+	    if (tokens[n][0] == '$') {
+		G_strchg(tokens[n], '$', ' ');
+		G_chop(tokens[n]);
+		strcat(tmp, G__getenv(tokens[n]));
+		G_debug(3, "   -> %s", G__getenv(tokens[n]));
+	    }
+	    else {
+		strcat(tmp, tokens[n]);
+	    }
 	}
 	}
-	*datadir = G_store ( tmp );
-	*database = G_store ( tokens[n] );
+	*datadir = G_store(tmp);
+	*database = G_store(tokens[n]);
 
 
-	G_free_tokens ( tokens );
+	G_free_tokens(tokens);
     }
     }
 
 
-    G_debug(2, "datadir = '%s'", *datadir );
-    G_debug(2, "database = '%s'", *database );
+    G_debug(2, "datadir = '%s'", *datadir);
+    G_debug(2, "database = '%s'", *database);
 
 
     return 1;
     return 1;
 }
 }
-

+ 1 - 0
db/drivers/mysql/dbdriver.h

@@ -19,6 +19,7 @@ int db__driver_get_num_rows();
 int db__driver_create_index();
 int db__driver_create_index();
 int db__driver_list_tables();
 int db__driver_list_tables();
 int db__driver_open_select_cursor();
 int db__driver_open_select_cursor();
+
 #define	init_dbdriver() do{\
 #define	init_dbdriver() do{\
 db_driver_create_table = db__driver_create_table;\
 db_driver_create_table = db__driver_create_table;\
 db_driver_close_cursor = db__driver_close_cursor;\
 db_driver_close_cursor = db__driver_close_cursor;\

+ 126 - 130
db/drivers/mysql/describe.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -15,37 +16,36 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_describe_table( dbString *table_name, dbTable **table)
+int db__driver_describe_table(dbString * table_name, dbTable ** table)
 {
 {
     dbString sql;
     dbString sql;
     MYSQL_RES *res;
     MYSQL_RES *res;
 
 
-    db_init_string ( &sql );
+    db_init_string(&sql);
 
 
-    db_set_string( &sql, "select * from ");
-    db_append_string ( &sql, db_get_string(table_name) );
-    db_append_string( &sql, " where 1 = 0");
+    db_set_string(&sql, "select * from ");
+    db_append_string(&sql, db_get_string(table_name));
+    db_append_string(&sql, " where 1 = 0");
 
 
-    if ( mysql_query ( connection, db_get_string(&sql) ) != 0 )
-    {
-	append_error ( db_get_string(&sql) );
-	append_error ( "\n" );
-	append_error ( mysql_error(connection) );
+    if (mysql_query(connection, db_get_string(&sql)) != 0) {
+	append_error(db_get_string(&sql));
+	append_error("\n");
+	append_error(mysql_error(connection));
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    res = mysql_store_result ( connection );
+    res = mysql_store_result(connection);
 
 
-    if ( res == NULL ) {
-	append_error ( db_get_string(&sql) );
-	append_error ( "\n" );
-	append_error ( mysql_error(connection) );
+    if (res == NULL) {
+	append_error(db_get_string(&sql));
+	append_error("\n");
+	append_error(mysql_error(connection));
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    if ( describe_table( res, table, NULL) == DB_FAILED ) {
+    if (describe_table(res, table, NULL) == DB_FAILED) {
 	append_error("Cannot describe table\n");
 	append_error("Cannot describe table\n");
 	report_error();
 	report_error();
 	mysql_free_result(res);
 	mysql_free_result(res);
@@ -54,78 +54,78 @@ int db__driver_describe_table( dbString *table_name, dbTable **table)
 
 
     mysql_free_result(res);
     mysql_free_result(res);
 
 
-    db_set_table_name( *table, db_get_string(table_name) );
+    db_set_table_name(*table, db_get_string(table_name));
 
 
     return DB_OK;
     return DB_OK;
 }
 }
 
 
 /* describe table, if c is not NULL cur->cols and cur->ncols is also set */
 /* describe table, if c is not NULL cur->cols and cur->ncols is also set */
-int describe_table( MYSQL_RES *res, dbTable **table, cursor *c)
+int describe_table(MYSQL_RES * res, dbTable ** table, cursor * c)
 {
 {
-    int  i, ncols, kcols;
+    int i, ncols, kcols;
     char *name;
     char *name;
-    int  sqltype, length;
+    int sqltype, length;
     dbColumn *column;
     dbColumn *column;
     MYSQL_FIELD *fields;
     MYSQL_FIELD *fields;
 
 
-    G_debug (3, "describe_table()");
+    G_debug(3, "describe_table()");
+
+    ncols = mysql_num_fields(res);
+    fields = mysql_fetch_fields(res);
 
 
-    ncols = mysql_num_fields ( res );
-    fields = mysql_fetch_fields ( res );
-    
     /* Count columns of known type */
     /* Count columns of known type */
     kcols = 0;
     kcols = 0;
     for (i = 0; i < ncols; i++) {
     for (i = 0; i < ncols; i++) {
-	field_info ( &(fields[i]), &sqltype, &length );
+	field_info(&(fields[i]), &sqltype, &length);
 
 
-	if ( sqltype == DB_SQL_TYPE_UNKNOWN ) continue;
-	    
-	kcols++; /* known types */
+	if (sqltype == DB_SQL_TYPE_UNKNOWN)
+	    continue;
+
+	kcols++;		/* known types */
     }
     }
 
 
-    G_debug (3, "kcols = %d", kcols);
+    G_debug(3, "kcols = %d", kcols);
 
 
     if (!(*table = db_alloc_table(kcols))) {
     if (!(*table = db_alloc_table(kcols))) {
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    if ( c ) {
+    if (c) {
 	c->ncols = kcols;
 	c->ncols = kcols;
-	c->cols = (int *) G_malloc ( kcols * sizeof(int) );
+	c->cols = (int *)G_malloc(kcols * sizeof(int));
     }
     }
 
 
-    db_set_table_name ( *table, "" );
-    db_set_table_description ( *table, "" );
+    db_set_table_name(*table, "");
+    db_set_table_description(*table, "");
 
 
     /* Currently not used in GRASS */
     /* Currently not used in GRASS */
     /*
     /*
-    db_set_table_delete_priv_granted (*table);
-    db_set_table_insert_priv_granted (*table);
-    db_set_table_delete_priv_not_granted (*table);
-    db_set_table_insert_priv_not_granted (*table);
-    */
+       db_set_table_delete_priv_granted (*table);
+       db_set_table_insert_priv_granted (*table);
+       db_set_table_delete_priv_not_granted (*table);
+       db_set_table_insert_priv_not_granted (*table);
+     */
 
 
     kcols = 0;
     kcols = 0;
-    for ( i = 0; i < ncols; i++ ) {
+    for (i = 0; i < ncols; i++) {
 	name = fields[i].name;
 	name = fields[i].name;
-	field_info ( &(fields[i]), &sqltype, &length );
+	field_info(&(fields[i]), &sqltype, &length);
 
 
-	G_debug(3, "col: %s, kcols %d, sqltype %d", 
-		    name, kcols, sqltype );
+	G_debug(3, "col: %s, kcols %d, sqltype %d", name, kcols, sqltype);
 
 
-	G_debug(3, "flags = %d", fields[i].flags ); 
+	G_debug(3, "flags = %d", fields[i].flags);
 
 
-	if ( sqltype == DB_SQL_TYPE_UNKNOWN ) {
+	if (sqltype == DB_SQL_TYPE_UNKNOWN) {
 	    /* Print warning and continue */
 	    /* Print warning and continue */
-	    G_warning ( _("MySQL driver: column '%s', type %d "
-			  "is not supported"), name, fields[i].type);
+	    G_warning(_("MySQL driver: column '%s', type %d "
+			"is not supported"), name, fields[i].type);
 	    continue;
 	    continue;
 	}
 	}
 
 
-	if ( fields[i].type == MYSQL_TYPE_LONGLONG )
-	    G_warning ( _("column '%s' : type BIGINT is stored as "
-		"integer (4 bytes) some data may be damaged"), name);
-	
+	if (fields[i].type == MYSQL_TYPE_LONGLONG)
+	    G_warning(_("column '%s' : type BIGINT is stored as "
+			"integer (4 bytes) some data may be damaged"), name);
+
 	column = db_get_table_column(*table, kcols);
 	column = db_get_table_column(*table, kcols);
 
 
 	db_set_column_name(column, name);
 	db_set_column_name(column, name);
@@ -133,11 +133,10 @@ int describe_table( MYSQL_RES *res, dbTable **table, cursor *c)
 	db_set_column_host_type(column, (int)fields[i].type);
 	db_set_column_host_type(column, (int)fields[i].type);
 	db_set_column_sqltype(column, sqltype);
 	db_set_column_sqltype(column, sqltype);
 
 
-        db_set_column_precision ( column, (int)fields[i].decimals );
- 	db_set_column_scale ( column, 0 );
+	db_set_column_precision(column, (int)fields[i].decimals);
+	db_set_column_scale(column, 0);
 
 
-	if ( !(fields[i].flags & NOT_NULL_FLAG ) )
-	{
+	if (!(fields[i].flags & NOT_NULL_FLAG)) {
 	    db_set_column_null_allowed(column);
 	    db_set_column_null_allowed(column);
 	}
 	}
 	db_set_column_has_undefined_default_value(column);
 	db_set_column_has_undefined_default_value(column);
@@ -145,12 +144,12 @@ int describe_table( MYSQL_RES *res, dbTable **table, cursor *c)
 
 
 	/* Currently not used in GRASS */
 	/* Currently not used in GRASS */
 	/*
 	/*
-        db_set_column_select_priv_granted (column);
-        db_set_column_update_priv_granted (column);
-        db_set_column_update_priv_not_granted (column); 
-	*/
+	   db_set_column_select_priv_granted (column);
+	   db_set_column_update_priv_granted (column);
+	   db_set_column_update_priv_not_granted (column); 
+	 */
 
 
-	if ( c ) {
+	if (c) {
 	    c->cols[kcols] = i;
 	    c->cols[kcols] = i;
 	}
 	}
 
 
@@ -161,82 +160,79 @@ int describe_table( MYSQL_RES *res, dbTable **table, cursor *c)
 }
 }
 
 
 /* Get sqltype for field */
 /* Get sqltype for field */
-void field_info ( MYSQL_FIELD *field, int * sqltype, int * length )
+void field_info(MYSQL_FIELD * field, int *sqltype, int *length)
 {
 {
     *length = field->length;
     *length = field->length;
 
 
-    switch ( field->type ) 
-    {
-	case MYSQL_TYPE_TINY:
-	    *sqltype = DB_SQL_TYPE_SMALLINT;
-	    break;
-
-	case MYSQL_TYPE_SHORT:
-	case MYSQL_TYPE_LONG:
-	case MYSQL_TYPE_INT24:
-	case MYSQL_TYPE_LONGLONG:
-	    *sqltype = DB_SQL_TYPE_INTEGER;
-	    break;
-
-	case MYSQL_TYPE_DECIMAL:
-	    *sqltype = DB_SQL_TYPE_DECIMAL;
-	    break;
-
-	case MYSQL_TYPE_FLOAT:
-	    *sqltype = DB_SQL_TYPE_REAL;
-	    break;
-
-	case MYSQL_TYPE_DOUBLE:
-	    *sqltype = DB_SQL_TYPE_DOUBLE_PRECISION;
-	    break;
-
-	case MYSQL_TYPE_TIMESTAMP: 
-	    *sqltype = DB_SQL_TYPE_TIMESTAMP;
-	    break;
-
-	case MYSQL_TYPE_DATE:
-	    *sqltype = DB_SQL_TYPE_DATE;
-	    break;
-
-	case MYSQL_TYPE_TIME:
-	    *sqltype = DB_SQL_TYPE_TIME;
-	    break;
-
-	case MYSQL_TYPE_DATETIME:
-	    //*sqltype = DB_SQL_TYPE_DATETIME;
-	    //*sqltype |= DB_DATETIME_MASK;
-	    *sqltype = DB_SQL_TYPE_TIMESTAMP;
-	    break;
-
-	case MYSQL_TYPE_YEAR:
-	    *sqltype = DB_SQL_TYPE_INTEGER;
-	    break;
-
-	case MYSQL_TYPE_STRING:
-	case MYSQL_TYPE_VAR_STRING:
-	case MYSQL_TYPE_SET:
-	case MYSQL_TYPE_ENUM:
-	    *sqltype = DB_SQL_TYPE_CHARACTER;
-	    break;
-	    
-	case MYSQL_TYPE_BLOB: 
-	    if ( field->flags & BINARY_FLAG  )
-	    {	
-		*sqltype = DB_SQL_TYPE_UNKNOWN;
-	    }
-	    else
-	    {
-	        *sqltype = DB_SQL_TYPE_TEXT;
-	    }
-	    break;
-
-	case MYSQL_TYPE_GEOMETRY:
-	case MYSQL_TYPE_NULL:
+    switch (field->type) {
+    case MYSQL_TYPE_TINY:
+	*sqltype = DB_SQL_TYPE_SMALLINT;
+	break;
+
+    case MYSQL_TYPE_SHORT:
+    case MYSQL_TYPE_LONG:
+    case MYSQL_TYPE_INT24:
+    case MYSQL_TYPE_LONGLONG:
+	*sqltype = DB_SQL_TYPE_INTEGER;
+	break;
+
+    case MYSQL_TYPE_DECIMAL:
+	*sqltype = DB_SQL_TYPE_DECIMAL;
+	break;
+
+    case MYSQL_TYPE_FLOAT:
+	*sqltype = DB_SQL_TYPE_REAL;
+	break;
+
+    case MYSQL_TYPE_DOUBLE:
+	*sqltype = DB_SQL_TYPE_DOUBLE_PRECISION;
+	break;
+
+    case MYSQL_TYPE_TIMESTAMP:
+	*sqltype = DB_SQL_TYPE_TIMESTAMP;
+	break;
+
+    case MYSQL_TYPE_DATE:
+	*sqltype = DB_SQL_TYPE_DATE;
+	break;
+
+    case MYSQL_TYPE_TIME:
+	*sqltype = DB_SQL_TYPE_TIME;
+	break;
+
+    case MYSQL_TYPE_DATETIME:
+	//*sqltype = DB_SQL_TYPE_DATETIME;
+	//*sqltype |= DB_DATETIME_MASK;
+	*sqltype = DB_SQL_TYPE_TIMESTAMP;
+	break;
+
+    case MYSQL_TYPE_YEAR:
+	*sqltype = DB_SQL_TYPE_INTEGER;
+	break;
+
+    case MYSQL_TYPE_STRING:
+    case MYSQL_TYPE_VAR_STRING:
+    case MYSQL_TYPE_SET:
+    case MYSQL_TYPE_ENUM:
+	*sqltype = DB_SQL_TYPE_CHARACTER;
+	break;
+
+    case MYSQL_TYPE_BLOB:
+	if (field->flags & BINARY_FLAG) {
 	    *sqltype = DB_SQL_TYPE_UNKNOWN;
 	    *sqltype = DB_SQL_TYPE_UNKNOWN;
-	    break;
+	}
+	else {
+	    *sqltype = DB_SQL_TYPE_TEXT;
+	}
+	break;
 
 
-	default:
-	    *sqltype = DB_SQL_TYPE_UNKNOWN;
+    case MYSQL_TYPE_GEOMETRY:
+    case MYSQL_TYPE_NULL:
+	*sqltype = DB_SQL_TYPE_UNKNOWN;
+	break;
+
+    default:
+	*sqltype = DB_SQL_TYPE_UNKNOWN;
     }
     }
 
 
     return;
     return;

+ 3 - 5
db/drivers/mysql/driver.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -16,16 +17,13 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_init  (int argc, char *argv[])
-
+int db__driver_init(int argc, char *argv[])
 {
 {
     init_error();
     init_error();
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_finish()
+int db__driver_finish()
 {
 {
     return DB_OK;
     return DB_OK;
 }
 }

+ 10 - 13
db/drivers/mysql/error.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -18,28 +19,24 @@
 #include "proto.h"
 #include "proto.h"
 
 
 /* init error message */
 /* init error message */
-void
-init_error ( void )
+void init_error(void)
 {
 {
-    if ( !errMsg ) {
+    if (!errMsg) {
 	errMsg = (dbString *) G_malloc(sizeof(dbString));
 	errMsg = (dbString *) G_malloc(sizeof(dbString));
-        db_init_string (errMsg);
+	db_init_string(errMsg);
     }
     }
 
 
-    db_set_string ( errMsg, _("DBMI-MySQL driver error:\n") );
+    db_set_string(errMsg, _("DBMI-MySQL driver error:\n"));
 }
 }
 
 
 /* append error message */
 /* append error message */
-void
-append_error ( const char *msg )
+void append_error(const char *msg)
 {
 {
-    db_append_string ( errMsg, (char *)msg);
+    db_append_string(errMsg, (char *)msg);
 }
 }
 
 
-void
-report_error ( void )
+void report_error(void)
 {
 {
-    db_append_string ( errMsg, "\n");
-    db_error ( db_get_string (errMsg) );
+    db_append_string(errMsg, "\n");
+    db_error(db_get_string(errMsg));
 }
 }
-

+ 23 - 25
db/drivers/mysql/execute.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -17,9 +18,9 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_execute_immediate(dbString *sql)
+int db__driver_execute_immediate(dbString * sql)
 {
 {
-    char     *str;
+    char *str;
 
 
     init_error();
     init_error();
 
 
@@ -33,38 +34,36 @@ int db__driver_execute_immediate(dbString *sql)
      * result is error. 
      * result is error. 
      * Because of this, all occurencies of \ in sql must be 
      * Because of this, all occurencies of \ in sql must be 
      * escaped by \ */
      * escaped by \ */
-    str = G_str_replace ( db_get_string(sql), "\\", "\\\\" );
+    str = G_str_replace(db_get_string(sql), "\\", "\\\\");
 
 
-    G_debug ( 3, "Escaped SQL: %s", str );
+    G_debug(3, "Escaped SQL: %s", str);
 
 
-    if ( mysql_query ( connection, str ) != 0 )
-    {
-	append_error( "Cannot execute: \n" );
-	append_error( str );
-	append_error( "\n" );
-	append_error ( mysql_error(connection) );
+    if (mysql_query(connection, str) != 0) {
+	append_error("Cannot execute: \n");
+	append_error(str);
+	append_error("\n");
+	append_error(mysql_error(connection));
 	report_error();
 	report_error();
-	if ( str ) 
-	    G_free ( str );
+	if (str)
+	    G_free(str);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
-    if ( str ) 
-	G_free ( str );
+
+    if (str)
+	G_free(str);
 
 
     return DB_OK;
     return DB_OK;
 }
 }
 
 
 int db__driver_begin_transaction(void)
 int db__driver_begin_transaction(void)
 {
 {
-    G_debug (2, "mysql: START TRANSACTION");
+    G_debug(2, "mysql: START TRANSACTION");
 
 
     init_error();
     init_error();
 
 
-    if ( mysql_query ( connection, "START TRANSACTION" ) != 0 )
-    {
-	append_error( "Cannot start transaction: \n" );
-	append_error ( mysql_error(connection) );
+    if (mysql_query(connection, "START TRANSACTION") != 0) {
+	append_error("Cannot start transaction: \n");
+	append_error(mysql_error(connection));
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
@@ -74,14 +73,13 @@ int db__driver_begin_transaction(void)
 
 
 int db__driver_commit_transaction(void)
 int db__driver_commit_transaction(void)
 {
 {
-    G_debug (2, "mysql: COMMIT");
+    G_debug(2, "mysql: COMMIT");
 
 
     init_error();
     init_error();
 
 
-    if ( mysql_query ( connection, "COMMIT" ) != 0 )
-    {
-	append_error( "Cannot commit transaction: \n" );
-	append_error ( mysql_error(connection) );
+    if (mysql_query(connection, "COMMIT") != 0) {
+	append_error("Cannot commit transaction: \n");
+	append_error(mysql_error(connection));
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }

+ 179 - 172
db/drivers/mysql/fetch.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -16,49 +17,46 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 #include "globals.h"
 #include "globals.h"
-#include "proto.h" 
+#include "proto.h"
 
 
-int
-db__driver_fetch(dbCursor *cn, int position, int *more)
+int db__driver_fetch(dbCursor * cn, int position, int *more)
 {
 {
-    cursor     *c;
-    dbToken    token;    
-    dbTable    *table;
-    int        i;
+    cursor *c;
+    dbToken token;
+    dbTable *table;
+    int i;
 
 
     /* get cursor token */
     /* get cursor token */
     token = db_get_cursor_token(cn);
     token = db_get_cursor_token(cn);
 
 
     /* get the cursor by its token */
     /* get the cursor by its token */
     if (!(c = (cursor *) db_find_token(token))) {
     if (!(c = (cursor *) db_find_token(token))) {
-	append_error ( _("Cursor not found") );
+	append_error(_("Cursor not found"));
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     /* fetch on position */
     /* fetch on position */
-    switch (position)
-    { 
-	case DB_NEXT:
-	    c->row = mysql_fetch_row(c->res);
-	    break;
-	    
-	case DB_CURRENT:
-	    break;
-	    
-	case DB_PREVIOUS:
-	case DB_FIRST:
-	case DB_LAST:
-	default:
-	    append_error ( _("Cursor position is not supported "
-			     "by MySQL driver") );
-	    report_error();
-	    return DB_FAILED;
+    switch (position) {
+    case DB_NEXT:
+	c->row = mysql_fetch_row(c->res);
+	break;
+
+    case DB_CURRENT:
+	break;
+
+    case DB_PREVIOUS:
+    case DB_FIRST:
+    case DB_LAST:
+    default:
+	append_error(_("Cursor position is not supported "
+		       "by MySQL driver"));
+	report_error();
+	return DB_FAILED;
     }
     }
 
 
-    G_debug ( 3, "row = %d nrows = %d", c->row, c->nrows );
-    if ( c->row == NULL ) 
-    {
+    G_debug(3, "row = %d nrows = %d", c->row, c->nrows);
+    if (c->row == NULL) {
 	*more = 0;
 	*more = 0;
 	return DB_OK;
 	return DB_OK;
     }
     }
@@ -68,57 +66,56 @@ db__driver_fetch(dbCursor *cn, int position, int *more)
     /* get the data out of the descriptor into the table */
     /* get the data out of the descriptor into the table */
     table = db_get_cursor_table(cn);
     table = db_get_cursor_table(cn);
 
 
-    for (i = 0; i < c->ncols; i++) 
-    {
+    for (i = 0; i < c->ncols; i++) {
 	int col, sqltype, mysqltype;
 	int col, sqltype, mysqltype;
-	dbColumn   *column;
-	dbValue    *value;
+	dbColumn *column;
+	dbValue *value;
 	char *val;
 	char *val;
 
 
-	col = c->cols[i]; /* known column */
- 		
-	column = db_get_table_column (table, i);
-	mysqltype  = db_get_column_host_type(column);
+	col = c->cols[i];	/* known column */
+
+	column = db_get_table_column(table, i);
+	mysqltype = db_get_column_host_type(column);
 	sqltype = db_get_column_sqltype(column);
 	sqltype = db_get_column_sqltype(column);
 
 
-	value  = db_get_column_value (column);
-	db_zero_string (&value->s);
+	value = db_get_column_value(column);
+	db_zero_string(&value->s);
 	value->t.year = 0;
 	value->t.year = 0;
 	value->t.month = 0;
 	value->t.month = 0;
 	value->t.day = 0;
 	value->t.day = 0;
 	value->t.hour = 0;
 	value->t.hour = 0;
 	value->t.minute = 0;
 	value->t.minute = 0;
 	value->t.seconds = 0.0;
 	value->t.seconds = 0.0;
-	
+
 	val = c->row[i];
 	val = c->row[i];
-	if ( !val ) {
+	if (!val) {
 	    value->isNull = 1;
 	    value->isNull = 1;
 	    continue;
 	    continue;
-	} else {
+	}
+	else {
 	    value->isNull = 0;
 	    value->isNull = 0;
 	}
 	}
 
 
-	G_debug (3, "col %d, mysqltype %d, sqltype %d, val = '%s'", 
-		    col, mysqltype, sqltype, c->row[col] );
+	G_debug(3, "col %d, mysqltype %d, sqltype %d, val = '%s'",
+		col, mysqltype, sqltype, c->row[col]);
 
 
-	/* defined in /usr/include/mysql/mysql_com.h */	
-	switch (mysqltype) 
-	{
+	/* defined in /usr/include/mysql/mysql_com.h */
+	switch (mysqltype) {
 	    int ns;
 	    int ns;
 
 
-	    case MYSQL_TYPE_TINY:
-	    case MYSQL_TYPE_SHORT:
-	    case MYSQL_TYPE_LONG:
-	    case MYSQL_TYPE_INT24:
-	    case MYSQL_TYPE_LONGLONG:
-            case MYSQL_TYPE_YEAR:
-	    	value->i = atoi ( val );
-		break;
+	case MYSQL_TYPE_TINY:
+	case MYSQL_TYPE_SHORT:
+	case MYSQL_TYPE_LONG:
+	case MYSQL_TYPE_INT24:
+	case MYSQL_TYPE_LONGLONG:
+	case MYSQL_TYPE_YEAR:
+	    value->i = atoi(val);
+	    break;
 
 
-	    case MYSQL_TYPE_FLOAT:
-	    case MYSQL_TYPE_DOUBLE:
-	    	value->d = atof ( val );
-		break;
+	case MYSQL_TYPE_FLOAT:
+	case MYSQL_TYPE_DOUBLE:
+	    value->d = atof(val);
+	    break;
 
 
 	    /* MySQL TIMESTAMP < 4.1: YYYYMMDDHHMMSS TIMESTAMP(14)
 	    /* MySQL TIMESTAMP < 4.1: YYYYMMDDHHMMSS TIMESTAMP(14)
 	     *                        YYMMDDHHMMSS   TIMESTAMP(12)
 	     *                        YYMMDDHHMMSS   TIMESTAMP(12)
@@ -126,145 +123,155 @@ db__driver_fetch(dbCursor *cn, int position, int *more)
 	     *                        YYYYMMDD       TIMESTAMP(8)
 	     *                        YYYYMMDD       TIMESTAMP(8)
 	     *                        YYMMDD         TIMESTAMP(6)
 	     *                        YYMMDD         TIMESTAMP(6)
 	     *                        YYMM           TIMESTAMP(4)
 	     *                        YYMM           TIMESTAMP(4)
-	     * 			      YY             YY
+	     *                        YY             YY
 	     * MySQL TIMESTAMP >= 4.1: 'YYYY-MM-DD HH:MM:SS' (19 chars) */
 	     * MySQL TIMESTAMP >= 4.1: 'YYYY-MM-DD HH:MM:SS' (19 chars) */
-	    case MYSQL_TYPE_TIMESTAMP:
+	case MYSQL_TYPE_TIMESTAMP:
 	    {
 	    {
 		char valbuf[25], buf[10];
 		char valbuf[25], buf[10];
-	        memset ( valbuf, 0, 25 );	
-		strcpy ( valbuf, val );
-
-		switch ( strlen(val) ) 
-		{
-		    case 2: 
-		    case 4: 
-		    case 6: 
-		    case 10: 
-		    case 12: 
-		        strncpy ( buf, val, 2 ); buf[2] = 0;
-			value->t.year = atoi ( buf );
-		        strncpy ( buf, val+2, 2 ); buf[2] = 0;
-			value->t.month = atoi ( buf );
-		        strncpy ( buf, val+4, 2 ); buf[2] = 0;
-			value->t.day = atoi ( buf );
-		        strncpy ( buf, val+6, 2 ); buf[2] = 0;
-			value->t.hour = atoi ( buf );
-		        strncpy ( buf, val+8, 2 ); buf[2] = 0;
-			value->t.minute = atoi ( buf );
-		        strncpy ( buf, val+10, 2 ); buf[2] = 0;
-			value->t.seconds = atof ( buf );
-			break;
-
-		    case 8:
-		    case 14:
-		        strncpy ( buf, val, 4 ); buf[4] = 0;
-			value->t.year = atoi ( buf );
-		        strncpy ( buf, val+4, 2 ); buf[2] = 0;
-			value->t.month = atoi ( buf );
-		        strncpy ( buf, val+6, 2 ); buf[2] = 0;
-			value->t.day = atoi ( buf );
-		        strncpy ( buf, val+8, 2 ); buf[2] = 0;
-			value->t.hour = atoi ( buf );
-		        strncpy ( buf, val+10, 2 ); buf[2] = 0;
-			value->t.minute = atoi ( buf );
-		        strncpy ( buf, val+12, 2 ); buf[2] = 0;
-			value->t.seconds = atof ( buf );
-			break;
-
-		    case 19:
-		        ns = sscanf( val, "%4d-%2d-%2d %2d:%2d:%lf",
-			     &(value->t.year), &(value->t.month), 
-			     &(value->t.day), &(value->t.hour), 
-			     &(value->t.minute), &(value->t.seconds) );
-
-			if ( ns != 6 ) {
-			    append_error ( _("Cannot scan timestamp: ") );
-			    append_error ( val );
-			    report_error();
-			    return DB_FAILED;
-			}
-			break;
-
-		    default:
-			append_error ( _("Unknown timestamp format: ") );
-			append_error ( val );
+
+		memset(valbuf, 0, 25);
+		strcpy(valbuf, val);
+
+		switch (strlen(val)) {
+		case 2:
+		case 4:
+		case 6:
+		case 10:
+		case 12:
+		    strncpy(buf, val, 2);
+		    buf[2] = 0;
+		    value->t.year = atoi(buf);
+		    strncpy(buf, val + 2, 2);
+		    buf[2] = 0;
+		    value->t.month = atoi(buf);
+		    strncpy(buf, val + 4, 2);
+		    buf[2] = 0;
+		    value->t.day = atoi(buf);
+		    strncpy(buf, val + 6, 2);
+		    buf[2] = 0;
+		    value->t.hour = atoi(buf);
+		    strncpy(buf, val + 8, 2);
+		    buf[2] = 0;
+		    value->t.minute = atoi(buf);
+		    strncpy(buf, val + 10, 2);
+		    buf[2] = 0;
+		    value->t.seconds = atof(buf);
+		    break;
+
+		case 8:
+		case 14:
+		    strncpy(buf, val, 4);
+		    buf[4] = 0;
+		    value->t.year = atoi(buf);
+		    strncpy(buf, val + 4, 2);
+		    buf[2] = 0;
+		    value->t.month = atoi(buf);
+		    strncpy(buf, val + 6, 2);
+		    buf[2] = 0;
+		    value->t.day = atoi(buf);
+		    strncpy(buf, val + 8, 2);
+		    buf[2] = 0;
+		    value->t.hour = atoi(buf);
+		    strncpy(buf, val + 10, 2);
+		    buf[2] = 0;
+		    value->t.minute = atoi(buf);
+		    strncpy(buf, val + 12, 2);
+		    buf[2] = 0;
+		    value->t.seconds = atof(buf);
+		    break;
+
+		case 19:
+		    ns = sscanf(val, "%4d-%2d-%2d %2d:%2d:%lf",
+				&(value->t.year), &(value->t.month),
+				&(value->t.day), &(value->t.hour),
+				&(value->t.minute), &(value->t.seconds));
+
+		    if (ns != 6) {
+			append_error(_("Cannot scan timestamp: "));
+			append_error(val);
 			report_error();
 			report_error();
 			return DB_FAILED;
 			return DB_FAILED;
+		    }
+		    break;
+
+		default:
+		    append_error(_("Unknown timestamp format: "));
+		    append_error(val);
+		    report_error();
+		    return DB_FAILED;
 		}
 		}
 	    }
 	    }
-		break;
+	    break;
 
 
 	    /* MySQL DATE: 'YYYY-MM-DD' */
 	    /* MySQL DATE: 'YYYY-MM-DD' */
-	    case MYSQL_TYPE_DATE:
-		ns = sscanf( val, "%4d-%2d-%2d", &(value->t.year), 
-			     &(value->t.month), &(value->t.day) ); 
+	case MYSQL_TYPE_DATE:
+	    ns = sscanf(val, "%4d-%2d-%2d", &(value->t.year),
+			&(value->t.month), &(value->t.day));
+
+	    if (ns != 3) {
+		append_error(_("Cannot scan date: "));
+		append_error(val);
+		report_error();
+		return DB_FAILED;
+	    }
+	    break;
 
 
-		if ( ns != 3 ) {
-		    append_error ( _("Cannot scan date: ") );
-		    append_error ( val );
-		    report_error();
-		    return DB_FAILED;
-		}
-		break;
-			
 	    /* MySQL DATETIME: 'HH:MM:SS' */
 	    /* MySQL DATETIME: 'HH:MM:SS' */
-	    case MYSQL_TYPE_TIME:
-		ns = sscanf( val, "%2d:%2d:%lf", &(value->t.hour), 
-			     &(value->t.minute), &(value->t.seconds) );
-
-		if ( ns != 3 ) {
-		    append_error ( _("Cannot scan time: ") );
-		    append_error ( val );
-		    report_error();
-		    return DB_FAILED;
-		}
-		break;
+	case MYSQL_TYPE_TIME:
+	    ns = sscanf(val, "%2d:%2d:%lf", &(value->t.hour),
+			&(value->t.minute), &(value->t.seconds));
+
+	    if (ns != 3) {
+		append_error(_("Cannot scan time: "));
+		append_error(val);
+		report_error();
+		return DB_FAILED;
+	    }
+	    break;
 
 
 	    /* MySQL DATETIME: 'YYYY-MM-DD HH:MM:SS' */
 	    /* MySQL DATETIME: 'YYYY-MM-DD HH:MM:SS' */
-	    case MYSQL_TYPE_DATETIME:
-		ns = sscanf( val, "%4d-%2d-%2d %2d:%2d:%lf",
-			     &(value->t.year), &(value->t.month), 
-			     &(value->t.day), &(value->t.hour), 
-			     &(value->t.minute), &(value->t.seconds) );
-
-		if ( ns != 6 ) {
-		    append_error ( _("Cannot scan datetime: ") );
-		    append_error ( val );
-		    report_error();
-		    return DB_FAILED;
-		}
-		break;
-
-	    case MYSQL_TYPE_STRING:
-	    case MYSQL_TYPE_VAR_STRING:
-	    case MYSQL_TYPE_SET:
-	    case MYSQL_TYPE_ENUM:
-	    case MYSQL_TYPE_BLOB:
-		db_set_string ( &(value->s),  val );
-		break;
+	case MYSQL_TYPE_DATETIME:
+	    ns = sscanf(val, "%4d-%2d-%2d %2d:%2d:%lf",
+			&(value->t.year), &(value->t.month),
+			&(value->t.day), &(value->t.hour),
+			&(value->t.minute), &(value->t.seconds));
+
+	    if (ns != 6) {
+		append_error(_("Cannot scan datetime: "));
+		append_error(val);
+		report_error();
+		return DB_FAILED;
+	    }
+	    break;
+
+	case MYSQL_TYPE_STRING:
+	case MYSQL_TYPE_VAR_STRING:
+	case MYSQL_TYPE_SET:
+	case MYSQL_TYPE_ENUM:
+	case MYSQL_TYPE_BLOB:
+	    db_set_string(&(value->s), val);
+	    break;
 	}
 	}
     }
     }
-    G_debug (3, "Row fetched" );
+    G_debug(3, "Row fetched");
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_get_num_rows (cn )
-            dbCursor *cn;
+int db__driver_get_num_rows(cn)
+     dbCursor *cn;
 {
 {
-    cursor     *c;
-    dbToken    token;
+    cursor *c;
+    dbToken token;
 
 
     /* get cursor token */
     /* get cursor token */
     token = db_get_cursor_token(cn);
     token = db_get_cursor_token(cn);
 
 
     /* get the cursor by its token */
     /* get the cursor by its token */
     if (!(c = (cursor *) db_find_token(token))) {
     if (!(c = (cursor *) db_find_token(token))) {
-       append_error("Cursor not found");
-       report_error();
-       return DB_FAILED;
+	append_error("Cursor not found");
+	report_error();
+	return DB_FAILED;
     }
     }
 
 
-    return ( c->nrows );
+    return (c->nrows);
 }
 }
-

+ 14 - 14
db/drivers/mysql/globals.h

@@ -1,27 +1,27 @@
 #include "mysql.h"
 #include "mysql.h"
 
 
 /* cursors */
 /* cursors */
-typedef struct _cursor {
+typedef struct _cursor
+{
     MYSQL_RES *res;
     MYSQL_RES *res;
-    int nrows;             /* number of rows in query result */
+    int nrows;			/* number of rows in query result */
     MYSQL_ROW row;
     MYSQL_ROW row;
     dbToken token;
     dbToken token;
-    int type;              /* type of cursor: SELECT, UPDATE, INSERT */
-    int *cols;             /* indexes of known (type) columns */ 
-    int ncols;             /* number of known columns */
-} cursor;  
+    int type;			/* type of cursor: SELECT, UPDATE, INSERT */
+    int *cols;			/* indexes of known (type) columns */
+    int ncols;			/* number of known columns */
+} cursor;
 
 
-typedef struct {
+typedef struct
+{
     char *host, *dbname, *user, *password;
     char *host, *dbname, *user, *password;
     unsigned int port;
     unsigned int port;
 } CONNPAR;
 } CONNPAR;
 
 
 #ifdef MAIN
 #ifdef MAIN
-    MYSQL *connection; /* Database connection */
-    dbString *errMsg = NULL; /* error message */
+MYSQL *connection;		/* Database connection */
+dbString *errMsg = NULL;	/* error message */
 #else
 #else
-    extern MYSQL *connection; 
-    extern dbString *errMsg;
-#endif 
-
-
+extern MYSQL *connection;
+extern dbString *errMsg;
+#endif

+ 38 - 39
db/drivers/mysql/index.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -15,54 +16,52 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_create_index ( dbIndex *index )
+int db__driver_create_index(dbIndex * index)
 {
 {
     int i, ncols;
     int i, ncols;
     dbString sql;
     dbString sql;
-    
-    G_debug (3, "db__create_index()");
 
 
-    db_init_string (&sql);
+    G_debug(3, "db__create_index()");
+
+    db_init_string(&sql);
     init_error();
     init_error();
 
 
-    ncols = db_get_index_number_of_columns ( index );
-
-    db_set_string ( &sql, "CREATE" );
-    if ( db_test_index_type_unique(index) ) 
-	db_append_string ( &sql, " UNIQUE" );
-
-    db_append_string ( &sql, " INDEX " );
-    db_append_string ( &sql, db_get_index_name(index) );
-    db_append_string ( &sql, " ON " );
-    
-    db_append_string ( &sql, db_get_index_table_name(index) );
-    
-    db_append_string ( &sql, " ( " );
-
-    for ( i = 0; i < ncols; i++ ) {
-	if ( i > 0 )
-            db_append_string ( &sql, ", " );
-	
-        db_append_string ( &sql, db_get_index_column_name(index,i) );
+    ncols = db_get_index_number_of_columns(index);
+
+    db_set_string(&sql, "CREATE");
+    if (db_test_index_type_unique(index))
+	db_append_string(&sql, " UNIQUE");
+
+    db_append_string(&sql, " INDEX ");
+    db_append_string(&sql, db_get_index_name(index));
+    db_append_string(&sql, " ON ");
+
+    db_append_string(&sql, db_get_index_table_name(index));
+
+    db_append_string(&sql, " ( ");
+
+    for (i = 0; i < ncols; i++) {
+	if (i > 0)
+	    db_append_string(&sql, ", ");
+
+	db_append_string(&sql, db_get_index_column_name(index, i));
     }
     }
-    
-    db_append_string ( &sql, " )" );
-
-    G_debug (3, " SQL: %s", db_get_string(&sql) );
-    
-    if ( mysql_query ( connection, db_get_string(&sql) ) != 0 )
-    {
-        append_error( "Cannot create index:\n");
-	append_error( db_get_string(&sql) );
-	append_error( "\n" );
-	append_error ( mysql_error(connection) );
+
+    db_append_string(&sql, " )");
+
+    G_debug(3, " SQL: %s", db_get_string(&sql));
+
+    if (mysql_query(connection, db_get_string(&sql)) != 0) {
+	append_error("Cannot create index:\n");
+	append_error(db_get_string(&sql));
+	append_error("\n");
+	append_error(mysql_error(connection));
 	report_error();
 	report_error();
-	db_free_string ( &sql);
+	db_free_string(&sql);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
-    db_free_string ( &sql);
-    
+
+    db_free_string(&sql);
+
     return DB_OK;
     return DB_OK;
 }
 }

+ 13 - 15
db/drivers/mysql/listtab.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -18,8 +19,7 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_list_tables(dbString **tlist, int *tcount,
-                            int system)
+int db__driver_list_tables(dbString ** tlist, int *tcount, int system)
 {
 {
     int i;
     int i;
     dbString *list;
     dbString *list;
@@ -30,28 +30,26 @@ int db__driver_list_tables(dbString **tlist, int *tcount,
     *tlist = NULL;
     *tlist = NULL;
     *tcount = 0;
     *tcount = 0;
 
 
-    res = mysql_list_tables ( connection, NULL );
-    
-    if ( res  == NULL ) 
-    {
-	append_error ( _("Cannot get list of tables:\n") );
-	append_error ( mysql_error(connection) );
+    res = mysql_list_tables(connection, NULL);
+
+    if (res == NULL) {
+	append_error(_("Cannot get list of tables:\n"));
+	append_error(mysql_error(connection));
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    mysql_store_result ( connection );
+    mysql_store_result(connection);
 
 
-    nrows = (int) mysql_num_rows ( res );
-    list = db_alloc_string_array ( nrows );
+    nrows = (int)mysql_num_rows(res);
+    list = db_alloc_string_array(nrows);
 
 
     i = 0;
     i = 0;
-    while ( (row = mysql_fetch_row(res)) != NULL )
-    {
-	db_set_string ( &list[i], row[0] );
+    while ((row = mysql_fetch_row(res)) != NULL) {
+	db_set_string(&list[i], row[0]);
 	i++;
 	i++;
     }
     }
 
 
-    mysql_free_result ( res );
+    mysql_free_result(res);
 
 
     *tlist = list;
     *tlist = list;
     *tcount = nrows;
     *tcount = nrows;

+ 3 - 2
db/drivers/mysql/main.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -19,6 +20,6 @@
 
 
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 {
-	init_dbdriver();
-	exit(db_driver(argc, argv));
+    init_dbdriver();
+    exit(db_driver(argc, argv));
 }
 }

+ 58 - 62
db/drivers/mysql/parse.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -19,76 +20,71 @@
 #include "proto.h"
 #include "proto.h"
 
 
 /*
 /*
-* \brief Parse connection string in form:
-*    1) 'database_name'
-*    2) 'host=xx,port=xx,dbname=xx'
-*  \return DB_OK Success
-*  \return DB_FAILED Failed to parse database
-*/
-int parse_conn ( char *str, CONNPAR *conn )
+ * \brief Parse connection string in form:
+ *    1) 'database_name'
+ *    2) 'host=xx,port=xx,dbname=xx'
+ *  \return DB_OK Success
+ *  \return DB_FAILED Failed to parse database
+ */
+int parse_conn(char *str, CONNPAR * conn)
 {
 {
-    int  i;
+    int i;
     char **tokens, delm[2];
     char **tokens, delm[2];
 
 
-    G_debug (3, "parse_conn : %s", str ); 
-    
+    G_debug(3, "parse_conn : %s", str);
+
     /* reset */
     /* reset */
-    conn->host 	   = NULL;
-    conn->port 	   = 0;
-    conn->dbname   = NULL;
-    conn->user 	   = NULL;
+    conn->host = NULL;
+    conn->port = 0;
+    conn->dbname = NULL;
+    conn->user = NULL;
     conn->password = NULL;
     conn->password = NULL;
-    
-    if ( strchr(str, '=') == NULL ) { /*db name only */
-	conn->dbname = G_store ( str );
-    } else {
-	delm[0] = ','; delm[1] = '\0';
-        tokens = G_tokenize ( str, delm );
+
+    if (strchr(str, '=') == NULL) {	/*db name only */
+	conn->dbname = G_store(str);
+    }
+    else {
+	delm[0] = ',';
+	delm[1] = '\0';
+	tokens = G_tokenize(str, delm);
 	i = 0;
 	i = 0;
-	while ( tokens[i] ) {
-	   G_debug (3, "token %d : %s", i, tokens[i] ); 
-	   if ( strncmp(tokens[i], "host", 4 ) == 0 )
-	   {
-	       conn->host = G_store ( tokens[i] + 5 );
-	   }
-	   else if ( strncmp(tokens[i], "port", 4 ) == 0 )
-	   {
-	       long port = atol( tokens[i] + 5 );
-	       if ( port <= 0 )
-	       { 
-	           append_error ( _("Wrong port number in MySQL database "
-			            "definition: ") );
-		   append_error ( tokens[i] + 5 );
-		   return DB_FAILED;
-	       }
-	       conn->port = (unsigned int)port;
-	   }
-	   else if ( strncmp(tokens[i], "dbname", 6 ) == 0 )
-	   {
-	       conn->dbname = G_store ( tokens[i] + 7 );
-	   }
-	   else if ( strncmp(tokens[i], "user", 4 ) == 0 )
-	   {
-	       G_warning ( _("'user' in database definition "
-			     "is not supported, use db.login") );
-	   }
-	   else if ( strncmp(tokens[i], "password", 8 ) == 0 )
-	   {
-	       G_warning ( _("'password' in database definition "
-			     "is not supported, use db.login") );
-	   }
-	   else 
-	   {
-	       append_error ( _("Unknown option in database definition for ") );
-	       append_error ( "MySQL: " );
-	       append_error ( tokens[i] );
-	       return DB_FAILED;
-	   }
-	   i++;
+	while (tokens[i]) {
+	    G_debug(3, "token %d : %s", i, tokens[i]);
+	    if (strncmp(tokens[i], "host", 4) == 0) {
+		conn->host = G_store(tokens[i] + 5);
+	    }
+	    else if (strncmp(tokens[i], "port", 4) == 0) {
+		long port = atol(tokens[i] + 5);
+
+		if (port <= 0) {
+		    append_error(_("Wrong port number in MySQL database "
+				   "definition: "));
+		    append_error(tokens[i] + 5);
+		    return DB_FAILED;
+		}
+		conn->port = (unsigned int)port;
+	    }
+	    else if (strncmp(tokens[i], "dbname", 6) == 0) {
+		conn->dbname = G_store(tokens[i] + 7);
+	    }
+	    else if (strncmp(tokens[i], "user", 4) == 0) {
+		G_warning(_("'user' in database definition "
+			    "is not supported, use db.login"));
+	    }
+	    else if (strncmp(tokens[i], "password", 8) == 0) {
+		G_warning(_("'password' in database definition "
+			    "is not supported, use db.login"));
+	    }
+	    else {
+		append_error(_("Unknown option in database definition for "));
+		append_error("MySQL: ");
+		append_error(tokens[i]);
+		return DB_FAILED;
+	    }
+	    i++;
 	}
 	}
-	G_free_tokens ( tokens );	
+	G_free_tokens(tokens);
     }
     }
 
 
     return DB_OK;
     return DB_OK;
 }
 }
-

+ 9 - 9
db/drivers/mysql/proto.h

@@ -1,15 +1,15 @@
 /* error.c */
 /* error.c */
-void init_error ( void );
-void append_error ( const char * );
-void report_error ( void );
+void init_error(void);
+void append_error(const char *);
+void report_error(void);
 
 
 /* cursor.c */
 /* cursor.c */
-cursor * alloc_cursor ();
-void free_cursor ( cursor * );
+cursor *alloc_cursor();
+void free_cursor(cursor *);
 
 
-/* describe.c*/
-int describe_table( MYSQL_RES *, dbTable **, cursor * );
-void field_info ( MYSQL_FIELD *, int *, int * );
+/* describe.c */
+int describe_table(MYSQL_RES *, dbTable **, cursor *);
+void field_info(MYSQL_FIELD *, int *, int *);
 
 
 /* parse.c */
 /* parse.c */
-int parse_conn ( char *, CONNPAR *);
+int parse_conn(char *, CONNPAR *);

+ 22 - 21
db/drivers/mysql/select.c

@@ -1,3 +1,4 @@
+
 /**********************************************************
 /**********************************************************
  * MODULE:    mysql
  * MODULE:    mysql
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
@@ -17,12 +18,11 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_open_select_cursor (dbString *sel, dbCursor *dbc, int mode)
-
+int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode)
 {
 {
-    cursor   *c;
-    dbTable  *table;
-    char     *str;
+    cursor *c;
+    dbTable *table;
+    char *str;
 
 
     init_error();
     init_error();
 
 
@@ -36,39 +36,40 @@ int db__driver_open_select_cursor (dbString *sel, dbCursor *dbc, int mode)
 
 
     /* \ must be escaped, see explanation in 
     /* \ must be escaped, see explanation in 
      * db_driver_execute_immediate() */
      * db_driver_execute_immediate() */
-    str = G_str_replace ( db_get_string(sel), "\\", "\\\\" );
-    G_debug ( 3, "Escaped SQL: %s", str );
+    str = G_str_replace(db_get_string(sel), "\\", "\\\\");
+    G_debug(3, "Escaped SQL: %s", str);
 
 
-    if ( mysql_query ( connection, str ) != 0 )
-    {
+    if (mysql_query(connection, str) != 0) {
 	append_error(_("Cannot select data: \n"));
 	append_error(_("Cannot select data: \n"));
-	append_error ( db_get_string(sel) );
-	append_error ( "\n" );
-	append_error ( mysql_error(connection) );
-        if ( str ) G_free ( str );
+	append_error(db_get_string(sel));
+	append_error("\n");
+	append_error(mysql_error(connection));
+	if (str)
+	    G_free(str);
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    if ( str ) G_free ( str );
-    c->res = mysql_store_result ( connection );
+    if (str)
+	G_free(str);
+    c->res = mysql_store_result(connection);
 
 
-    if ( c->res == NULL ) {
-	append_error ( db_get_string(sel) );
-	append_error ( "\n" );
-	append_error ( mysql_error(connection) );
+    if (c->res == NULL) {
+	append_error(db_get_string(sel));
+	append_error("\n");
+	append_error(mysql_error(connection));
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    if ( describe_table( c->res, &table, c) == DB_FAILED ) {
+    if (describe_table(c->res, &table, c) == DB_FAILED) {
 	append_error("Cannot describe table\n");
 	append_error("Cannot describe table\n");
 	report_error();
 	report_error();
 	mysql_free_result(c->res);
 	mysql_free_result(c->res);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    c->nrows = (int) mysql_num_rows ( c->res );
+    c->nrows = (int)mysql_num_rows(c->res);
 
 
     /* record table with dbCursor */
     /* record table with dbCursor */
     db_set_cursor_table(dbc, table);
     db_set_cursor_table(dbc, table);

+ 16 - 18
db/drivers/odbc/connect.c

@@ -3,44 +3,42 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int open_connection ()
+int open_connection()
 {
 {
-    SQLRETURN   ret;  
+    SQLRETURN ret;
 
 
     /* Allocate Environment handle and register version */
     /* Allocate Environment handle and register version */
-    ret = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE, &ODenvi);
-    if (( ret != SQL_SUCCESS) && ( ret != SQL_SUCCESS_WITH_INFO))
-    {
+    ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODenvi);
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
 	report_error("SQLAllocHandle()");
 	report_error("SQLAllocHandle()");
 	return DB_FAILED;
 	return DB_FAILED;
-    }  
+    }
 
 
-    ret = SQLSetEnvAttr(ODenvi, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
-    if ((ret != SQL_SUCCESS) && ( ret != SQL_SUCCESS_WITH_INFO))
-    {  
-    	report_error("SQLSetEnvAttr()");
-	SQLFreeHandle(SQL_HANDLE_ENV, ODenvi); 
+    ret =
+	SQLSetEnvAttr(ODenvi, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
+	report_error("SQLSetEnvAttr()");
+	SQLFreeHandle(SQL_HANDLE_ENV, ODenvi);
 	return DB_FAILED;
 	return DB_FAILED;
-    }  
+    }
 
 
     /* Allocate connection handle */
     /* Allocate connection handle */
     ret = SQLAllocHandle(SQL_HANDLE_DBC, ODenvi, &ODconn);
     ret = SQLAllocHandle(SQL_HANDLE_DBC, ODenvi, &ODconn);
-    if (( ret != SQL_SUCCESS) && ( ret != SQL_SUCCESS_WITH_INFO))
-    {
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
 	report_error("SQLAllocHandle()");
 	report_error("SQLAllocHandle()");
 	SQLFreeHandle(SQL_HANDLE_ENV, ODenvi);
 	SQLFreeHandle(SQL_HANDLE_ENV, ODenvi);
 	return DB_FAILED;
 	return DB_FAILED;
-    }  
+    }
 
 
     /* Set timeout */
     /* Set timeout */
-    SQLSetConnectAttr( ODconn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
+    SQLSetConnectAttr(ODconn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 5, 0);
 
 
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int close_connection ()
+int close_connection()
 {
 {
     SQLFreeHandle(SQL_HANDLE_DBC, ODconn);
     SQLFreeHandle(SQL_HANDLE_DBC, ODconn);
-    SQLFreeHandle(SQL_HANDLE_ENV, ODenvi);     
+    SQLFreeHandle(SQL_HANDLE_ENV, ODenvi);
     return DB_OK;
     return DB_OK;
 }
 }

+ 18 - 17
db/drivers/odbc/create_table.c

@@ -3,39 +3,40 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_create_table (dbTable *table)
+int db__driver_create_table(dbTable * table)
 {
 {
     dbString sql;
     dbString sql;
     cursor *c;
     cursor *c;
     char msg[OD_MSG];
     char msg[OD_MSG];
     char *emsg = NULL;
     char *emsg = NULL;
-    SQLRETURN   ret;
-    SQLINTEGER   err;
-    
-    G_debug (3, "db__driver_create_table()");
+    SQLRETURN ret;
+    SQLINTEGER err;
 
 
-    db_init_string (&sql);
-    db_table_to_sql ( table, &sql );
-    
-    G_debug (3, " SQL: %s", db_get_string(&sql) );
+    G_debug(3, "db__driver_create_table()");
+
+    db_init_string(&sql);
+    db_table_to_sql(table, &sql);
+
+    G_debug(3, " SQL: %s", db_get_string(&sql));
 
 
     c = alloc_cursor();
     c = alloc_cursor();
-      if (c == NULL)
-	  return DB_FAILED;
+    if (c == NULL)
+	return DB_FAILED;
 
 
-    ret = SQLExecDirect ( c->stmt, db_get_string(&sql), SQL_NTS );
+    ret = SQLExecDirect(c->stmt, db_get_string(&sql), SQL_NTS);
 
 
     if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
     if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
-	SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt, 1, NULL, &err, msg, sizeof(msg), NULL);
-	G_asprintf(&emsg, "SQLExecDirect():\n%s\n%s (%d)\n", db_get_string(&sql), msg, (int) err);
+	SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt, 1, NULL, &err, msg,
+		      sizeof(msg), NULL);
+	G_asprintf(&emsg, "SQLExecDirect():\n%s\n%s (%d)\n",
+		   db_get_string(&sql), msg, (int)err);
 	report_error(emsg);
 	report_error(emsg);
-        G_free(emsg);
+	G_free(emsg);
 
 
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    free_cursor (c);
+    free_cursor(c);
 
 
     return DB_OK;
     return DB_OK;
 }
 }

+ 26 - 31
db/drivers/odbc/cursor.c

@@ -6,8 +6,7 @@
 #include "proto.h"
 #include "proto.h"
 
 
 
 
-int
-db__driver_close_cursor(dbCursor *dbc)
+int db__driver_close_cursor(dbCursor * dbc)
 {
 {
     cursor *c;
     cursor *c;
 
 
@@ -23,50 +22,46 @@ db__driver_close_cursor(dbCursor *dbc)
 }
 }
 
 
 
 
-cursor * alloc_cursor()
+cursor *alloc_cursor()
 {
 {
-    cursor     *c;
-    SQLRETURN  ret;
-    char       msg[OD_MSG];
-    char       *emsg = NULL;
+    cursor *c;
+    SQLRETURN ret;
+    char msg[OD_MSG];
+    char *emsg = NULL;
     SQLINTEGER err;
     SQLINTEGER err;
 
 
     /* allocate the cursor */
     /* allocate the cursor */
     c = (cursor *) db_malloc(sizeof(cursor));
     c = (cursor *) db_malloc(sizeof(cursor));
-    if (c == NULL)
-    {
-        report_error("allocate cursor");
-        return c; 
-    } 
-    
-    ret=SQLAllocHandle(SQL_HANDLE_STMT, ODconn, &c->stmt);
-    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
-    {
-        SQLGetDiagRec(SQL_HANDLE_DBC, ODconn, 1, NULL, &err, msg, sizeof(msg), NULL);
-        G_asprintf(&emsg, "AllocStatement()\n%s (%d)\n", msg, (int) err);
-        report_error(emsg);
-        G_free(emsg);
+    if (c == NULL) {
+	report_error("allocate cursor");
+	return c;
+    }
 
 
-        return c;
-    }  
+    ret = SQLAllocHandle(SQL_HANDLE_STMT, ODconn, &c->stmt);
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
+	SQLGetDiagRec(SQL_HANDLE_DBC, ODconn, 1, NULL, &err, msg, sizeof(msg),
+		      NULL);
+	G_asprintf(&emsg, "AllocStatement()\n%s (%d)\n", msg, (int)err);
+	report_error(emsg);
+	G_free(emsg);
+
+	return c;
+    }
 
 
     /* tokenize it */
     /* tokenize it */
     c->token = db_new_token(c);
     c->token = db_new_token(c);
-    if (c->token < 0)       
-    {
-	free_cursor (c);
+    if (c->token < 0) {
+	free_cursor(c);
 	c = NULL;
 	c = NULL;
-        report_error("db_new_token()");
+	report_error("db_new_token()");
     }
     }
 
 
     return c;
     return c;
 }
 }
 
 
-void free_cursor( cursor *c)
+void free_cursor(cursor * c)
 {
 {
     db_drop_token(c->token);
     db_drop_token(c->token);
-    SQLFreeHandle(SQL_HANDLE_STMT, c->stmt);  
-    G_free(c);  
+    SQLFreeHandle(SQL_HANDLE_STMT, c->stmt);
+    G_free(c);
 }
 }
-
-

+ 28 - 30
db/drivers/odbc/db.c

@@ -4,51 +4,49 @@
 #include <grass/gis.h>
 #include <grass/gis.h>
 #include "odbc.h"
 #include "odbc.h"
 #include "globals.h"
 #include "globals.h"
-#include "proto.h" 
+#include "proto.h"
 
 
-int
-db__driver_open_database (dbHandle *handle)
+int db__driver_open_database(dbHandle * handle)
 {
 {
-    char        msg[OD_MSG];
-    const char  *name;
-    char        *emsg = NULL;
-    SQLRETURN   ret; 
-    SQLINTEGER  err;
+    char msg[OD_MSG];
+    const char *name;
+    char *emsg = NULL;
+    SQLRETURN ret;
+    SQLINTEGER err;
     dbConnection connection;
     dbConnection connection;
 
 
     /* Open connection */
     /* Open connection */
-    if ( open_connection() != DB_OK )
-        return DB_FAILED;  
+    if (open_connection() != DB_OK)
+	return DB_FAILED;
 
 
-    db_get_connection( &connection );
+    db_get_connection(&connection);
     name = db_get_handle_dbname(handle);
     name = db_get_handle_dbname(handle);
-    
+
     /* if name is empty use connection.databaseName */
     /* if name is empty use connection.databaseName */
-    if( strlen(name) == 0 )
-    {
-        name = connection.databaseName;
-    } 
-
-    /* Connect to the datasource */ 
-    ret = SQLConnect( ODconn, (SQLCHAR*) name, SQL_NTS,
-           (SQLCHAR*) connection.user, SQL_NTS, (SQLCHAR*) connection.password, SQL_NTS );    
-    if ((ret != SQL_SUCCESS) && ( ret != SQL_SUCCESS_WITH_INFO) )
-    {
-        SQLGetDiagRec( SQL_HANDLE_DBC, ODconn, 1, NULL, &err, msg, sizeof(msg), NULL );
-	G_asprintf(&emsg, "SQLConnect():\n%s (%d)\n", msg, (int) err);
-	report_error( emsg );
-        G_free(emsg);
+    if (strlen(name) == 0) {
+	name = connection.databaseName;
+    }
+
+    /* Connect to the datasource */
+    ret = SQLConnect(ODconn, (SQLCHAR *) name, SQL_NTS,
+		     (SQLCHAR *) connection.user, SQL_NTS,
+		     (SQLCHAR *) connection.password, SQL_NTS);
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
+	SQLGetDiagRec(SQL_HANDLE_DBC, ODconn, 1, NULL, &err, msg, sizeof(msg),
+		      NULL);
+	G_asprintf(&emsg, "SQLConnect():\n%s (%d)\n", msg, (int)err);
+	report_error(emsg);
+	G_free(emsg);
 
 
 	return DB_FAILED;
 	return DB_FAILED;
-    }    
+    }
 
 
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_close_database()
+int db__driver_close_database()
 {
 {
-    SQLDisconnect( ODconn );  
+    SQLDisconnect(ODconn);
     close_connection();
     close_connection();
     return DB_OK;
     return DB_OK;
 }
 }

+ 1 - 0
db/drivers/odbc/dbdriver.h

@@ -18,6 +18,7 @@ int db__driver_list_databases();
 int db__driver_list_tables();
 int db__driver_list_tables();
 int db__driver_open_select_cursor();
 int db__driver_open_select_cursor();
 int db__driver_drop_table();
 int db__driver_drop_table();
+
 #define	init_dbdriver() do{\
 #define	init_dbdriver() do{\
 db_driver_create_table = db__driver_create_table;\
 db_driver_create_table = db__driver_create_table;\
 db_driver_close_cursor = db__driver_close_cursor;\
 db_driver_close_cursor = db__driver_close_cursor;\

+ 119 - 117
db/drivers/odbc/describe.c

@@ -5,79 +5,78 @@
 #include "proto.h"
 #include "proto.h"
 #include <stdio.h>
 #include <stdio.h>
 
 
-int set_column_type ( dbColumn *column, int otype );   
+int set_column_type(dbColumn * column, int otype);
 
 
-int db__driver_describe_table (table_name, table)
-    dbString *table_name;
-    dbTable **table;
+int db__driver_describe_table(table_name, table)
+     dbString *table_name;
+     dbTable **table;
 {
 {
-    char        *name = NULL;
-    SQLINTEGER  err;    
-    SQLRETURN   ret;
-    cursor      *c;
-    char        s[100];
-    char        msg[OD_MSG];
-    char        *emsg;
+    char *name = NULL;
+    SQLINTEGER err;
+    SQLRETURN ret;
+    cursor *c;
+    char s[100];
+    char msg[OD_MSG];
+    char *emsg;
 
 
     /* allocate cursor */
     /* allocate cursor */
     c = alloc_cursor();
     c = alloc_cursor();
     if (c == NULL)
     if (c == NULL)
-        return DB_FAILED;
+	return DB_FAILED;
 
 
-    name = db_get_string (table_name);
+    name = db_get_string(table_name);
 
 
-    SQLSetStmtAttr( c->stmt, SQL_MAX_ROWS, (SQLPOINTER *)1, 0);
+    SQLSetStmtAttr(c->stmt, SQL_MAX_ROWS, (SQLPOINTER *) 1, 0);
 
 
-    sprintf(s,"select * from %s", name);
+    sprintf(s, "select * from %s", name);
 
 
-    ret = SQLExecDirect(c->stmt,s,SQL_NTS); 
-    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
-    {
-        SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt, 1, NULL, &err, msg, sizeof(msg), NULL);        
-	G_asprintf(&emsg, "SQLExecDirect():\n%s\n%s (%d)\n",s,msg,(int) err);
-        report_error(emsg);
-        return DB_FAILED;
+    ret = SQLExecDirect(c->stmt, s, SQL_NTS);
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
+	SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt, 1, NULL, &err, msg,
+		      sizeof(msg), NULL);
+	G_asprintf(&emsg, "SQLExecDirect():\n%s\n%s (%d)\n", s, msg,
+		   (int)err);
+	report_error(emsg);
+	return DB_FAILED;
     }
     }
-    
+
     describe_table(c->stmt, table);
     describe_table(c->stmt, table);
 
 
-    free_cursor (c);
+    free_cursor(c);
 
 
     /* set the table name */
     /* set the table name */
-    db_set_table_name (*table, name);
+    db_set_table_name(*table, name);
 
 
     /* set the table description */
     /* set the table description */
     db_set_table_description(*table, "");
     db_set_table_description(*table, "");
 
 
     /* 
     /* 
-    db_set_table_delete_priv_granted (*table);
-    db_set_table_delete_priv_not_granted (*table);
-    db_set_table_insert_priv_granted (*table);
-    db_set_table_insert_priv_not_granted (*table);
-    */
+       db_set_table_delete_priv_granted (*table);
+       db_set_table_delete_priv_not_granted (*table);
+       db_set_table_insert_priv_granted (*table);
+       db_set_table_insert_priv_not_granted (*table);
+     */
 
 
     return DB_OK;
     return DB_OK;
 }
 }
 
 
 
 
-int
-describe_table(stmt, table)
-    SQLHSTMT  stmt;
-    dbTable **table;
+int describe_table(stmt, table)
+     SQLHSTMT stmt;
+     dbTable **table;
 {
 {
-    dbColumn     *column;       
-    int          col, intval;
-    SQLUSMALLINT ncols;      
-    SQLRETURN    ret;  
-    SQLCHAR      charval[100];
+    dbColumn *column;
+    int col, intval;
+    SQLUSMALLINT ncols;
+    SQLRETURN ret;
+    SQLCHAR charval[100];
 
 
     /* get the number of colummns */
     /* get the number of colummns */
-    ret = SQLNumResultCols( stmt, &ncols);
-    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
-    {
+    ret = SQLNumResultCols(stmt, &ncols);
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
 	report_error("SQLNumResultCols()");
 	report_error("SQLNumResultCols()");
 	return DB_FAILED;
 	return DB_FAILED;
-    }  
+    }
 
 
     /* allocate a table structure to correspond to our system descriptor */
     /* allocate a table structure to correspond to our system descriptor */
     if (!(*table = db_alloc_table(ncols))) {
     if (!(*table = db_alloc_table(ncols))) {
@@ -85,130 +84,133 @@ describe_table(stmt, table)
     }
     }
 
 
     /* Get col names and attributes */
     /* Get col names and attributes */
-    for (col=0; col < ncols; col++)
-    {    
-        column = db_get_table_column (*table, col);
+    for (col = 0; col < ncols; col++) {
+	column = db_get_table_column(*table, col);
 
 
-        SQLColAttribute( stmt, col+1, SQL_COLUMN_NAME, charval, sizeof(charval), NULL, NULL );
-        db_set_column_name (column, charval);   
+	SQLColAttribute(stmt, col + 1, SQL_COLUMN_NAME, charval,
+			sizeof(charval), NULL, NULL);
+	db_set_column_name(column, charval);
 
 
 	/* label(title) is not description, but I did not found better attribute and it can say something about column */
 	/* label(title) is not description, but I did not found better attribute and it can say something about column */
-        SQLColAttribute( stmt, col+1, SQL_COLUMN_LABEL, charval, sizeof(charval), NULL, NULL );
-        db_set_column_description(column, charval);
+	SQLColAttribute(stmt, col + 1, SQL_COLUMN_LABEL, charval,
+			sizeof(charval), NULL, NULL);
+	db_set_column_description(column, charval);
 
 
-        SQLColAttribute( stmt, col+1, SQL_COLUMN_LENGTH , NULL, 0, NULL, &intval );
-        db_set_column_length (column, intval);
+	SQLColAttribute(stmt, col + 1, SQL_COLUMN_LENGTH, NULL, 0, NULL,
+			&intval);
+	db_set_column_length(column, intval);
 
 
-        SQLColAttribute( stmt, col+1, SQL_COLUMN_PRECISION , NULL, 0, NULL, &intval );
-        db_set_column_precision (column, intval);
+	SQLColAttribute(stmt, col + 1, SQL_COLUMN_PRECISION, NULL, 0, NULL,
+			&intval);
+	db_set_column_precision(column, intval);
 
 
-        SQLColAttribute( stmt, col+1, SQL_COLUMN_SCALE , NULL, 0, NULL, &intval ); 
-        db_set_column_scale (column, intval);
+	SQLColAttribute(stmt, col + 1, SQL_COLUMN_SCALE, NULL, 0, NULL,
+			&intval);
+	db_set_column_scale(column, intval);
 
 
-        SQLColAttribute( stmt, col+1, SQL_COLUMN_NULLABLE , NULL, 0, NULL, &intval );  
-        if ( intval == SQL_NULLABLE )
-            db_set_column_null_allowed (column);
-        else
-            db_unset_column_null_allowed (column);
+	SQLColAttribute(stmt, col + 1, SQL_COLUMN_NULLABLE, NULL, 0, NULL,
+			&intval);
+	if (intval == SQL_NULLABLE)
+	    db_set_column_null_allowed(column);
+	else
+	    db_unset_column_null_allowed(column);
 
 
 	/*
 	/*
-        db_set_column_select_priv_not_granted (column);
-	db_set_column_select_priv_not_granted (column);
-	db_set_column_update_priv_granted (column);
-	db_set_column_update_priv_not_granted (column);
-	*/
+	   db_set_column_select_priv_not_granted (column);
+	   db_set_column_select_priv_not_granted (column);
+	   db_set_column_update_priv_granted (column);
+	   db_set_column_update_priv_not_granted (column);
+	 */
 
 
 	/* because set_column_type() uses other attributes (length, precision,...) must be called at the end */
 	/* because set_column_type() uses other attributes (length, precision,...) must be called at the end */
-        SQLColAttribute( stmt, col+1, SQL_COLUMN_TYPE,  NULL, 0, NULL, &intval );
-        set_column_type (column, intval);
-	db_set_column_host_type (column, intval);   	
+	SQLColAttribute(stmt, col + 1, SQL_COLUMN_TYPE, NULL, 0, NULL,
+			&intval);
+	set_column_type(column, intval);
+	db_set_column_host_type(column, intval);
 
 
 	/* set default value after we recognized type */
 	/* set default value after we recognized type */
 	/*
 	/*
-	db_set_column_has_defined_default_value(column);
-	db_set_column_has_undefined_default_value(column);
-	db_set_column_use_default_value(column);
-	db_unset_column_use_default_value(column);
-	*/
+	   db_set_column_has_defined_default_value(column);
+	   db_set_column_has_undefined_default_value(column);
+	   db_set_column_use_default_value(column);
+	   db_unset_column_use_default_value(column);
+	 */
 	/* and set column.defaultValue */
 	/* and set column.defaultValue */
-    }															
+    }
 
 
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int 
-set_column_type ( column, otype)
-    dbColumn *column;
-    int otype;
+int set_column_type(column, otype)
+     dbColumn *column;
+     int otype;
 {
 {
     int dbtype;
     int dbtype;
-    
+
     /* determine the DBMI datatype from ODBC type */
     /* determine the DBMI datatype from ODBC type */
-    switch ( otype )
-    {  
-    /* numbers */
+    switch (otype) {
+	/* numbers */
     case SQL_INTEGER:
     case SQL_INTEGER:
-        dbtype = DB_SQL_TYPE_INTEGER;
-        break;
+	dbtype = DB_SQL_TYPE_INTEGER;
+	break;
     case SQL_SMALLINT:
     case SQL_SMALLINT:
-        dbtype = DB_SQL_TYPE_SMALLINT;
-        break;    
+	dbtype = DB_SQL_TYPE_SMALLINT;
+	break;
     case SQL_REAL:
     case SQL_REAL:
-    	    dbtype = DB_SQL_TYPE_REAL;    
+	dbtype = DB_SQL_TYPE_REAL;
 	break;
 	break;
     case SQL_DOUBLE:
     case SQL_DOUBLE:
-        dbtype = DB_SQL_TYPE_DOUBLE_PRECISION;
-        break;
+	dbtype = DB_SQL_TYPE_DOUBLE_PRECISION;
+	break;
     case SQL_FLOAT:
     case SQL_FLOAT:
-        if (db_get_column_precision(column) == 24 )
-    	    dbtype = DB_SQL_TYPE_REAL;    
-        else
-            dbtype = DB_SQL_TYPE_DOUBLE_PRECISION; /* precision == 53 */
+	if (db_get_column_precision(column) == 24)
+	    dbtype = DB_SQL_TYPE_REAL;
+	else
+	    dbtype = DB_SQL_TYPE_DOUBLE_PRECISION;	/* precision == 53 */
 	break;
 	break;
     case SQL_DECIMAL:
     case SQL_DECIMAL:
-        dbtype = DB_SQL_TYPE_DECIMAL;
-        break;
+	dbtype = DB_SQL_TYPE_DECIMAL;
+	break;
     case SQL_NUMERIC:
     case SQL_NUMERIC:
-        dbtype = DB_SQL_TYPE_NUMERIC;
-        break;
+	dbtype = DB_SQL_TYPE_NUMERIC;
+	break;
 
 
-    /* strings */
+	/* strings */
     case SQL_CHAR:
     case SQL_CHAR:
-        dbtype = DB_SQL_TYPE_CHARACTER;
-        break;   
+	dbtype = DB_SQL_TYPE_CHARACTER;
+	break;
     case SQL_VARCHAR:
     case SQL_VARCHAR:
-        dbtype = DB_SQL_TYPE_CHARACTER;
+	dbtype = DB_SQL_TYPE_CHARACTER;
 	break;
 	break;
     case SQL_LONGVARCHAR:
     case SQL_LONGVARCHAR:
-        dbtype = DB_SQL_TYPE_CHARACTER;
-	break;	
+	dbtype = DB_SQL_TYPE_CHARACTER;
+	break;
 
 
-    /* date & time */
+	/* date & time */
     case SQL_DATE:
     case SQL_DATE:
 	dbtype = DB_SQL_TYPE_DATE;
 	dbtype = DB_SQL_TYPE_DATE;
-        break;  
+	break;
     case SQL_TYPE_DATE:
     case SQL_TYPE_DATE:
 	dbtype = DB_SQL_TYPE_DATE;
 	dbtype = DB_SQL_TYPE_DATE;
-        break;	
+	break;
     case SQL_TIME:
     case SQL_TIME:
 	dbtype = DB_SQL_TYPE_TIME;
 	dbtype = DB_SQL_TYPE_TIME;
-        break;  
+	break;
     case SQL_TYPE_TIME:
     case SQL_TYPE_TIME:
 	dbtype = DB_SQL_TYPE_TIME;
 	dbtype = DB_SQL_TYPE_TIME;
-        break;
+	break;
     case SQL_TIMESTAMP:
     case SQL_TIMESTAMP:
 	dbtype = DB_SQL_TYPE_TIMESTAMP;
 	dbtype = DB_SQL_TYPE_TIMESTAMP;
-        break;
+	break;
     case SQL_TYPE_TIMESTAMP:
     case SQL_TYPE_TIMESTAMP:
 	dbtype = DB_SQL_TYPE_TIMESTAMP;
 	dbtype = DB_SQL_TYPE_TIMESTAMP;
-        break;
+	break;
 
 
     default:
     default:
-        dbtype = DB_SQL_TYPE_UNKNOWN;
-        break;
+	dbtype = DB_SQL_TYPE_UNKNOWN;
+	break;
     }
     }
-	 
-    db_set_column_sqltype (column, dbtype);
+
+    db_set_column_sqltype(column, dbtype);
     return DB_OK;
     return DB_OK;
-} 
-  		    							    													    							
+}

+ 3 - 4
db/drivers/odbc/driver.c

@@ -2,14 +2,13 @@
 #include "odbc.h"
 #include "odbc.h"
 #include "globals.h"
 #include "globals.h"
 
 
-int
-db__driver_init (argc, argv) char *argv[];
+int db__driver_init(argc, argv)
+     char *argv[];
 {
 {
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_finish()
+int db__driver_finish()
 {
 {
     return DB_OK;
     return DB_OK;
 }
 }

+ 4 - 4
db/drivers/odbc/error.c

@@ -1,3 +1,4 @@
+
 /**
 /**
  * \file error.c
  * \file error.c
  *
  *
@@ -26,12 +27,11 @@
  * \param[in] err error message
  * \param[in] err error message
  */
  */
 
 
-void
-report_error (char *err)
+void report_error(char *err)
 {
 {
     char *msg = NULL;
     char *msg = NULL;
 
 
-    G_asprintf (&msg, "DBMI-ODBC driver error: %s", err);
-    db_error (msg);
+    G_asprintf(&msg, "DBMI-ODBC driver error: %s", err);
+    db_error(msg);
     G_free(msg);
     G_free(msg);
 }
 }

+ 17 - 17
db/drivers/odbc/execute.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       execute
  * MODULE:       execute
@@ -19,35 +20,34 @@
 #include "proto.h"
 #include "proto.h"
 
 
 
 
-int
-db__driver_execute_immediate (dbString *sql)
+int db__driver_execute_immediate(dbString * sql)
 {
 {
     char *s, msg[OD_MSG];
     char *s, msg[OD_MSG];
     char *emsg;
     char *emsg;
     cursor *c;
     cursor *c;
-    SQLRETURN   ret;
-    SQLINTEGER   err;     
+    SQLRETURN ret;
+    SQLINTEGER err;
 
 
-    s = db_get_string (sql);
+    s = db_get_string(sql);
 
 
     /* allocate cursor */
     /* allocate cursor */
     c = alloc_cursor();
     c = alloc_cursor();
-        if (c == NULL)
-            return DB_FAILED;   
-		    
-    ret = SQLExecDirect(c->stmt,s,SQL_NTS);
-    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
-    {
-	SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt, 1, NULL, &err, msg, sizeof(msg), NULL);
-	G_asprintf(&emsg, "SQLExecDirect():\n%s\n%s (%d)\n", s, msg, (int) err);
+    if (c == NULL)
+	return DB_FAILED;
+
+    ret = SQLExecDirect(c->stmt, s, SQL_NTS);
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
+	SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt, 1, NULL, &err, msg,
+		      sizeof(msg), NULL);
+	G_asprintf(&emsg, "SQLExecDirect():\n%s\n%s (%d)\n", s, msg,
+		   (int)err);
 	report_error(emsg);
 	report_error(emsg);
-        G_free(emsg);
+	G_free(emsg);
 
 
 	return DB_FAILED;
 	return DB_FAILED;
-    }   
+    }
 
 
-    free_cursor (c);
+    free_cursor(c);
 
 
     return DB_OK;
     return DB_OK;
 }
 }
-

+ 88 - 83
db/drivers/odbc/fetch.c

@@ -1,23 +1,22 @@
 #include <grass/dbmi.h>
 #include <grass/dbmi.h>
 #include "odbc.h"
 #include "odbc.h"
 #include "globals.h"
 #include "globals.h"
-#include "proto.h" 
+#include "proto.h"
 
 
-int
-db__driver_fetch(cn, position, more)
-    dbCursor *cn;
-    int position;
-    int *more;
+int db__driver_fetch(cn, position, more)
+     dbCursor *cn;
+     int position;
+     int *more;
 {
 {
-    cursor     *c;
-    dbToken    token;    
-    dbTable    *table;
-    dbColumn   *column;
-    dbValue    *value;
-    int        col, ncols;
+    cursor *c;
+    dbToken token;
+    dbTable *table;
+    dbColumn *column;
+    dbValue *value;
+    int col, ncols;
     SQLINTEGER len;
     SQLINTEGER len;
-    int        htype, sqltype, ctype;
-    SQLRETURN  ret;
+    int htype, sqltype, ctype;
+    SQLRETURN ret;
     DATE_STRUCT date;
     DATE_STRUCT date;
     TIME_STRUCT time;
     TIME_STRUCT time;
     TIMESTAMP_STRUCT timestamp;
     TIMESTAMP_STRUCT timestamp;
@@ -26,100 +25,103 @@ db__driver_fetch(cn, position, more)
     token = db_get_cursor_token(cn);
     token = db_get_cursor_token(cn);
 
 
     /* get the cursor by its token */
     /* get the cursor by its token */
-    if (!(c = (cursor *) db_find_token(token))) 
-    {
+    if (!(c = (cursor *) db_find_token(token))) {
 	db_error("cursor not found");
 	db_error("cursor not found");
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     /* fetch on position */
     /* fetch on position */
-    switch (position)
-    { 
+    switch (position) {
     case DB_NEXT:
     case DB_NEXT:
-	ret = SQLFetchScroll( c->stmt, SQL_FETCH_NEXT, 0 );  
+	ret = SQLFetchScroll(c->stmt, SQL_FETCH_NEXT, 0);
 	break;
 	break;
     case DB_CURRENT:
     case DB_CURRENT:
-	ret = SQLFetchScroll( c->stmt, SQL_FETCH_RELATIVE, 0 );
+	ret = SQLFetchScroll(c->stmt, SQL_FETCH_RELATIVE, 0);
 	break;
 	break;
     case DB_PREVIOUS:
     case DB_PREVIOUS:
-    	ret = SQLFetchScroll( c->stmt, SQL_FETCH_PRIOR , 0 );
-    	break;
+	ret = SQLFetchScroll(c->stmt, SQL_FETCH_PRIOR, 0);
+	break;
     case DB_FIRST:
     case DB_FIRST:
-	ret = SQLFetchScroll( c->stmt, SQL_FETCH_FIRST, 0 );
+	ret = SQLFetchScroll(c->stmt, SQL_FETCH_FIRST, 0);
 	break;
 	break;
     case DB_LAST:
     case DB_LAST:
-	ret = SQLFetchScroll( c->stmt, SQL_FETCH_LAST, 0 );
+	ret = SQLFetchScroll(c->stmt, SQL_FETCH_LAST, 0);
 	break;
 	break;
     default:
     default:
 	return DB_FAILED;
 	return DB_FAILED;
     };
     };
 
 
-    if ( ret == SQL_NO_DATA )
-    {
+    if (ret == SQL_NO_DATA) {
 	*more = 0;
 	*more = 0;
 	return DB_OK;
 	return DB_OK;
     }
     }
-    if ( ! SQL_SUCCEEDED( ret ))
-    {
+    if (!SQL_SUCCEEDED(ret)) {
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
     *more = 1;
     *more = 1;
 
 
     /* get the data out of the descriptor into the table */
     /* get the data out of the descriptor into the table */
     table = db_get_cursor_table(cn);
     table = db_get_cursor_table(cn);
-    ncols = db_get_table_number_of_columns (table);
-    for (col = 1; col <= ncols; col++)
-    {
-	column = db_get_table_column (table, col-1);
-	value  = db_get_column_value (column);
-	db_free_string (&value->s);
-	
+    ncols = db_get_table_number_of_columns(table);
+    for (col = 1; col <= ncols; col++) {
+	column = db_get_table_column(table, col - 1);
+	value = db_get_column_value(column);
+	db_free_string(&value->s);
+
 	/* Is null? */
 	/* Is null? */
-	SQLGetData( c->stmt, col, SQL_C_CHAR, NULL, 0, &len );
-	if ( len == SQL_NULL_DATA )
-	{
+	SQLGetData(c->stmt, col, SQL_C_CHAR, NULL, 0, &len);
+	if (len == SQL_NULL_DATA) {
 	    value->isNull = 1;
 	    value->isNull = 1;
 	    continue;
 	    continue;
 	}
 	}
-	else    
+	else
 	    value->isNull = 0;
 	    value->isNull = 0;
 
 
 	sqltype = db_get_column_sqltype(column);
 	sqltype = db_get_column_sqltype(column);
-	ctype   = db_sqltype_to_Ctype(sqltype);
-	htype    = db_get_column_host_type(column);
-	
-	switch (ctype)
-	{
+	ctype = db_sqltype_to_Ctype(sqltype);
+	htype = db_get_column_host_type(column);
+
+	switch (ctype) {
 	case DB_C_TYPE_STRING:
 	case DB_C_TYPE_STRING:
-	    if (htype  == SQL_CHAR)
-	    {
-		len=db_get_column_length (column);
-		db_enlarge_string (&value->s, len+1);
-		ret = SQLGetData( c->stmt, col, SQL_C_CHAR, value->s.string, len+1, NULL );    
+	    if (htype == SQL_CHAR) {
+		len = db_get_column_length(column);
+		db_enlarge_string(&value->s, len + 1);
+		ret =
+		    SQLGetData(c->stmt, col, SQL_C_CHAR, value->s.string,
+			       len + 1, NULL);
 	    }
 	    }
-	    else if ( htype  == SQL_VARCHAR) {
-		ret = SQLGetData( c->stmt, col, SQL_C_CHAR, NULL, 0, &len );    
-		db_enlarge_string (&value->s, len+1);
-		ret = SQLGetData( c->stmt, col, SQL_C_CHAR, value->s.string, len+1, NULL );    
+	    else if (htype == SQL_VARCHAR) {
+		ret = SQLGetData(c->stmt, col, SQL_C_CHAR, NULL, 0, &len);
+		db_enlarge_string(&value->s, len + 1);
+		ret =
+		    SQLGetData(c->stmt, col, SQL_C_CHAR, value->s.string,
+			       len + 1, NULL);
 	    }
 	    }
-	    else { /* now the same as SQL_VARCHAR, could differ for other htype ?  */
-		ret = SQLGetData( c->stmt, col, SQL_C_CHAR, NULL, 0, &len );    
-		db_enlarge_string (&value->s, len+1);
-		ret = SQLGetData( c->stmt, col, SQL_C_CHAR, value->s.string, len+1, NULL );    
+	    else {		/* now the same as SQL_VARCHAR, could differ for other htype ?  */
+		ret = SQLGetData(c->stmt, col, SQL_C_CHAR, NULL, 0, &len);
+		db_enlarge_string(&value->s, len + 1);
+		ret =
+		    SQLGetData(c->stmt, col, SQL_C_CHAR, value->s.string,
+			       len + 1, NULL);
 	    }
 	    }
 	    break;
 	    break;
 	case DB_C_TYPE_INT:
 	case DB_C_TYPE_INT:
-	    ret = SQLGetData( c->stmt, col, SQL_C_LONG, &value->i, sizeof( value->i ), NULL );    
+	    ret =
+		SQLGetData(c->stmt, col, SQL_C_LONG, &value->i,
+			   sizeof(value->i), NULL);
 	    break;
 	    break;
 	case DB_C_TYPE_DOUBLE:
 	case DB_C_TYPE_DOUBLE:
-	    ret = SQLGetData( c->stmt, col, SQL_C_DOUBLE, &value->d, sizeof( value->d ), NULL );    
+	    ret =
+		SQLGetData(c->stmt, col, SQL_C_DOUBLE, &value->d,
+			   sizeof(value->d), NULL);
 	    break;
 	    break;
-	    
+
 	case DB_C_TYPE_DATETIME:
 	case DB_C_TYPE_DATETIME:
-	    switch( sqltype )
-	    {
+	    switch (sqltype) {
 	    case DB_SQL_TYPE_DATE:
 	    case DB_SQL_TYPE_DATE:
-	        ret = SQLGetData( c->stmt, col, SQL_C_TYPE_DATE, &date, sizeof( date ), NULL );    
+		ret =
+		    SQLGetData(c->stmt, col, SQL_C_TYPE_DATE, &date,
+			       sizeof(date), NULL);
 		value->t.year = date.year;
 		value->t.year = date.year;
 		value->t.month = date.month;
 		value->t.month = date.month;
 		value->t.day = date.day;
 		value->t.day = date.day;
@@ -128,7 +130,9 @@ db__driver_fetch(cn, position, more)
 		value->t.seconds = 0.0;
 		value->t.seconds = 0.0;
 		break;
 		break;
 	    case DB_SQL_TYPE_TIME:
 	    case DB_SQL_TYPE_TIME:
-	        ret = SQLGetData( c->stmt, col, SQL_C_TYPE_TIME, &time, sizeof( time ), NULL );    
+		ret =
+		    SQLGetData(c->stmt, col, SQL_C_TYPE_TIME, &time,
+			       sizeof(time), NULL);
 		value->t.year = 0;
 		value->t.year = 0;
 		value->t.month = 0;
 		value->t.month = 0;
 		value->t.day = 0;
 		value->t.day = 0;
@@ -137,7 +141,9 @@ db__driver_fetch(cn, position, more)
 		value->t.seconds = time.second;
 		value->t.seconds = time.second;
 		break;
 		break;
 	    case DB_SQL_TYPE_TIMESTAMP:
 	    case DB_SQL_TYPE_TIMESTAMP:
-	        ret = SQLGetData( c->stmt, col, SQL_C_TYPE_TIMESTAMP, &timestamp, sizeof( timestamp ), NULL );
+		ret =
+		    SQLGetData(c->stmt, col, SQL_C_TYPE_TIMESTAMP, &timestamp,
+			       sizeof(timestamp), NULL);
 		value->t.year = timestamp.year;
 		value->t.year = timestamp.year;
 		value->t.month = timestamp.month;
 		value->t.month = timestamp.month;
 		value->t.day = timestamp.day;
 		value->t.day = timestamp.day;
@@ -145,41 +151,40 @@ db__driver_fetch(cn, position, more)
 		value->t.minute = timestamp.minute;
 		value->t.minute = timestamp.minute;
 		value->t.seconds = timestamp.second;
 		value->t.seconds = timestamp.second;
 		break;
 		break;
-	    /*	
-	    case DB_SQL_TYPE_INTERVAL:
-	    	break;
-	    default: 
-	    	break;
-            */
+		/*  
+		   case DB_SQL_TYPE_INTERVAL:
+		   break;
+		   default: 
+		   break;
+		 */
 	    }
 	    }
 
 
 	default:
 	default:
-	    len=db_get_column_length (column);
-	    db_enlarge_string (&value->s, len+1);
-	    ret = SQLGetData( c->stmt, col, SQL_C_CHAR, value->s.string, len+1, NULL );    
+	    len = db_get_column_length(column);
+	    db_enlarge_string(&value->s, len + 1);
+	    ret =
+		SQLGetData(c->stmt, col, SQL_C_CHAR, value->s.string, len + 1,
+			   NULL);
 	    break;
 	    break;
 	}
 	}
     }
     }
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_get_num_rows (cn )
-    dbCursor *cn;
+int db__driver_get_num_rows(cn)
+     dbCursor *cn;
 {
 {
-    cursor     *c;
-    dbToken    token;    
+    cursor *c;
+    dbToken token;
 
 
     /* get cursor token */
     /* get cursor token */
     token = db_get_cursor_token(cn);
     token = db_get_cursor_token(cn);
 
 
     /* get the cursor by its token */
     /* get the cursor by its token */
-    if (!(c = (cursor *) db_find_token(token))) 
-    {
+    if (!(c = (cursor *) db_find_token(token))) {
 	db_error("cursor not found");
 	db_error("cursor not found");
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    return ( c->nrows );
+    return (c->nrows);
 }
 }
-

+ 13 - 12
db/drivers/odbc/globals.h

@@ -1,19 +1,20 @@
 #define MAX_CHAR_SIZE	1000	/* replace by value from ODBC */
 #define MAX_CHAR_SIZE	1000	/* replace by value from ODBC */
-#define DB_MSG 800  /* max length message for report_error() */
-#define OD_MSG 500  /* max length of message returned by SQLGetDiagRec() */
+#define DB_MSG 800		/* max length message for report_error() */
+#define OD_MSG 500		/* max length of message returned by SQLGetDiagRec() */
 
 
 /* cursors */
 /* cursors */
-typedef struct _cursor {
-    SQLHSTMT    stmt;
+typedef struct _cursor
+{
+    SQLHSTMT stmt;
     dbToken token;
     dbToken token;
-    int type;              /* type of cursor: SELECT, UPDATE, INSERT */
-    int nrows;             /* number of rows selected by SELECT statement */
-} cursor;  
+    int type;			/* type of cursor: SELECT, UPDATE, INSERT */
+    int nrows;			/* number of rows selected by SELECT statement */
+} cursor;
 
 
 #ifdef MAIN
 #ifdef MAIN
-    SQLHENV	ODenvi;  /* Handle ODBC environment */
-    SQLHDBC    	ODconn;      /* Handle connection  */
+SQLHENV ODenvi;			/* Handle ODBC environment */
+SQLHDBC ODconn;			/* Handle connection  */
 #else
 #else
-    extern  SQLHENV  ODenvi;  /* Handle ODBC environment */
-    extern  SQLHDBC  ODconn;      /* Handle connection  */
-#endif 
+extern SQLHENV ODenvi;		/* Handle ODBC environment */
+extern SQLHDBC ODconn;		/* Handle connection  */
+#endif

+ 27 - 32
db/drivers/odbc/listdb.c

@@ -3,58 +3,53 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_list_databases (dbpath, npaths, dblist, dbcount)
-    dbString  *dbpath;
-    int       npaths;
-    dbHandle  **dblist;
-    int       *dbcount;
+int db__driver_list_databases(dbpath, npaths, dblist, dbcount)
+     dbString *dbpath;
+     int npaths;
+     dbHandle **dblist;
+     int *dbcount;
 {
 {
-    int          i, count=0;
-    dbHandle     *list;
-    char         dsn[SQL_MAX_DSN_LENGTH],desc[100];
+    int i, count = 0;
+    dbHandle *list;
+    char dsn[SQL_MAX_DSN_LENGTH], desc[100];
     SQLUSMALLINT next;
     SQLUSMALLINT next;
 
 
-    *dblist  = NULL;
+    *dblist = NULL;
     *dbcount = 0;
     *dbcount = 0;
 
 
-    if ( open_connection() != DB_OK )
+    if (open_connection() != DB_OK)
 	return DB_FAILED;
 	return DB_FAILED;
 
 
-    next=SQL_FETCH_FIRST;
-    while( SQLDataSources( ODenvi, next, dsn, sizeof(dsn),
-        NULL, desc, sizeof(desc), NULL) == SQL_SUCCESS)
-    {
-	next=SQL_FETCH_NEXT;
+    next = SQL_FETCH_FIRST;
+    while (SQLDataSources(ODenvi, next, dsn, sizeof(dsn),
+			  NULL, desc, sizeof(desc), NULL) == SQL_SUCCESS) {
+	next = SQL_FETCH_NEXT;
 	count++;
 	count++;
-    }      
+    }
 
 
-    list = db_alloc_handle_array (count);  
-    if(list == NULL) 
-    {
+    list = db_alloc_handle_array(count);
+    if (list == NULL) {
 	report_error("db_alloc_handle_array()");
 	report_error("db_alloc_handle_array()");
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    i=0;
-    next=SQL_FETCH_FIRST;
-    while( SQLDataSources(ODenvi, next, dsn, sizeof(dsn),
-    	    NULL, desc, sizeof(desc), NULL) == SQL_SUCCESS)
-    {
-	db_init_handle (&list[i]);
-	if(  db_set_handle(&list[i],dsn, desc ) != DB_OK)
-	{
+    i = 0;
+    next = SQL_FETCH_FIRST;
+    while (SQLDataSources(ODenvi, next, dsn, sizeof(dsn),
+			  NULL, desc, sizeof(desc), NULL) == SQL_SUCCESS) {
+	db_init_handle(&list[i]);
+	if (db_set_handle(&list[i], dsn, desc) != DB_OK) {
 	    report_error("db_set_handle()");
 	    report_error("db_set_handle()");
-	    db_free_handle_array (list, count);
+	    db_free_handle_array(list, count);
 	    return DB_FAILED;
 	    return DB_FAILED;
 	}
 	}
-	next=SQL_FETCH_NEXT;
+	next = SQL_FETCH_NEXT;
 	i++;
 	i++;
-    }      
+    }
 
 
     *dblist = list;
     *dblist = list;
     *dbcount = count;
     *dbcount = count;
     close_connection();
     close_connection();
-    
+
     return DB_OK;
     return DB_OK;
 }
 }

+ 37 - 43
db/drivers/odbc/listtab.c

@@ -3,81 +3,75 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_list_tables (tlist, tcount, system)
-    dbString **tlist;
-    int *tcount;
-    int system;
+int db__driver_list_tables(tlist, tcount, system)
+     dbString **tlist;
+     int *tcount;
+     int system;
 {
 {
-    cursor      *c;
-    dbString 	*list;
-    int 	count=0;
-    SQLCHAR     tableName[SQL_MAX_TABLE_NAME_LEN];      
-    SQLINTEGER  indi, nrow=0;   
-    SQLRETURN   ret;
-    char        ttype[50];
-    
+    cursor *c;
+    dbString *list;
+    int count = 0;
+    SQLCHAR tableName[SQL_MAX_TABLE_NAME_LEN];
+    SQLINTEGER indi, nrow = 0;
+    SQLRETURN ret;
+    char ttype[50];
+
     *tlist = NULL;
     *tlist = NULL;
     *tcount = 0;
     *tcount = 0;
 
 
     /* allocate cursor */
     /* allocate cursor */
     c = alloc_cursor();
     c = alloc_cursor();
     if (c == NULL)
     if (c == NULL)
-        return DB_FAILED;  
+	return DB_FAILED;
 
 
     /* Execute SQL */
     /* Execute SQL */
     if (system)
     if (system)
-	sprintf(ttype,"SYSTEM TABLE");
+	sprintf(ttype, "SYSTEM TABLE");
     else
     else
-	sprintf(ttype,"TABLE, VIEW");
+	sprintf(ttype, "TABLE, VIEW");
 
 
-    ret = SQLTables( c->stmt, NULL, 0, NULL, 0, NULL, 0, ttype, sizeof(ttype) );
+    ret = SQLTables(c->stmt, NULL, 0, NULL, 0, NULL, 0, ttype, sizeof(ttype));
 
 
-    if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
-    {
-        report_error("SQLTables()");
-	return DB_FAILED;  
-    }  
+    if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
+	report_error("SQLTables()");
+	return DB_FAILED;
+    }
 
 
-    SQLBindCol( c->stmt, 3, SQL_C_CHAR, tableName, sizeof(tableName),  &indi );
+    SQLBindCol(c->stmt, 3, SQL_C_CHAR, tableName, sizeof(tableName), &indi);
 
 
     /* Get number of rows */
     /* Get number of rows */
     /* WARNING: after SQLTables(), SQLRowCount() doesn't sets number of rows
     /* WARNING: after SQLTables(), SQLRowCount() doesn't sets number of rows
      * to number of tables! ODBC developers said, this is correct. */
      * to number of tables! ODBC developers said, this is correct. */
     nrow = 0;
     nrow = 0;
-    ret = SQLFetch( c->stmt );
-    while ( ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO ) {
+    ret = SQLFetch(c->stmt);
+    while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
 	nrow++;
 	nrow++;
-	ret = SQLFetch( c->stmt );
+	ret = SQLFetch(c->stmt);
     }
     }
-    
+
     list = db_alloc_string_array(nrow);
     list = db_alloc_string_array(nrow);
     if (list == NULL)
     if (list == NULL)
-	return DB_FAILED;       
+	return DB_FAILED;
 
 
     /* Get table names */
     /* Get table names */
     /* ret = SQLFetch( c->stmt ); */
     /* ret = SQLFetch( c->stmt ); */
-    ret = SQLFetchScroll( c->stmt, SQL_FETCH_FIRST, 0 );
-    while ( ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO )  
-    {
-	if ( indi == SQL_NULL_DATA )
-	{
-	    if(db_set_string (&list[count], "Unknown") != DB_OK)
+    ret = SQLFetchScroll(c->stmt, SQL_FETCH_FIRST, 0);
+    while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
+	if (indi == SQL_NULL_DATA) {
+	    if (db_set_string(&list[count], "Unknown") != DB_OK)
 		return DB_FAILED;
 		return DB_FAILED;
-        }
-	else  
-	{
-    	    if(db_set_string (&list[count], (char *)tableName) != DB_OK)
+	}
+	else {
+	    if (db_set_string(&list[count], (char *)tableName) != DB_OK)
 		return DB_FAILED;
 		return DB_FAILED;
-	}	
+	}
 	count++;
 	count++;
-        ret = SQLFetch( c->stmt );
+	ret = SQLFetch(c->stmt);
     }
     }
-    						
-    free_cursor (c);
+
+    free_cursor(c);
 
 
     *tlist = list;
     *tlist = list;
     *tcount = count;
     *tcount = count;
     return DB_OK;
     return DB_OK;
 }
 }
-

+ 3 - 3
db/drivers/odbc/main.c

@@ -8,8 +8,8 @@
 
 
 int main(int argc, char **argv)
 int main(int argc, char **argv)
 {
 {
-	init_dbdriver();
-	exit (db_driver (argc, argv));
+    init_dbdriver();
+    exit(db_driver(argc, argv));
 
 
-	return 0;
+    return 0;
 }
 }

+ 15 - 15
db/drivers/odbc/odbc.h

@@ -6,26 +6,26 @@
 #endif
 #endif
 
 
 /* configure checks for sql.h and stores in $(ODBCINC) */
 /* configure checks for sql.h and stores in $(ODBCINC) */
-#include <sql.h>     
-#include <sqlext.h>  
+#include <sql.h>
+#include <sqlext.h>
 #include <sqltypes.h>
 #include <sqltypes.h>
 
 
 #endif
 #endif
 
 
 /* ********* unused:
 /* ********* unused:
-*#ifndef __FreeBSD__
-*#include <odbc/sql.h>
-*#include <odbc/sqlext.h>
-*#include <odbc/sqltypes.h>
-*
-*#else
-*/
+ *#ifndef __FreeBSD__
+ *#include <odbc/sql.h>
+ *#include <odbc/sqlext.h>
+ *#include <odbc/sqltypes.h>
+ *
+ *#else
+ */
 /* FreeBSD unixODBC port installs these header files in /usr/local/include */
 /* FreeBSD unixODBC port installs these header files in /usr/local/include */
 
 
 /* ********* unused:
 /* ********* unused:
-*#include <sql.h>
-*#include <sqlext.h>
-*#include <sqltypes.h>
-*
-*#endif
-*/
+ *#include <sql.h>
+ *#include <sqlext.h>
+ *#include <sqltypes.h>
+ *
+ *#endif
+ */

+ 7 - 7
db/drivers/odbc/proto.h

@@ -1,13 +1,13 @@
 /* error.c */
 /* error.c */
-void report_error ( char *err );
+void report_error(char *err);
 
 
 /* connect.c */
 /* connect.c */
-int open_connection ( );
-int close_connection ( );
+int open_connection();
+int close_connection();
 
 
 /* cursor.c */
 /* cursor.c */
-cursor * alloc_cursor ();
-void free_cursor ( cursor *c );
+cursor *alloc_cursor();
+void free_cursor(cursor * c);
 
 
-/* describe.c*/
-int describe_table(SQLHSTMT   stmt, dbTable **table);
+/* describe.c */
+int describe_table(SQLHSTMT stmt, dbTable ** table);

+ 29 - 28
db/drivers/odbc/select.c

@@ -4,63 +4,64 @@
 #include "proto.h"
 #include "proto.h"
 #include <stdio.h>
 #include <stdio.h>
 
 
-int
-db__driver_open_select_cursor(sel, dbc, mode)
-    dbString *sel;
-    dbCursor *dbc;
-    int mode;
+int db__driver_open_select_cursor(sel, dbc, mode)
+     dbString *sel;
+     dbCursor *dbc;
+     int mode;
 {
 {
-    cursor      *c;
-    SQLRETURN   ret;
-    SQLINTEGER  err;
-    char        *sql, msg[OD_MSG], emsg[DB_MSG];      
-    dbTable     *table;
-    int         nrows;
+    cursor *c;
+    SQLRETURN ret;
+    SQLINTEGER err;
+    char *sql, msg[OD_MSG], emsg[DB_MSG];
+    dbTable *table;
+    int nrows;
 
 
     /* allocate cursor */
     /* allocate cursor */
     c = alloc_cursor();
     c = alloc_cursor();
     if (c == NULL)
     if (c == NULL)
 	return DB_FAILED;
 	return DB_FAILED;
 
 
-    db_set_cursor_mode(dbc,mode);
+    db_set_cursor_mode(dbc, mode);
     db_set_cursor_type_readonly(dbc);
     db_set_cursor_type_readonly(dbc);
 
 
     sql = db_get_string(sel);
     sql = db_get_string(sel);
 
 
-    ret = SQLExecDirect(c->stmt,sql,SQL_NTS);
-    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
-    {
-	SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt,1, NULL, &err, msg, sizeof(msg), NULL);
-	sprintf(emsg, "SQLExecDirect():\n%s\n%s (%d)\n", sql, msg, (int) err);
-	report_error(emsg);  
+    ret = SQLExecDirect(c->stmt, sql, SQL_NTS);
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
+	SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt, 1, NULL, &err, msg,
+		      sizeof(msg), NULL);
+	sprintf(emsg, "SQLExecDirect():\n%s\n%s (%d)\n", sql, msg, (int)err);
+	report_error(emsg);
 	return DB_FAILED;
 	return DB_FAILED;
-    }  
+    }
 
 
     describe_table(c->stmt, &table);
     describe_table(c->stmt, &table);
 
 
 
 
     db_set_cursor_table(dbc, table);
     db_set_cursor_table(dbc, table);
-   
+
     /* record table with dbCursor */
     /* record table with dbCursor */
-    db_set_cursor_table(dbc, table);         
+    db_set_cursor_table(dbc, table);
 
 
     /* set dbCursor's token for my cursor */
     /* set dbCursor's token for my cursor */
     db_set_cursor_token(dbc, c->token);
     db_set_cursor_token(dbc, c->token);
 
 
     /* It seems that there is no function in ODBC to get number of selected rows.
     /* It seems that there is no function in ODBC to get number of selected rows.
-    *  SQLRowCount() works for insert, update, delete. */
+     *  SQLRowCount() works for insert, update, delete. */
     nrows = 0;
     nrows = 0;
-    while ( 1 ) {
-	ret = SQLFetchScroll( c->stmt, SQL_FETCH_NEXT, 0 );
-	if ( ret == SQL_NO_DATA ) { break; }
-	if ( ! SQL_SUCCEEDED( ret )) {
+    while (1) {
+	ret = SQLFetchScroll(c->stmt, SQL_FETCH_NEXT, 0);
+	if (ret == SQL_NO_DATA) {
+	    break;
+	}
+	if (!SQL_SUCCEEDED(ret)) {
 	    return DB_FAILED;
 	    return DB_FAILED;
 	}
 	}
 	nrows++;
 	nrows++;
     }
     }
     c->nrows = nrows;
     c->nrows = nrows;
-    SQLFetchScroll( c->stmt, SQL_FETCH_FIRST, 0 );
-    SQLFetchScroll( c->stmt, SQL_FETCH_PRIOR, 0 );
+    SQLFetchScroll(c->stmt, SQL_FETCH_FIRST, 0);
+    SQLFetchScroll(c->stmt, SQL_FETCH_PRIOR, 0);
 
 
     return DB_OK;
     return DB_OK;
 }
 }

+ 131 - 134
db/drivers/odbc/table.c

@@ -1,3 +1,4 @@
+
 /**
 /**
  * \file table.c
  * \file table.c
  *
  *
@@ -28,87 +29,87 @@
  *   {NULL | CURRENT | USER} ] [ NOT NULL ], ... ) via EXECUTE IMMEDIATE
  *   {NULL | CURRENT | USER} ] [ NOT NULL ], ... ) via EXECUTE IMMEDIATE
  */
  */
 /*db_driver_create_table (table)
 /*db_driver_create_table (table)
-dbTable *table;
-{
-  dbString cmd;
-  $char *sqlcmd;
-  dbColumn *col;
-  dbValue *defaultValue;
-  int i;
-
-  db_init_string(&cmd);
-  db_append_string(&cmd, "CREATE TABLE ");
-  db_append_string(&cmd, db_get_table_name(table));
-  db_append_string(&cmd, " (");
-  for (i = 0; i < db_get_table_number_of_columns(table); i++) {
-    if (i) db_append_string(&cmd,",");
-    col = db_get_table_column(table, i);
-    db_append_string(&cmd, db_get_column_name(col));
-    db_append_string(&cmd, " ");
-    build_column_type_name(col, &cmd);
-    build_column_default(col, &cmd);
-    if (!db_test_column_null_allowed(col)) {
-      db_append_string(&cmd, " NOT NULL ");
-    }
-  }
-  db_append_string(&cmd, ")");
-
-  sqlcmd = db_get_string(&cmd);
-  $EXECUTE IMMEDIATE $sqlcmd;
-  db_free_string(&cmd);
-  if (sql_error(NULL)) {
-    return DB_FAILED;
-  }
-  return DB_OK;
-}*/
+   dbTable *table;
+   {
+   dbString cmd;
+   $char *sqlcmd;
+   dbColumn *col;
+   dbValue *defaultValue;
+   int i;
+
+   db_init_string(&cmd);
+   db_append_string(&cmd, "CREATE TABLE ");
+   db_append_string(&cmd, db_get_table_name(table));
+   db_append_string(&cmd, " (");
+   for (i = 0; i < db_get_table_number_of_columns(table); i++) {
+   if (i) db_append_string(&cmd,",");
+   col = db_get_table_column(table, i);
+   db_append_string(&cmd, db_get_column_name(col));
+   db_append_string(&cmd, " ");
+   build_column_type_name(col, &cmd);
+   build_column_default(col, &cmd);
+   if (!db_test_column_null_allowed(col)) {
+   db_append_string(&cmd, " NOT NULL ");
+   }
+   }
+   db_append_string(&cmd, ")");
+
+   sqlcmd = db_get_string(&cmd);
+   $EXECUTE IMMEDIATE $sqlcmd;
+   db_free_string(&cmd);
+   if (sql_error(NULL)) {
+   return DB_FAILED;
+   }
+   return DB_OK;
+   } */
 
 
 /*
 /*
-build_column_type_name(col, cmd)
-dbColumn *col;
-dbString *cmd;
-{
-  int sql_type;
-  char buf[50];
-
-  sql_type = db_get_column_sqltype(col);
-  db_append_string(cmd, db_sqltype_name(sql_type));
-*/
+   build_column_type_name(col, cmd)
+   dbColumn *col;
+   dbString *cmd;
+   {
+   int sql_type;
+   char buf[50];
+
+   sql_type = db_get_column_sqltype(col);
+   db_append_string(cmd, db_sqltype_name(sql_type));
+ */
   /* append the (precision, scale) or (length) if necessary */
   /* append the (precision, scale) or (length) if necessary */
 /*  switch (sql_type) {
 /*  switch (sql_type) {
-    case DB_SQL_TYPE_CHARACTER:
-	sprintf(buf, "(%d)", db_get_column_length(col));
-	db_append_string(cmd, buf);
-	break;
-    case DB_SQL_TYPE_DECIMAL:
-    case DB_SQL_TYPE_NUMERIC:
-	sprintf(buf, "(%d,%d)", db_get_column_precision(col),
-	db_get_column_scale(col));
-	db_append_string(cmd, buf);
-	break;
-    default: break;
-  }
-}
-*/ 
+   case DB_SQL_TYPE_CHARACTER:
+   sprintf(buf, "(%d)", db_get_column_length(col));
+   db_append_string(cmd, buf);
+   break;
+   case DB_SQL_TYPE_DECIMAL:
+   case DB_SQL_TYPE_NUMERIC:
+   sprintf(buf, "(%d,%d)", db_get_column_precision(col),
+   db_get_column_scale(col));
+   db_append_string(cmd, buf);
+   break;
+   default: break;
+   }
+   }
+ */
 /*
 /*
-build_column_default(col, cmd)
-dbColumn *col;
-dbString *cmd;
-{
-  dbString defaultString;
-
-  if(db_test_column_has_default_value(col)) {
-    db_append_string(cmd, " DEFAULT ");
-    db_init_string(&defaultString);
-    db_convert_column_default_value_to_string(col, &defaultString);
-    if (db_get_column_sqltype(col) == DB_SQL_TYPE_CHARACTER)
-      db_append_string(cmd, "\"");
-    db_append_string(cmd, db_get_string(&defaultString));
-    if (db_get_column_sqltype(col) == DB_SQL_TYPE_CHARACTER)
-      db_append_string(cmd, "\"");
-    db_free_string(&defaultString);
-    db_append_string(cmd, " ");
-  }
-}*/
+   build_column_default(col, cmd)
+   dbColumn *col;
+   dbString *cmd;
+   {
+   dbString defaultString;
+
+   if(db_test_column_has_default_value(col)) {
+   db_append_string(cmd, " DEFAULT ");
+   db_init_string(&defaultString);
+   db_convert_column_default_value_to_string(col, &defaultString);
+   if (db_get_column_sqltype(col) == DB_SQL_TYPE_CHARACTER)
+   db_append_string(cmd, "\"");
+   db_append_string(cmd, db_get_string(&defaultString));
+   if (db_get_column_sqltype(col) == DB_SQL_TYPE_CHARACTER)
+   db_append_string(cmd, "\"");
+   db_free_string(&defaultString);
+   db_append_string(cmd, " ");
+   }
+   } */
 
 
 
 
 /**
 /**
@@ -120,83 +121,79 @@ dbString *cmd;
  * \return int DB_FAILED on error; DB_OK on success
  * \return int DB_FAILED on error; DB_OK on success
  */
  */
 
 
-int db__driver_drop_table (dbString *name)
+int db__driver_drop_table(dbString * name)
 {
 {
-    char        cmd[200];
-    cursor      *c;
-    SQLRETURN   ret;
-    char        msg[OD_MSG];
-    char        *emsg = NULL;    
-    SQLINTEGER  err;  
-    SQLCHAR     ttype[50], *tname;
-    SQLINTEGER  nrow=0;
+    char cmd[200];
+    cursor *c;
+    SQLRETURN ret;
+    char msg[OD_MSG];
+    char *emsg = NULL;
+    SQLINTEGER err;
+    SQLCHAR ttype[50], *tname;
+    SQLINTEGER nrow = 0;
 
 
 
 
     /* allocate cursor */
     /* allocate cursor */
     c = alloc_cursor();
     c = alloc_cursor();
     if (c == NULL)
     if (c == NULL)
-        return DB_FAILED;
-	
+	return DB_FAILED;
+
     tname = db_get_string(name);
     tname = db_get_string(name);
 
 
-    ret = SQLTables( c->stmt, NULL, 0, NULL, 0, tname, sizeof(tname), NULL, 0 ); 
-    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
-    {
-        report_error("SQLTables()");
-        return DB_FAILED;
+    ret = SQLTables(c->stmt, NULL, 0, NULL, 0, tname, sizeof(tname), NULL, 0);
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
+	report_error("SQLTables()");
+	return DB_FAILED;
     }
     }
 
 
     /* Get number of rows */
     /* Get number of rows */
     ret = SQLRowCount(c->stmt, &nrow);
     ret = SQLRowCount(c->stmt, &nrow);
-    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
-    {
-        report_error("SQLRowCount()");
-        return DB_FAILED;
-    } 
-
-    if ( nrow == 0 )
-    {
-        G_asprintf(&emsg, "Table %s doesn't exist\n", tname);
-        report_error(emsg);
-        G_free(emsg);
-
-        return DB_FAILED;
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
+	report_error("SQLRowCount()");
+	return DB_FAILED;
     }
     }
-    
-    ret = SQLFetchScroll( c->stmt, SQL_FETCH_NEXT, 0 );
-    ret = SQLGetData( c->stmt, 4, SQL_C_CHAR, ttype, sizeof(ttype), NULL );
 
 
-    if ( strcmp(ttype, "TABLE") == 0 )
-    {
+    if (nrow == 0) {
+	G_asprintf(&emsg, "Table %s doesn't exist\n", tname);
+	report_error(emsg);
+	G_free(emsg);
+
+	return DB_FAILED;
+    }
+
+    ret = SQLFetchScroll(c->stmt, SQL_FETCH_NEXT, 0);
+    ret = SQLGetData(c->stmt, 4, SQL_C_CHAR, ttype, sizeof(ttype), NULL);
+
+    if (strcmp(ttype, "TABLE") == 0) {
 	sprintf(cmd, "DROP TABLE %s", tname);
 	sprintf(cmd, "DROP TABLE %s", tname);
     }
     }
-    else if ( strcmp(ttype, "VIEW") == 0 )
-    {
+    else if (strcmp(ttype, "VIEW") == 0) {
 	sprintf(cmd, "DROP VIEW %s", tname);
 	sprintf(cmd, "DROP VIEW %s", tname);
     }
     }
-    else
-    {
-        G_asprintf(&emsg, "Table %s isn't 'TABLE' or 'VIEW' but %s\n",tname, ttype);
-        report_error(emsg);
-        G_free(emsg);
+    else {
+	G_asprintf(&emsg, "Table %s isn't 'TABLE' or 'VIEW' but %s\n", tname,
+		   ttype);
+	report_error(emsg);
+	G_free(emsg);
 
 
-        return DB_FAILED;
+	return DB_FAILED;
     }
     }
-    
-    SQLCloseCursor( c->stmt );
-    
-    ret = SQLExecDirect(c->stmt,cmd,SQL_NTS);
-    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO))
-    {
-        SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt,1, NULL, &err,msg,sizeof(msg),NULL);
-        G_asprintf(&emsg, "SQLExecDirect():\n%s\n%s (%d)\n", cmd, msg, (int) err);
-        report_error(emsg);
-        G_free(emsg);
-
-        return DB_FAILED;
+
+    SQLCloseCursor(c->stmt);
+
+    ret = SQLExecDirect(c->stmt, cmd, SQL_NTS);
+    if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
+	SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt, 1, NULL, &err, msg,
+		      sizeof(msg), NULL);
+	G_asprintf(&emsg, "SQLExecDirect():\n%s\n%s (%d)\n", cmd, msg,
+		   (int)err);
+	report_error(emsg);
+	G_free(emsg);
+
+	return DB_FAILED;
     }
     }
-			 
-    free_cursor (c);
-					     
-    return DB_OK; 				
+
+    free_cursor(c);
+
+    return DB_OK;
 }
 }

+ 18 - 20
db/drivers/ogr/cursor.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -20,12 +21,11 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_close_cursor( dbCursor *dbc )
+int db__driver_close_cursor(dbCursor * dbc)
 {
 {
     cursor *c;
     cursor *c;
 
 
-    G_debug ( 3, "db_driver_close_cursor()" );
+    G_debug(3, "db_driver_close_cursor()");
 
 
     init_error();
     init_error();
 
 
@@ -37,28 +37,28 @@ db__driver_close_cursor( dbCursor *dbc )
     /* free_cursor(cursor) */
     /* free_cursor(cursor) */
     free_cursor(c);
     free_cursor(c);
 
 
-    G_debug ( 3, "Cursor closed" );
+    G_debug(3, "Cursor closed");
 
 
     return DB_OK;
     return DB_OK;
 }
 }
 
 
 
 
-cursor * alloc_cursor()
+cursor *alloc_cursor()
 {
 {
-    cursor     *c;
+    cursor *c;
 
 
     /* allocate the cursor */
     /* allocate the cursor */
     c = (cursor *) db_malloc(sizeof(cursor));
     c = (cursor *) db_malloc(sizeof(cursor));
     if (c == NULL) {
     if (c == NULL) {
-        append_error("Cannot allocate cursor.");
-        return NULL; 
-    } 
+	append_error("Cannot allocate cursor.");
+	return NULL;
+    }
 
 
     /* tokenize it */
     /* tokenize it */
     c->token = db_new_token(c);
     c->token = db_new_token(c);
     if (c->token < 0) {
     if (c->token < 0) {
-        append_error("Cannot ad new token.");
-        return NULL; 
+	append_error("Cannot ad new token.");
+	return NULL;
     }
     }
 
 
     c->hFeature = NULL;
     c->hFeature = NULL;
@@ -66,18 +66,16 @@ cursor * alloc_cursor()
     return c;
     return c;
 }
 }
 
 
-void free_cursor( cursor *c )
+void free_cursor(cursor * c)
 {
 {
-    if ( c->hFeature )
-       OGR_F_Destroy( c->hFeature );
+    if (c->hFeature)
+	OGR_F_Destroy(c->hFeature);
 
 
-    if ( c->hLayer )
-       OGR_DS_ReleaseResultSet( hDs, c->hLayer );
+    if (c->hLayer)
+	OGR_DS_ReleaseResultSet(hDs, c->hLayer);
 
 
-    G_free ( c->cols );
-    G_free(c);  
+    G_free(c->cols);
+    G_free(c);
 
 
     db_drop_token(c->token);
     db_drop_token(c->token);
 }
 }
-
-

+ 13 - 12
db/drivers/ogr/db.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -21,7 +22,7 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_open_database( dbHandle *handle )
+int db__driver_open_database(dbHandle * handle)
 {
 {
     const char *name;
     const char *name;
     dbConnection connection;
     dbConnection connection;
@@ -31,34 +32,34 @@ int db__driver_open_database( dbHandle *handle )
     name = db_get_handle_dbname(handle);
     name = db_get_handle_dbname(handle);
 
 
     /* if name is empty use connection.databaseName */
     /* if name is empty use connection.databaseName */
-    if ( strlen(name) == 0 ) 
+    if (strlen(name) == 0)
 	name = connection.databaseName;
 	name = connection.databaseName;
 
 
-    G_debug(3, "db_driver_open_database() name = '%s'", name );
+    G_debug(3, "db_driver_open_database() name = '%s'", name);
 
 
-    OGRRegisterAll ();
+    OGRRegisterAll();
 
 
-    hDs = OGROpen ( name, FALSE, NULL);
+    hDs = OGROpen(name, FALSE, NULL);
 
 
-    if ( hDs == NULL) {
-	append_error ( "Cannot open OGR data source" );
-	report_error ();
+    if (hDs == NULL) {
+	append_error("Cannot open OGR data source");
+	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    G_debug(3, "Datasource opened" );
+    G_debug(3, "Datasource opened");
 
 
     return DB_OK;
     return DB_OK;
 }
 }
 
 
 int db__driver_close_database()
 int db__driver_close_database()
 {
 {
-    G_debug(3, "db_driver_close_database()" );
+    G_debug(3, "db_driver_close_database()");
 
 
     init_error();
     init_error();
-    OGR_DS_Destroy ( hDs );
+    OGR_DS_Destroy(hDs);
 
 
-    G_debug(3, "Database closed" );
+    G_debug(3, "Database closed");
 
 
     return DB_OK;
     return DB_OK;
 }
 }

+ 1 - 0
db/drivers/ogr/dbdriver.h

@@ -14,6 +14,7 @@ int db__driver_fetch();
 int db__driver_get_num_rows();
 int db__driver_get_num_rows();
 int db__driver_list_tables();
 int db__driver_list_tables();
 int db__driver_open_select_cursor();
 int db__driver_open_select_cursor();
+
 #define	init_dbdriver() do{\
 #define	init_dbdriver() do{\
 db_driver_close_cursor = db__driver_close_cursor;\
 db_driver_close_cursor = db__driver_close_cursor;\
 db_driver_open_database = db__driver_open_database;\
 db_driver_open_database = db__driver_open_database;\

+ 100 - 88
db/drivers/ogr/describe.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -21,32 +22,35 @@
 #include "proto.h"
 #include "proto.h"
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
-int db__driver_describe_table( dbString *table_name, dbTable **table )
+int db__driver_describe_table(dbString * table_name, dbTable ** table)
 {
 {
     int i, nlayers;
     int i, nlayers;
     OGRLayerH hLayer = NULL;
     OGRLayerH hLayer = NULL;
     OGRFeatureDefnH hFeatureDefn;
     OGRFeatureDefnH hFeatureDefn;
 
 
     /* Find data source */
     /* Find data source */
-    nlayers = OGR_DS_GetLayerCount( hDs );
+    nlayers = OGR_DS_GetLayerCount(hDs);
 
 
     for (i = 0; i < nlayers; i++) {
     for (i = 0; i < nlayers; i++) {
-	hLayer =  OGR_DS_GetLayer( hDs, i );
-	hFeatureDefn = OGR_L_GetLayerDefn ( hLayer );
-	if ( G_strcasecmp ( (char *) OGR_FD_GetName( hFeatureDefn ), db_get_string(table_name) ) == 0) {
+	hLayer = OGR_DS_GetLayer(hDs, i);
+	hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
+	if (G_strcasecmp
+	    ((char *)OGR_FD_GetName(hFeatureDefn),
+	     db_get_string(table_name)) == 0) {
 	    break;
 	    break;
 	}
 	}
 	hLayer = NULL;
 	hLayer = NULL;
     }
     }
 
 
-    if (  hLayer == NULL ) {
-	append_error("Table '%s' does not exist\n", db_get_string(table_name) );
+    if (hLayer == NULL) {
+	append_error("Table '%s' does not exist\n",
+		     db_get_string(table_name));
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
-	G_debug (3, "->>");
-    if ( describe_table( hLayer, table, NULL) == DB_FAILED ) {
+
+    G_debug(3, "->>");
+    if (describe_table(hLayer, table, NULL) == DB_FAILED) {
 	append_error("Cannot describe table\n");
 	append_error("Cannot describe table\n");
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
@@ -58,114 +62,121 @@ int db__driver_describe_table( dbString *table_name, dbTable **table )
 /* describe table, if c is not NULL cur->cols and cur->ncols is also set 
 /* describe table, if c is not NULL cur->cols and cur->ncols is also set 
  * cursor may be null
  * cursor may be null
  */
  */
-int describe_table( OGRLayerH hLayer, dbTable **table, cursor *c)
+int describe_table(OGRLayerH hLayer, dbTable ** table, cursor * c)
 {
 {
-    int      i, ncols, kcols;
+    int i, ncols, kcols;
     dbColumn *column;
     dbColumn *column;
     OGRFeatureDefnH hFeatureDefn;
     OGRFeatureDefnH hFeatureDefn;
     OGRFieldDefnH hFieldDefn;
     OGRFieldDefnH hFieldDefn;
     const char *fieldName;
     const char *fieldName;
-    int   ogrType;
-    int   *cols;
+    int ogrType;
+    int *cols;
+
+    G_debug(3, "describe_table()");
 
 
-    G_debug (3, "describe_table()");
+    hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
+    ncols = OGR_FD_GetFieldCount(hFeatureDefn);
 
 
-    hFeatureDefn = OGR_L_GetLayerDefn ( hLayer );
-    ncols = OGR_FD_GetFieldCount ( hFeatureDefn );
-    
-    G_debug (3, "ncols = %d", ncols);
+    G_debug(3, "ncols = %d", ncols);
 
 
     /* Identify known columns */
     /* Identify known columns */
-    cols = (int *) G_malloc ( ncols * sizeof(int) );
+    cols = (int *)G_malloc(ncols * sizeof(int));
     kcols = 0;
     kcols = 0;
     for (i = 0; i < ncols; i++) {
     for (i = 0; i < ncols; i++) {
-	hFieldDefn = OGR_FD_GetFieldDefn( hFeatureDefn, i );
-	ogrType = OGR_Fld_GetType( hFieldDefn );
-	OGR_Fld_GetNameRef( hFieldDefn );
-	fieldName = OGR_Fld_GetNameRef( hFieldDefn );
-
-	if ( ogrType != OFTInteger && ogrType != OFTReal && ogrType != OFTString ) {
-	    G_warning ( _("OGR driver: column '%s', OGR type %d  is not supported"), fieldName, ogrType);
+	hFieldDefn = OGR_FD_GetFieldDefn(hFeatureDefn, i);
+	ogrType = OGR_Fld_GetType(hFieldDefn);
+	OGR_Fld_GetNameRef(hFieldDefn);
+	fieldName = OGR_Fld_GetNameRef(hFieldDefn);
+
+	if (ogrType != OFTInteger && ogrType != OFTReal &&
+	    ogrType != OFTString) {
+	    G_warning(_
+		      ("OGR driver: column '%s', OGR type %d  is not supported"),
+		      fieldName, ogrType);
 	    cols[i] = 0;
 	    cols[i] = 0;
-	} else { 
+	}
+	else {
 	    cols[i] = 1;
 	    cols[i] = 1;
 	    kcols++;
 	    kcols++;
 	}
 	}
     }
     }
-    
+
     if (!(*table = db_alloc_table(kcols))) {
     if (!(*table = db_alloc_table(kcols))) {
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     /* set the table name */
     /* set the table name */
     /* TODO */
     /* TODO */
-    db_set_table_name(*table, "" );
+    db_set_table_name(*table, "");
 
 
     /* set the table description */
     /* set the table description */
     db_set_table_description(*table, "");
     db_set_table_description(*table, "");
 
 
     /* TODO */
     /* TODO */
     /*
     /*
-    db_set_table_delete_priv_granted (*table);
-    db_set_table_insert_priv_granted (*table);
-    db_set_table_delete_priv_not_granted (*table);
-    db_set_table_insert_priv_not_granted (*table);
-    */
+       db_set_table_delete_priv_granted (*table);
+       db_set_table_insert_priv_granted (*table);
+       db_set_table_delete_priv_not_granted (*table);
+       db_set_table_insert_priv_not_granted (*table);
+     */
 
 
     for (i = 0; i < ncols; i++) {
     for (i = 0; i < ncols; i++) {
-	int   sqlType;
-	int   size, precision, scale;
-
-	if ( !(cols[i]) ) continue; /* unknown type */
-	
-	hFieldDefn = OGR_FD_GetFieldDefn( hFeatureDefn, i );
-	ogrType = OGR_Fld_GetType( hFieldDefn );
-	fieldName = OGR_Fld_GetNameRef( hFieldDefn );
-
-	G_debug (3, "field %d : ogrType = %d, name = %s", i, ogrType, fieldName);
-	
-	switch ( ogrType ) {
-	    case OFTInteger:
-		sqlType = DB_SQL_TYPE_INTEGER;
-		size = OGR_Fld_GetWidth (hFieldDefn); /* OK ? */
-	        precision = 0;
-		break;
-		
-	    case OFTReal:
-		sqlType = DB_SQL_TYPE_DOUBLE_PRECISION;
-		size = OGR_Fld_GetWidth (hFieldDefn); /* OK ? */
-	        precision = OGR_Fld_GetPrecision (hFieldDefn);
-		break;
-		
-	    case OFTString:
-		sqlType = DB_SQL_TYPE_CHARACTER;
-		size = OGR_Fld_GetWidth (hFieldDefn);
-		if ( size == 0 ) {
-		   G_warning ( _("column '%s', type 'string': unknown width -> stored as varchar(250) "
-			         "some data may be lost"), fieldName );
-		   size = 250;
-		}
-	        precision = 0;
-		break;
-		
-	    default:
-		G_warning ( _("Unknown type") );
-		break;
+	int sqlType;
+	int size, precision, scale;
+
+	if (!(cols[i]))
+	    continue;		/* unknown type */
+
+	hFieldDefn = OGR_FD_GetFieldDefn(hFeatureDefn, i);
+	ogrType = OGR_Fld_GetType(hFieldDefn);
+	fieldName = OGR_Fld_GetNameRef(hFieldDefn);
+
+	G_debug(3, "field %d : ogrType = %d, name = %s", i, ogrType,
+		fieldName);
+
+	switch (ogrType) {
+	case OFTInteger:
+	    sqlType = DB_SQL_TYPE_INTEGER;
+	    size = OGR_Fld_GetWidth(hFieldDefn);	/* OK ? */
+	    precision = 0;
+	    break;
+
+	case OFTReal:
+	    sqlType = DB_SQL_TYPE_DOUBLE_PRECISION;
+	    size = OGR_Fld_GetWidth(hFieldDefn);	/* OK ? */
+	    precision = OGR_Fld_GetPrecision(hFieldDefn);
+	    break;
+
+	case OFTString:
+	    sqlType = DB_SQL_TYPE_CHARACTER;
+	    size = OGR_Fld_GetWidth(hFieldDefn);
+	    if (size == 0) {
+		G_warning(_
+			  ("column '%s', type 'string': unknown width -> stored as varchar(250) "
+			   "some data may be lost"), fieldName);
+		size = 250;
+	    }
+	    precision = 0;
+	    break;
+
+	default:
+	    G_warning(_("Unknown type"));
+	    break;
 	}
 	}
 
 
-	column = db_get_table_column (*table, i);
+	column = db_get_table_column(*table, i);
 
 
 	db_set_column_host_type(column, ogrType);
 	db_set_column_host_type(column, ogrType);
 	db_set_column_sqltype(column, sqlType);
 	db_set_column_sqltype(column, sqlType);
 	db_set_column_name(column, fieldName);
 	db_set_column_name(column, fieldName);
 	db_set_column_length(column, size);
 	db_set_column_length(column, size);
-        db_set_column_precision (column, precision);
+	db_set_column_precision(column, precision);
 
 
-        /* TODO */
-        scale = 0;  
-        /*
- 	db_set_column_scale (column, scale);
- 	*/
+	/* TODO */
+	scale = 0;
+	/*
+	   db_set_column_scale (column, scale);
+	 */
 
 
 	/* TODO */
 	/* TODO */
 	db_set_column_null_allowed(column);
 	db_set_column_null_allowed(column);
@@ -174,17 +185,18 @@ int describe_table( OGRLayerH hLayer, dbTable **table, cursor *c)
 
 
 	/* TODO */
 	/* TODO */
 	/*
 	/*
-        db_set_column_select_priv_granted (column);
-        db_set_column_update_priv_granted (column);
-        db_set_column_update_priv_not_granted (column); 
-	*/
+	   db_set_column_select_priv_granted (column);
+	   db_set_column_update_priv_granted (column);
+	   db_set_column_update_priv_not_granted (column); 
+	 */
     }
     }
 
 
-    if ( c ) {
+    if (c) {
 	c->cols = cols;
 	c->cols = cols;
-        c->ncols = ncols;
-    } else {
-	G_free ( cols );
+	c->ncols = ncols;
+    }
+    else {
+	G_free(cols);
     }
     }
 
 
     return DB_OK;
     return DB_OK;

+ 3 - 4
db/drivers/ogr/driver.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -18,15 +19,13 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_init ( int argc, char *argv[] )
+int db__driver_init(int argc, char *argv[])
 {
 {
     init_error();
     init_error();
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_finish()
+int db__driver_finish()
 {
 {
     return DB_OK;
     return DB_OK;
 }
 }

+ 19 - 22
db/drivers/ogr/error.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -23,44 +24,40 @@
 #include "proto.h"
 #include "proto.h"
 
 
 /* init error message */
 /* init error message */
-void
-init_error ( void )
+void init_error(void)
 {
 {
-    if ( !errMsg ) {
+    if (!errMsg) {
 	errMsg = (dbString *) G_malloc(sizeof(dbString));
 	errMsg = (dbString *) G_malloc(sizeof(dbString));
-        db_init_string (errMsg);
+	db_init_string(errMsg);
     }
     }
 
 
-    db_set_string ( errMsg, "DBMI-OGR driver error:\n");
+    db_set_string(errMsg, "DBMI-OGR driver error:\n");
 }
 }
 
 
 /* append error message */
 /* append error message */
-void
-append_error ( const char *fmt, ...)
+void append_error(const char *fmt, ...)
 {
 {
     FILE *fp = NULL;
     FILE *fp = NULL;
     char *work = NULL;
     char *work = NULL;
     int count = 0;
     int count = 0;
     va_list ap;
     va_list ap;
 
 
-    va_start (ap, fmt);
+    va_start(ap, fmt);
     if ((fp = tmpfile())) {
     if ((fp = tmpfile())) {
-        count = vfprintf (fp, fmt, ap);
-        if (count >= 0 && (work = G_calloc (count+1,1)) ) {
-	    rewind (fp);
-	    fread (work, 1, count, fp);
-	    db_append_string ( errMsg, work);
-	    G_free (work);
-        }
-        fclose (fp);
+	count = vfprintf(fp, fmt, ap);
+	if (count >= 0 && (work = G_calloc(count + 1, 1))) {
+	    rewind(fp);
+	    fread(work, 1, count, fp);
+	    db_append_string(errMsg, work);
+	    G_free(work);
+	}
+	fclose(fp);
     }
     }
-    va_end (ap);
+    va_end(ap);
 }
 }
 
 
-void
-report_error ( void )
+void report_error(void)
 {
 {
-    db_append_string ( errMsg, "\n");
-    db_error ( db_get_string (errMsg) );
+    db_append_string(errMsg, "\n");
+    db_error(db_get_string(errMsg));
 }
 }
-

+ 78 - 75
db/drivers/ogr/fetch.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -19,58 +20,57 @@
 #include <grass/dbmi.h>
 #include <grass/dbmi.h>
 #include "ogr_api.h"
 #include "ogr_api.h"
 #include "globals.h"
 #include "globals.h"
-#include "proto.h" 
+#include "proto.h"
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
-int
-db__driver_fetch( dbCursor *cn, int position, int *more )
+int db__driver_fetch(dbCursor * cn, int position, int *more)
 {
 {
-    cursor     *c;
-    dbToken    token;    
-    dbTable    *table;
-    int        i, col;
+    cursor *c;
+    dbToken token;
+    dbTable *table;
+    int i, col;
 
 
-    G_debug (3, "db_driver_fetch()" );
+    G_debug(3, "db_driver_fetch()");
 
 
     /* get cursor token */
     /* get cursor token */
     token = db_get_cursor_token(cn);
     token = db_get_cursor_token(cn);
 
 
     /* get the cursor by its token */
     /* get the cursor by its token */
     if (!(c = (cursor *) db_find_token(token))) {
     if (!(c = (cursor *) db_find_token(token))) {
-	append_error ("Cursor not found");
+	append_error("Cursor not found");
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     /* fetch on position */
     /* fetch on position */
     switch (position) {
     switch (position) {
-	case DB_NEXT:
-            G_debug (4, "DB_NEXT:" );
-            if ( c->hFeature )
-	        OGR_F_Destroy( c->hFeature );
-	    c->hFeature = OGR_L_GetNextFeature( c->hLayer );
-	    break;
-	case DB_CURRENT:
-	    break;
-	case DB_PREVIOUS:
-	    append_error ("DB_PREVIOUS not supported");
-	    report_error();
-	    return DB_FAILED;
-	    break;
-	case DB_FIRST:
-	    OGR_L_ResetReading ( c->hLayer );
-            if ( c->hFeature )
-	        OGR_F_Destroy( c->hFeature );
-	    c->hFeature = OGR_L_GetNextFeature( c->hLayer );
-	    break;
-	case DB_LAST:
-	    append_error ("DB_LAST not supported");
-	    report_error();
-	    return DB_FAILED;
-	    break;
+    case DB_NEXT:
+	G_debug(4, "DB_NEXT:");
+	if (c->hFeature)
+	    OGR_F_Destroy(c->hFeature);
+	c->hFeature = OGR_L_GetNextFeature(c->hLayer);
+	break;
+    case DB_CURRENT:
+	break;
+    case DB_PREVIOUS:
+	append_error("DB_PREVIOUS not supported");
+	report_error();
+	return DB_FAILED;
+	break;
+    case DB_FIRST:
+	OGR_L_ResetReading(c->hLayer);
+	if (c->hFeature)
+	    OGR_F_Destroy(c->hFeature);
+	c->hFeature = OGR_L_GetNextFeature(c->hLayer);
+	break;
+    case DB_LAST:
+	append_error("DB_LAST not supported");
+	report_error();
+	return DB_FAILED;
+	break;
     };
     };
-    
-    if ( c->hFeature == NULL ) {
+
+    if (c->hFeature == NULL) {
 	*more = 0;
 	*more = 0;
 	return DB_OK;
 	return DB_OK;
     }
     }
@@ -82,71 +82,74 @@ db__driver_fetch( dbCursor *cn, int position, int *more )
 
 
     col = -1;
     col = -1;
     for (i = 0; i < c->ncols; i++) {
     for (i = 0; i < c->ncols; i++) {
-	int       ogrType, sqlType;
-	dbColumn  *column;
-	dbValue   *value;
+	int ogrType, sqlType;
+	dbColumn *column;
+	dbValue *value;
 
 
-	if ( !(c->cols[i]) ) { continue; } /* unknown type */
+	if (!(c->cols[i])) {
+	    continue;
+	}			/* unknown type */
 	col++;
 	col++;
 
 
-	column = db_get_table_column (table, col);
-	ogrType  = db_get_column_host_type(column);
+	column = db_get_table_column(table, col);
+	ogrType = db_get_column_host_type(column);
 	sqlType = db_get_column_sqltype(column);
 	sqlType = db_get_column_sqltype(column);
 
 
-	value  = db_get_column_value (column);
-	db_zero_string (&value->s);
+	value = db_get_column_value(column);
+	db_zero_string(&value->s);
 
 
 	/* Is null? */
 	/* Is null? */
-	if ( OGR_F_IsFieldSet ( c->hFeature, i) ) {
+	if (OGR_F_IsFieldSet(c->hFeature, i)) {
 	    value->isNull = 0;
 	    value->isNull = 0;
-	} else {
+	}
+	else {
 	    value->isNull = 1;
 	    value->isNull = 1;
 	    continue;
 	    continue;
 	}
 	}
 
 
-	G_debug (3, "col %d, ogrType %d, sqlType %d: val = '%s'", 
-		    col, ogrType, sqlType, OGR_F_GetFieldAsString( c->hFeature, i) );
-	
-	switch ( ogrType ) {
-	    case OFTInteger:
-	    	value->i = OGR_F_GetFieldAsInteger ( c->hFeature, i );
-		break;
-		
-	    case OFTReal:
-	    	value->d = OGR_F_GetFieldAsDouble ( c->hFeature, i );
-		break;
-
-	    case OFTString:
-		db_set_string ( &(value->s), (char *) OGR_F_GetFieldAsString(c->hFeature, i) );
-		break;
-
-	    default:
-		G_warning ( _("Unknown type") );
-		break;
+	G_debug(3, "col %d, ogrType %d, sqlType %d: val = '%s'",
+		col, ogrType, sqlType, OGR_F_GetFieldAsString(c->hFeature,
+							      i));
+
+	switch (ogrType) {
+	case OFTInteger:
+	    value->i = OGR_F_GetFieldAsInteger(c->hFeature, i);
+	    break;
+
+	case OFTReal:
+	    value->d = OGR_F_GetFieldAsDouble(c->hFeature, i);
+	    break;
+
+	case OFTString:
+	    db_set_string(&(value->s),
+			  (char *)OGR_F_GetFieldAsString(c->hFeature, i));
+	    break;
+
+	default:
+	    G_warning(_("Unknown type"));
+	    break;
 	}
 	}
     }
     }
-    G_debug (4, "Row fetched" );
+    G_debug(4, "Row fetched");
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_get_num_rows (dbCursor *cn )
+int db__driver_get_num_rows(dbCursor * cn)
 {
 {
-    cursor     *c;
-    dbToken    token;
+    cursor *c;
+    dbToken token;
 
 
-    G_debug (3, "db_driver_get_num_rows()" );
+    G_debug(3, "db_driver_get_num_rows()");
 
 
     /* get cursor token */
     /* get cursor token */
     token = db_get_cursor_token(cn);
     token = db_get_cursor_token(cn);
 
 
     /* get the cursor by its token */
     /* get the cursor by its token */
     if (!(c = (cursor *) db_find_token(token))) {
     if (!(c = (cursor *) db_find_token(token))) {
-       append_error("Cursor not found");
-       report_error();
-       return DB_FAILED;
+	append_error("Cursor not found");
+	report_error();
+	return DB_FAILED;
     }
     }
 
 
-    return ( OGR_L_GetFeatureCount ( c->hLayer, 1 ) );
+    return (OGR_L_GetFeatureCount(c->hLayer, 1));
 }
 }
-

+ 14 - 14
db/drivers/ogr/globals.h

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -15,21 +16,20 @@
 *****************************************************************************/
 *****************************************************************************/
 
 
 /* cursor */
 /* cursor */
-typedef struct {
+typedef struct
+{
     dbToken token;
     dbToken token;
-    OGRLayerH hLayer;     /* results */
-    OGRFeatureH hFeature; /* current feature */
-    int type;             /* type of cursor: SELECT, UPDATE, INSERT */
-    int *cols;            /* 1 type is known, 0 type is unknown */
-    int ncols;            /* num columns */
-} cursor;  
+    OGRLayerH hLayer;		/* results */
+    OGRFeatureH hFeature;	/* current feature */
+    int type;			/* type of cursor: SELECT, UPDATE, INSERT */
+    int *cols;			/* 1 type is known, 0 type is unknown */
+    int ncols;			/* num columns */
+} cursor;
 
 
 #ifdef MAIN
 #ifdef MAIN
-    OGRDataSourceH hDs;
-    dbString *errMsg = NULL; /* error message */
+OGRDataSourceH hDs;
+dbString *errMsg = NULL;	/* error message */
 #else
 #else
-    extern OGRDataSourceH hDs;
-    extern dbString *errMsg;
-#endif 
-
-
+extern OGRDataSourceH hDs;
+extern dbString *errMsg;
+#endif

+ 12 - 12
db/drivers/ogr/listtab.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -19,7 +20,7 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_list_tables( dbString **tlist, int *tcount, int system )
+int db__driver_list_tables(dbString ** tlist, int *tcount, int system)
 {
 {
     int i, nlayers;
     int i, nlayers;
     dbString *list;
     dbString *list;
@@ -30,25 +31,24 @@ int db__driver_list_tables( dbString **tlist, int *tcount, int system )
     *tlist = NULL;
     *tlist = NULL;
     *tcount = 0;
     *tcount = 0;
 
 
-    nlayers = OGR_DS_GetLayerCount( hDs );
-    G_debug ( 3, "%d layers found", nlayers );
-    
-    list = db_alloc_string_array ( nlayers );
-    
-    if (list == NULL ) {
-	append_error ( "Cannot db_alloc_string_array()");
+    nlayers = OGR_DS_GetLayerCount(hDs);
+    G_debug(3, "%d layers found", nlayers);
+
+    list = db_alloc_string_array(nlayers);
+
+    if (list == NULL) {
+	append_error("Cannot db_alloc_string_array()");
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     for (i = 0; i < nlayers; i++) {
     for (i = 0; i < nlayers; i++) {
-	hLayer =  OGR_DS_GetLayer( hDs, i );
-	hFeatureDefn = OGR_L_GetLayerDefn ( hLayer );
-	db_set_string ( &(list[i]), (char *) OGR_FD_GetName( hFeatureDefn ) );
+	hLayer = OGR_DS_GetLayer(hDs, i);
+	hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
+	db_set_string(&(list[i]), (char *)OGR_FD_GetName(hFeatureDefn));
     }
     }
 
 
     *tlist = list;
     *tlist = list;
     *tcount = nlayers;
     *tcount = nlayers;
     return DB_OK;
     return DB_OK;
 }
 }
-

+ 4 - 3
db/drivers/ogr/main.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -20,8 +21,8 @@
 #include "globals.h"
 #include "globals.h"
 #include "dbdriver.h"
 #include "dbdriver.h"
 
 
-int main( int argc, char *argv[] )
+int main(int argc, char *argv[])
 {
 {
-	init_dbdriver();
-	exit (db_driver (argc, argv));
+    init_dbdriver();
+    exit(db_driver(argc, argv));
 }
 }

+ 8 - 7
db/drivers/ogr/proto.h

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -14,13 +15,13 @@
 *
 *
 *****************************************************************************/
 *****************************************************************************/
 /* error.c */
 /* error.c */
-void init_error ( void );
-void append_error ( const char *fmt, ...);
-void report_error ( void );
+void init_error(void);
+void append_error(const char *fmt, ...);
+void report_error(void);
 
 
 /* cursor.c */
 /* cursor.c */
-cursor * alloc_cursor ();
-void free_cursor ( cursor * );
+cursor *alloc_cursor();
+void free_cursor(cursor *);
 
 
-/* describe.c*/
-int describe_table( OGRLayerH, dbTable **, cursor * );
+/* describe.c */
+int describe_table(OGRLayerH, dbTable **, cursor *);

+ 9 - 9
db/drivers/ogr/select.c

@@ -1,3 +1,4 @@
+
 /*****************************************************************************
 /*****************************************************************************
 *
 *
 * MODULE:       OGR driver 
 * MODULE:       OGR driver 
@@ -19,7 +20,7 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_open_select_cursor( dbString *sel, dbCursor *dbc, int mode )
+int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode)
 {
 {
     cursor *c;
     cursor *c;
     dbTable *table;
     dbTable *table;
@@ -34,20 +35,20 @@ int db__driver_open_select_cursor( dbString *sel, dbCursor *dbc, int mode )
     db_set_cursor_mode(dbc, mode);
     db_set_cursor_mode(dbc, mode);
     db_set_cursor_type_readonly(dbc);
     db_set_cursor_type_readonly(dbc);
 
 
-    c->hLayer = OGR_DS_ExecuteSQL ( hDs, db_get_string(sel), NULL, NULL );
-    
-    if ( c->hLayer == NULL ) {
+    c->hLayer = OGR_DS_ExecuteSQL(hDs, db_get_string(sel), NULL, NULL);
+
+    if (c->hLayer == NULL) {
 	append_error("Cannot select: \n");
 	append_error("Cannot select: \n");
-	append_error(db_get_string(sel) );
-	append_error( "\n" );
+	append_error(db_get_string(sel));
+	append_error("\n");
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    if ( describe_table( c->hLayer, &table, c) == DB_FAILED ) {
+    if (describe_table(c->hLayer, &table, c) == DB_FAILED) {
 	append_error("Cannot describe table\n");
 	append_error("Cannot describe table\n");
 	report_error();
 	report_error();
-	OGR_DS_ReleaseResultSet( hDs, c->hLayer );
+	OGR_DS_ReleaseResultSet(hDs, c->hLayer);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
@@ -59,4 +60,3 @@ int db__driver_open_select_cursor( dbString *sel, dbCursor *dbc, int mode )
 
 
     return DB_OK;
     return DB_OK;
 }
 }
-

+ 84 - 86
db/drivers/postgres/create_table.c

@@ -2,102 +2,100 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_create_table (dbTable *table)
+int db__driver_create_table(dbTable * table)
 {
 {
     int col, ncols;
     int col, ncols;
-    dbColumn   *column;
+    dbColumn *column;
     const char *colname;
     const char *colname;
     int sqltype;
     int sqltype;
     char buf[500];
     char buf[500];
     PGresult *res;
     PGresult *res;
     dbString sql;
     dbString sql;
     dbConnection connection;
     dbConnection connection;
-    
-    G_debug (3, "db__driver_create_table()");
+
+    G_debug(3, "db__driver_create_table()");
 
 
     init_error();
     init_error();
 
 
-    db_init_string (&sql);
+    db_init_string(&sql);
 
 
     /* db_table_to_sql ( table, &sql ); */
     /* db_table_to_sql ( table, &sql ); */
 
 
-    db_set_string ( &sql, "create table ");
-    db_append_string ( &sql, db_get_table_name ( table ) );
-    db_append_string ( &sql, " ( ");
+    db_set_string(&sql, "create table ");
+    db_append_string(&sql, db_get_table_name(table));
+    db_append_string(&sql, " ( ");
 
 
     ncols = db_get_table_number_of_columns(table);
     ncols = db_get_table_number_of_columns(table);
 
 
-    for ( col = 0; col < ncols; col++ ) {
-        column = db_get_table_column (table, col);
-	colname = db_get_column_name (column);
-	sqltype = db_get_column_sqltype (column);
-	
-	G_debug ( 3, "%s (%s)", colname, db_sqltype_name(sqltype) );
-
-	if ( col > 0 ) db_append_string ( &sql, ", " );
-	db_append_string ( &sql, colname );
-	db_append_string ( &sql, " " );
-	switch ( sqltype ) 
-        {
-	    case DB_SQL_TYPE_CHARACTER:
-                sprintf (buf, "varchar(%d)", 
-			db_get_column_length (column) );
-                db_append_string ( &sql, buf);
-		break;
-	    case DB_SQL_TYPE_TEXT:
-		db_append_string ( &sql, "text");
-                break;
-
-	    case DB_SQL_TYPE_SMALLINT:
-		db_append_string ( &sql, "smallint");
-		break;
-	    case DB_SQL_TYPE_INTEGER:
-		db_append_string ( &sql, "integer");
-		break;
-
-	    case DB_SQL_TYPE_REAL:
-		db_append_string ( &sql, "real");
-		break;
-
-            /* TODO: better numeric types */
-	    case DB_SQL_TYPE_DOUBLE_PRECISION:
-	    case DB_SQL_TYPE_DECIMAL:
-	    case DB_SQL_TYPE_NUMERIC:
-	    case DB_SQL_TYPE_INTERVAL:
-		db_append_string ( &sql, "double precision");
-		break;
-
-            case DB_SQL_TYPE_DATE:
-                db_append_string ( &sql, "date");
-                break;
-            case DB_SQL_TYPE_TIME:
-                db_append_string ( &sql, "time");
-                break;
-            case DB_SQL_TYPE_TIMESTAMP:
-                db_append_string ( &sql, "timestamp");
-                break;
-
- 	    default:
-                G_warning ( "Unknown column type (%s)", colname);
-		return DB_FAILED;
+    for (col = 0; col < ncols; col++) {
+	column = db_get_table_column(table, col);
+	colname = db_get_column_name(column);
+	sqltype = db_get_column_sqltype(column);
+
+	G_debug(3, "%s (%s)", colname, db_sqltype_name(sqltype));
+
+	if (col > 0)
+	    db_append_string(&sql, ", ");
+	db_append_string(&sql, colname);
+	db_append_string(&sql, " ");
+	switch (sqltype) {
+	case DB_SQL_TYPE_CHARACTER:
+	    sprintf(buf, "varchar(%d)", db_get_column_length(column));
+	    db_append_string(&sql, buf);
+	    break;
+	case DB_SQL_TYPE_TEXT:
+	    db_append_string(&sql, "text");
+	    break;
+
+	case DB_SQL_TYPE_SMALLINT:
+	    db_append_string(&sql, "smallint");
+	    break;
+	case DB_SQL_TYPE_INTEGER:
+	    db_append_string(&sql, "integer");
+	    break;
+
+	case DB_SQL_TYPE_REAL:
+	    db_append_string(&sql, "real");
+	    break;
+
+	    /* TODO: better numeric types */
+	case DB_SQL_TYPE_DOUBLE_PRECISION:
+	case DB_SQL_TYPE_DECIMAL:
+	case DB_SQL_TYPE_NUMERIC:
+	case DB_SQL_TYPE_INTERVAL:
+	    db_append_string(&sql, "double precision");
+	    break;
+
+	case DB_SQL_TYPE_DATE:
+	    db_append_string(&sql, "date");
+	    break;
+	case DB_SQL_TYPE_TIME:
+	    db_append_string(&sql, "time");
+	    break;
+	case DB_SQL_TYPE_TIMESTAMP:
+	    db_append_string(&sql, "timestamp");
+	    break;
+
+	default:
+	    G_warning("Unknown column type (%s)", colname);
+	    return DB_FAILED;
 	}
 	}
     }
     }
-    db_append_string ( &sql, " )" );
+    db_append_string(&sql, " )");
+
 
 
+    G_debug(3, " SQL: %s", db_get_string(&sql));
 
 
-    G_debug (3, " SQL: %s", db_get_string(&sql) );
-    
-    res = PQexec(pg_conn, db_get_string(&sql) ); 
+    res = PQexec(pg_conn, db_get_string(&sql));
 
 
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
-        append_error( "Cannot create table:\n");
-	append_error( db_get_string(&sql) );
-	append_error( "\n" );
+	append_error("Cannot create table:\n");
+	append_error(db_get_string(&sql));
+	append_error("\n");
 	append_error(PQerrorMessage(pg_conn));
 	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
-	db_free_string ( &sql);
+	db_free_string(&sql);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
@@ -106,32 +104,32 @@ db__driver_create_table (dbTable *table)
     /* Grant privileges */
     /* Grant privileges */
     db_get_connection(&connection);
     db_get_connection(&connection);
 
 
-    db_set_string ( &sql, "grant select on " );
-    db_append_string ( &sql, db_get_table_name ( table ) );
-    db_append_string ( &sql, " to public" );
+    db_set_string(&sql, "grant select on ");
+    db_append_string(&sql, db_get_table_name(table));
+    db_append_string(&sql, " to public");
 
 
-    if ( connection.group ) {
-        db_append_string ( &sql, ", group " );
-	db_append_string ( &sql, connection.group );
+    if (connection.group) {
+	db_append_string(&sql, ", group ");
+	db_append_string(&sql, connection.group);
     }
     }
-    
-    G_debug (3, " SQL: %s", db_get_string(&sql) );
 
 
-    res = PQexec(pg_conn, db_get_string(&sql) ); 
+    G_debug(3, " SQL: %s", db_get_string(&sql));
+
+    res = PQexec(pg_conn, db_get_string(&sql));
 
 
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
-        append_error( "Cannot grant select on table:\n");
-	append_error( db_get_string(&sql) );
-	append_error( "\n" );
+	append_error("Cannot grant select on table:\n");
+	append_error(db_get_string(&sql));
+	append_error("\n");
 	append_error(PQerrorMessage(pg_conn));
 	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
-	db_free_string ( &sql);
+	db_free_string(&sql);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
+
     PQclear(res);
     PQclear(res);
-    db_free_string ( &sql);
-    
+    db_free_string(&sql);
+
     return DB_OK;
     return DB_OK;
 }
 }

+ 12 - 16
db/drivers/postgres/cursor.c

@@ -4,9 +4,7 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_close_cursor (dbCursor *dbc)
-
+int db__driver_close_cursor(dbCursor * dbc)
 {
 {
     cursor *c;
     cursor *c;
 
 
@@ -24,24 +22,24 @@ db__driver_close_cursor (dbCursor *dbc)
 }
 }
 
 
 
 
-cursor * alloc_cursor(void)
+cursor *alloc_cursor(void)
 {
 {
-    cursor     *c;
+    cursor *c;
 
 
     /* allocate the cursor */
     /* allocate the cursor */
     c = (cursor *) db_malloc(sizeof(cursor));
     c = (cursor *) db_malloc(sizeof(cursor));
     if (c == NULL) {
     if (c == NULL) {
-        append_error("Cannot allocate cursor.");
-        return NULL; 
-    } 
+	append_error("Cannot allocate cursor.");
+	return NULL;
+    }
 
 
     c->res = NULL;
     c->res = NULL;
-    
+
     /* tokenize it */
     /* tokenize it */
     c->token = db_new_token(c);
     c->token = db_new_token(c);
     if (c->token < 0) {
     if (c->token < 0) {
-        append_error("Cannot ad new token.");
-        return NULL; 
+	append_error("Cannot ad new token.");
+	return NULL;
     }
     }
 
 
     c->cols = NULL;
     c->cols = NULL;
@@ -50,14 +48,12 @@ cursor * alloc_cursor(void)
     return c;
     return c;
 }
 }
 
 
-void free_cursor( cursor *c)
+void free_cursor(cursor * c)
 {
 {
     db_drop_token(c->token);
     db_drop_token(c->token);
 
 
     /* TODO close results if any */
     /* TODO close results if any */
 
 
-    G_free ( c->cols );
-    G_free(c);  
+    G_free(c->cols);
+    G_free(c);
 }
 }
-
-

+ 65 - 58
db/drivers/postgres/db.c

@@ -5,8 +5,7 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_open_database (dbHandle *handle)
-
+int db__driver_open_database(dbHandle * handle)
 {
 {
     char buf[500];
     char buf[500];
     const char *name, *schema, *user, *password;
     const char *name, *schema, *user, *password;
@@ -20,53 +19,58 @@ int db__driver_open_database (dbHandle *handle)
     name = db_get_handle_dbname(handle);
     name = db_get_handle_dbname(handle);
 
 
     /* if name is empty use connection.databaseName */
     /* if name is empty use connection.databaseName */
-    if (strlen(name) == 0) 
+    if (strlen(name) == 0)
 	name = connection.databaseName;
 	name = connection.databaseName;
 
 
-    G_debug(3, "db_driver_open_database() driver=pg database definition = '%s'", name );
+    G_debug(3,
+	    "db_driver_open_database() driver=pg database definition = '%s'",
+	    name);
 
 
-    if ( parse_conn ( name, &pgconn ) == DB_FAILED ) {
+    if (parse_conn(name, &pgconn) == DB_FAILED) {
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
-    G_debug(3, "host = %s, port = %s, options = %s, tty = %s, dbname = %s, user = %s, password = %s, "
-	       "schema = %s", pgconn.host, pgconn.port, pgconn.options, pgconn.tty,  
-		pgconn.dbname, pgconn.user, pgconn.password, pgconn.schema );
 
 
-    db_get_login ( "pg", name, &user, &password );
+    G_debug(3,
+	    "host = %s, port = %s, options = %s, tty = %s, dbname = %s, user = %s, password = %s, "
+	    "schema = %s", pgconn.host, pgconn.port, pgconn.options,
+	    pgconn.tty, pgconn.dbname, pgconn.user, pgconn.password,
+	    pgconn.schema);
+
+    db_get_login("pg", name, &user, &password);
+
+    pg_conn =
+	PQsetdbLogin(pgconn.host, pgconn.port, pgconn.options, pgconn.tty,
+		     pgconn.dbname, user, password);
 
 
-    pg_conn = PQsetdbLogin( pgconn.host, pgconn.port, pgconn.options, pgconn.tty, 
-		            pgconn.dbname, user, password );
-    
     if (PQstatus(pg_conn) == CONNECTION_BAD) {
     if (PQstatus(pg_conn) == CONNECTION_BAD) {
-	append_error ( "Cannot connect to Postgres: " );
-        append_error ( PQerrorMessage(pg_conn) );
-	report_error ();
+	append_error("Cannot connect to Postgres: ");
+	append_error(PQerrorMessage(pg_conn));
+	report_error();
 	PQfinish(pg_conn);
 	PQfinish(pg_conn);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     /* Set schema */
     /* Set schema */
-    schema = db_get_handle_dbschema (handle);
+    schema = db_get_handle_dbschema(handle);
 
 
     /* Cannot use default schema because link to table can point to different database */
     /* Cannot use default schema because link to table can point to different database */
     /*
     /*
-    if ( schema ) 
-	schema = connection.schemaName;
-    */
+       if ( schema ) 
+       schema = connection.schemaName;
+     */
 
 
-    if ( pgconn.schema ) {
+    if (pgconn.schema) {
 	schema = pgconn.schema;
 	schema = pgconn.schema;
     }
     }
-    
-    if ( schema && strlen(schema) > 0 ) {
-	sprintf ( buf, "set search_path to %s", schema );
-	res = PQexec ( pg_conn, buf );
-	
+
+    if (schema && strlen(schema) > 0) {
+	sprintf(buf, "set search_path to %s", schema);
+	res = PQexec(pg_conn, buf);
+
 	if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
 	if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
-	    append_error ( "Cannot set schema: " );
-	    append_error ( schema );
+	    append_error("Cannot set schema: ");
+	    append_error(schema);
 	    report_error();
 	    report_error();
 	    PQclear(res);
 	    PQclear(res);
 	    return DB_FAILED;
 	    return DB_FAILED;
@@ -74,70 +78,73 @@ int db__driver_open_database (dbHandle *handle)
     }
     }
 
 
     /* Read internal codes */
     /* Read internal codes */
-    res = PQexec(pg_conn, "select oid, typname from pg_type where typname in ( "
-	         "'bit', 'int2', 'int4', 'int8', 'serial', 'oid', "
-		 "'float4', 'float8', 'numeric', "
-		 "'char', 'bpchar', 'varchar', 'text', "
-		 "'time', 'date', 'timestamp', "
-	   	 "'bool', 'geometry' ) order by oid" );
+    res =
+	PQexec(pg_conn,
+	       "select oid, typname from pg_type where typname in ( "
+	       "'bit', 'int2', 'int4', 'int8', 'serial', 'oid', "
+	       "'float4', 'float8', 'numeric', "
+	       "'char', 'bpchar', 'varchar', 'text', "
+	       "'time', 'date', 'timestamp', "
+	       "'bool', 'geometry' ) order by oid");
 
 
     if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
     if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
-	append_error ( "Cannot select data types" );
+	append_error("Cannot select data types");
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     pg_ntypes = PQntuples(res);
     pg_ntypes = PQntuples(res);
-    pg_types = G_realloc ( pg_types, 2 * pg_ntypes * sizeof(int) );
+    pg_types = G_realloc(pg_types, 2 * pg_ntypes * sizeof(int));
 
 
-    for ( row = 0; row < pg_ntypes; row++ ) {
+    for (row = 0; row < pg_ntypes; row++) {
 	int pgtype, type;
 	int pgtype, type;
 
 
-	pgtype = atoi ( PQgetvalue(res, row, 0) );
+	pgtype = atoi(PQgetvalue(res, row, 0));
 
 
 	pg_types[row][0] = pgtype;
 	pg_types[row][0] = pgtype;
 
 
-	if ( strcmp( PQgetvalue(res, row, 1), "bit" ) == 0 )
+	if (strcmp(PQgetvalue(res, row, 1), "bit") == 0)
 	    type = PG_TYPE_BIT;
 	    type = PG_TYPE_BIT;
-	else if ( strcmp( PQgetvalue(res, row, 1), "int2" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "int2") == 0)
 	    type = PG_TYPE_INT2;
 	    type = PG_TYPE_INT2;
-	else if ( strcmp( PQgetvalue(res, row, 1), "int4" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "int4") == 0)
 	    type = PG_TYPE_INT4;
 	    type = PG_TYPE_INT4;
-	else if ( strcmp( PQgetvalue(res, row, 1), "int8" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "int8") == 0)
 	    type = PG_TYPE_INT8;
 	    type = PG_TYPE_INT8;
-	else if ( strcmp( PQgetvalue(res, row, 1), "serial" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "serial") == 0)
 	    type = PG_TYPE_SERIAL;
 	    type = PG_TYPE_SERIAL;
-	else if ( strcmp( PQgetvalue(res, row, 1), "oid" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "oid") == 0)
 	    type = PG_TYPE_OID;
 	    type = PG_TYPE_OID;
-	else if ( strcmp( PQgetvalue(res, row, 1), "float4" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "float4") == 0)
 	    type = PG_TYPE_FLOAT4;
 	    type = PG_TYPE_FLOAT4;
-	else if ( strcmp( PQgetvalue(res, row, 1), "float8" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "float8") == 0)
 	    type = PG_TYPE_FLOAT8;
 	    type = PG_TYPE_FLOAT8;
-	else if ( strcmp( PQgetvalue(res, row, 1), "numeric" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "numeric") == 0)
 	    type = PG_TYPE_NUMERIC;
 	    type = PG_TYPE_NUMERIC;
-	else if ( strcmp( PQgetvalue(res, row, 1), "char" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "char") == 0)
 	    type = PG_TYPE_CHAR;
 	    type = PG_TYPE_CHAR;
-	else if ( strcmp( PQgetvalue(res, row, 1), "bpchar" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "bpchar") == 0)
 	    type = PG_TYPE_BPCHAR;
 	    type = PG_TYPE_BPCHAR;
-	else if ( strcmp( PQgetvalue(res, row, 1), "varchar" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "varchar") == 0)
 	    type = PG_TYPE_VARCHAR;
 	    type = PG_TYPE_VARCHAR;
-	else if ( strcmp( PQgetvalue(res, row, 1), "text" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "text") == 0)
 	    type = PG_TYPE_TEXT;
 	    type = PG_TYPE_TEXT;
-	else if ( strcmp( PQgetvalue(res, row, 1), "date" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "date") == 0)
 	    type = PG_TYPE_DATE;
 	    type = PG_TYPE_DATE;
-	else if ( strcmp( PQgetvalue(res, row, 1), "time" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "time") == 0)
 	    type = PG_TYPE_TIME;
 	    type = PG_TYPE_TIME;
-	else if ( strcmp( PQgetvalue(res, row, 1), "timestamp" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "timestamp") == 0)
 	    type = PG_TYPE_TIMESTAMP;
 	    type = PG_TYPE_TIMESTAMP;
-	else if ( strcmp( PQgetvalue(res, row, 1), "bool" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "bool") == 0)
 	    type = PG_TYPE_BOOL;
 	    type = PG_TYPE_BOOL;
-	else if ( strcmp( PQgetvalue(res, row, 1), "geometry" ) == 0 )
+	else if (strcmp(PQgetvalue(res, row, 1), "geometry") == 0)
 	    type = PG_TYPE_POSTGIS_GEOM;
 	    type = PG_TYPE_POSTGIS_GEOM;
-	else 
+	else
 	    type = PG_TYPE_UNKNOWN;
 	    type = PG_TYPE_UNKNOWN;
 
 
-	G_debug ( 3, "pgtype = %d, \tname = %s -> \ttype = %d", pgtype,  PQgetvalue(res, row, 1), type );
+	G_debug(3, "pgtype = %d, \tname = %s -> \ttype = %d", pgtype,
+		PQgetvalue(res, row, 1), type);
 	pg_types[row][1] = type;
 	pg_types[row][1] = type;
     }
     }
 
 

+ 1 - 0
db/drivers/postgres/dbdriver.h

@@ -21,6 +21,7 @@ int db__driver_list_databases();
 int db__driver_list_tables();
 int db__driver_list_tables();
 int db__driver_grant_on_table();
 int db__driver_grant_on_table();
 int db__driver_open_select_cursor();
 int db__driver_open_select_cursor();
+
 #define	init_dbdriver() do{\
 #define	init_dbdriver() do{\
 db_driver_create_table = db__driver_create_table;\
 db_driver_create_table = db__driver_create_table;\
 db_driver_close_cursor = db__driver_close_cursor;\
 db_driver_close_cursor = db__driver_close_cursor;\

+ 125 - 114
db/drivers/postgres/describe.c

@@ -4,29 +4,29 @@
 #include "proto.h"
 #include "proto.h"
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
-int db__driver_describe_table( dbString *table_name, dbTable **table)
+int db__driver_describe_table(dbString * table_name, dbTable ** table)
 {
 {
     dbString sql;
     dbString sql;
     PGresult *res;
     PGresult *res;
 
 
-    db_init_string ( &sql );
+    db_init_string(&sql);
 
 
-    db_set_string( &sql, "select * from ");
-    db_append_string ( &sql, db_get_string(table_name) );
-    db_append_string( &sql, " where 1 = 0");
+    db_set_string(&sql, "select * from ");
+    db_append_string(&sql, db_get_string(table_name));
+    db_append_string(&sql, " where 1 = 0");
 
 
     res = PQexec(pg_conn, db_get_string(&sql));
     res = PQexec(pg_conn, db_get_string(&sql));
 
 
     if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
     if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
-	append_error ( db_get_string(&sql) );
-	append_error ( "\n" );
-	append_error ( PQerrorMessage(pg_conn) );
+	append_error(db_get_string(&sql));
+	append_error("\n");
+	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    if ( describe_table( res, table, NULL) == DB_FAILED ) {
+    if (describe_table(res, table, NULL) == DB_FAILED) {
 	append_error("Cannot describe table\n");
 	append_error("Cannot describe table\n");
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
@@ -39,7 +39,7 @@ int db__driver_describe_table( dbString *table_name, dbTable **table)
 }
 }
 
 
 /* describe table, if c is not NULL cur->cols and cur->ncols is also set */
 /* describe table, if c is not NULL cur->cols and cur->ncols is also set */
-int describe_table( PGresult *res, dbTable **table, cursor *c)
+int describe_table(PGresult * res, dbTable ** table, cursor * c)
 {
 {
     int i, ncols, kcols;
     int i, ncols, kcols;
     int pgtype, gpgtype;
     int pgtype, gpgtype;
@@ -47,30 +47,31 @@ int describe_table( PGresult *res, dbTable **table, cursor *c)
     int sqltype, fsize, precision, scale;
     int sqltype, fsize, precision, scale;
     dbColumn *column;
     dbColumn *column;
 
 
-    G_debug (3, "describe_table()");
+    G_debug(3, "describe_table()");
 
 
     ncols = PQnfields(res);
     ncols = PQnfields(res);
 
 
-    
+
     /* Count columns of known type */
     /* Count columns of known type */
     kcols = 0;
     kcols = 0;
     for (i = 0; i < ncols; i++) {
     for (i = 0; i < ncols; i++) {
-	get_column_info (res, i, &pgtype, &gpgtype, &sqltype, &fsize );
+	get_column_info(res, i, &pgtype, &gpgtype, &sqltype, &fsize);
+
+	if (sqltype == DB_SQL_TYPE_UNKNOWN)
+	    continue;
 
 
-	if ( sqltype == DB_SQL_TYPE_UNKNOWN ) continue;
-	    
-	kcols++; /* known types */
+	kcols++;		/* known types */
     }
     }
 
 
-    G_debug (3, "kcols = %d", kcols);
+    G_debug(3, "kcols = %d", kcols);
 
 
     if (!(*table = db_alloc_table(kcols))) {
     if (!(*table = db_alloc_table(kcols))) {
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    if ( c ) {
+    if (c) {
 	c->ncols = kcols;
 	c->ncols = kcols;
-	c->cols = (int *) G_malloc ( kcols * sizeof(int) );
+	c->cols = (int *)G_malloc(kcols * sizeof(int));
     }
     }
 
 
     /* set the table name */
     /* set the table name */
@@ -82,45 +83,54 @@ int describe_table( PGresult *res, dbTable **table, cursor *c)
 
 
     /* TODO */
     /* TODO */
     /*
     /*
-    db_set_table_delete_priv_granted (*table);
-    db_set_table_insert_priv_granted (*table);
-    db_set_table_delete_priv_not_granted (*table);
-    db_set_table_insert_priv_not_granted (*table);
-    */
+       db_set_table_delete_priv_granted (*table);
+       db_set_table_insert_priv_granted (*table);
+       db_set_table_delete_priv_not_granted (*table);
+       db_set_table_insert_priv_not_granted (*table);
+     */
 
 
     kcols = 0;
     kcols = 0;
     for (i = 0; i < ncols; i++) {
     for (i = 0; i < ncols; i++) {
 	fname = PQfname(res, i);
 	fname = PQfname(res, i);
-	get_column_info (res, i, &pgtype, &gpgtype, &sqltype, &fsize );
-	G_debug(3, "col: %s, kcols %d, pgtype : %d, gpgtype : %d, sqltype %d, fsize : %d", 
-		    fname, kcols, pgtype, gpgtype, sqltype, fsize);
+	get_column_info(res, i, &pgtype, &gpgtype, &sqltype, &fsize);
+	G_debug(3,
+		"col: %s, kcols %d, pgtype : %d, gpgtype : %d, sqltype %d, fsize : %d",
+		fname, kcols, pgtype, gpgtype, sqltype, fsize);
 
 
 	/* PG types defined in globals.h (and pg_type.h) */
 	/* PG types defined in globals.h (and pg_type.h) */
-	if ( sqltype == DB_SQL_TYPE_UNKNOWN ) {
+	if (sqltype == DB_SQL_TYPE_UNKNOWN) {
 	    if (gpgtype == PG_TYPE_POSTGIS_GEOM) {
 	    if (gpgtype == PG_TYPE_POSTGIS_GEOM) {
-		G_warning ( _("pg driver: PostGIS column '%s', type 'geometry'  will not be converted"), fname);
+		G_warning(_
+			  ("pg driver: PostGIS column '%s', type 'geometry'  will not be converted"),
+			  fname);
 		continue;
 		continue;
-	    } else {
+	    }
+	    else {
 		/* Warn, ignore and continue */
 		/* Warn, ignore and continue */
-		G_warning ( _("pg driver: column '%s', type %d  is not supported"), fname, pgtype);
+		G_warning(_
+			  ("pg driver: column '%s', type %d  is not supported"),
+			  fname, pgtype);
 		continue;
 		continue;
 	    }
 	    }
 	}
 	}
 
 
-	if ( gpgtype == PG_TYPE_INT8 )
-	    G_warning ( _("column '%s' : type int8 (bigint) is stored as integer (4 bytes) "
-		          "some data may be damaged"), fname);
-	
-	if ( gpgtype == PG_TYPE_VARCHAR && fsize < 0 ) {
-	    G_warning ( _("column '%s' : type character varying is stored as varchar(250) "
-		          "some data may be lost"), fname);
+	if (gpgtype == PG_TYPE_INT8)
+	    G_warning(_
+		      ("column '%s' : type int8 (bigint) is stored as integer (4 bytes) "
+		       "some data may be damaged"), fname);
+
+	if (gpgtype == PG_TYPE_VARCHAR && fsize < 0) {
+	    G_warning(_
+		      ("column '%s' : type character varying is stored as varchar(250) "
+		       "some data may be lost"), fname);
 	    fsize = 250;
 	    fsize = 250;
 	}
 	}
-	
-	if ( gpgtype == PG_TYPE_BOOL )
-	    G_warning ( _("column '%s' : type bool (boolean) is stored as char(1), values: 0 (false), "
-		          "1 (true)"), fname);
-	
+
+	if (gpgtype == PG_TYPE_BOOL)
+	    G_warning(_
+		      ("column '%s' : type bool (boolean) is stored as char(1), values: 0 (false), "
+		       "1 (true)"), fname);
+
 	column = db_get_table_column(*table, kcols);
 	column = db_get_table_column(*table, kcols);
 
 
 	db_set_column_name(column, fname);
 	db_set_column_name(column, fname);
@@ -128,13 +138,13 @@ int describe_table( PGresult *res, dbTable **table, cursor *c)
 	db_set_column_host_type(column, gpgtype);
 	db_set_column_host_type(column, gpgtype);
 	db_set_column_sqltype(column, sqltype);
 	db_set_column_sqltype(column, sqltype);
 
 
-        /* TODO */
-	precision = 0; 
-        scale = 0;  
-        /*
-        db_set_column_precision (column, precision);
- 	db_set_column_scale (column, scale);
- 	*/
+	/* TODO */
+	precision = 0;
+	scale = 0;
+	/*
+	   db_set_column_precision (column, precision);
+	   db_set_column_scale (column, scale);
+	 */
 
 
 	/* TODO */
 	/* TODO */
 	db_set_column_null_allowed(column);
 	db_set_column_null_allowed(column);
@@ -143,12 +153,12 @@ int describe_table( PGresult *res, dbTable **table, cursor *c)
 
 
 	/* TODO */
 	/* TODO */
 	/*
 	/*
-        db_set_column_select_priv_granted (column);
-        db_set_column_update_priv_granted (column);
-        db_set_column_update_priv_not_granted (column); 
-	*/
+	   db_set_column_select_priv_granted (column);
+	   db_set_column_update_priv_granted (column);
+	   db_set_column_update_priv_not_granted (column); 
+	 */
 
 
-	if ( c ) {
+	if (c) {
 	    c->cols[kcols] = i;
 	    c->cols[kcols] = i;
 	}
 	}
 
 
@@ -158,79 +168,80 @@ int describe_table( PGresult *res, dbTable **table, cursor *c)
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int get_column_info ( PGresult *res, int col, int *pgtype, int *gpgtype, int *sqltype, int *size)
+int get_column_info(PGresult * res, int col, int *pgtype, int *gpgtype,
+		    int *sqltype, int *size)
 {
 {
-    *pgtype = (int) PQftype(res, col);
-    *gpgtype = get_gpg_type ( *pgtype );
+    *pgtype = (int)PQftype(res, col);
+    *gpgtype = get_gpg_type(*pgtype);
 
 
     /* Convert internal type to PG_TYPE_* */
     /* Convert internal type to PG_TYPE_* */
-    
+
     /* TODO: we should load field names from pg_type table instead of using copy of #defines */
     /* TODO: we should load field names from pg_type table instead of using copy of #defines */
-    switch ( *gpgtype) {
-	case PG_TYPE_BIT:
-	case PG_TYPE_INT2:
-	case PG_TYPE_INT4:
-	case PG_TYPE_INT8:
-	case PG_TYPE_SERIAL:
-	case PG_TYPE_OID:
-	    *sqltype = DB_SQL_TYPE_INTEGER;
-	    *size = PQfsize(res, col);
-	    break;
-
-	case PG_TYPE_CHAR:
-	case PG_TYPE_BPCHAR:
-	case PG_TYPE_VARCHAR:
-	    *sqltype = DB_SQL_TYPE_CHARACTER;
-	    *size = PQfmod(res, col) - 4; /* Looks strange but works, something better? */
-	    break;
-	    
-	case PG_TYPE_TEXT: 
-	    *sqltype = DB_SQL_TYPE_TEXT;
-	    break;
-
-	case PG_TYPE_FLOAT4:
-	case PG_TYPE_FLOAT8:
-	case PG_TYPE_NUMERIC:
-	    *sqltype = DB_SQL_TYPE_DOUBLE_PRECISION;
-	    *size = PQfsize(res, col);
-	    break;
-	    
+    switch (*gpgtype) {
+    case PG_TYPE_BIT:
+    case PG_TYPE_INT2:
+    case PG_TYPE_INT4:
+    case PG_TYPE_INT8:
+    case PG_TYPE_SERIAL:
+    case PG_TYPE_OID:
+	*sqltype = DB_SQL_TYPE_INTEGER;
+	*size = PQfsize(res, col);
+	break;
+
+    case PG_TYPE_CHAR:
+    case PG_TYPE_BPCHAR:
+    case PG_TYPE_VARCHAR:
+	*sqltype = DB_SQL_TYPE_CHARACTER;
+	*size = PQfmod(res, col) - 4;	/* Looks strange but works, something better? */
+	break;
+
+    case PG_TYPE_TEXT:
+	*sqltype = DB_SQL_TYPE_TEXT;
+	break;
+
+    case PG_TYPE_FLOAT4:
+    case PG_TYPE_FLOAT8:
+    case PG_TYPE_NUMERIC:
+	*sqltype = DB_SQL_TYPE_DOUBLE_PRECISION;
+	*size = PQfsize(res, col);
+	break;
+
 	/* I'm not sure if text length is correct for size */
 	/* I'm not sure if text length is correct for size */
-	case PG_TYPE_DATE:
-	    *sqltype = DB_SQL_TYPE_DATE;
-	    *size = 10; /* YYYY-MM-DD */
-	    break;
-
-	case PG_TYPE_TIME:
-	    *sqltype = DB_SQL_TYPE_TIME;
-	    *size = 8; /* HH:MM:SS */
-	    break;
-
-	case PG_TYPE_TIMESTAMP: 
-	    *sqltype = DB_SQL_TYPE_TIMESTAMP;
-	    *size = 22; /* YYYY-MM-DD HH:MM:SS+TZ */
-	    break;
-
-	case PG_TYPE_BOOL:
-	    *sqltype = DB_SQL_TYPE_CHARACTER;
-	    *size = 1;
-	    break;
-
-	default:
-	    *sqltype = DB_SQL_TYPE_UNKNOWN;
-	    *size = 0;
+    case PG_TYPE_DATE:
+	*sqltype = DB_SQL_TYPE_DATE;
+	*size = 10;		/* YYYY-MM-DD */
+	break;
+
+    case PG_TYPE_TIME:
+	*sqltype = DB_SQL_TYPE_TIME;
+	*size = 8;		/* HH:MM:SS */
+	break;
+
+    case PG_TYPE_TIMESTAMP:
+	*sqltype = DB_SQL_TYPE_TIMESTAMP;
+	*size = 22;		/* YYYY-MM-DD HH:MM:SS+TZ */
+	break;
+
+    case PG_TYPE_BOOL:
+	*sqltype = DB_SQL_TYPE_CHARACTER;
+	*size = 1;
+	break;
+
+    default:
+	*sqltype = DB_SQL_TYPE_UNKNOWN;
+	*size = 0;
     }
     }
 
 
     return 0;
     return 0;
 }
 }
 
 
 /* for given internal postgres type returns GRASS Postgres type (one of PG_TYPE_*) */
 /* for given internal postgres type returns GRASS Postgres type (one of PG_TYPE_*) */
-int get_gpg_type (int pgtype )
+int get_gpg_type(int pgtype)
 {
 {
     int i;
     int i;
 
 
-    for ( i = 0; i < pg_ntypes; i++ ){
-	if ( pg_types[i][0] == pgtype )
+    for (i = 0; i < pg_ntypes; i++) {
+	if (pg_types[i][0] == pgtype)
 	    return pg_types[i][1];
 	    return pg_types[i][1];
     }
     }
     return PG_TYPE_UNKNOWN;
     return PG_TYPE_UNKNOWN;

+ 2 - 5
db/drivers/postgres/driver.c

@@ -2,16 +2,13 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_init  (int argc, char *argv[])
-
+int db__driver_init(int argc, char *argv[])
 {
 {
     init_error();
     init_error();
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_finish()
+int db__driver_finish()
 {
 {
     return DB_OK;
     return DB_OK;
 }
 }

+ 9 - 13
db/drivers/postgres/error.c

@@ -4,29 +4,25 @@
 #include "globals.h"
 #include "globals.h"
 
 
 /* init error message */
 /* init error message */
-void
-init_error ( void )
+void init_error(void)
 {
 {
-    if ( !errMsg ) {
+    if (!errMsg) {
 	errMsg = (dbString *) G_malloc(sizeof(dbString));
 	errMsg = (dbString *) G_malloc(sizeof(dbString));
-        db_init_string (errMsg);
+	db_init_string(errMsg);
     }
     }
 
 
-    db_set_string ( errMsg, "DBMI-Postgres driver error:\n");
+    db_set_string(errMsg, "DBMI-Postgres driver error:\n");
 }
 }
 
 
 /* append error message */
 /* append error message */
-void
-append_error ( const char *msg )
+void append_error(const char *msg)
 {
 {
-    db_append_string ( errMsg, msg);
+    db_append_string(errMsg, msg);
 }
 }
 
 
 
 
-void
-report_error ( void )
+void report_error(void)
 {
 {
-    db_append_string ( errMsg, "\n");
-    db_error ( db_get_string (errMsg) );
+    db_append_string(errMsg, "\n");
+    db_error(db_get_string(errMsg));
 }
 }
-

+ 20 - 21
db/drivers/postgres/execute.c

@@ -1,3 +1,4 @@
+
 /****************************************************************************
 /****************************************************************************
  *
  *
  * MODULE:       execute
  * MODULE:       execute
@@ -17,11 +18,10 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_execute_immediate (dbString *sql)
-
+int db__driver_execute_immediate(dbString * sql)
 {
 {
     PGresult *res;
     PGresult *res;
-    char     *str;
+    char *str;
 
 
     init_error();
     init_error();
 
 
@@ -31,26 +31,26 @@ int db__driver_execute_immediate (dbString *sql)
      * string \' GRASS modules escape ' by another ' and string passed to driver is \''
      * string \' GRASS modules escape ' by another ' and string passed to driver is \''
      * postgres takes \' as ' but second ' remains not escaped, result is error.
      * postgres takes \' as ' but second ' remains not escaped, result is error.
      * Because of this, all occurencies of \ in sql are escaped by \ */
      * Because of this, all occurencies of \ in sql are escaped by \ */
-    str = G_str_replace ( db_get_string(sql), "\\", "\\\\" );
+    str = G_str_replace(db_get_string(sql), "\\", "\\\\");
 
 
-    G_debug ( 3, "Escaped SQL: %s", str );
+    G_debug(3, "Escaped SQL: %s", str);
 
 
-    res = PQexec(pg_conn, str );
+    res = PQexec(pg_conn, str);
 
 
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
-	append_error( "Cannot execute: \n" );
-	append_error( str );
-	append_error( "\n" );
+	append_error("Cannot execute: \n");
+	append_error(str);
+	append_error("\n");
 	append_error(PQerrorMessage(pg_conn));
 	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
-	if ( str ) 
-	    G_free ( str );
+	if (str)
+	    G_free(str);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
-    if ( str ) 
-	G_free ( str );
+
+    if (str)
+	G_free(str);
     PQclear(res);
     PQclear(res);
 
 
     return DB_OK;
     return DB_OK;
@@ -60,18 +60,18 @@ int db__driver_begin_transaction(void)
 {
 {
     PGresult *res;
     PGresult *res;
 
 
-    G_debug (2, "pg : BEGIN");
+    G_debug(2, "pg : BEGIN");
 
 
     init_error();
     init_error();
     res = PQexec(pg_conn, "BEGIN");
     res = PQexec(pg_conn, "BEGIN");
 
 
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
-	append_error( "Cannot 'BEGIN' transaction");
+	append_error("Cannot 'BEGIN' transaction");
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
+
     PQclear(res);
     PQclear(res);
 
 
     return DB_OK;
     return DB_OK;
@@ -81,20 +81,19 @@ int db__driver_commit_transaction(void)
 {
 {
     PGresult *res;
     PGresult *res;
 
 
-    G_debug (2, "pg : COMMIT");
+    G_debug(2, "pg : COMMIT");
 
 
     init_error();
     init_error();
     res = PQexec(pg_conn, "COMMIT");
     res = PQexec(pg_conn, "COMMIT");
 
 
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
-	append_error( "Cannot 'COMMIT' transaction" );
+	append_error("Cannot 'COMMIT' transaction");
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
+
     PQclear(res);
     PQclear(res);
 
 
     return DB_OK;
     return DB_OK;
 }
 }
-

+ 122 - 122
db/drivers/postgres/fetch.c

@@ -4,31 +4,28 @@
 #include <string.h>
 #include <string.h>
 #include <grass/dbmi.h>
 #include <grass/dbmi.h>
 #include "globals.h"
 #include "globals.h"
-#include "proto.h" 
+#include "proto.h"
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
-int
-db__driver_fetch (dbCursor *cn, int position, int *more)
-
+int db__driver_fetch(dbCursor * cn, int position, int *more)
 {
 {
-    cursor     *c;
-    dbToken    token;    
-    dbTable    *table;
-    int        i;
+    cursor *c;
+    dbToken token;
+    dbTable *table;
+    int i;
 
 
     /* get cursor token */
     /* get cursor token */
     token = db_get_cursor_token(cn);
     token = db_get_cursor_token(cn);
 
 
     /* get the cursor by its token */
     /* get the cursor by its token */
     if (!(c = (cursor *) db_find_token(token))) {
     if (!(c = (cursor *) db_find_token(token))) {
-	append_error ("Cursor not found");
+	append_error("Cursor not found");
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     /* fetch on position */
     /* fetch on position */
-    switch (position)
-    { 
+    switch (position) {
     case DB_NEXT:
     case DB_NEXT:
 	c->row++;
 	c->row++;
 	break;
 	break;
@@ -36,7 +33,7 @@ db__driver_fetch (dbCursor *cn, int position, int *more)
 	break;
 	break;
     case DB_PREVIOUS:
     case DB_PREVIOUS:
 	c->row--;
 	c->row--;
-    	break;
+	break;
     case DB_FIRST:
     case DB_FIRST:
 	c->row = 0;
 	c->row = 0;
 	break;
 	break;
@@ -45,8 +42,8 @@ db__driver_fetch (dbCursor *cn, int position, int *more)
 	break;
 	break;
     };
     };
 
 
-    G_debug ( 3, "row = %d nrows = %d", c->row, c->nrows );
-    if ( c->row < 0 || c->row >= c->nrows ) {
+    G_debug(3, "row = %d nrows = %d", c->row, c->nrows);
+    if (c->row < 0 || c->row >= c->nrows) {
 	*more = 0;
 	*more = 0;
 	return DB_OK;
 	return DB_OK;
     }
     }
@@ -58,139 +55,142 @@ db__driver_fetch (dbCursor *cn, int position, int *more)
 
 
     for (i = 0; i < c->ncols; i++) {
     for (i = 0; i < c->ncols; i++) {
 	int col, gpgtype, sqltype;
 	int col, gpgtype, sqltype;
-	dbColumn   *column;
-	dbValue    *value;
+	dbColumn *column;
+	dbValue *value;
+
+	col = c->cols[i];	/* known cols */
 
 
-	col = c->cols[i]; /* known cols */
- 		
-	column = db_get_table_column (table, i);
-	gpgtype  = db_get_column_host_type(column);
+	column = db_get_table_column(table, i);
+	gpgtype = db_get_column_host_type(column);
 	sqltype = db_get_column_sqltype(column);
 	sqltype = db_get_column_sqltype(column);
 
 
-	value  = db_get_column_value (column);
-	db_zero_string (&value->s);
-	
+	value = db_get_column_value(column);
+	db_zero_string(&value->s);
+
 	/* Is null? */
 	/* Is null? */
-	if ( PQgetisnull(c->res, c->row, col ) ) {
+	if (PQgetisnull(c->res, c->row, col)) {
 	    value->isNull = 1;
 	    value->isNull = 1;
 	    continue;
 	    continue;
-	} else {
+	}
+	else {
 	    value->isNull = 0;
 	    value->isNull = 0;
 	}
 	}
 
 
-	G_debug (3, "row %d, col %d, gpgtype %d, sqltype %d: val = '%s'", 
-		    c->row, col, gpgtype, sqltype, PQgetvalue(c->res, c->row, col) );
-	
+	G_debug(3, "row %d, col %d, gpgtype %d, sqltype %d: val = '%s'",
+		c->row, col, gpgtype, sqltype, PQgetvalue(c->res, c->row,
+							  col));
+
 	switch (gpgtype) {
 	switch (gpgtype) {
 	    int ns, tz;
 	    int ns, tz;
-	    
-	    case PG_TYPE_CHAR:
-	    case PG_TYPE_BPCHAR:
-	    case PG_TYPE_VARCHAR:
-	    case PG_TYPE_TEXT:
-		db_set_string ( &(value->s),  PQgetvalue(c->res, c->row, col) );
-		break;
-
-	    case PG_TYPE_BIT:
-	    case PG_TYPE_INT2:
-	    case PG_TYPE_INT4:
-	    case PG_TYPE_INT8:
-	    case PG_TYPE_SERIAL:
-	    case PG_TYPE_OID:
-	    	value->i = atoi ( PQgetvalue(c->res, c->row, col) );
-		break;
-		
-	    case PG_TYPE_FLOAT4:
-	    case PG_TYPE_FLOAT8:
-	    case PG_TYPE_NUMERIC:
-	    	value->d = atof ( PQgetvalue(c->res, c->row, col) );
-		break;
-		
- 	    /* Note: we have set DATESTYLE TO ISO in db_driver_open_select_cursor() so datetime
+
+	case PG_TYPE_CHAR:
+	case PG_TYPE_BPCHAR:
+	case PG_TYPE_VARCHAR:
+	case PG_TYPE_TEXT:
+	    db_set_string(&(value->s), PQgetvalue(c->res, c->row, col));
+	    break;
+
+	case PG_TYPE_BIT:
+	case PG_TYPE_INT2:
+	case PG_TYPE_INT4:
+	case PG_TYPE_INT8:
+	case PG_TYPE_SERIAL:
+	case PG_TYPE_OID:
+	    value->i = atoi(PQgetvalue(c->res, c->row, col));
+	    break;
+
+	case PG_TYPE_FLOAT4:
+	case PG_TYPE_FLOAT8:
+	case PG_TYPE_NUMERIC:
+	    value->d = atof(PQgetvalue(c->res, c->row, col));
+	    break;
+
+	    /* Note: we have set DATESTYLE TO ISO in db_driver_open_select_cursor() so datetime
 	     *       format should be ISO */
 	     *       format should be ISO */
 
 
-	    case PG_TYPE_DATE:
-		/* Example: '1999-01-25' */
-		ns = sscanf( PQgetvalue(c->res, c->row, col), "%4d-%2d-%2d",
-			     &(value->t.year), &(value->t.month), &(value->t.day) ); 
-
-		if ( ns != 3 ) {
-		    append_error ( "Cannot scan date:");
-		    append_error ( PQgetvalue(c->res, c->row, col) );
-		    report_error();
-		    return DB_FAILED;
-		}
-		value->t.hour = 0;
-		value->t.minute = 0;
-		value->t.seconds = 0.0;
-		break;
-
-	    case PG_TYPE_TIME:
-		/* Example: '04:05:06.25', '04:05:06' */
-		ns = sscanf( PQgetvalue(c->res, c->row, col), "%2d:%2d:%lf",
-			     &(value->t.hour), &(value->t.minute), &(value->t.seconds) );
-
-		if ( ns != 3 ) {
-		    append_error ( "Cannot scan time:");
-		    append_error ( PQgetvalue(c->res, c->row, col) );
-		    report_error();
-		    return DB_FAILED;
-		}
-		value->t.year = 0;
-		value->t.month = 0;
-		value->t.day = 0;
-		break;
-
-	    case PG_TYPE_TIMESTAMP:
-		/* Example: '1999-01-25 04:05:06.25+01', '1999-01-25 04:05:06' */
-		ns = sscanf( PQgetvalue(c->res, c->row, col), "%4d-%2d-%2d %2d:%2d:%lf%3d",
-			     &(value->t.year), &(value->t.month), &(value->t.day), 
-			     &(value->t.hour), &(value->t.minute), &(value->t.seconds), &tz );
-
-		if ( ns == 7 ) {
-		    append_error ( "Cannot scan timestamp (no idea how to process time zone):");
-		    append_error ( PQgetvalue(c->res, c->row, col) );
-		    report_error();
-		    return DB_FAILED;
-		} else if ( ns < 6 ) {
-		    append_error ( "Cannot scan timestamp (not enough arguments):");
-		    append_error ( PQgetvalue(c->res, c->row, col) );
-		    report_error();
-		    return DB_FAILED;
-		}
-		break;
-
-	    case PG_TYPE_BOOL:
-		if ( strcmp ( PQgetvalue(c->res, c->row, col), "t") == 0 )
-		    db_set_string ( &(value->s), "1");
-		else if ( strcmp ( PQgetvalue(c->res, c->row, col), "f") == 0 )
-		    db_set_string ( &(value->s), "0" );
-		else
-		    G_warning (_("Cannot recognize boolean value"));
-		break;
+	case PG_TYPE_DATE:
+	    /* Example: '1999-01-25' */
+	    ns = sscanf(PQgetvalue(c->res, c->row, col), "%4d-%2d-%2d",
+			&(value->t.year), &(value->t.month), &(value->t.day));
+
+	    if (ns != 3) {
+		append_error("Cannot scan date:");
+		append_error(PQgetvalue(c->res, c->row, col));
+		report_error();
+		return DB_FAILED;
+	    }
+	    value->t.hour = 0;
+	    value->t.minute = 0;
+	    value->t.seconds = 0.0;
+	    break;
+
+	case PG_TYPE_TIME:
+	    /* Example: '04:05:06.25', '04:05:06' */
+	    ns = sscanf(PQgetvalue(c->res, c->row, col), "%2d:%2d:%lf",
+			&(value->t.hour), &(value->t.minute),
+			&(value->t.seconds));
+
+	    if (ns != 3) {
+		append_error("Cannot scan time:");
+		append_error(PQgetvalue(c->res, c->row, col));
+		report_error();
+		return DB_FAILED;
+	    }
+	    value->t.year = 0;
+	    value->t.month = 0;
+	    value->t.day = 0;
+	    break;
+
+	case PG_TYPE_TIMESTAMP:
+	    /* Example: '1999-01-25 04:05:06.25+01', '1999-01-25 04:05:06' */
+	    ns = sscanf(PQgetvalue(c->res, c->row, col),
+			"%4d-%2d-%2d %2d:%2d:%lf%3d", &(value->t.year),
+			&(value->t.month), &(value->t.day), &(value->t.hour),
+			&(value->t.minute), &(value->t.seconds), &tz);
+
+	    if (ns == 7) {
+		append_error
+		    ("Cannot scan timestamp (no idea how to process time zone):");
+		append_error(PQgetvalue(c->res, c->row, col));
+		report_error();
+		return DB_FAILED;
+	    }
+	    else if (ns < 6) {
+		append_error("Cannot scan timestamp (not enough arguments):");
+		append_error(PQgetvalue(c->res, c->row, col));
+		report_error();
+		return DB_FAILED;
+	    }
+	    break;
+
+	case PG_TYPE_BOOL:
+	    if (strcmp(PQgetvalue(c->res, c->row, col), "t") == 0)
+		db_set_string(&(value->s), "1");
+	    else if (strcmp(PQgetvalue(c->res, c->row, col), "f") == 0)
+		db_set_string(&(value->s), "0");
+	    else
+		G_warning(_("Cannot recognize boolean value"));
+	    break;
 	}
 	}
     }
     }
-    G_debug (3, "Row fetched" );
+    G_debug(3, "Row fetched");
     return DB_OK;
     return DB_OK;
 }
 }
 
 
-int
-db__driver_get_num_rows  (dbCursor *cn)
-
+int db__driver_get_num_rows(dbCursor * cn)
 {
 {
-    cursor     *c;
-    dbToken    token;
+    cursor *c;
+    dbToken token;
 
 
     /* get cursor token */
     /* get cursor token */
     token = db_get_cursor_token(cn);
     token = db_get_cursor_token(cn);
 
 
     /* get the cursor by its token */
     /* get the cursor by its token */
     if (!(c = (cursor *) db_find_token(token))) {
     if (!(c = (cursor *) db_find_token(token))) {
-       append_error("Cursor not found");
-       report_error();
-       return DB_FAILED;
+	append_error("Cursor not found");
+	report_error();
+	return DB_FAILED;
     }
     }
 
 
-    return ( c->nrows );
+    return (c->nrows);
 }
 }
-

+ 44 - 43
db/drivers/postgres/globals.h

@@ -1,64 +1,65 @@
 #include <libpq-fe.h>
 #include <libpq-fe.h>
 
 
 /* cursors */
 /* cursors */
-typedef struct _cursor {
+typedef struct _cursor
+{
     PGresult *res;
     PGresult *res;
-    int nrows;             /* number of rows in query result */
-    int row;               /* current row */
+    int nrows;			/* number of rows in query result */
+    int row;			/* current row */
     dbToken token;
     dbToken token;
-    int type;              /* type of cursor: SELECT, UPDATE, INSERT */
-    int *cols;             /* indexes of known (type) columns */ 
-    int ncols;             /* number of known columns */
-} cursor;  
+    int type;			/* type of cursor: SELECT, UPDATE, INSERT */
+    int *cols;			/* indexes of known (type) columns */
+    int ncols;			/* number of known columns */
+} cursor;
 
 
-typedef struct {
-        char *host, *port, *options, *tty, *dbname, *user, *password, *schema;
+typedef struct
+{
+    char *host, *port, *options, *tty, *dbname, *user, *password, *schema;
 } PGCONN;
 } PGCONN;
 
 
 /* PostgreSQL data types defined in GRASS
 /* PostgreSQL data types defined in GRASS
-      (see also: /usr/include/pgsql/server/catalog/pg_type.h)
+   (see also: /usr/include/pgsql/server/catalog/pg_type.h)
    PostGIS types are encoded as 17xxx.
    PostGIS types are encoded as 17xxx.
    Types/OIDs are fetched in db.c from server.
    Types/OIDs are fetched in db.c from server.
  */
  */
-typedef enum {     /* name in pg_type, aliases */
-  PG_TYPE_UNKNOWN,  /* all types not supported by GRASS */
+typedef enum
+{				/* name in pg_type, aliases */
+    PG_TYPE_UNKNOWN,		/* all types not supported by GRASS */
 
 
-  PG_TYPE_BIT,       /* bit */
-  PG_TYPE_INT2,      /* int2,	smallint */
-  PG_TYPE_INT4,      /* int4,	integer, int */
-  PG_TYPE_INT8,      /* int8,	bigint */
-  PG_TYPE_SERIAL,    /* serial */
-  PG_TYPE_OID,       /* oid */
+    PG_TYPE_BIT,		/* bit */
+    PG_TYPE_INT2,		/* int2,   smallint */
+    PG_TYPE_INT4,		/* int4,   integer, int */
+    PG_TYPE_INT8,		/* int8,   bigint */
+    PG_TYPE_SERIAL,		/* serial */
+    PG_TYPE_OID,		/* oid */
 
 
-  PG_TYPE_FLOAT4,    /* float4, real */
-  PG_TYPE_FLOAT8,    /* float8, double precision */
-  PG_TYPE_NUMERIC,   /* numeric, decimal */
-  
-  PG_TYPE_CHAR,      /* char,	character */
-  PG_TYPE_BPCHAR,    /* ??? blank padded character, oid of this type is returned for char fields */
-  PG_TYPE_VARCHAR,   /* varchar,	character varying */
-  PG_TYPE_TEXT,      /* text */
-  
-  PG_TYPE_DATE,      /* date */
-  PG_TYPE_TIME,      /* time */
-  PG_TYPE_TIMESTAMP, /* timestamp */
+    PG_TYPE_FLOAT4,		/* float4, real */
+    PG_TYPE_FLOAT8,		/* float8, double precision */
+    PG_TYPE_NUMERIC,		/* numeric, decimal */
 
 
-  PG_TYPE_BOOL,      /* bool, boolean */
+    PG_TYPE_CHAR,		/* char,   character */
+    PG_TYPE_BPCHAR,		/* ??? blank padded character, oid of this type is returned for char fields */
+    PG_TYPE_VARCHAR,		/* varchar,        character varying */
+    PG_TYPE_TEXT,		/* text */
 
 
-  PG_TYPE_POSTGIS_GEOM /* Geometry column of PostGIS, GRASS internal type */
+    PG_TYPE_DATE,		/* date */
+    PG_TYPE_TIME,		/* time */
+    PG_TYPE_TIMESTAMP,		/* timestamp */
+
+    PG_TYPE_BOOL,		/* bool, boolean */
+
+    PG_TYPE_POSTGIS_GEOM	/* Geometry column of PostGIS, GRASS internal type */
 } PG_TYPES;
 } PG_TYPES;
 
 
 
 
 #ifdef MAIN
 #ifdef MAIN
-    PGconn *pg_conn; /* Database connection */
-    int (*pg_types)[2] = NULL;   /* array of types, first is internal code, second PG_TYPE_* */
-    int pg_ntypes = 0;
-    dbString *errMsg = NULL; /* error message */
+PGconn *pg_conn;		/* Database connection */
+int (*pg_types)[2] = NULL;	/* array of types, first is internal code, second PG_TYPE_* */
+int pg_ntypes = 0;
+dbString *errMsg = NULL;	/* error message */
 #else
 #else
-    extern PGconn *pg_conn; 
-    extern dbString *errMsg;
-    extern int (*pg_types)[2];
-    extern int pg_ntypes;
-#endif 
-
-
+extern PGconn *pg_conn;
+extern dbString *errMsg;
+extern int (*pg_types)[2];
+extern int pg_ntypes;
+#endif

+ 35 - 36
db/drivers/postgres/index.c

@@ -2,58 +2,57 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_create_index ( dbIndex *index )
+int db__driver_create_index(dbIndex * index)
 {
 {
     int i, ncols;
     int i, ncols;
     PGresult *res;
     PGresult *res;
     dbString sql;
     dbString sql;
-    
-    G_debug (3, "db__create_index()");
 
 
-    db_init_string (&sql);
+    G_debug(3, "db__create_index()");
+
+    db_init_string(&sql);
     init_error();
     init_error();
 
 
-    ncols = db_get_index_number_of_columns ( index );
-
-    db_set_string ( &sql, "create" );
-    if ( db_test_index_type_unique(index) ) 
-	db_append_string ( &sql, " unique" );
-
-    db_append_string ( &sql, " index " );
-    db_append_string ( &sql, db_get_index_name(index) );
-    db_append_string ( &sql, " on " );
-    
-    db_append_string ( &sql, db_get_index_table_name(index) );
-    
-    db_append_string ( &sql, " ( " );
-
-    for ( i = 0; i < ncols; i++ ) {
-	if ( i > 0 )
-            db_append_string ( &sql, ", " );
-	
-        db_append_string ( &sql, db_get_index_column_name(index,i) );
+    ncols = db_get_index_number_of_columns(index);
+
+    db_set_string(&sql, "create");
+    if (db_test_index_type_unique(index))
+	db_append_string(&sql, " unique");
+
+    db_append_string(&sql, " index ");
+    db_append_string(&sql, db_get_index_name(index));
+    db_append_string(&sql, " on ");
+
+    db_append_string(&sql, db_get_index_table_name(index));
+
+    db_append_string(&sql, " ( ");
+
+    for (i = 0; i < ncols; i++) {
+	if (i > 0)
+	    db_append_string(&sql, ", ");
+
+	db_append_string(&sql, db_get_index_column_name(index, i));
     }
     }
-    
-    db_append_string ( &sql, " )" );
 
 
-    G_debug (3, " SQL: %s", db_get_string(&sql) );
-    
-    res = PQexec(pg_conn, db_get_string(&sql) ); 
+    db_append_string(&sql, " )");
+
+    G_debug(3, " SQL: %s", db_get_string(&sql));
+
+    res = PQexec(pg_conn, db_get_string(&sql));
 
 
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
-        append_error( "Cannot create index:\n");
-	append_error( db_get_string(&sql) );
-	append_error( "\n" );
+	append_error("Cannot create index:\n");
+	append_error(db_get_string(&sql));
+	append_error("\n");
 	append_error(PQerrorMessage(pg_conn));
 	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
-	db_free_string ( &sql);
+	db_free_string(&sql);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
+
     PQclear(res);
     PQclear(res);
-    db_free_string ( &sql);
-    
+    db_free_string(&sql);
+
     return DB_OK;
     return DB_OK;
 }
 }

+ 20 - 18
db/drivers/postgres/listdb.c

@@ -2,50 +2,53 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_list_databases (dbString *dbpath, int npaths, dbHandle **dblist, int *dbcount)
-
+int db__driver_list_databases(dbString * dbpath, int npaths,
+			      dbHandle ** dblist, int *dbcount)
 {
 {
     int i;
     int i;
     PGCONN pgconn;
     PGCONN pgconn;
     PGresult *res;
     PGresult *res;
     int rec_num = 0;
     int rec_num = 0;
     dbHandle *list;
     dbHandle *list;
-    
+
     init_error();
     init_error();
     *dblist = NULL;
     *dblist = NULL;
     *dbcount = 0;
     *dbcount = 0;
 
 
     /* TODO: the solution below is not good as user usually does not have permissions for "template1" */
     /* TODO: the solution below is not good as user usually does not have permissions for "template1" */
-    append_error( "db_driver_list_databases() is not implemented in pg driver" );
+    append_error
+	("db_driver_list_databases() is not implemented in pg driver");
     report_error();
     report_error();
     return DB_FAILED;
     return DB_FAILED;
-    
-    if ( npaths > 0 ) {
-        G_debug (3, "location: %s", db_get_string ( dbpath ) );
-	if ( parse_conn ( db_get_string(dbpath), &pgconn ) == DB_FAILED ) {
+
+    if (npaths > 0) {
+	G_debug(3, "location: %s", db_get_string(dbpath));
+	if (parse_conn(db_get_string(dbpath), &pgconn) == DB_FAILED) {
 	    report_error();
 	    report_error();
 	    return DB_FAILED;
 	    return DB_FAILED;
 	}
 	}
     }
     }
 
 
     G_debug(3, "host = %s, port = %s, options = %s, tty = %s",
     G_debug(3, "host = %s, port = %s, options = %s, tty = %s",
-                pgconn.host, pgconn.port, pgconn.options, pgconn.tty);
+	    pgconn.host, pgconn.port, pgconn.options, pgconn.tty);
 
 
-    pg_conn = PQsetdb( pgconn.host, pgconn.port, pgconn.options, pgconn.tty, "template1");
+    pg_conn =
+	PQsetdb(pgconn.host, pgconn.port, pgconn.options, pgconn.tty,
+		"template1");
 
 
     if (PQstatus(pg_conn) == CONNECTION_BAD) {
     if (PQstatus(pg_conn) == CONNECTION_BAD) {
-	append_error( "Cannot connect to Postgres:\n" );
-        append_error( PQerrorMessage(pg_conn) );
+	append_error("Cannot connect to Postgres:\n");
+	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
-	PQfinish (pg_conn);
+	PQfinish(pg_conn);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     res = PQexec(pg_conn, "select datname from pg_database");
     res = PQexec(pg_conn, "select datname from pg_database");
 
 
     if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
     if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
-	append_error( "Cannot select from Postgres:\n" );
-	append_error( PQerrorMessage(pg_conn) );
+	append_error("Cannot select from Postgres:\n");
+	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
 	PQfinish(pg_conn);
 	PQfinish(pg_conn);
@@ -56,7 +59,7 @@ int db__driver_list_databases (dbString *dbpath, int npaths, dbHandle **dblist,
 
 
     list = db_alloc_handle_array(rec_num);
     list = db_alloc_handle_array(rec_num);
     if (list == NULL) {
     if (list == NULL) {
-	append_error ( "Cannot db_alloc_handle_array()" );
+	append_error("Cannot db_alloc_handle_array()");
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
@@ -64,7 +67,7 @@ int db__driver_list_databases (dbString *dbpath, int npaths, dbHandle **dblist,
     for (i = 0; i < rec_num; i++) {
     for (i = 0; i < rec_num; i++) {
 	db_init_handle(&list[i]);
 	db_init_handle(&list[i]);
 	if (db_set_handle(&list[i], PQgetvalue(res, i, 0), NULL) != DB_OK) {
 	if (db_set_handle(&list[i], PQgetvalue(res, i, 0), NULL) != DB_OK) {
-	    append_error( "db_set_handle()" );
+	    append_error("db_set_handle()");
 	    report_error();
 	    report_error();
 	    db_free_handle_array(list, rec_num);
 	    db_free_handle_array(list, rec_num);
 	    return DB_FAILED;
 	    return DB_FAILED;
@@ -79,4 +82,3 @@ int db__driver_list_databases (dbString *dbpath, int npaths, dbHandle **dblist,
 
 
     return DB_OK;
     return DB_OK;
 }
 }
-

+ 37 - 31
db/drivers/postgres/listtab.c

@@ -4,10 +4,10 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_list_tables (dbString **tlist, int *tcount, int system)
-
+int db__driver_list_tables(dbString ** tlist, int *tcount, int system)
 {
 {
-    int i, nrows, trows, vrows, ncols, tablecol, tschemacol, viewcol, vschemacol;
+    int i, nrows, trows, vrows, ncols, tablecol, tschemacol, viewcol,
+	vschemacol;
     dbString *list;
     dbString *list;
     PGresult *rest, *resv;
     PGresult *rest, *resv;
     char buf[1000];
     char buf[1000];
@@ -18,11 +18,13 @@ int db__driver_list_tables (dbString **tlist, int *tcount, int system)
 
 
 
 
     /* Get table names */
     /* Get table names */
-    rest = PQexec(pg_conn, "select * from pg_tables where tablename !~ 'pg_*' order by tablename");
-    
+    rest =
+	PQexec(pg_conn,
+	       "select * from pg_tables where tablename !~ 'pg_*' order by tablename");
+
     if (!rest || PQresultStatus(rest) != PGRES_TUPLES_OK) {
     if (!rest || PQresultStatus(rest) != PGRES_TUPLES_OK) {
-	append_error ( "Cannot select table names\n" );
-	append_error ( PQerrorMessage(pg_conn) );
+	append_error("Cannot select table names\n");
+	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
 	PQclear(rest);
 	PQclear(rest);
 	return DB_FAILED;
 	return DB_FAILED;
@@ -32,20 +34,22 @@ int db__driver_list_tables (dbString **tlist, int *tcount, int system)
     ncols = PQnfields(rest);
     ncols = PQnfields(rest);
     tschemacol = -1;
     tschemacol = -1;
     for (i = 0; i < ncols; i++) {
     for (i = 0; i < ncols; i++) {
-	if ( strcmp(PQfname(rest, i),"tablename") == 0 )
+	if (strcmp(PQfname(rest, i), "tablename") == 0)
 	    tablecol = i;
 	    tablecol = i;
 
 
-	if ( strcmp(PQfname(rest, i),"schemaname") == 0 )
+	if (strcmp(PQfname(rest, i), "schemaname") == 0)
 	    tschemacol = i;
 	    tschemacol = i;
     }
     }
 
 
 
 
     /* Get view names */
     /* Get view names */
-    resv = PQexec(pg_conn, "SELECT * FROM pg_views WHERE schemaname NOT IN ('pg_catalog','information_schema') AND viewname !~ '^pg_'");
-    
+    resv =
+	PQexec(pg_conn,
+	       "SELECT * FROM pg_views WHERE schemaname NOT IN ('pg_catalog','information_schema') AND viewname !~ '^pg_'");
+
     if (!resv || PQresultStatus(resv) != PGRES_TUPLES_OK) {
     if (!resv || PQresultStatus(resv) != PGRES_TUPLES_OK) {
-	append_error ( "Cannot select view names\n" );
-	append_error ( PQerrorMessage(pg_conn) );
+	append_error("Cannot select view names\n");
+	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
 	PQclear(resv);
 	PQclear(resv);
 	return DB_FAILED;
 	return DB_FAILED;
@@ -55,10 +59,10 @@ int db__driver_list_tables (dbString **tlist, int *tcount, int system)
     ncols = PQnfields(resv);
     ncols = PQnfields(resv);
     vschemacol = -1;
     vschemacol = -1;
     for (i = 0; i < ncols; i++) {
     for (i = 0; i < ncols; i++) {
-	if ( strcmp(PQfname(resv, i),"viewname") == 0 )
+	if (strcmp(PQfname(resv, i), "viewname") == 0)
 	    viewcol = i;
 	    viewcol = i;
 
 
-	if ( strcmp(PQfname(resv, i),"schemaname") == 0 )
+	if (strcmp(PQfname(resv, i), "schemaname") == 0)
 	    vschemacol = i;
 	    vschemacol = i;
     }
     }
 
 
@@ -67,36 +71,38 @@ int db__driver_list_tables (dbString **tlist, int *tcount, int system)
     trows = PQntuples(rest);
     trows = PQntuples(rest);
     vrows = PQntuples(resv);
     vrows = PQntuples(resv);
     nrows = trows + vrows;
     nrows = trows + vrows;
-    
+
     list = db_alloc_string_array(nrows);
     list = db_alloc_string_array(nrows);
-    
-    if (list == NULL ) {
-	append_error ( "Cannot db_alloc_string_array()");
+
+    if (list == NULL) {
+	append_error("Cannot db_alloc_string_array()");
 	report_error();
 	report_error();
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
     for (i = 0; i < trows; i++) {
     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) );
+	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 );
+	db_set_string(&list[i], buf);
     }
     }
 
 
     PQclear(rest);
     PQclear(rest);
 
 
 
 
     for (i = 0; i < vrows; i++) {
     for (i = 0; i < vrows; i++) {
-	if ( vschemacol >= 0 ) {
-	   sprintf ( buf, "%s.%s", (char *) PQgetvalue(resv, i, vschemacol), 
-		                   (char *) PQgetvalue(resv, i, viewcol) );
-	} else {
-	   sprintf ( buf, "%s", (char *) PQgetvalue(resv, i, viewcol) );
+	if (vschemacol >= 0) {
+	    sprintf(buf, "%s.%s", (char *)PQgetvalue(resv, i, vschemacol),
+		    (char *)PQgetvalue(resv, i, viewcol));
+	}
+	else {
+	    sprintf(buf, "%s", (char *)PQgetvalue(resv, i, viewcol));
 	}
 	}
-	db_set_string(&list[i], buf );
+	db_set_string(&list[i], buf);
     }
     }
 
 
     PQclear(resv);
     PQclear(resv);

+ 2 - 2
db/drivers/postgres/main.c

@@ -6,6 +6,6 @@
 
 
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 {
-	init_dbdriver();
-	exit (db_driver (argc, argv));
+    init_dbdriver();
+    exit(db_driver(argc, argv));
 }
 }

+ 50 - 46
db/drivers/postgres/parse.c

@@ -7,18 +7,18 @@
 #include <grass/glocale.h>
 #include <grass/glocale.h>
 
 
 /*
 /*
-* \brief Parse connection string in form:
-*    1) 'database_name'
-*    2) 'host=xx,port=xx,dbname=xx'
-*  
-*  returns:  DB_OK     - OK
-*            DB_FAILED - error
-*/
-int parse_conn ( const char *str, PGCONN *pgconn )
+ * \brief Parse connection string in form:
+ *    1) 'database_name'
+ *    2) 'host=xx,port=xx,dbname=xx'
+ *  
+ *  returns:  DB_OK     - OK
+ *            DB_FAILED - error
+ */
+int parse_conn(const char *str, PGCONN * pgconn)
 {
 {
-    int  i;
+    int i;
     char **tokens, delm[2];
     char **tokens, delm[2];
-    
+
     /* reset */
     /* reset */
     pgconn->host = NULL;
     pgconn->host = NULL;
     pgconn->port = NULL;
     pgconn->port = NULL;
@@ -28,45 +28,49 @@ int parse_conn ( const char *str, PGCONN *pgconn )
     pgconn->user = NULL;
     pgconn->user = NULL;
     pgconn->password = NULL;
     pgconn->password = NULL;
     pgconn->schema = NULL;
     pgconn->schema = NULL;
- 
-    G_debug (3, "parse_conn : %s", str ); 
-    
-    if ( strchr(str, '=') == NULL ) { /*db name only */
-	pgconn->dbname = G_store ( str );
-    } else {
-	delm[0] = ','; delm[1] = '\0';
-        tokens = G_tokenize ( str, delm );
+
+    G_debug(3, "parse_conn : %s", str);
+
+    if (strchr(str, '=') == NULL) {	/*db name only */
+	pgconn->dbname = G_store(str);
+    }
+    else {
+	delm[0] = ',';
+	delm[1] = '\0';
+	tokens = G_tokenize(str, delm);
 	i = 0;
 	i = 0;
-	while ( tokens[i] ) {
-	   G_debug (3, "token %d : %s", i, tokens[i] ); 
-	   if ( strncmp(tokens[i], "host", 4 ) == 0 )
-	       pgconn->host = G_store ( tokens[i] + 5 );
-	   else if ( strncmp(tokens[i], "port", 4 ) == 0 )
-	       pgconn->port = G_store ( tokens[i] + 5 );
-	   else if ( strncmp(tokens[i], "options", 7 ) == 0 )
-	       pgconn->options = G_store ( tokens[i] + 8 );
-	   else if ( strncmp(tokens[i], "tty", 3 ) == 0 )
-	       pgconn->tty = G_store ( tokens[i] + 4 );
-	   else if ( strncmp(tokens[i], "dbname", 6 ) == 0 )
-	       pgconn->dbname = G_store ( tokens[i] + 7 );
-	   else if ( strncmp(tokens[i], "user", 4 ) == 0 )
-	       G_warning ( _("'user' in database definition is not supported, use db.login") );
-	       /* pgconn->user = G_store ( tokens[i] + 5 ); */
-	   else if ( strncmp(tokens[i], "password", 8 ) == 0 )
-	       /* pgconn->password = G_store ( tokens[i] + 9 ); */
-	       G_warning ( _("'password' in database definition is not supported, use db.login") );
-	   else if ( strncmp(tokens[i], "schema", 6 ) == 0 )
-	       pgconn->schema = G_store ( tokens[i] + 7 );
-	   else {
-	       append_error ( _("Unknown option in database definition for PostgreSQL: "));
-	       append_error ( tokens[i] );
-	       return DB_FAILED;
-	   }
-	   i++;
+	while (tokens[i]) {
+	    G_debug(3, "token %d : %s", i, tokens[i]);
+	    if (strncmp(tokens[i], "host", 4) == 0)
+		pgconn->host = G_store(tokens[i] + 5);
+	    else if (strncmp(tokens[i], "port", 4) == 0)
+		pgconn->port = G_store(tokens[i] + 5);
+	    else if (strncmp(tokens[i], "options", 7) == 0)
+		pgconn->options = G_store(tokens[i] + 8);
+	    else if (strncmp(tokens[i], "tty", 3) == 0)
+		pgconn->tty = G_store(tokens[i] + 4);
+	    else if (strncmp(tokens[i], "dbname", 6) == 0)
+		pgconn->dbname = G_store(tokens[i] + 7);
+	    else if (strncmp(tokens[i], "user", 4) == 0)
+		G_warning(_
+			  ("'user' in database definition is not supported, use db.login"));
+	    /* pgconn->user = G_store ( tokens[i] + 5 ); */
+	    else if (strncmp(tokens[i], "password", 8) == 0)
+		/* pgconn->password = G_store ( tokens[i] + 9 ); */
+		G_warning(_
+			  ("'password' in database definition is not supported, use db.login"));
+	    else if (strncmp(tokens[i], "schema", 6) == 0)
+		pgconn->schema = G_store(tokens[i] + 7);
+	    else {
+		append_error(_
+			     ("Unknown option in database definition for PostgreSQL: "));
+		append_error(tokens[i]);
+		return DB_FAILED;
+	    }
+	    i++;
 	}
 	}
-	G_free_tokens ( tokens );	
+	G_free_tokens(tokens);
     }
     }
 
 
     return DB_OK;
     return DB_OK;
 }
 }
-

+ 29 - 30
db/drivers/postgres/priv.c

@@ -2,56 +2,55 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int
-db__driver_grant_on_table (dbString *tableName, int priv, int to)
+int db__driver_grant_on_table(dbString * tableName, int priv, int to)
 {
 {
     PGresult *res;
     PGresult *res;
     dbString sql;
     dbString sql;
     dbConnection connection;
     dbConnection connection;
-    
-    G_debug (3, "db__driver_grant_on_table()");
+
+    G_debug(3, "db__driver_grant_on_table()");
 
 
     db_get_connection(&connection);
     db_get_connection(&connection);
-    db_init_string (&sql);
+    db_init_string(&sql);
     init_error();
     init_error();
 
 
-    db_set_string ( &sql, "grant " );
-    if ( priv | DB_PRIV_SELECT ) 
-	db_append_string ( &sql, "select " );
+    db_set_string(&sql, "grant ");
+    if (priv | DB_PRIV_SELECT)
+	db_append_string(&sql, "select ");
+
+    db_append_string(&sql, "on ");
+    db_append_string(&sql, db_get_string(tableName));
 
 
-    db_append_string ( &sql, "on " );
-    db_append_string ( &sql, db_get_string(tableName) );
-    
-    db_append_string ( &sql, " to " );
+    db_append_string(&sql, " to ");
 
 
-    if ( to | DB_GROUP && connection.group ) {
-        db_append_string ( &sql, "group " );
-	db_append_string ( &sql, connection.group );
+    if (to | DB_GROUP && connection.group) {
+	db_append_string(&sql, "group ");
+	db_append_string(&sql, connection.group);
 
 
-	if ( to | DB_PUBLIC ) 
-	    db_append_string ( &sql, ", " );
+	if (to | DB_PUBLIC)
+	    db_append_string(&sql, ", ");
     }
     }
-    
-    if ( to | DB_PUBLIC ) 
-        db_append_string ( &sql, "public" );
 
 
-    G_debug (3, " SQL: %s", db_get_string(&sql) );
-    
-    res = PQexec(pg_conn, db_get_string(&sql) ); 
+    if (to | DB_PUBLIC)
+	db_append_string(&sql, "public");
+
+    G_debug(3, " SQL: %s", db_get_string(&sql));
+
+    res = PQexec(pg_conn, db_get_string(&sql));
 
 
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
-        append_error( "Cannot grant on table:\n");
-	append_error( db_get_string(&sql) );
-	append_error( "\n" );
+	append_error("Cannot grant on table:\n");
+	append_error(db_get_string(&sql));
+	append_error("\n");
 	append_error(PQerrorMessage(pg_conn));
 	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
-	db_free_string ( &sql);
+	db_free_string(&sql);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
+
     PQclear(res);
     PQclear(res);
-    db_free_string ( &sql);
-    
+    db_free_string(&sql);
+
     return DB_OK;
     return DB_OK;
 }
 }

+ 10 - 10
db/drivers/postgres/proto.h

@@ -1,16 +1,16 @@
 /* error.c */
 /* error.c */
-void init_error ( void );
-void append_error ( const char * );
-void report_error ( void );
+void init_error(void);
+void append_error(const char *);
+void report_error(void);
 
 
 /* cursor.c */
 /* cursor.c */
-cursor * alloc_cursor (void);
-void free_cursor ( cursor * );
+cursor *alloc_cursor(void);
+void free_cursor(cursor *);
 
 
-/* describe.c*/
-int describe_table( PGresult *, dbTable **, cursor * );
-int get_column_info ( PGresult *, int, int *, int *, int *, int * );
-int get_gpg_type ( int );
+/* describe.c */
+int describe_table(PGresult *, dbTable **, cursor *);
+int get_column_info(PGresult *, int, int *, int *, int *, int *);
+int get_gpg_type(int);
 
 
 /* parse.c */
 /* parse.c */
-int parse_conn ( const char *, PGCONN *);
+int parse_conn(const char *, PGCONN *);

+ 17 - 18
db/drivers/postgres/select.c

@@ -4,13 +4,12 @@
 #include "globals.h"
 #include "globals.h"
 #include "proto.h"
 #include "proto.h"
 
 
-int db__driver_open_select_cursor (dbString *sel, dbCursor *dbc, int mode)
-
+int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode)
 {
 {
     PGresult *res;
     PGresult *res;
-    cursor   *c;
-    dbTable  *table;
-    char     *str;
+    cursor *c;
+    dbTable *table;
+    char *str;
 
 
     init_error();
     init_error();
 
 
@@ -18,12 +17,12 @@ int db__driver_open_select_cursor (dbString *sel, dbCursor *dbc, int mode)
     res = PQexec(pg_conn, "SET DATESTYLE TO ISO");
     res = PQexec(pg_conn, "SET DATESTYLE TO ISO");
 
 
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
-	append_error( "Cannot set DATESTYLE\n" );
+	append_error("Cannot set DATESTYLE\n");
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
-    
+
     PQclear(res);
     PQclear(res);
 
 
     /* allocate cursor */
     /* allocate cursor */
@@ -35,27 +34,27 @@ int db__driver_open_select_cursor (dbString *sel, dbCursor *dbc, int mode)
     db_set_cursor_type_readonly(dbc);
     db_set_cursor_type_readonly(dbc);
 
 
     /* \ must be escaped, see explanation in db_driver_execute_immediate() */
     /* \ must be escaped, see explanation in db_driver_execute_immediate() */
-    str = G_str_replace ( db_get_string(sel), "\\", "\\\\" );
-    G_debug ( 3, "Escaped SQL: %s", str );
+    str = G_str_replace(db_get_string(sel), "\\", "\\\\");
+    G_debug(3, "Escaped SQL: %s", str);
+
+    c->res = PQexec(pg_conn, str);
 
 
-    c->res = PQexec(pg_conn, str );
-    
     if (!c->res || PQresultStatus(c->res) != PGRES_TUPLES_OK) {
     if (!c->res || PQresultStatus(c->res) != PGRES_TUPLES_OK) {
 	append_error("Cannot select: \n");
 	append_error("Cannot select: \n");
-	append_error(db_get_string(sel) );
-	append_error( "\n" );
+	append_error(db_get_string(sel));
+	append_error("\n");
 	append_error(PQerrorMessage(pg_conn));
 	append_error(PQerrorMessage(pg_conn));
 	report_error();
 	report_error();
 	PQclear(c->res);
 	PQclear(c->res);
-        if ( str )
-            G_free ( str );
+	if (str)
+	    G_free(str);
 	return DB_FAILED;
 	return DB_FAILED;
     }
     }
 
 
-    if ( str )
-	G_free ( str );
+    if (str)
+	G_free(str);
 
 
-    if ( describe_table( c->res, &table, c) == DB_FAILED ) {
+    if (describe_table(c->res, &table, c) == DB_FAILED) {
 	append_error("Cannot describe table\n");
 	append_error("Cannot describe table\n");
 	report_error();
 	report_error();
 	PQclear(res);
 	PQclear(res);

+ 90 - 91
db/drivers/sqlite/create_table.c

@@ -1,3 +1,4 @@
+
 /**
 /**
  * \file create_table.c
  * \file create_table.c
  *
  *
@@ -25,117 +26,115 @@
  * \return int DB_FAILED on error; DB_OK on success
  * \return int DB_FAILED on error; DB_OK on success
  */
  */
 
 
-int
-db__driver_create_table (dbTable *table)
+int db__driver_create_table(dbTable * table)
 {
 {
     int col, ncols;
     int col, ncols;
     sqlite3_stmt *statement;
     sqlite3_stmt *statement;
     dbString sql;
     dbString sql;
     const char *rest;
     const char *rest;
-    int   ret;
-    
-    G_debug (3, "db__driver_create_table()");
-    
+    int ret;
+
+    G_debug(3, "db__driver_create_table()");
+
     init_error();
     init_error();
 
 
-    db_init_string (&sql);
+    db_init_string(&sql);
 
 
     /* db_table_to_sql ( table, &sql ); */
     /* db_table_to_sql ( table, &sql ); */
 
 
-    db_set_string ( &sql, "create table ");
-    db_append_string ( &sql, db_get_table_name ( table ) );
-    db_append_string ( &sql, " ( ");
+    db_set_string(&sql, "create table ");
+    db_append_string(&sql, db_get_table_name(table));
+    db_append_string(&sql, " ( ");
 
 
     ncols = db_get_table_number_of_columns(table);
     ncols = db_get_table_number_of_columns(table);
 
 
-    for ( col = 0; col < ncols; col++ ) {
-        dbColumn *column = db_get_table_column (table, col);
-	const char *colname = db_get_column_name (column);
-	int sqltype = db_get_column_sqltype (column);
-	int collen = db_get_column_length (column);
+    for (col = 0; col < ncols; col++) {
+	dbColumn *column = db_get_table_column(table, col);
+	const char *colname = db_get_column_name(column);
+	int sqltype = db_get_column_sqltype(column);
+	int collen = db_get_column_length(column);
 	char buf[32];
 	char buf[32];
 
 
-	G_debug ( 3, "%s (%s)", colname, db_sqltype_name(sqltype) );
-
-	if ( col > 0 ) db_append_string ( &sql, ", " );
-	db_append_string ( &sql, colname );
-	db_append_string ( &sql, " " );
-	switch ( sqltype ) {
-	    case DB_SQL_TYPE_CHARACTER:
-		sprintf(buf, "varchar(%d)", collen);
-		db_append_string ( &sql, buf);
-                break;
-	    case DB_SQL_TYPE_SMALLINT:
-		db_append_string ( &sql, "smallint");
-		break;
-	    case DB_SQL_TYPE_INTEGER:
-		db_append_string ( &sql, "integer");
-		break;
-	    case DB_SQL_TYPE_REAL:
-		db_append_string ( &sql, "real");
-		break;
-	    case DB_SQL_TYPE_DOUBLE_PRECISION:
-		db_append_string ( &sql, "double precision");
-		break;
-	    case DB_SQL_TYPE_DECIMAL:
-		db_append_string ( &sql, "decimal");
-		break;
-	    case DB_SQL_TYPE_NUMERIC:
-		db_append_string ( &sql, "numeric");
-		break;
-	    case DB_SQL_TYPE_DATE:
-		db_append_string ( &sql, "date");
-		break;
-	    case DB_SQL_TYPE_TIME:
-		db_append_string ( &sql, "time");
-		break;
-	    case DB_SQL_TYPE_TIMESTAMP:
-		db_append_string ( &sql, "timestamp");
-		break;
-	    case DB_SQL_TYPE_INTERVAL:
-		db_append_string ( &sql, "interval");
-		break;
-	    case DB_SQL_TYPE_TEXT:
-		db_append_string ( &sql, "text");
-                break;
-	    case DB_SQL_TYPE_SERIAL:
-		db_append_string ( &sql, "serial");
-                break;
- 	    default:
-                G_warning ( "Unknown column type (%s)", colname);
-		return DB_FAILED;
+	G_debug(3, "%s (%s)", colname, db_sqltype_name(sqltype));
+
+	if (col > 0)
+	    db_append_string(&sql, ", ");
+	db_append_string(&sql, colname);
+	db_append_string(&sql, " ");
+	switch (sqltype) {
+	case DB_SQL_TYPE_CHARACTER:
+	    sprintf(buf, "varchar(%d)", collen);
+	    db_append_string(&sql, buf);
+	    break;
+	case DB_SQL_TYPE_SMALLINT:
+	    db_append_string(&sql, "smallint");
+	    break;
+	case DB_SQL_TYPE_INTEGER:
+	    db_append_string(&sql, "integer");
+	    break;
+	case DB_SQL_TYPE_REAL:
+	    db_append_string(&sql, "real");
+	    break;
+	case DB_SQL_TYPE_DOUBLE_PRECISION:
+	    db_append_string(&sql, "double precision");
+	    break;
+	case DB_SQL_TYPE_DECIMAL:
+	    db_append_string(&sql, "decimal");
+	    break;
+	case DB_SQL_TYPE_NUMERIC:
+	    db_append_string(&sql, "numeric");
+	    break;
+	case DB_SQL_TYPE_DATE:
+	    db_append_string(&sql, "date");
+	    break;
+	case DB_SQL_TYPE_TIME:
+	    db_append_string(&sql, "time");
+	    break;
+	case DB_SQL_TYPE_TIMESTAMP:
+	    db_append_string(&sql, "timestamp");
+	    break;
+	case DB_SQL_TYPE_INTERVAL:
+	    db_append_string(&sql, "interval");
+	    break;
+	case DB_SQL_TYPE_TEXT:
+	    db_append_string(&sql, "text");
+	    break;
+	case DB_SQL_TYPE_SERIAL:
+	    db_append_string(&sql, "serial");
+	    break;
+	default:
+	    G_warning("Unknown column type (%s)", colname);
+	    return DB_FAILED;
 	}
 	}
     }
     }
-    db_append_string ( &sql, " )" );
-
-    G_debug (3, " SQL: %s", db_get_string(&sql) );
-    
-    ret = sqlite3_prepare ( sqlite, db_get_string(&sql), -1,
-                            &statement, &rest );
-
-    if ( ret != SQLITE_OK ) {
-        append_error( "Cannot create table:\n");
-        append_error( db_get_string(&sql) );
-        append_error( "\n" );
-        append_error ((char *) sqlite3_errmsg (sqlite));
-        report_error();
-        sqlite3_finalize ( statement );
-        db_free_string ( &sql);
-        return DB_FAILED;
+    db_append_string(&sql, " )");
+
+    G_debug(3, " SQL: %s", db_get_string(&sql));
+
+    ret = sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement, &rest);
+
+    if (ret != SQLITE_OK) {
+	append_error("Cannot create table:\n");
+	append_error(db_get_string(&sql));
+	append_error("\n");
+	append_error((char *)sqlite3_errmsg(sqlite));
+	report_error();
+	sqlite3_finalize(statement);
+	db_free_string(&sql);
+	return DB_FAILED;
     }
     }
 
 
-    ret = sqlite3_step ( statement );
+    ret = sqlite3_step(statement);
 
 
-    if ( ret != SQLITE_DONE )
-    {
-        append_error("Error in sqlite3_step():\n");
-        append_error ((char *) sqlite3_errmsg (sqlite));
-        report_error( );
-        return DB_FAILED;
+    if (ret != SQLITE_DONE) {
+	append_error("Error in sqlite3_step():\n");
+	append_error((char *)sqlite3_errmsg(sqlite));
+	report_error();
+	return DB_FAILED;
     }
     }
-        
-    sqlite3_finalize ( statement );
-    db_free_string ( &sql);
-    
+
+    sqlite3_finalize(statement);
+    db_free_string(&sql);
+
     return DB_OK;
     return DB_OK;
 }
 }

+ 0 - 0
db/drivers/sqlite/cursor.c


Деякі файли не було показано, через те що забагато файлів було змінено