[GRASS-SVN] r43479 - in grass/trunk: include lib/db/dbmi_base lib/db/dbmi_base/test

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Sep 16 09:00:46 EDT 2010


Author: huhabla
Date: 2010-09-16 13:00:46 +0000 (Thu, 16 Sep 2010)
New Revision: 43479

Modified:
   grass/trunk/include/proto_dbmi.h
   grass/trunk/lib/db/dbmi_base/table.c
   grass/trunk/lib/db/dbmi_base/test/test_table.c
Log:
New table cloumn access method and tests implemented.
New Prototypes added to header file.


Modified: grass/trunk/include/proto_dbmi.h
===================================================================
--- grass/trunk/include/proto_dbmi.h	2010-09-16 10:13:16 UTC (rev 43478)
+++ grass/trunk/include/proto_dbmi.h	2010-09-16 13:00:46 UTC (rev 43479)
@@ -177,7 +177,8 @@
 const char *db_get_index_table_name(dbIndex * index);
 int db_get_num_rows(dbCursor * cursor);
 char *db_get_string(const dbString * x);
-dbColumn *db_get_table_column(dbTable * table, int n);
+dbColumn *db_get_table_column(dbTable *, int);
+dbColumn *db_get_table_column_by_name(dbTable *, const char*);
 int db_get_table_delete_priv(dbTable * table);
 const char *db_get_table_description(dbTable * table);
 int db_get_table_insert_priv(dbTable * table);
@@ -334,6 +335,7 @@
 void db__set_protocol_fds(FILE * send, FILE * recv);
 int db_set_string(dbString * x, const char *s);
 int db_set_string_no_copy(dbString * x, char *s);
+int db_set_table_column(dbTable *, int, dbColumn *);
 void db_set_table_delete_priv_granted(dbTable * table);
 void db_set_table_delete_priv_not_granted(dbTable * table);
 int db_set_table_description(dbTable * table, const char *description);

Modified: grass/trunk/lib/db/dbmi_base/table.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/table.c	2010-09-16 10:13:16 UTC (rev 43478)
+++ grass/trunk/lib/db/dbmi_base/table.c	2010-09-16 13:00:46 UTC (rev 43479)
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <string.h>
 #include <grass/gis.h>
 #include <grass/dbmi.h>
 
@@ -272,7 +273,7 @@
   \param idx     column index (starting with '0')
 
   \return pointer to dbColumn
-  \return NULL on error
+  \return NULL if not found
 */
 dbColumn *db_get_table_column(dbTable * table, int idx)
 {
@@ -282,6 +283,35 @@
 }
 
 /*!
+  \brief Returns column structure for given table and column name
+
+  \param table pointer to dbTable
+  \param name the name of the column
+
+  \return pointer to dbColumn
+  \return NULL if not found
+*/
+dbColumn *db_get_table_column_by_name(dbTable * table, const char* name)
+{
+    dbColumn *c = NULL;
+    int i, columns = table->numColumns;
+
+    for(i = 0; i < columns; i++ ) {
+        c = db_get_table_column(table, i);
+
+        if(c == NULL)
+            return c;
+
+        if(strcmp(name, db_get_string(&c->columnName)) == 0)
+            break;
+
+        c = NULL;
+    }
+
+    return c;
+}
+
+/*!
  * \brief Set a specific column for given table and column number
  *
  * \param table Pointer to dbTable

Modified: grass/trunk/lib/db/dbmi_base/test/test_table.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/test/test_table.c	2010-09-16 10:13:16 UTC (rev 43478)
+++ grass/trunk/lib/db/dbmi_base/test/test_table.c	2010-09-16 13:00:46 UTC (rev 43479)
@@ -119,6 +119,33 @@
             sum++;
         }
 
+	/*Now test the set column and get column by name functions*/
+	db_set_table_column(t2, 0, create_column("new_first", "new first column", DB_SQL_TYPE_DOUBLE_PRECISION));
+	db_set_table_column(t2, 1, create_column("new_second", "new second column", DB_SQL_TYPE_REAL));
+	db_set_table_column(t2, 2, create_column("new_third", "new third column", DB_SQL_TYPE_INTEGER));
+
+        G_message("##### Second table new columns:\n");
+        db_print_table_definition(stdout, t2);
+
+	dbColumn *c1 = db_get_table_column_by_name(t2, "new_first");
+	dbColumn *c2 = db_get_table_column_by_name(t2, "new_second");
+	dbColumn *c3 = db_get_table_column_by_name(t2, "new_third");
+
+
+        /*We check the column names*/
+        if(strcmp(c1->columnName.string, "new_first") != 0) {
+            G_warning("Error set table or get table by name first column");
+            sum++;
+        }
+        if(strcmp(c2->columnName.string, "new_second") != 0) {
+            G_warning("Error set table or get table by name second column");
+            sum++;
+        }
+        if(strcmp(c3->columnName.string, "new_third") != 0) {
+            G_warning("Error set table or get table by name third column");
+            sum++;
+        }
+
 	return sum;
 }
 



More information about the grass-commit mailing list