Ver código fonte

New table cloumn access method and tests implemented.
New Prototypes added to header file.


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

Soeren Gebbert 14 anos atrás
pai
commit
b85397f825
3 arquivos alterados com 61 adições e 2 exclusões
  1. 3 1
      include/proto_dbmi.h
  2. 31 1
      lib/db/dbmi_base/table.c
  3. 27 0
      lib/db/dbmi_base/test/test_table.c

+ 3 - 1
include/proto_dbmi.h

@@ -177,7 +177,8 @@ int db_get_index_number_of_columns(dbIndex * index);
 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 @@ int db_set_index_type_unique(dbIndex * index);
 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);

+ 31 - 1
lib/db/dbmi_base/table.c

@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <string.h>
 #include <grass/gis.h>
 #include <grass/dbmi.h>
 
@@ -272,7 +273,7 @@ int db_get_table_delete_priv(dbTable * table)
   \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 @@ dbColumn *db_get_table_column(dbTable * table, int idx)
 }
 
 /*!
+  \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

+ 27 - 0
lib/db/dbmi_base/test/test_table.c

@@ -119,6 +119,33 @@ int test_table(void)
             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;
 }