[GRASS-SVN] r30545 - in grass/trunk: db/base include
lib/db/dbmi_base lib/vector/Vlib
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Mar 13 03:02:47 EDT 2008
Author: hamish
Date: 2008-03-13 03:02:47 -0400 (Thu, 13 Mar 2008)
New Revision: 30545
Modified:
grass/trunk/db/base/connect.c
grass/trunk/db/base/db.connect.html
grass/trunk/include/dbmi.h
grass/trunk/include/proto_dbmi.h
grass/trunk/lib/db/dbmi_base/default_name.c
grass/trunk/lib/vector/Vlib/field.c
Log:
abstract default DB settings: add DB_DEFAULT_DRIVER, db_set_default_connection(), and 'db.connect -c'. (trac bug #7)
Modified: grass/trunk/db/base/connect.c
===================================================================
--- grass/trunk/db/base/connect.c 2008-03-13 06:39:39 UTC (rev 30544)
+++ grass/trunk/db/base/connect.c 2008-03-13 07:02:47 UTC (rev 30545)
@@ -3,9 +3,11 @@
* MODULE: db.connect
* AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
* Alex Shevlakov <sixote yahoo.com>,
- * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
+ * Glynn Clements <glynn gclements.plus.com>,
+ * Markus Neteler <neteler itc.it>,
+ * Hamish Bowman <hamish_b yahoo com>
* PURPOSE: set parameters for connection to database
- * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
+ * COPYRIGHT: (C) 2002-2008 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
@@ -21,18 +23,19 @@
#include <grass/glocale.h>
/* database for DBF can be written with variables:
- database=$GISDBASE/$LOCATION_NAME/$MAPSET/dbf
+ * database=$GISDBASE/$LOCATION_NAME/$MAPSET/dbf
*/
int
main(int argc, char *argv[])
{
dbConnection conn;
- struct Flag *print;
+ struct Flag *print, *check_set_default;
/* struct Option *driver, *database, *user, *password, *keycol;*/
struct Option *driver, *database, *schema, *group;
struct GModule *module;
+
/* Initialize the GIS calls */
G_gisinit(argv[0]) ;
@@ -46,7 +49,12 @@
print = G_define_flag();
print->key = 'p';
print->description = _("Print current connection parameters and exit");
-
+
+ check_set_default = G_define_flag();
+ check_set_default->key = 'c';
+ check_set_default->description =
+ _("Check connection parameters, set if uninitialized, and exit");
+
driver = G_define_standard_option(G_OPT_DRIVER);
driver->options = db_list_drivers();
driver->answer = db_get_default_driver_name();
@@ -92,29 +100,10 @@
if(G_parser(argc, argv))
exit(EXIT_FAILURE);
- /* set connection*/
- if( !print->answer)
- {
- db_get_connection( &conn ); /* read current */
-
- if ( driver->answer )
- conn.driverName = driver->answer;
-
- if ( database->answer )
- conn.databaseName = database->answer;
- if ( schema->answer )
- conn.schemaName = schema->answer;
-
- if ( group->answer )
- conn.group = group->answer;
-
- db_set_connection( &conn );
-
- }
- else{
+ if(print->answer) {
/* get and print connection */
- if (db_get_connection( &conn ) == DB_OK){
+ 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 : "");
@@ -123,8 +112,59 @@
else
G_fatal_error(_("Database connection not defined. "
"Run db.connect."));
+
+ exit(EXIT_SUCCESS);
}
+
+ if(check_set_default->answer) {
+ /* check connection and set to system-wide default in required */
+/*
+ * TODO: improve db_{get,set}_connection() to not return DB_OK on error
+ * (thus currently there is no point in checking for that here)
+ */
+ db_get_connection(&conn);
+
+ if ( !conn.driverName && !conn.databaseName ) {
+
+ db_set_default_connection();
+ db_get_connection(&conn);
+
+ G_message(_("Default driver / database set to:\n"
+ "driver: %s\ndatabase: %s"), conn.driverName, conn.databaseName);
+ }
+ /* they must be a matched pair, so if one is set but not the other
+ then give up and let the user figure it out */
+ else if ( !conn.driverName ) {
+ G_fatal_error( _("Default driver is not set") );
+ }
+ else if ( !conn.databaseName ) {
+ G_fatal_error( _("Default database is not set") );
+ }
+
+ /* connection either already existed or now exists */
+ exit(EXIT_SUCCESS);
+ }
+
+
+ /* set connection*/
+ db_get_connection( &conn ); /* read current */
+
+ if ( driver->answer )
+ conn.driverName = driver->answer;
+
+ if ( database->answer )
+ conn.databaseName = database->answer;
+
+ if ( schema->answer )
+ conn.schemaName = schema->answer;
+
+ if ( group->answer )
+ conn.group = group->answer;
+
+ db_set_connection( &conn );
+
+
exit(EXIT_SUCCESS);
}
Modified: grass/trunk/db/base/db.connect.html
===================================================================
--- grass/trunk/db/base/db.connect.html 2008-03-13 06:39:39 UTC (rev 30544)
+++ grass/trunk/db/base/db.connect.html 2008-03-13 07:02:47 UTC (rev 30545)
@@ -1,30 +1,42 @@
<h2>DESCRIPTION</h2>
-<em>db.connect</em> allows the user to set parameters for
-connection to database. These parameters are then taken by
-modules as default values and user do not need enter
-parameters each time. Values are stored in user file;
-the connection is not tested for validity.
+<em>db.connect</em> allows the user to set database connection parameters.
+These parameters are then taken as default values by modules so that the
+user does not need to enter the parameters each time.
-<h2>OPTIONS</h2>
+<H2>NOTES</H2>
-Typing <em>db.connect -p</em> displays current connection parameters.
+Values are stored in the mapset's <tt>VAR</tt> file;
+the connection is not tested for validity.
+<P>
+The <b>-p</b> flag will display the current connection parameters.
+<P>
+The <b>-c</b> flag will silently check if the connection parameters have
+been set, and if not will set them to use GRASS's default values.
+(useful in scripts before you attempt to create a new database table)
+<P>
+To connect a vector map to a database table, use <em>v.db.connect</em>
+or <em>v.db.addtable</em>.
<h2>EXAMPLES</h2>
<h3>DBF</h3>
-Local storage (the dbf/ subdirectory in the mapset must exist or must be created by the user):<br>
+Local storage (the dbf/ subdirectory in the mapset must exist or must be
+created by the user):
+<br>
<div class="code"><pre>
db.connect driver=dbf database='$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/'
db.tables -p
</pre></div>
+
<h3>SQLite</h3>
-Local storage:<br>
+Local storage:
+<br>
<div class="code"><pre>
db.connect driver=sqlite database='$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite.db'
db.connect -p
@@ -33,9 +45,11 @@
<P>
The SQLite database is created automatically when used the first time.
+
<h3>ODBC</h3>
-Network storage: (may require the use of <a href=db.login.html>db.login</a>):<br>
+Network storage: (may require the use of <a href=db.login.html>db.login</a>):
+<br>
<div class="code"><pre>
db.connect driver=odbc database=gtest
db.login user=myname [pass=secret]
@@ -43,9 +57,11 @@
db.tables -p
</pre></div>
+
<h3>PostgreSQL</h3>
-Network storage: (may require the use of <a href=db.login.html>db.login</a>):<br>
+Network storage: (may require the use of <a href=db.login.html>db.login</a>):
+<br>
<div class="code"><pre>
db.connect driver=pg database="host=myserver.itc.it,dbname=mydb"
db.login user=myname [pass=secret]
@@ -53,9 +69,11 @@
db.tables -p
</pre></div>
+
<h3>PostgreSQL with different port</h3>
-Network storage: (may require the use of <a href=db.login.html>db.login</a>):<br>
+Network storage: (may require the use of <a href=db.login.html>db.login</a>):
+<br>
<div class="code"><pre>
db.connect driver=pg database="host=myserver.itc.it,dbname=mydb,port=6666"
db.login user=myname [pass=secret]
@@ -63,9 +81,11 @@
db.tables -p
</pre></div>
+
<h3>MySQL (local)</h3>
-Local storage (<a href=db.login.html>db.login</a> may not be needed):<br>
+Local storage (<a href=db.login.html>db.login</a> may not be needed):
+<br>
<div class="code"><pre>
db.connect driver=mysql database=mydb
db.login user=myname [pass=secret]
@@ -73,9 +93,11 @@
db.tables -p
</pre></div>
+
<h3>MySQL (external server)</h3>
-Network storage: (may require the use of <a href=db.login.html>db.login</a>):<br>
+Network storage: (may require the use of <a href=db.login.html>db.login</a>):
+<br>
<div class="code"><pre>
db.connect driver=mysql database="host=myserver.itc.it,dbname=mydb"
db.login user=myname [pass=secret]
@@ -83,10 +105,7 @@
db.tables -p
</pre></div>
-<h2>NOTES</h2>
-To connect a vector map to a database table, use <em>v.db.connect</em> or <em>v.db.addtable</em>.
-
<h2>SEE ALSO</h2>
<em>
@@ -98,8 +117,10 @@
<a HREF="v.db.connect.html">v.db.connect</a>,
<a HREF="sql.html">GRASS SQL interface</a></em>
+
<h2>AUTHOR</h2>
Radim Blazek, ITC-Irst, Trento, Italy
-<p><i>Last changed: $Date$</i>
+<p>
+<i>Last changed: $Date$</i>
Modified: grass/trunk/include/dbmi.h
===================================================================
--- grass/trunk/include/dbmi.h 2008-03-13 06:39:39 UTC (rev 30544)
+++ grass/trunk/include/dbmi.h 2008-03-13 07:02:47 UTC (rev 30545)
@@ -6,6 +6,8 @@
#define DB_VERSION "0"
+#define DB_DEFAULT_DRIVER "dbf"
+
/* DB Prodedure Numbers */
#define DB_PROC_VERSION 999
Modified: grass/trunk/include/proto_dbmi.h
===================================================================
--- grass/trunk/include/proto_dbmi.h 2008-03-13 06:39:39 UTC (rev 30544)
+++ grass/trunk/include/proto_dbmi.h 2008-03-13 07:02:47 UTC (rev 30545)
@@ -292,6 +292,7 @@
void db_set_cursor_type_insert (dbCursor *cursor );
void db_set_cursor_type_readonly (dbCursor *cursor );
void db_set_cursor_type_update (dbCursor *cursor );
+int db_set_default_connection (void);
void db_set_error_who (char *me );
int db_set_handle (dbHandle *handle , char *dbName , char *dbPath );
int db_set_index_column_name (dbIndex *index , int column_num , char *name );
Modified: grass/trunk/lib/db/dbmi_base/default_name.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/default_name.c 2008-03-13 06:39:39 UTC (rev 30544)
+++ grass/trunk/lib/db/dbmi_base/default_name.c 2008-03-13 07:02:47 UTC (rev 30545)
@@ -1,4 +1,5 @@
#include <stdlib.h>
+#include <string.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
@@ -66,3 +67,44 @@
return NULL;
}
+
+
+/*!
+ \fn char * db_set_default_connection(void)
+ \brief sets up database connection settings using GRASS default from dbmi.h
+ \return returns DB_OK (TODO: DB_OK on success, DB_* error code on fail)
+*/
+int db_set_default_connection(void)
+{
+ dbConnection connection;
+ char buf[GPATH_MAX];
+
+ /* is this really needed ? */
+ db_get_connection(&connection);
+
+ if(strcmp(DB_DEFAULT_DRIVER, "dbf") == 0 ) {
+ /* Set default values and create dbf db dir */
+
+ connection.driverName = "dbf";
+ connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/";
+ db_set_connection( &connection );
+
+ sprintf ( buf, "%s/%s/dbf", G_location_path(), G_mapset());
+ G__make_mapset_element ( "dbf" );
+ }
+ else if (strcmp(DB_DEFAULT_DRIVER, "sqlite") == 0 ) {
+ /* Set default values and create dbf db dir */
+
+ connection.driverName = "sqlite";
+/*
+ * TODO: Use one DB for entire mapset (LFS problems?)
+ * or per-map DBs in $MASPET/vector/mapname/sqlite.db (how to set that here?)
+ * or $MAPSET/sqlite/mapname.sql as with dbf?
+ */
+ connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite.db";
+ db_set_connection( &connection );
+ }
+ else G_fatal_error("Programmer error");
+
+ return DB_OK;
+}
Modified: grass/trunk/lib/vector/Vlib/field.c
===================================================================
--- grass/trunk/lib/vector/Vlib/field.c 2008-03-13 06:39:39 UTC (rev 30544)
+++ grass/trunk/lib/vector/Vlib/field.c 2008-03-13 07:02:47 UTC (rev 30545)
@@ -281,21 +281,24 @@
G_debug (2, "drv = %s db = %s", drv, db );
- if ( !connection.driverName && !connection.databaseName ) { /* Set default values and create dbf db dir */
- G_warning ( _("Default driver / database set to:\n"
- "driver: dbf\ndatabase: $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/") );
-
- connection.driverName = "dbf";
- connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/";
- db_set_connection( &connection );
-
- sprintf ( buf, "%s/%s/%s/dbf", Map->gisdbase, Map->location, Map->mapset );
- G__make_mapset_element ( "dbf" );
- } else if ( !connection.driverName ) {
+
+ if ( !connection.driverName && !connection.databaseName ) {
+ /* Set default values and create dbf db dir */
+ db_set_default_connection();
+ db_get_connection(&connection);
+
+ G_warning(_("Default driver / database set to:\n"
+ "driver: %s\ndatabase: %s"), connection.driverName,
+ connection.databaseName);
+ }
+ /* they must be a matched pair, so if one is set but not the other
+ then give up and let the user figure it out */
+ else if ( !connection.driverName ) {
G_fatal_error ( _("Default driver is not set") );
} else if ( !connection.databaseName ) {
G_fatal_error ( _("Default database is not set") );
}
+
drv = connection.driverName;
db = connection.databaseName;
More information about the grass-commit
mailing list