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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Sep 13 14:10:07 EDT 2010


Author: huhabla
Date: 2010-09-13 18:10:07 +0000 (Mon, 13 Sep 2010)
New Revision: 43453

Added:
   grass/trunk/lib/db/dbmi_base/test/
   grass/trunk/lib/db/dbmi_base/test/Makefile
   grass/trunk/lib/db/dbmi_base/test/test.dbmi_base.lib.html
   grass/trunk/lib/db/dbmi_base/test/test_columns.c
   grass/trunk/lib/db/dbmi_base/test/test_dbmi_base_lib.h
   grass/trunk/lib/db/dbmi_base/test/test_main.c
   grass/trunk/lib/db/dbmi_base/test/test_table.c
Modified:
   grass/trunk/include/proto_dbmi.h
   grass/trunk/lib/db/dbmi_base/column.c
   grass/trunk/lib/db/dbmi_base/dbmscap.c
   grass/trunk/lib/db/dbmi_base/dirent.c
   grass/trunk/lib/db/dbmi_base/table.c
   grass/trunk/lib/db/dbmi_client/column.c
   grass/trunk/lib/db/dbmi_client/printtab.c
Log:
Added new dbmi table and column functions. Updated dbmi doxygen doc.
Added tests for new dbmi table and column functions. Corrected some prototype definitions.


Modified: grass/trunk/include/proto_dbmi.h
===================================================================
--- grass/trunk/include/proto_dbmi.h	2010-09-13 01:11:18 UTC (rev 43452)
+++ grass/trunk/include/proto_dbmi.h	2010-09-13 18:10:07 UTC (rev 43453)
@@ -7,6 +7,7 @@
 void db__add_cursor_to_driver_state(dbCursor * cursor);
 int db_alloc_cursor_column_flags(dbCursor * cursor);
 int db_alloc_cursor_table(dbCursor * cursor, int ncols);
+int db_append_table_column(dbTable * , dbColumn *);
 dbDirent *db_alloc_dirent_array(int count);
 dbHandle *db_alloc_handle_array(int count);
 dbIndex *db_alloc_index_array(int count);
@@ -31,6 +32,7 @@
 void db_char_to_lowercase(char *s);
 void db_char_to_uppercase(char *s);
 void db_clear_error(void);
+dbTable *db_clone_table(dbTable *);
 void db__close_all_cursors(void);
 int db_close_cursor(dbCursor * cursor);
 int db_close_database(dbDriver * driver);
@@ -52,6 +54,7 @@
 					  dbString * string);
 int db_convert_value_to_string(dbValue * value, int sqltype,
 			       dbString * string);
+dbColumn *db_copy_column(dbColumn *, dbColumn *);
 void db_copy_dbmscap_entry(dbDbmscap * dst, dbDbmscap * src);
 int db_copy_string(dbString * dst, const dbString * src);
 int db_table_to_sql(dbTable *, dbString *);

Modified: grass/trunk/lib/db/dbmi_base/column.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/column.c	2010-09-13 01:11:18 UTC (rev 43452)
+++ grass/trunk/lib/db/dbmi_base/column.c	2010-09-13 18:10:07 UTC (rev 43453)
@@ -17,22 +17,7 @@
 #include <grass/gis.h>
 #include <grass/dbmi.h>
 
-/*!
-  \brief Returns column structure for given table and column number
-  
-  \param table pointer to dbTable
-  \param n     column index (starting with '0')
 
-  \return pointer to dbColumn
-  \return NULL on error
-*/
-dbColumn *db_get_table_column(dbTable * table, int n)
-{
-    if (n < 0 || n >= table->numColumns)
-	return ((dbColumn *) NULL);
-    return &table->columns[n];
-}
-
 /*!
   \brief Returns column value for given column structure
 
@@ -471,3 +456,38 @@
     db_free_string(&column->description);
     db_free_string(&column->defaultValue.s);
 }
+
+
+/*!
+ * \brief Copy a db column from source to destination
+ *
+ * \param src The column to copy from
+ * \param dest An allocated column to copy to which will be initialized. In case dest is NULL a new column will be allocated and returned
+ * \return The pointer of copied/allocated column
+ */
+dbColumn *db_copy_column(dbColumn *dest, dbColumn *src)
+{
+    dbColumn *new = dest;
+
+    if(new == NULL)
+        new = (dbColumn *) db_calloc(sizeof(dbColumn), 1);
+    else
+        db_init_column(new);
+
+    db_copy_string(&new->columnName, &src->columnName);
+    db_copy_string(&new->description, &src->description);
+    db_copy_value(&new->defaultValue, &src->defaultValue);
+    db_copy_value(&new->value, &src->value);
+    new->dataLen = src->dataLen;
+    new->hasDefaultValue = src->hasDefaultValue;
+    new->hostDataType = src->hostDataType;
+    new->nullAllowed = src->nullAllowed;
+    new->precision = src->precision;
+    new->scale = src->scale;
+    new->select = src->select;
+    new->sqlDataType = src->sqlDataType;
+    new->update = src->update;
+    new->useDefaultValue = src->useDefaultValue;
+    
+    return new;
+}

Modified: grass/trunk/lib/db/dbmi_base/dbmscap.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/dbmscap.c	2010-09-13 01:11:18 UTC (rev 43452)
+++ grass/trunk/lib/db/dbmi_base/dbmscap.c	2010-09-13 18:10:07 UTC (rev 43453)
@@ -29,7 +29,8 @@
     NULL
 };
 
-static void add_entry();
+static void
+add_entry(dbDbmscap ** list, char *name, char *startup, char *comment);
 
 static char *dbmscap_filename(int err_flag)
 {

Modified: grass/trunk/lib/db/dbmi_base/dirent.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/dirent.c	2010-09-13 01:11:18 UTC (rev 43452)
+++ grass/trunk/lib/db/dbmi_base/dirent.c	2010-09-13 18:10:07 UTC (rev 43453)
@@ -19,8 +19,9 @@
 extern DIR *opendir();
 extern dir_entry *readdir();
 
-static int get_perm();
-static void sort_dirent();
+static int cmp_dirent(const void *aa, const void *bb);
+static int get_perm(char *path);
+static void sort_dirent(dbDirent * a, int n);
 
 
 /*!

Modified: grass/trunk/lib/db/dbmi_base/table.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/table.c	2010-09-13 01:11:18 UTC (rev 43452)
+++ grass/trunk/lib/db/dbmi_base/table.c	2010-09-13 18:10:07 UTC (rev 43453)
@@ -3,10 +3,9 @@
 #include <grass/dbmi.h>
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Alloacte a table with a specific number of columns
+   \param The number of columns which should be allocated
+   \return The allocated table or NULL in case of an error
  */
 dbTable *db_alloc_table(int ncols)
 {
@@ -32,10 +31,8 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Initialize the table to zero
+   \param table pointer to dbTable
  */
 void db_init_table(dbTable * table)
 {
@@ -45,10 +42,8 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+ * \brief Free the table
+ * \param table pointer to dbTable
  */
 void db_free_table(dbTable * table)
 {
@@ -63,10 +58,10 @@
 }
 
 /*!
-   \fn 
-   \brief 
+   \brief Set the name of the table
+ * \param table pointer to dbTable
+ * \param name The name of the table
    \return 
-   \param 
  */
 int db_set_table_name(dbTable * table, const char *name)
 {
@@ -74,10 +69,9 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Get the name of the table
+   \param table pointer to dbTable
+   \return name of the table
  */
 const char *db_get_table_name(dbTable * table)
 {
@@ -85,10 +79,10 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Set the description of the table
+ * \param table pointer to dbTable
+ * \param name The description of the table
+   \return
  */
 int db_set_table_description(dbTable * table, const char *description)
 {
@@ -96,10 +90,9 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Get the description of the table
+   \param table pointer to dbTable
+   \return description of the table
  */
 const char *db_get_table_description(dbTable * table)
 {
@@ -107,10 +100,9 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Return the number of columns of the table
+ * \param table pointer to dbTable
+   \return Number of columns
  */
 int db_get_table_number_of_columns(dbTable * table)
 {
@@ -157,10 +149,8 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Grant selection privileges for all columns
+   \param table pointer to dbTable
  */
 void db_set_table_select_priv_granted(dbTable * table)
 {
@@ -168,10 +158,8 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Set selection privileges not granted for all columns
+   \param table pointer to dbTable
  */
 void db_set_table_select_priv_not_granted(dbTable * table)
 {
@@ -190,10 +178,8 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Grant update privileges for all columns
+   \param table pointer to dbTable
  */
 void db_set_table_update_priv_granted(dbTable * table)
 {
@@ -201,10 +187,8 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Set update privileges not granted for all columns
+   \param table pointer to dbTable
  */
 void db_set_table_update_priv_not_granted(dbTable * table)
 {
@@ -223,10 +207,8 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Grant insert privileges for table
+   \param table pointer to dbTable
  */
 void db_set_table_insert_priv_granted(dbTable * table)
 {
@@ -234,10 +216,8 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Set insert privileges not granted for table
+   \param table pointer to dbTable
  */
 void db_set_table_insert_priv_not_granted(dbTable * table)
 {
@@ -256,10 +236,8 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Grant delete privileges for table
+   \param table pointer to dbTable
  */
 void db_set_table_delete_priv_granted(dbTable * table)
 {
@@ -267,10 +245,8 @@
 }
 
 /*!
-   \fn 
-   \brief 
-   \return 
-   \param 
+   \brief Set delete privileges not granted for table
+   \param table pointer to dbTable
  */
 void db_set_table_delete_priv_not_granted(dbTable * table)
 {
@@ -288,10 +264,98 @@
     return table->priv_delete;
 }
 
+
 /*!
+  \brief Returns column structure for given table and column number
+
+  \param table pointer to dbTable
+  \param idx     column index (starting with '0')
+
+  \return pointer to dbColumn
+  \return NULL on error
+*/
+dbColumn *db_get_table_column(dbTable * table, int idx)
+{
+    if (idx < 0 || idx >= table->numColumns)
+	return ((dbColumn *) NULL);
+    return &table->columns[idx];
+}
+
+/*!
+ * \brief Set a specific column for given table and column number
+ *
+ * \param table Pointer to dbTable
+ * \param idx Column index (starting with '0').  The index must be in range.
+ * \param column Pointer to a dbColumn to insert.
+ * A copy of the column stored, so the original column can be deleted.
+ * \return DB_OK on success
+ * \return DB_FAILURE on error
+ */
+int db_set_table_column(dbTable * table, int idx, dbColumn *column)
+{
+    if (idx < 0 || idx >= table->numColumns)
+	return DB_FAILED;
+    db_copy_column(&table->columns[idx], column);
+    return DB_OK;
+}
+
+/*!
+ * \brief Append a specific column to given table
+ *
+ * \param table Pointer to dbTable
+ * \param column Pointer to a dbColumn to append.
+ * A copy of the column is stored, so the original column can be deleted.
+ * \return DB_OK on success
+ * \return DB_FAILURE on error
+ */
+int db_append_table_column(dbTable * table, dbColumn *column)
+{
+    table->columns = (dbColumn*)db_realloc((void*)table->columns, sizeof(dbColumn)*(table->numColumns + 1));
+    if(table->columns == NULL)
+        return DB_FAILED;
+    db_copy_column(&table->columns[table->numColumns], column);
+    table->numColumns++;
+    return DB_OK;
+}
+
+/*!
+ * \brief Make a new exact copy of an existing table
+ *
+ * New memory is allocated for the clone, the columns-content will be copied too.
+ *
+ * \param src Pointer to dbTable
+ * \return A new alloacted clone of the given table on success 
+ * \return NULL on error
+ */
+dbTable *db_clone_table(dbTable *src)
+{
+    int i, n = db_get_table_number_of_columns(src);
+    dbTable *new = db_alloc_table(n);
+    if(new == NULL)
+        return (new = NULL);
+
+    db_copy_string(&new->description, &src->description);
+    db_copy_string(&new->tableName, &src->tableName);
+
+    /* Deep copy the columns */
+    for(i = 0; i < n; i++)
+    {
+        db_copy_column(&new->columns[i], &src->columns[i]);
+    }
+
+    new->numColumns = n;
+    new->priv_delete = src->priv_delete;
+    new->priv_insert = src->priv_insert;
+
+    return new;
+}
+
+/*!
    \brief Create SQL CREATE sring from table definition
-   \return 
-   \param 
+   \param table
+ * \param sql The dbString to store the SQL CREATE string
+ * \return DB_OK on success
+ *  \return DB_FAILED on error
  */
 int db_table_to_sql(dbTable * table, dbString * sql)
 {

Added: grass/trunk/lib/db/dbmi_base/test/Makefile
===================================================================
--- grass/trunk/lib/db/dbmi_base/test/Makefile	                        (rev 0)
+++ grass/trunk/lib/db/dbmi_base/test/Makefile	2010-09-13 18:10:07 UTC (rev 43453)
@@ -0,0 +1,9 @@
+MODULE_TOPDIR = ../../../..
+
+PGM=test.dbmi_base.lib
+
+LIBES = $(GISLIB) $(DBMILIB)
+DEPENDENCIES = $(GISDEP) $(DBMIDEP)
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd

Added: grass/trunk/lib/db/dbmi_base/test/test.dbmi_base.lib.html
===================================================================
--- grass/trunk/lib/db/dbmi_base/test/test.dbmi_base.lib.html	                        (rev 0)
+++ grass/trunk/lib/db/dbmi_base/test/test.dbmi_base.lib.html	2010-09-13 18:10:07 UTC (rev 43453)
@@ -0,0 +1 @@
+Unit and integration tests for the dbmi base library. Only basic table and column tests are currently implemented.

Added: grass/trunk/lib/db/dbmi_base/test/test_columns.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/test/test_columns.c	                        (rev 0)
+++ grass/trunk/lib/db/dbmi_base/test/test_columns.c	2010-09-13 18:10:07 UTC (rev 43453)
@@ -0,0 +1,154 @@
+/****************************************************************************
+ *
+ * MODULE:       test.dbmi_base.lib
+ *
+ * AUTHOR(S):    Original author
+ *               Soeren Gebbert soerengebbert <at> googlemail <dot> com
+ * 		 2010 Braunschweig, Germany
+ *
+ * PURPOSE:      Unit and integration tests for the dbmi_base library
+ *
+ * COPYRIGHT:    (C) 2007 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
+ *   	    	for details.
+ *
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include <grass/dbmi.h>
+#include "test_dbmi_base_lib.h"
+
+/* prototypes */
+static int test_copy_column(void);
+
+/* ************************************************************************* */
+/* Performe the column unit tests  ****************************************** */
+/* ************************************************************************* */
+int unit_test_column(void)
+{
+	int sum = 0;
+
+	G_message(_("\n++ Running column unit tests ++"));
+
+	sum += test_copy_column();
+
+	if (sum > 0)
+		G_warning(_("\n-- column unit tests failure --"));
+	else
+		G_message(_("\n-- column unit tests finished successfully --"));
+
+	return sum;
+}
+
+/* *************************************************************** */
+/* Test copy dbColumn functionality ****************************** */
+/* *************************************************************** */
+int test_copy_column(void)
+{
+	int sum = 0;
+        dbColumn *column, *c2;
+
+	G_message(_("\n++ Run test copy column ++"));
+
+        /*Test the set and copy function*/
+        column = (dbColumn*)db_calloc(sizeof(dbColumn), 2);
+        db_init_column(&column[0]);
+        db_init_column(&column[1]);
+
+        /*Write some initial values*/
+        db_set_value_double(&column[0].defaultValue, 0.5);
+        db_set_value_double(&column[0].value, 10.5);
+        
+        db_set_column_description(&column[0], "Test column");
+        db_set_column_host_type(&column[0], 1);
+        db_set_column_length(&column[0], 8);
+        db_set_column_name(&column[0], "test");
+        db_set_column_null_allowed(&column[0]);
+        db_set_column_precision(&column[0], 20);
+        db_set_column_scale(&column[0], 1);
+        db_set_column_select_priv_granted(&column[0]);
+        db_set_column_sqltype(&column[0], DB_SQL_TYPE_DOUBLE_PRECISION);
+        db_set_column_select_priv_granted(&column[0]);
+        db_set_column_update_priv_granted(&column[0]);
+        db_set_column_use_default_value(&column[0]);
+
+        /*Copy the column*/
+        db_copy_column(&column[1], &column[0]);
+        c2 = db_copy_column(NULL, &column[1]);
+
+        fprintf(stdout, "##### First column:\n");
+        db_print_column_definition(stdout, &column[0]);
+        fprintf(stdout, "##### Second column:\n");
+        db_print_column_definition(stdout, &column[1]);
+        fprintf(stdout, "##### Third column:\n");
+        db_print_column_definition(stdout, c2);
+
+        /*Compare the columns*/
+        if(strcmp(column[0].columnName.string, c2->columnName.string) != 0) {
+            G_warning("Error copying column name");
+            sum++;
+        }
+        if(strcmp(column[0].description.string, c2->description.string) != 0) {
+            G_warning("Error copying column description");
+            sum++;
+        }
+        if(column[0].dataLen != c2->dataLen) {
+            G_warning("Error copying dataLen");
+            sum++;
+        }
+        if(column[0].defaultValue.d != c2->defaultValue.d) {
+            G_warning("Error copying default value");
+            sum++;
+        }
+        if(column[0].hasDefaultValue != c2->hasDefaultValue) {
+            G_warning("Error copying hasDefaultValue");
+            sum++;
+        }
+        if(column[0].hostDataType != column[1].hostDataType) {
+            G_warning("Error copying hostDataType");
+            sum++;
+        }
+        if(column[0].nullAllowed != c2->nullAllowed) {
+            G_warning("Error copying nullAllowed");
+            sum++;
+        }
+        if(column[0].precision != c2->precision) {
+            G_warning("Error copying precision");
+            sum++;
+        }
+        if(column[0].scale != c2->scale) {
+            G_warning("Error copying scale");
+            sum++;
+        }
+        if(column[0].select != c2->select) {
+            G_warning("Error copying select");
+            sum++;
+        }
+        if(column[0].sqlDataType != c2->sqlDataType) {
+            G_warning("Error copying sqlDataType");
+            sum++;
+        }
+        if(column[0].update != c2->update) {
+            G_warning("Error copying update");
+            sum++;
+        }
+        if(column[0].useDefaultValue != c2->useDefaultValue) {
+            G_warning("Error copying useDefaultValue");
+            sum++;
+        }
+        if(column[0].value.d != c2->value.d) {
+            G_warning("Error copying value");
+            sum++;
+        }
+
+	G_message(_("\n++ Test copy column finished ++"));
+
+	return sum;
+}
+

Added: grass/trunk/lib/db/dbmi_base/test/test_dbmi_base_lib.h
===================================================================
--- grass/trunk/lib/db/dbmi_base/test/test_dbmi_base_lib.h	                        (rev 0)
+++ grass/trunk/lib/db/dbmi_base/test/test_dbmi_base_lib.h	2010-09-13 18:10:07 UTC (rev 43453)
@@ -0,0 +1,23 @@
+/****************************************************************************
+ *
+ * MODULE:       test.dbmi_base.lib
+ *
+ * AUTHOR(S):    Original author
+ *               Soeren Gebbert soerengebbert <at> googlemail <dot> com
+ * 		 2010 Braunschweig, Germany
+ *
+ * PURPOSE:      Unit and integration tests for the dbmi_base library
+ *
+ * COPYRIGHT:    (C) 2007 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
+ *   	    	for details.
+ *
+ *****************************************************************************/
+
+#ifndef _TEST_DBMI_BASE_LIB_H_
+#define _TEST_DBMI_BASE_LIB_H_
+int unit_test_table(void);
+int unit_test_column(void);
+#endif

Added: grass/trunk/lib/db/dbmi_base/test/test_main.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/test/test_main.c	                        (rev 0)
+++ grass/trunk/lib/db/dbmi_base/test/test_main.c	2010-09-13 18:10:07 UTC (rev 43453)
@@ -0,0 +1,133 @@
+/****************************************************************************
+ *
+ * MODULE:       test.dbmi_base.lib
+ *   	    	
+ * AUTHOR(S):    Original author 
+ *               Soeren Gebbert soerengebbert <at> googlemail <dot> com
+ * 		 2010 Braunschweig, Germany
+ *
+ * PURPOSE:      Unit and integration tests for the dbmi_base library
+ *
+ * COPYRIGHT:    (C) 2007 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
+ *   	    	for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include <grass/dbmi.h>
+#include "test_dbmi_base_lib.h"
+
+/*- Parameters and global variables -----------------------------------------*/
+typedef struct {
+    struct Option *integration, *unit;
+    struct Flag *full, *testunit, *testint;
+} paramType;
+
+paramType param; /*Parameters */
+
+/*- prototypes --------------------------------------------------------------*/
+static void set_params(void); /*Fill the paramType structure */
+
+/* ************************************************************************* */
+/* Set up the arguments we are expecting ********************************** */
+
+/* ************************************************************************* */
+void set_params(void) {
+    param.unit = G_define_option();
+    param.unit->key = "unit";
+    param.unit->type = TYPE_STRING;
+    param.unit->required = NO;
+    param.unit->options = "column,table";
+    param.unit->description = _("Choose the unit tests to run");
+
+    param.integration = G_define_option();
+    param.integration->key = "integration";
+    param.integration->type = TYPE_STRING;
+    param.integration->required = NO;
+    param.integration->options = "";
+    param.integration->description = _("Choose the integration tests to run");
+    
+    param.testunit = G_define_flag();
+    param.testunit->key = 'u';
+    param.testunit->description = _("Run all unit tests");
+
+    param.testint = G_define_flag();
+    param.testint->key = 'i';
+    param.testint->description = _("Run all integration tests");
+
+    param.full = G_define_flag();
+    param.full->key = 'a';
+    param.full->description = _("Run all unit and integration tests");
+
+}
+
+/* ************************************************************************* */
+/* ************************************************************************* */
+
+/* ************************************************************************* */
+int main(int argc, char *argv[]) {
+    struct GModule *module;
+    int returnstat = 0, i;
+
+    /* Initialize GRASS */
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    module->description
+            = _("Performs benchmarks, unit and integration tests for the gmath library");
+
+    /* Get parameters from user */
+    set_params();
+
+    if (G_parser(argc, argv))
+        exit(EXIT_FAILURE);
+
+    /*Run the unit tests */
+    if (param.testunit->answer || param.full->answer) {
+        returnstat += unit_test_column();
+        returnstat += unit_test_table();
+    }
+
+    /*Run the integration tests */
+    if (param.testint->answer || param.full->answer) {
+        ;
+    }
+
+    /*Run single tests */
+    if (!param.full->answer) {
+        /*unit tests */
+        if (!param.testunit->answer) {
+            i = 0;
+            if (param.unit->answers)
+                while (param.unit->answers[i]) {
+                    if (strcmp(param.unit->answers[i], "column") == 0)
+                        returnstat += unit_test_column();
+                    if (strcmp(param.unit->answers[i], "table") == 0)
+                        returnstat += unit_test_table();
+
+                    i++;
+            }
+        }
+        /*integration tests */
+        if (!param.testint->answer) {
+            i = 0;
+            if (param.integration->answers)
+                while (param.integration->answers[i]) {
+                    ;
+            }
+        }
+    }
+    
+    if (returnstat != 0)
+        G_warning("Errors detected while testing the dbmi_base lib");
+    else
+        G_message("\n-- dbmi_base lib tests finished successfully --");
+
+    return (returnstat);
+}

Added: grass/trunk/lib/db/dbmi_base/test/test_table.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/test/test_table.c	                        (rev 0)
+++ grass/trunk/lib/db/dbmi_base/test/test_table.c	2010-09-13 18:10:07 UTC (rev 43453)
@@ -0,0 +1,152 @@
+/****************************************************************************
+ *
+ * MODULE:       test.dbmi_base.lib
+ *
+ * AUTHOR(S):    Original author
+ *               Soeren Gebbert soerengebbert <at> googlemail <dot> com
+ * 		 2010 Braunschweig, Germany
+ *
+ * PURPOSE:      Unit and integration tests for the dbmi_base library
+ *
+ * COPYRIGHT:    (C) 2007 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
+ *   	    	for details.
+ *
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include <grass/dbmi.h>
+#include "test_dbmi_base_lib.h"
+
+/* prototypes */
+static int test_table(void);
+static dbColumn * create_column(const char *name, const char *desctiption, int sqltype);
+
+/* ************************************************************************* */
+/* Performe the table unit tests  ****************************************** */
+/* ************************************************************************* */
+int unit_test_table(void)
+{
+	int sum = 0;
+
+	G_message(_("\n++ Running table unit tests ++"));
+
+	sum += test_table();
+
+	if (sum > 0)
+		G_warning(_("\n-- Table unit tests failure --"));
+	else
+		G_message(_("\n-- Table unit tests finished successfully --"));
+
+	return sum;
+}
+
+/* *************************************************************** */
+/* Test some functions of the dbTable functionality ************** */
+/* *************************************************************** */
+int test_table(void)
+{
+	int sum = 0;
+        dbTable *t1 = db_alloc_table(0);
+        dbTable *t2;
+
+        db_init_table(t1);
+
+        /*Append three columns of the different type*/
+        db_append_table_column(t1, create_column("first", "first column", DB_SQL_TYPE_DOUBLE_PRECISION));
+        db_append_table_column(t1, create_column("second", "second column", DB_SQL_TYPE_REAL));
+        db_append_table_column(t1, create_column("third", "third column", DB_SQL_TYPE_DECIMAL));
+
+        if(t1->numColumns != 3) {
+            G_warning("Error appending columns");
+            sum++;
+        }
+
+        db_set_table_delete_priv_granted(t1);
+        db_set_table_description(t1, "test table");
+        db_set_table_insert_priv_granted(t1);
+        db_set_table_name(t1, "test");
+        db_set_table_select_priv_granted(t1);
+        db_set_table_update_priv_granted(t1);
+
+        G_message("##### First table:\n");
+        db_print_table_definition(stdout, t1);
+
+        /*Clone the table*/
+        t2 = db_clone_table(t1);
+
+        G_message("##### Second table:\n");
+        db_print_table_definition(stdout, t2);
+
+        /*Compare the tables*/
+        if(strcmp(t1->description.string, t2->description.string) != 0) {
+            G_warning("Error copying description");
+            sum++;
+        }
+        if(strcmp(t1->tableName.string, t2->tableName.string) != 0) {
+            G_warning("Error copying tableName");
+            sum++;
+        }
+        if(t1->numColumns != t2->numColumns) {
+            G_warning("Error copying table numColumns");
+            sum++;
+        }
+        if(t1->priv_delete != t2->priv_delete) {
+            G_warning("Error copying privileg delete");
+            sum++;
+        }
+        if(t1->priv_insert != t2->priv_insert) {
+            G_warning("Error copying privileg insert");
+            sum++;
+        }
+        /*We check only the column names*/
+        if(strcmp(t1->columns[0].columnName.string, t2->columns[0].columnName.string) != 0) {
+            G_warning("Error copying first column");
+            sum++;
+        }
+        if(strcmp(t1->columns[1].columnName.string, t2->columns[1].columnName.string) != 0) {
+            G_warning("Error copying second column");
+            sum++;
+        }
+        if(strcmp(t1->columns[2].columnName.string, t2->columns[2].columnName.string) != 0) {
+            G_warning("Error copying third column");
+            sum++;
+        }
+
+	return sum;
+}
+
+/* *************************************************************** */
+/* Simple table creation ***************************************** */
+/* *************************************************************** */
+dbColumn *create_column(const char *name, const char *desctiption, int sqltype)
+{
+        dbColumn *column = (dbColumn*)db_calloc(sizeof(dbColumn), 1);
+        db_init_column(column);
+
+        /*Write some initial values*/
+        db_set_value_double(&column->defaultValue, 0.5);
+        db_set_value_double(&column->value, 10.5);
+
+        db_set_column_description(column, desctiption);
+        db_set_column_host_type(column, 1);
+        db_set_column_length(column, 8);
+        db_set_column_name(column, name);
+        db_set_column_null_allowed(column);
+        db_set_column_precision(column, 20);
+        db_set_column_scale(column, 1);
+        db_set_column_select_priv_granted(column);
+        db_set_column_sqltype(column, sqltype);
+        db_set_column_select_priv_granted(column);
+        db_set_column_update_priv_granted(column);
+        db_set_column_use_default_value(column);
+
+        return column;
+}
+

Modified: grass/trunk/lib/db/dbmi_client/column.c
===================================================================
--- grass/trunk/lib/db/dbmi_client/column.c	2010-09-13 01:11:18 UTC (rev 43452)
+++ grass/trunk/lib/db/dbmi_client/column.c	2010-09-13 18:10:07 UTC (rev 43453)
@@ -100,7 +100,7 @@
 {
     int i, ncols;
     dbTable *Table;
-    dbColumn *Col, *NCol;
+    dbColumn *Col;
     dbString tabname;
 
     db_init_string(&tabname);
@@ -119,25 +119,7 @@
     for (i = 0; i < ncols; i++) {
 	Col = db_get_table_column(Table, i);
 	if (G_strcasecmp(db_get_column_name(Col), cname) == 0) {
-	    NCol = (dbColumn *) malloc(sizeof(dbColumn));
-	    db_init_column(NCol);
-	    db_set_string(&(NCol->columnName), db_get_column_name(Col));
-	    db_set_string(&(NCol->description),
-			  db_get_column_description(Col));
-	    NCol->sqlDataType = Col->sqlDataType;
-	    NCol->hostDataType = Col->hostDataType;
-	    db_copy_value(&(NCol->value), &(Col->value));
-	    NCol->dataLen = Col->dataLen;
-	    NCol->precision = Col->precision;
-	    NCol->scale = Col->scale;
-	    NCol->nullAllowed = Col->nullAllowed;
-	    NCol->hasDefaultValue = Col->hasDefaultValue;
-	    NCol->useDefaultValue = Col->useDefaultValue;
-	    db_copy_value(&(NCol->defaultValue), &(Col->defaultValue));
-	    NCol->select = Col->select;
-	    NCol->update = Col->update;
-
-	    *Column = NCol;
+	    *Column = db_copy_column(NULL, Col);
 	    return DB_OK;
 	}
     }

Modified: grass/trunk/lib/db/dbmi_client/printtab.c
===================================================================
--- grass/trunk/lib/db/dbmi_client/printtab.c	2010-09-13 01:11:18 UTC (rev 43452)
+++ grass/trunk/lib/db/dbmi_client/printtab.c	2010-09-13 18:10:07 UTC (rev 43453)
@@ -15,7 +15,7 @@
 #include <string.h>
 #include <grass/dbmi.h>
 
-static void print_priv();
+static void print_priv(FILE * fd, char *label, int priv);
 
 /*!
   \brief Print table definition info



More information about the grass-commit mailing list