[GRASS-SVN] r64278 - grass/trunk/lib/vector/Vlib

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jan 22 00:04:23 PST 2015


Author: mmetz
Date: 2015-01-22 00:04:23 -0800 (Thu, 22 Jan 2015)
New Revision: 64278

Modified:
   grass/trunk/lib/vector/Vlib/copy.c
   grass/trunk/lib/vector/Vlib/map.c
Log:
Vlib: avoid code duplication

Modified: grass/trunk/lib/vector/Vlib/copy.c
===================================================================
--- grass/trunk/lib/vector/Vlib/copy.c	2015-01-22 07:47:28 UTC (rev 64277)
+++ grass/trunk/lib/vector/Vlib/copy.c	2015-01-22 08:04:23 UTC (rev 64278)
@@ -577,16 +577,15 @@
 int Vect_copy_tables(const struct Map_info *In, struct Map_info *Out,
                      int field)
 {
-    int i, n, ret, type;
-    struct field_info *Fi, *Fin;
-    dbDriver *driver;
+    int i, n, type;
+    struct field_info *Fi;
 
     n = Vect_get_num_dblinks(In);
 
     G_debug(2, "Vect_copy_tables(): copying %d tables", n);
 
     type = GV_1TABLE;
-    if (n > 1)
+    if (field < 1 && n > 1)
         type = GV_MTABLE;
 
     for (i = 0; i < n; i++) {
@@ -599,42 +598,13 @@
         if (field > 0 && Fi->number != field)
             continue;
 
-        Fin = Vect_default_field_info(Out, Fi->number, Fi->name, type);
-        G_debug(2, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
-                Fi->driver, Fi->database, Fi->table, Fin->driver,
-                Fin->database, Fin->table);
+	if (Vect_copy_table(In, Out, Fi->number, Fi->number, Fi->name,
+	                    type) != 0) {
 
-        ret =
-            Vect_map_add_dblink(Out, Fi->number, Fi->name, Fin->table,
-                                Fi->key, Fin->database, Fin->driver);
-        if (ret == -1) {
-            G_warning(_("Unable to add database link for vector map <%s>"),
-                      Out->name);
-            return -1;
-        }
-
-        ret = db_copy_table(Fi->driver, Fi->database, Fi->table,
-                            Fin->driver, Vect_subst_var(Fin->database, Out),
-                            Fin->table);
-        if (ret == DB_FAILED) {
-            G_warning(_("Unable to copy table <%s>"), Fin->table);
-            return -1;
-        }
-
-        driver =
-            db_start_driver_open_database(Fin->driver,
-                                          Vect_subst_var(Fin->database, Out));
-        if (driver == NULL) {
-            G_warning(_("Unable to open database <%s> by driver <%s>"),
-                      Fin->database, Fin->driver);
-        }
-        else {
-            if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK)
-                G_warning(_("Unable to create index for table <%s>, key <%s>"),
-                          Fin->table, Fin->key);
-
-            db_close_database_shutdown_driver(driver);
-        }
+            G_warning(_("Unable to copy table <%s> for layer %d from <%s> to <%s>"),
+	                Fi->table, Fi->number, Vect_get_full_name(In), Vect_get_name(Out));
+	    return -1;
+	}
     }
 
     return 0;
@@ -724,8 +694,9 @@
     int ret;
     struct field_info *Fi, *Fin;
     const char *name, *key;
+    dbDriver *driver;
 
-    G_debug(2, "Vect_copy_table(): field_in = %d field_out = %d", field_in,
+    G_debug(2, "Vect_copy_table_by_cats(): field_in = %d field_out = %d", field_in,
             field_out);
 
     Fi = Vect_get_field(In, field_in);
@@ -768,5 +739,30 @@
         return -1;
     }
 
+    driver = db_start_driver_open_database(Fin->driver,
+				           Vect_subst_var(Fin->database,
+                                                            Out));
+
+    if (!driver) {
+	G_warning(_("Unable to open database <%s> with driver <%s>"),
+		      Fin->database, Fin->driver);
+	return -1;
+    }
+
+    /* do not allow duplicate keys */
+    if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK) {
+	G_warning(_("Unable to create index"));
+	return -1;
+    }
+
+    if (db_grant_on_table(driver, Fin->table, DB_PRIV_SELECT,
+	 DB_GROUP | DB_PUBLIC) != DB_OK) {
+	G_warning(_("Unable to grant privileges on table <%s>"),
+		      Fin->table);
+	return -1;
+    }
+
+    db_close_database_shutdown_driver(driver);
+
     return 0;
 }

Modified: grass/trunk/lib/vector/Vlib/map.c
===================================================================
--- grass/trunk/lib/vector/Vlib/map.c	2015-01-22 07:47:28 UTC (rev 64277)
+++ grass/trunk/lib/vector/Vlib/map.c	2015-01-22 08:04:23 UTC (rev 64278)
@@ -86,9 +86,8 @@
  */
 int Vect_copy(const char *in, const char *mapset, const char *out)
 {
-    int i, n, ret, type;
+    int i, ret;
     struct Map_info In, Out;
-    struct field_info *Fi, *Fin;
     char old_path[GPATH_MAX], new_path[GPATH_MAX], buf[GPATH_MAX];
     const char *files[] = { GV_FRMT_ELEMENT, GV_COOR_ELEMENT,
         GV_HEAD_ELEMENT, GV_HIST_ELEMENT,
@@ -98,8 +97,6 @@
     const char *inmapset;
     char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
 
-    dbDriver *driver;
-
     G_debug(2, "Copy vector '%s' in '%s' to '%s'", in, mapset, out);
     /* check for [A-Za-z][A-Za-z0-9_]* in name */
     if (Vect_legal_filename(out) < 0)
@@ -169,52 +166,11 @@
 	G_fatal_error(_("Unable to open vector map <%s>"), out);
 
     /* Copy tables */
-    n = Vect_get_num_dblinks(&In);
-    type = GV_1TABLE;
-    if (n > 1)
-        type = GV_MTABLE;
-    for (i = 0; i < n; i++) {
-        Fi = Vect_get_dblink(&In, i);
-        if (Fi == NULL) {
-            G_warning(_("Database connection not defined for layer %d"),
-                      In.dblnk->field[i].number);
-            Vect_close(&In);
-            Vect_close(&Out);
-            return -1;
-        }
-        Fin = Vect_default_field_info(&Out, Fi->number, Fi->name, type);
-        G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
-                Fi->driver, Fi->database, Fi->table, Fin->driver,
-                Fin->database, Fin->table);
+    if (Vect_copy_tables(&In, &Out, 0) != 0) {
+	Vect_close(&In);
+	Vect_close(&Out);
 
-        Vect_map_add_dblink(&Out, Fi->number, Fi->name, Fin->table, Fi->key,
-                            Fin->database, Fin->driver);
-
-        ret = db_copy_table(Fi->driver, Fi->database, Fi->table,
-                            Fin->driver, Vect_subst_var(Fin->database, &Out),
-                            Fin->table);
-        if (ret == DB_FAILED) {
-            G_warning(_("Unable to copy table <%s>"), Fin->table);
-            Vect_close(&In);
-            Vect_close(&Out);
-            return -1;
-        }
-
-        driver =
-            db_start_driver_open_database(Fin->driver,
-                                          Vect_subst_var(Fin->database,
-                                                         &Out));
-        if (driver == NULL) {
-            G_warning(_("Unable to open database <%s> by driver <%s>"),
-                      Fin->database, Fin->driver);
-        }
-        else {
-            if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK)
-                G_warning(_("Unable to create index for table <%s>, key <%s>"),
-                          Fi->table, Fi->key);
-
-            db_close_database_shutdown_driver(driver);
-        }
+	return 1;
     }
 
     Vect_close(&In);



More information about the grass-commit mailing list