r17192 - gnucash/branches/gda-dev2 - 1) Split the gda backend code into 2 pieces. The first, in the sql

Phil Longstaff plongstaff at cvs.gnucash.org
Sat Jun 7 11:38:40 EDT 2008


Author: plongstaff
Date: 2008-06-07 11:38:38 -0400 (Sat, 07 Jun 2008)
New Revision: 17192
Trac: http://svn.gnucash.org/trac/changeset/17192

Added:
   gnucash/branches/gda-dev2/src/backend/dbi/
   gnucash/branches/gda-dev2/src/backend/dbi/Makefile.am
   gnucash/branches/gda-dev2/src/backend/dbi/gnc-backend-dbi.c
   gnucash/branches/gda-dev2/src/backend/dbi/gnc-backend-dbi.h
   gnucash/branches/gda-dev2/src/backend/dbi/gncmod-backend-dbi.c
   gnucash/branches/gda-dev2/src/backend/dbi/test/
   gnucash/branches/gda-dev2/src/backend/dbi/test/Makefile.am
   gnucash/branches/gda-dev2/src/backend/dbi/test/test-load-backend.c
   gnucash/branches/gda-dev2/src/backend/sql/
   gnucash/branches/gda-dev2/src/backend/sql/Makefile.am
   gnucash/branches/gda-dev2/src/backend/sql/gnc-account-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-account-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-util-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-util-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-book-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-book-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-budget-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-budget-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-commodity-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-commodity-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-lots-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-lots-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-price-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-price-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-recurrence-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-recurrence-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-schedxaction-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-schedxaction-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-slots-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-slots-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gnc-transaction-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/gnc-transaction-sql.h
   gnucash/branches/gda-dev2/src/backend/sql/gncmod-backend-sql.c
   gnucash/branches/gda-dev2/src/backend/sql/test/
   gnucash/branches/gda-dev2/src/backend/sql/test/Makefile.am
   gnucash/branches/gda-dev2/src/backend/sql/test/test-load-backend.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-sql.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-sql.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gncmod-business-backend-sql.c
   gnucash/branches/gda-dev2/src/pixmaps/22x22/
   gnucash/branches/gda-dev2/src/pixmaps/22x22/gnucash-icon.png
   gnucash/branches/gda-dev2/src/pixmaps/24x24/
   gnucash/branches/gda-dev2/src/pixmaps/24x24/gnucash-icon.png
Removed:
   gnucash/branches/gda-dev2/src/backend/gda/gnc-account-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-account-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-util-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-util-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gnc-book-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-book-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gnc-budget-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-budget-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gnc-commodity-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-commodity-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gnc-lots-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-lots-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gnc-price-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-price-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gnc-recurrence-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-recurrence-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gnc-schedxaction-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-schedxaction-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gnc-slots-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-slots-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gnc-transaction-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-transaction-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/gda/
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-gda.h
   gnucash/branches/gda-dev2/src/business/business-core/sql/gncmod-business-backend-gda.c
Modified:
   gnucash/branches/gda-dev2/configure.in
   gnucash/branches/gda-dev2/src/backend/Makefile.am
   gnucash/branches/gda-dev2/src/backend/gda/Makefile.am
   gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-gda.c
   gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-gda.h
   gnucash/branches/gda-dev2/src/backend/gda/gncmod-backend-gda.c
   gnucash/branches/gda-dev2/src/business/business-core/Makefile.am
   gnucash/branches/gda-dev2/src/business/business-core/sql/Makefile.am
   gnucash/branches/gda-dev2/src/business/business-gnome/business-gnome.scm
   gnucash/branches/gda-dev2/src/engine/gnc-engine.c
   gnucash/branches/gda-dev2/src/gnome-utils/dialog-database-connection.c
   gnucash/branches/gda-dev2/src/valgrind-gnucash.supp
   gnucash/branches/gda-dev2/src/valgrind-libfontconfig.supp
Log:
1) Split the gda backend code into 2 pieces.  The first, in the sql 
directory is a base library which works in terms of connections, 
statements, results and rows.  This sql library includes all of the code 
to load and save individual engine and business objects.  The second 
piece is a gda specific piece.  This piece contains the qof backend code 
as well as implementations for the sql connection, statement, result and 
row in terms of libgda.

The business backend is completely moved to the sql directory and has 
now libgda or libdbi specific code.  It will tie into whichever library 
the main backend uses.

2) Add a second backend built on the sql code to use libdbi (sqlite3 
only).  This backend will create tables if they don't exist and uses the 
file url.

3) In the engine, load the dbi and xml backends (gda commented out for 
now).

4) Complete commit of some pixmaps from an earlier merge from trunk.



Modified: gnucash/branches/gda-dev2/configure.in
===================================================================
--- gnucash/branches/gda-dev2/configure.in	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/configure.in	2008-06-07 15:38:38 UTC (rev 17192)
@@ -617,7 +617,7 @@
    esac],[want_gda=true])
 if test x${want_gda} = xtrue
 then
-  PKG_CHECK_MODULES(LIBGDA, libgda-3.0 >= 2.99.2, [GDA_DIR=gda])
+  PKG_CHECK_MODULES(LIBGDA, libgda-4.0 >= 3.99.2, [GDA_DIR=gda])
 fi
 AC_SUBST(GDA_DIR)
 AC_SUBST(LIBGDA_CFLAGS)
@@ -1478,6 +1478,8 @@
           src/app-utils/Makefile
           src/app-utils/test/Makefile
           src/backend/Makefile
+		  src/backend/dbi/Makefile
+		  src/backend/dbi/test/Makefile
           src/backend/file/Makefile
           src/backend/file/test/Makefile
           src/backend/file/test/test-files/Makefile
@@ -1486,6 +1488,8 @@
 		  src/backend/gda/test/Makefile
           src/backend/postgres/Makefile
           src/backend/postgres/test/Makefile
+		  src/backend/sql/Makefile
+		  src/backend/sql/test/Makefile
           src/bin/Makefile
           src/bin/overrides/Makefile
           src/bin/test/Makefile
@@ -1566,7 +1570,7 @@
           src/test-core/Makefile
           src/business/Makefile
           src/business/business-core/Makefile
-          src/business/business-core/gda/Makefile
+          src/business/business-core/sql/Makefile
           src/business/business-core/test/Makefile
           src/business/business-core/file/Makefile
           src/business/business-utils/Makefile

Modified: gnucash/branches/gda-dev2/src/backend/Makefile.am
===================================================================
--- gnucash/branches/gda-dev2/src/backend/Makefile.am	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/Makefile.am	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,3 +1,3 @@
 
-SUBDIRS = file ${SQL_DIR} ${GDA_DIR}
-DIST_SUBDIRS = file postgres gda
+SUBDIRS = file ${SQL_DIR} ${GDA_DIR} sql dbi
+DIST_SUBDIRS = file postgres gda sql dbi


Property changes on: gnucash/branches/gda-dev2/src/backend/dbi
___________________________________________________________________
Name: svn:ignore
   + Makefile
Makefile.in
.libs
.deps



Added: gnucash/branches/gda-dev2/src/backend/dbi/Makefile.am
===================================================================
--- gnucash/branches/gda-dev2/src/backend/dbi/Makefile.am	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/dbi/Makefile.am	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,34 @@
+SUBDIRS = . test
+
+# Now a shared library AND a GModule
+pkglib_LTLIBRARIES = libgncmod-backend-dbi.la
+
+
+AM_CFLAGS = \
+  -I.. -I../.. \
+  -DLOCALE_DIR=\""$(datadir)/locale"\" \
+  -I${top_srcdir}/src/backend \
+  -I${top_srcdir}/src/backend/sql \
+  -I${top_srcdir}/src/engine \
+  -I${top_srcdir}/src/core-utils\
+  -I${top_srcdir}/lib/libc\
+  ${QOF_CFLAGS} \
+  ${GLIB_CFLAGS} \
+  ${GCONF_CFLAGS} \
+  ${WARN_CFLAGS}
+
+libgncmod_backend_dbi_la_SOURCES = \
+  gnc-backend-dbi.c
+
+noinst_HEADERS = \
+  gnc-backend-dbi.h
+
+libgncmod_backend_dbi_la_LDFLAGS = -module -avoid-version
+libgncmod_backend_dbi_la_LIBADD = \
+   ${GLIB_LIBS} ${GCONF_LIBS} \
+   ${top_builddir}/src/backend/sql/libgncmod-backend-sql.la \
+   ${top_builddir}/src/engine/libgncmod-engine.la \
+   -ldbi \
+   ${QOF_LIBS}
+
+INCLUDES = -DG_LOG_DOMAIN=\"gnc.backend.dbi\"

Added: gnucash/branches/gda-dev2/src/backend/dbi/gnc-backend-dbi.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/dbi/gnc-backend-dbi.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/dbi/gnc-backend-dbi.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,821 @@
+/********************************************************************
+ * gnc-backend-dbi.c: load and save data to SQL via libdbi          *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-backend-dbi.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db using libdbi
+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <glib/gstdio.h>
+
+#include <dbi/dbi.h>
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-backend-sql.h"
+
+#include "qof.h"
+#include "qofquery-p.h"
+#include "qofquerycore-p.h"
+#include "Account.h"
+#include "TransLog.h"
+#include "gnc-engine.h"
+#include "SX-book.h"
+#include "Recurrence.h"
+
+#include "gnc-gconf-utils.h"
+
+#include "gnc-backend-dbi.h"
+
+#define TRANSACTION_NAME "trans"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+static GncSqlConnection* create_dbi_connection( dbi_conn conn );
+
+/* ================================================================= */
+
+static void
+create_tables_cb( const gchar* type, gpointer data_p, gpointer be_p )
+{
+    GncSqlDataType_t* pData = data_p;
+    GncDbiBackend* be = be_p;
+
+    g_return_if_fail( type != NULL && data_p != NULL && be_p != NULL );
+    g_return_if_fail( pData->version == GNC_SQL_BACKEND_VERSION );
+
+    if( pData->create_tables != NULL ) {
+        (pData->create_tables)( &be->sql_be );
+    }
+}
+
+static void
+error_fn( dbi_conn conn, void* user_data )
+{
+    GncDbiBackend *be = (GncDbiBackend*)user_data;
+	const gchar* msg;
+
+	dbi_conn_error( conn, &msg );
+	PERR( "DBI error: %s\n", msg );
+}
+
+static void
+gnc_dbi_session_begin( QofBackend *qbe, QofSession *session, 
+	                   const gchar *book_id,
+                       gboolean ignore_lock,
+				       gboolean create_if_nonexistent )
+{
+    GncDbiBackend *be = (GncDbiBackend*)qbe;
+    GError* error = NULL;
+    gchar* dsn;
+    gchar* username;
+    gchar* password;
+	gchar* provider;
+	gboolean uriOK;
+	gint result;
+	gchar* dirname;
+	gchar* basename;
+
+	g_return_if_fail( qbe != NULL );
+	g_return_if_fail( session != NULL );
+	g_return_if_fail( book_id != NULL );
+
+    ENTER (" ");
+
+	dirname = g_path_get_dirname( book_id );
+	basename = g_path_get_basename( book_id );
+
+	be->conn = dbi_conn_new( "sqlite3" );
+	if( be->conn == NULL ) {
+		PERR( "Unable to create sqlite3 dbi connection\n" );
+        qof_backend_set_error( qbe, ERR_BACKEND_BAD_URL );
+		LEAVE( " " );
+		return;
+	}
+	dbi_conn_error_handler( be->conn, error_fn, be );
+	dbi_conn_set_option( be->conn, "host", "localhost" );
+	dbi_conn_set_option( be->conn, "dbname", basename );
+	dbi_conn_set_option( be->conn, "sqlite3_dbdir", dirname );
+	result = dbi_conn_connect( be->conn );
+	g_free( basename );
+	g_free( dirname );
+	if( result < 0 ) {
+		PERR( "Unable to connect to %s: %d\n", book_id, result );
+        qof_backend_set_error( qbe, ERR_BACKEND_BAD_URL );
+        LEAVE( " " );
+        return;
+	}
+
+	be->sql_be.conn = create_dbi_connection( be->conn );
+
+    LEAVE (" ");
+}
+
+/* ================================================================= */
+
+static void
+gnc_dbi_session_end( QofBackend *be_start )
+{
+    GncDbiBackend *be = (GncDbiBackend*)be_start;
+
+	g_return_if_fail( be_start != NULL );
+
+    ENTER (" ");
+
+    dbi_conn_close( be->conn );
+	gnc_sql_finalize_version_info( &be->sql_be );
+
+    LEAVE (" ");
+}
+
+static void
+gnc_dbi_destroy_backend( QofBackend *be )
+{
+	g_return_if_fail( be != NULL );
+
+    g_free( be );
+}
+
+/* ================================================================= */
+
+static void
+gnc_dbi_load( QofBackend* qbe, QofBook *book )
+{
+    GncDbiBackend *be = (GncDbiBackend*)qbe;
+    GncSqlDataType_t* pData;
+	int i;
+	Account* root;
+
+	g_return_if_fail( qbe != NULL );
+	g_return_if_fail( book != NULL );
+
+    ENTER( "be=%p, book=%p", be, book );
+
+    g_assert( be->primary_book == NULL );
+    be->primary_book = book;
+
+	// Set up table version information
+	gnc_sql_init_version_info( &be->sql_be );
+
+    // Call all object backends to create any required tables
+    qof_object_foreach_backend( GNC_SQL_BACKEND, create_tables_cb, be );
+
+	gnc_sql_load( &be->sql_be, book );
+
+    LEAVE( "" );
+}
+
+/* ================================================================= */
+
+static gboolean
+gnc_dbi_save_may_clobber_data( QofBackend* qbe )
+{
+    GncDbiBackend* be = (GncDbiBackend*)qbe;
+	const gchar* dbname;
+	dbi_result tables;
+	gint numTables;
+
+	/* Data may be clobbered iff the number of tables != 0 */
+	dbname = dbi_conn_get_option( be->conn, "dbname" );
+	tables = dbi_conn_get_table_list( be->conn, dbname, NULL );
+    numTables = dbi_result_get_numrows( tables );
+	dbi_result_free( tables );
+
+	return (numTables != 0);
+}
+
+static void
+gnc_dbi_sync_all( QofBackend* fbe, QofBook *book )
+{
+    GncDbiBackend* be = (GncDbiBackend*)fbe;
+    dbi_result tables;
+    GError* error = NULL;
+    gint row;
+    gint numTables;
+	gboolean status;
+	const gchar* dbname;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( book != NULL );
+
+    ENTER( "book=%p, primary=%p", book, be->primary_book );
+
+    /* Destroy the current contents of the database */
+	dbname = dbi_conn_get_option( be->conn, "dbname" );
+	tables = dbi_conn_get_table_list( be->conn, dbname, NULL );
+	while( dbi_result_next_row( tables ) ) {
+		const gchar* table_name;
+		dbi_result result;
+
+		table_name = dbi_result_get_string_idx( tables, 0 );
+		result = dbi_conn_queryf( be->conn, "DROP TABLE %s", table_name );
+	}
+
+    /* Save all contents */
+	be->is_pristine_db = TRUE;
+	be->primary_book = book;
+	gnc_sql_sync_all( &be->sql_be, book );
+
+    LEAVE( "book=%p", book );
+}
+
+/* ================================================================= */
+
+static QofBackend*
+gnc_dbi_backend_new(void)
+{
+    GncDbiBackend *gnc_be;
+    QofBackend *be;
+    static gboolean initialized = FALSE;
+
+    gnc_be = g_new0(GncDbiBackend, 1);
+    be = (QofBackend*) gnc_be;
+    qof_backend_init(be);
+
+    be->session_begin = gnc_dbi_session_begin;
+    be->session_end = gnc_dbi_session_end;
+    be->destroy_backend = gnc_dbi_destroy_backend;
+
+    be->load = gnc_dbi_load;
+    be->save_may_clobber_data = gnc_dbi_save_may_clobber_data;
+
+    /* The gda backend treats accounting periods transactionally. */
+    be->begin = gnc_sql_begin_edit;
+    be->commit = gnc_sql_commit_edit;
+    be->rollback = gnc_sql_rollback_edit;
+
+    be->counter = NULL;
+
+    /* The gda backend will not be multi-user (for now)... */
+    be->events_pending = NULL;
+    be->process_events = NULL;
+
+    be->sync = gnc_dbi_sync_all;
+    be->load_config = NULL;
+    be->get_config = NULL;
+
+    be->export = NULL;
+
+    gnc_be->primary_book = NULL;
+
+    if( !initialized ) {
+        int num_drivers = dbi_initialize( "/usr/lib/dbd" );
+		gnc_sql_init( &gnc_be->sql_be );
+        initialized = TRUE;
+    }
+
+    return be;
+}
+
+static void
+gnc_dbi_provider_free( QofBackendProvider *prov )
+{
+	g_return_if_fail( prov != NULL );
+
+    prov->provider_name = NULL;
+    prov->access_method = NULL;
+    g_free (prov);
+}
+
+/*
+ * Checks to see whether the file is an sqlite file or not
+ *
+ */
+static gboolean
+gnc_dbi_check_sqlite_file( const gchar *path )
+{
+	FILE* f;
+	gchar buf[50];
+
+	// BAD if the path is null
+	g_return_val_if_fail( path != NULL, FALSE );
+
+	f = g_fopen( path, "r" );
+
+	// OK if the file doesn't exist - new file
+	if( f == NULL ) {
+		PINFO( "doesn't exist (errno=%d) -> DBI", errno );
+		return TRUE;
+	}
+
+	// OK if file has the correct header
+	fread( buf, sizeof(buf), 1, f );
+	fclose( f );
+	if( g_str_has_prefix( buf, "SQLite format 3" ) ) {
+		PINFO( "has SQLite format string -> DBI" );
+		return TRUE;
+	}
+	PINFO( "exists, does not have SQLite format string -> not DBI" );
+
+	// Otherwise, BAD
+	return FALSE;
+}
+
+G_MODULE_EXPORT void
+qof_backend_module_init(void)
+{
+    QofBackendProvider *prov;
+
+    prov = g_new0 (QofBackendProvider, 1);
+    prov->provider_name = "GnuCash Libdbi Backend";
+    prov->access_method = "file";
+    prov->partial_book_supported = FALSE;
+    prov->backend_new = gnc_dbi_backend_new;
+    prov->provider_free = gnc_dbi_provider_free;
+    prov->check_data_type = gnc_dbi_check_sqlite_file;
+    qof_backend_register_provider (prov);
+}
+
+/* --------------------------------------------------------- */
+typedef struct
+{
+	GncSqlRow base;
+
+	dbi_result result;
+} GncDbiSqlRow;
+
+static void
+row_dispose( GncSqlRow* row )
+{
+	GncDbiSqlRow* dbi_row = (GncDbiSqlRow*)row;
+
+	g_free( dbi_row );
+}
+
+static const GValue*
+row_get_value_at_col_name( GncSqlRow* row, const gchar* col_name )
+{
+	GncDbiSqlRow* dbi_row = (GncDbiSqlRow*)row;
+	gushort type;
+	GValue* value;
+
+	type = dbi_result_get_field_type( dbi_row->result, col_name );
+	value = g_new0( GValue, 1 );
+	switch( type ) {
+		case DBI_TYPE_INTEGER:
+			g_value_init( value, G_TYPE_INT );
+			g_value_set_int( value, dbi_result_get_int( dbi_row->result, col_name ) );
+			break;
+		case DBI_TYPE_DECIMAL:
+			g_value_init( value, G_TYPE_DOUBLE );
+			g_value_set_double( value, dbi_result_get_double( dbi_row->result, col_name ) );
+			break;
+		case DBI_TYPE_STRING:
+			g_value_init( value, G_TYPE_STRING );
+			g_value_set_string( value, dbi_result_get_string( dbi_row->result, col_name ) );
+			break;
+		default:
+			PERR( "Unknown DBI_TYPE: %d\n", type );
+			g_free( value );
+			return NULL;
+	}
+	
+	return value;
+}
+
+static GncSqlRow*
+create_dbi_row( dbi_result result )
+{
+	GncDbiSqlRow* row;
+
+	row = g_new0( GncDbiSqlRow, 1 );
+	row->base.getValueAtColName = row_get_value_at_col_name;
+	row->base.dispose = row_dispose;
+	row->result = result;
+
+	return (GncSqlRow*)row;
+}
+/* --------------------------------------------------------- */
+typedef struct
+{
+	GncSqlResult base;
+
+	dbi_result result;
+	gint num_rows;
+	gint cur_row;
+} GncDbiSqlResult;
+
+static void
+result_dispose( GncSqlResult* result )
+{
+	GncDbiSqlResult* dbi_result = (GncDbiSqlResult*)result;
+
+	dbi_result_free( dbi_result->result );
+	g_free( result );
+}
+
+static gint
+result_get_num_rows( GncSqlResult* result )
+{
+	GncDbiSqlResult* dbi_result = (GncDbiSqlResult*)result;
+
+	return dbi_result->num_rows;
+}
+
+static GncSqlRow*
+result_get_first_row( GncSqlResult* result )
+{
+	GncDbiSqlResult* dbi_result = (GncDbiSqlResult*)result;
+
+	if( dbi_result->num_rows > 0 ) {
+		dbi_result_first_row( dbi_result->result );
+		dbi_result->cur_row = 1;
+		return create_dbi_row( dbi_result->result );
+	} else {
+		return NULL;
+	}
+}
+
+static GncSqlRow*
+result_get_next_row( GncSqlResult* result )
+{
+	GncDbiSqlResult* dbi_result = (GncDbiSqlResult*)result;
+
+	if( dbi_result->cur_row < dbi_result->num_rows ) {
+		dbi_result_next_row( dbi_result->result );
+		dbi_result->cur_row++;
+		return create_dbi_row( dbi_result->result );
+	} else {
+		return NULL;
+	}
+}
+
+static GncSqlResult*
+create_dbi_result( dbi_result result )
+{
+	GncDbiSqlResult* dbi_result;
+
+	dbi_result = g_new0( GncDbiSqlResult, 1 );
+	dbi_result->base.dispose = result_dispose;
+	dbi_result->base.getNumRows = result_get_num_rows;
+	dbi_result->base.getFirstRow = result_get_first_row;
+	dbi_result->base.getNextRow = result_get_next_row;
+	dbi_result->result = result;
+	dbi_result->num_rows = dbi_result_get_numrows( result );
+	dbi_result->cur_row = 0;
+
+	return (GncSqlResult*)dbi_result;
+}
+/* --------------------------------------------------------- */
+typedef struct
+{
+	GncSqlStatement base;
+
+	GString* sql;
+} GncDbiSqlStatement;
+
+static void
+stmt_dispose( GncSqlStatement* stmt )
+{
+	GncDbiSqlStatement* dbi_stmt = (GncDbiSqlStatement*)stmt;
+
+	if( dbi_stmt->sql != NULL ) {
+		g_string_free( dbi_stmt->sql, TRUE );
+	}
+	g_free( stmt );
+}
+
+static gchar*
+stmt_to_sql( GncSqlStatement* stmt )
+{
+	GncDbiSqlStatement* dbi_stmt = (GncDbiSqlStatement*)stmt;
+
+	return dbi_stmt->sql->str;
+}
+
+static void
+stmt_add_where_cond( GncSqlStatement* stmt, QofIdTypeConst type_name,
+					gpointer obj, const col_cvt_t* table_row, GValue* value )
+{
+	GncDbiSqlStatement* dbi_stmt = (GncDbiSqlStatement*)stmt;
+	gchar* buf;
+
+	buf = g_strdup_printf( " WHERE %s = %s", table_row->col_name,
+						gnc_sql_get_sql_value( value ) );
+	g_string_append( dbi_stmt->sql, buf );
+	g_free( buf );
+}
+
+static GncSqlStatement*
+create_dbi_statement( const gchar* sql )
+{
+	GncDbiSqlStatement* stmt;
+
+	stmt = g_new0( GncDbiSqlStatement, 1 );
+	stmt->base.dispose = stmt_dispose;
+	stmt->base.toSql = stmt_to_sql;
+	stmt->base.addWhereCond = stmt_add_where_cond;
+	stmt->sql = g_string_new( sql );
+
+	return (GncSqlStatement*)stmt;
+}
+/* --------------------------------------------------------- */
+typedef struct
+{
+	GncSqlConnection base;
+
+	dbi_conn conn;
+} GncDbiSqlConnection;
+
+static void
+conn_dispose( GncSqlConnection* conn )
+{
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+
+	g_free( conn );
+}
+
+static GncSqlResult*
+conn_execute_select_statement( GncSqlConnection* conn, GncSqlStatement* stmt )
+{
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+	GncDbiSqlStatement* dbi_stmt = (GncDbiSqlStatement*)stmt;
+	dbi_result result;
+														
+	result = dbi_conn_query( dbi_conn->conn, dbi_stmt->sql->str );
+	if( result == NULL ) {
+		PERR( "Error executing SQL %s\n", dbi_stmt->sql->str );
+		return NULL;
+	}
+	return create_dbi_result( result );
+}
+
+static gint
+conn_execute_nonselect_statement( GncSqlConnection* conn, GncSqlStatement* stmt )
+{
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+	GncDbiSqlStatement* dbi_stmt = (GncDbiSqlStatement*)stmt;
+	dbi_result result;
+
+	result = dbi_conn_query( dbi_conn->conn, dbi_stmt->sql->str );
+	if( result == NULL ) {
+		PERR( "Error executing SQL %s\n", dbi_stmt->sql->str );
+		return 0;
+	}
+	return dbi_result_get_numrows_affected( result );
+}
+
+static GncSqlStatement*
+conn_create_statement_from_sql( GncSqlConnection* conn, const gchar* sql )
+{
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+
+	return create_dbi_statement( sql );
+}
+
+static GValue*
+create_gvalue_from_string( gchar* s )
+{
+	GValue* s_gval;
+
+	s_gval = g_new0( GValue, 1 );
+	g_value_init( s_gval, G_TYPE_STRING );
+	g_value_take_string( s_gval, s );
+
+	return s_gval;
+}
+
+static gboolean
+conn_does_table_exist( GncSqlConnection* conn, const gchar* table_name )
+{
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+	gint nTables;
+	dbi_result tables;
+	const gchar* dbname;
+
+	g_return_val_if_fail( conn != NULL, FALSE );
+	g_return_val_if_fail( table_name != NULL, FALSE );
+
+	dbname = dbi_conn_get_option( dbi_conn->conn, "dbname" );
+	tables = dbi_conn_get_table_list( dbi_conn->conn, dbname, table_name );
+	nTables = dbi_result_get_numrows( tables );
+
+	if( nTables == 1 ) {
+		return TRUE;
+	} else {
+		return FALSE;
+	}
+}
+
+static void
+conn_begin_transaction( GncSqlConnection* conn )
+{
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+}
+
+static void
+conn_rollback_transaction( GncSqlConnection* conn )
+{
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+}
+
+static void
+conn_commit_transaction( GncSqlConnection* conn )
+{
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+}
+
+static const gchar*
+conn_get_column_type_name( GncSqlConnection* conn, GType type, gint size )
+{
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+
+	switch( type ) {
+		case G_TYPE_INT:
+		case G_TYPE_INT64:
+			return "integer";
+			break;
+		case G_TYPE_DOUBLE:
+			return "double";
+			break;
+		case G_TYPE_STRING:
+			return "string";
+			break;
+		default:
+			PERR( "Unknown GType: %d\n", type );
+			return "";
+	}
+}
+
+static void
+add_table_column( GString* ddl, const GncSqlColumnInfo* info )
+{
+    gchar* buf;
+	GError* error = NULL;
+	gboolean ok;
+
+	g_return_if_fail( ddl != NULL );
+	g_return_if_fail( info != NULL );
+
+	g_string_append_printf( ddl, "%s %s", info->name, info->type_name );
+    if( info->size != 0 ) {
+		g_string_append_printf( ddl, "(%d)", info->size );
+    }
+	if( info->is_primary_key ) {
+		g_string_append( ddl, " PRIMARY KEY" );
+	}
+	if( !info->null_allowed ) {
+		g_string_append( ddl, " NOT NULL" );
+	}
+}
+
+static void
+conn_create_table( GncSqlConnection* conn, const gchar* table_name,
+				const GList* col_info_list )
+{
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+	GString* ddl;
+	const GList* list_node;
+	guint col_num;
+
+	g_return_if_fail( conn != NULL );
+	g_return_if_fail( table_name != NULL );
+	g_return_if_fail( col_info_list != NULL );
+    
+	ddl = g_string_new( "" );
+	g_string_printf( ddl, "CREATE TABLE %s (", table_name );
+    for( list_node = col_info_list, col_num = 0; list_node != NULL;
+				list_node = list_node->next, col_num++ ) {
+		GncSqlColumnInfo* info = (GncSqlColumnInfo*)(list_node->data);
+
+		if( col_num != 0 ) {
+			g_string_append( ddl, ", " );
+		}
+		add_table_column( ddl, info );
+    }
+	g_string_append( ddl, ")" );
+        
+	dbi_conn_query( dbi_conn->conn, ddl->str );
+	g_string_free( ddl, TRUE );
+}
+
+static void
+conn_create_index( GncSqlConnection* conn, const gchar* index_name,
+					const gchar* table_name, const col_cvt_t* col_table )
+{
+#if 0
+    GdaServerOperation *op;
+    GdaServerProvider *server;
+	GdaConnection* cnn;
+	GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
+	GError* error = NULL;
+    
+    g_return_if_fail( conn != NULL );
+	g_return_if_fail( index_name != NULL );
+	g_return_if_fail( table_name != NULL );
+	g_return_if_fail( col_table != NULL );
+    
+	cnn = gda_conn->conn;
+	g_return_if_fail( cnn != NULL );
+    g_return_if_fail( GDA_IS_CONNECTION(cnn) );
+    g_return_if_fail( gda_connection_is_opened(cnn) );
+
+    server = gda_conn->server;
+	g_return_if_fail( server != NULL );
+    
+    op = gda_server_provider_create_operation( server, cnn, 
+                           GDA_SERVER_OPERATION_CREATE_INDEX, NULL, &error );
+    if( error != NULL ) {
+		PERR( "gda_server_provider_create_operation(): %s\n", error->message );
+	}
+    if( op != NULL && GDA_IS_SERVER_OPERATION(op) ) {
+        gint col;
+		gboolean ok;
+        
+		ok = gda_server_operation_set_value_at( op, index_name, &error,
+								"/INDEX_DEF_P/INDEX_NAME" );
+    	if( error != NULL ) {
+			PERR( "set INDEX_NAME: %s\n", error->message );
+		}
+		if( !ok ) return;
+		ok = gda_server_operation_set_value_at( op, "", &error,
+								"/INDEX_DEF_P/INDEX_TYPE" );
+    	if( error != NULL ) {
+			PERR( "set INDEX_TYPE: %s\n", error->message );
+		}
+		if( !ok ) return;
+		ok = gda_server_operation_set_value_at( op, "TRUE", &error,
+								"/INDEX_DEF_P/INDEX_IFNOTEXISTS" );
+    	if( error != NULL ) {
+			PERR( "set INDEX_IFNOTEXISTS: %s\n", error->message );
+		}
+		if( !ok ) return;
+		ok = gda_server_operation_set_value_at( op, table_name, &error,
+								"/INDEX_DEF_P/INDEX_ON_TABLE" );
+    	if( error != NULL ) {
+			PERR( "set INDEX_ON_TABLE: %s\n", error->message );
+		}
+		if( !ok ) return;
+
+        for( col = 0; col_table[col].col_name != NULL; col++ ) {
+			guint item;
+
+			if( col != 0 ) {
+				item = gda_server_operation_add_item_to_sequence( op, "/INDEX_FIELDS_S" );
+				g_assert( item == col );
+			}
+			ok = gda_server_operation_set_value_at( op, col_table->col_name, &error,
+													"/INDEX_FIELDS_S/%d/INDEX_FIELD", col );
+			if( error != NULL ) {
+				PERR( "set INDEX_FIELD %s: %s\n", col_table->col_name, error->message );
+			}
+			if( !ok ) break;
+        }
+        
+        if( !gda_server_provider_perform_operation( server, cnn, op, &error ) ) {
+            g_object_unref( op );
+            return;
+        }
+
+        g_object_unref( op );
+    }
+#endif
+}
+
+static GncSqlConnection*
+create_dbi_connection( dbi_conn conn )
+{
+	GncDbiSqlConnection* dbi_conn;
+
+	dbi_conn = g_new0( GncDbiSqlConnection, 1 );
+	dbi_conn->base.dispose = conn_dispose;
+	dbi_conn->base.executeSelectStatement = conn_execute_select_statement;
+	dbi_conn->base.executeNonSelectStatement = conn_execute_nonselect_statement;
+	dbi_conn->base.createStatementFromSql = conn_create_statement_from_sql;
+	dbi_conn->base.doesTableExist = conn_does_table_exist;
+	dbi_conn->base.beginTransaction = conn_begin_transaction;
+	dbi_conn->base.rollbackTransaction = conn_rollback_transaction;
+	dbi_conn->base.commitTransaction = conn_commit_transaction;
+	dbi_conn->base.getColumnTypeName = conn_get_column_type_name;
+	dbi_conn->base.createTable = conn_create_table;
+	dbi_conn->base.createIndex = conn_create_index;
+	dbi_conn->conn = conn;
+
+	return (GncSqlConnection*)dbi_conn;
+}
+
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/dbi/gnc-backend-dbi.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/dbi/gnc-backend-dbi.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/dbi/gnc-backend-dbi.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,55 @@
+/********************************************************************
+ * gnc-backend-dbi.h: load and save data to SQL via libdbi          *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-backend-dbi.h
+ *  @brief load and save data to SQL via libdbi
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database via libdbi
+ */
+
+#ifndef GNC_BACKEND_DBI_H_
+#define GNC_BACKEND_DBI_H_
+
+#include <gmodule.h>
+
+G_MODULE_EXPORT void
+qof_backend_module_init(void);
+
+struct GncDbiBackend_struct
+{
+  GncSqlBackend sql_be;
+
+  dbi_conn conn;
+
+  QofBook *primary_book;	/* The primary, main open book */
+  gboolean	loading;		/* We are performing an initial load */
+  gboolean  in_query;
+  gboolean  supports_transactions;
+  gboolean  is_pristine_db;	// Are we saving to a new pristine db?
+
+  gint obj_total;			// Total # of objects (for percentage calculation)
+  gint operations_done;		// Number of operations (save/load) done
+//  GHashTable* versions;		// Version number for each table
+};
+typedef struct GncDbiBackend_struct GncDbiBackend;
+
+#endif /* GNC_BACKEND_DBI_H_ */

Added: gnucash/branches/gda-dev2/src/backend/dbi/gncmod-backend-dbi.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/dbi/gncmod-backend-dbi.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/dbi/gncmod-backend-dbi.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,63 @@
+/*********************************************************************
+ * gncmod-backend-dbi.c
+ * module definition/initialization for the dbi backend module
+ *
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *********************************************************************/
+
+#include <stdio.h>
+#include <gmodule.h>
+/* #include <glib-gobject.h> */
+
+#include "gnc-module.h"
+#include "gnc-module-api.h"
+
+/* version of the gnc module system interface we require */
+int gnc_module_system_interface = 0;
+
+/* module versioning uses libtool semantics. */
+int gnc_module_current  = 0;
+int gnc_module_revision = 0;
+int gnc_module_age      = 0;
+
+static GNCModule engine;
+
+gchar *
+gnc_module_path(void)
+{
+  return g_strdup("gnucash/backend/dbi");
+}
+
+gchar *
+gnc_module_description(void)
+{
+  return g_strdup("The DBI/SQL backend for GnuCash");
+}
+
+int
+gnc_module_init(int refcount)
+{
+  engine = gnc_module_load("gnucash/engine", 0);
+  if(!engine) return FALSE;
+
+  /* Need to initialize g-type engine for gconf */
+  if (refcount == 0) {
+    g_type_init();
+  }
+
+  return TRUE;
+}
+
+int
+gnc_module_end(int refcount)
+{
+  int unload = TRUE;
+
+  if (engine)
+    unload = gnc_module_unload(engine);
+
+  if (refcount == 0)
+    engine = NULL;
+
+  return unload;
+}


Property changes on: gnucash/branches/gda-dev2/src/backend/dbi/test
___________________________________________________________________
Name: svn:ignore
   + Makefile
Makefile.in
.deps



Added: gnucash/branches/gda-dev2/src/backend/dbi/test/Makefile.am
===================================================================
--- gnucash/branches/gda-dev2/src/backend/dbi/test/Makefile.am	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/dbi/test/Makefile.am	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,48 @@
+SUBDIRS = .
+
+TESTS = \
+  test-load-backend
+
+GNC_TEST_DEPS := \
+  --gnc-module-dir ${top_builddir}/src/engine \
+  --guile-load-dir ${top_builddir}/src/engine \
+  --library-dir    ${top_builddir}/lib/libqof/qof \
+  --library-dir    ${top_builddir}/src/core-utils \
+  --library-dir    ${top_builddir}/src/gnc-module \
+  --library-dir    ${top_builddir}/src/engine \
+  --library-dir    ${top_builddir}/src/backend/gda
+
+TESTS_ENVIRONMENT := \
+  GNC_ACCOUNT_PATH=${top_srcdir}/accounts/C \
+  SRCDIR=${srcdir} \
+  $(shell ${top_srcdir}/src/gnc-test-env --no-exports ${GNC_TEST_DEPS})
+
+check_PROGRAMS = \
+  test-load-backend
+
+#noinst_HEADERS = test-file-stuff.h
+
+LDADD = ${top_builddir}/src/test-core/libgncmod-test.la \
+        ${top_builddir}/src/gnc-module/libgnc-module.la \
+        ${top_builddir}/src/engine/libgncmod-engine.la \
+        ${top_builddir}/src/engine/test-core/libgncmod-test-engine.la \
+        ${top_builddir}/src/core-utils/libgnc-core-utils.la \
+        ${QOF_LIBS} \
+        ${top_builddir}/lib/libc/libc-missing.la
+
+AM_CFLAGS = \
+  -I${top_srcdir}/lib/libc \
+  -I${top_srcdir}/src \
+  -I${top_srcdir}/src/core-utils \
+  -I${top_srcdir}/src/gnc-module \
+  -I${top_srcdir}/src/test-core \
+  -I${top_srcdir}/src/engine \
+  -I${top_srcdir}/src/engine/test-core \
+  -I${top_srcdir}/src/backend/gda \
+  -I${top_srcdir}/src/backend/qsf \
+  ${GLIB_CFLAGS} \
+  ${QOF_CFLAGS} \
+  ${GUILE_INCS} \
+  ${GCONF_CFLAGS}
+
+INCLUDES = -DG_LOG_DOMAIN=\"gnc.backend.gda\"

Added: gnucash/branches/gda-dev2/src/backend/dbi/test/test-load-backend.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/dbi/test/test-load-backend.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/dbi/test/test-load-backend.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,45 @@
+/***************************************************************************
+ *            test-load-backend.c
+ *
+ *  Replaces the guile version to test the GModule file backend loading.
+ *
+ *  Sun Oct  9 18:58:47 2005
+ *  Copyright  2005  Neil Williams
+ *  linux at codehelp.co.uk
+ ****************************************************************************/
+
+/*
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ *  02110-1301, USA.
+ */
+ 
+#include "config.h"
+#include "qof.h"
+#include "cashobjects.h"
+#include "test-stuff.h"
+
+#define GNC_LIB_NAME "gncmod-backend-gda"
+
+int main (int argc, char ** argv)
+{
+    qof_init();
+    cashobjects_register();
+    do_test(
+        qof_load_backend_library ("../.libs/", GNC_LIB_NAME),
+        " loading gnc-backend-gda GModule failed");
+    print_test_results();
+    qof_close();
+    exit(get_rv());
+}

Modified: gnucash/branches/gda-dev2/src/backend/gda/Makefile.am
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/Makefile.am	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/Makefile.am	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,7 +1,6 @@
 SUBDIRS = . test
 
 # Now a shared library AND a GModule
-lib_LTLIBRARIES = libgnc-backend-gda-utils.la
 pkglib_LTLIBRARIES = libgncmod-backend-gda.la
 
 
@@ -12,53 +11,24 @@
   -I${top_srcdir}/src/engine \
   -I${top_srcdir}/src/core-utils\
   -I${top_srcdir}/lib/libc\
+  -I${top_srcdir}/src/backend/sql \
   ${QOF_CFLAGS} \
   ${GLIB_CFLAGS} \
   ${LIBGDA_CFLAGS} \
   ${GCONF_CFLAGS} \
   ${WARN_CFLAGS}
 
-libgnc_backend_gda_utils_la_SOURCES = \
-  gnc-backend-util-gda.c \
-  gnc-account-gda.c \
-  gnc-book-gda.c \
-  gnc-budget-gda.c \
-  gnc-commodity-gda.c \
-  gnc-lots-gda.c \
-  gnc-price-gda.c \
-  gnc-recurrence-gda.c \
-  gnc-schedxaction-gda.c \
-  gnc-slots-gda.c \
-  gnc-transaction-gda.c
-
 libgncmod_backend_gda_la_SOURCES = \
   gnc-backend-gda.c
 
 noinst_HEADERS = \
-  gnc-account-gda.h \
-  gnc-backend-gda.h \
-  gnc-backend-util-gda.h \
-  gnc-book-gda.h \
-  gnc-budget-gda.h \
-  gnc-commodity-gda.h \
-  gnc-lots-gda.h \
-  gnc-price-gda.h \
-  gnc-recurrence-gda.h \
-  gnc-schedxaction-gda.h \
-  gnc-slots-gda.h \
-  gnc-transaction-gda.h
+  gnc-backend-gda.h
 
-libgnc_backend_gda_utils_la_LIBADD = \
-   ${GLIB_LIBS} ${GCONF_LIBS} \
-   ${top_builddir}/src/engine/libgncmod-engine.la \
-   ${LIBGDA_LIBS} \
-   ${QOF_LIBS}
-
 libgncmod_backend_gda_la_LDFLAGS = -module -avoid-version
 libgncmod_backend_gda_la_LIBADD = \
    ${GLIB_LIBS} ${GCONF_LIBS} \
    ${top_builddir}/src/engine/libgncmod-engine.la \
-   libgnc-backend-gda-utils.la \
+   ${top_builddir}/src/backend/sql/libgncmod-backend-sql.la \
    ${LIBGDA_LIBS} \
    ${QOF_LIBS}
 

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-account-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-account-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-account-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,405 +0,0 @@
-/********************************************************************
- * gnc-account-gda.c: load and save data to SQL via libgda          *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-account-gda.c
- *  @brief load and save data to SQL 
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-#include "Account.h"
-#include "AccountP.h"
-#include "gnc-commodity.h"
-
-#include "gnc-backend-util-gda.h"
-
-#include "gnc-account-gda.h"
-#include "gnc-commodity-gda.h"
-#include "gnc-slots-gda.h"
-#include "gnc-transaction-gda.h"
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TABLE_NAME "accounts"
-#define TABLE_VERSION 1
-
-static gpointer get_parent( gpointer pObject, const QofParam* );
-static void set_parent( gpointer pObject, gpointer pValue );
-static void set_parent_guid( gpointer pObject, gpointer pValue );
-
-#define ACCOUNT_MAX_NAME_LEN /*2048*/20
-#define ACCOUNT_MAX_TYPE_LEN 2048
-#define ACCOUNT_MAX_CODE_LEN 2048
-#define ACCOUNT_MAX_DESCRIPTION_LEN 2048
-
-static const col_cvt_t col_table[] =
-{
-    { "guid",           CT_GUID,         0,                           COL_NNUL|COL_PKEY, "guid" },
-    { "name",           CT_STRING,       ACCOUNT_MAX_NAME_LEN,        COL_NNUL,          "name" },
-    { "account_type",   CT_STRING,       ACCOUNT_MAX_TYPE_LEN,        COL_NNUL,          NULL, ACCOUNT_TYPE_ },
-    { "commodity_guid", CT_COMMODITYREF, 0,                           COL_NNUL,          "commodity" },
-    { "parent_guid",    CT_GUID,         0,                           0,                 NULL, NULL, get_parent, set_parent },
-    { "code",           CT_STRING,       ACCOUNT_MAX_CODE_LEN,        0,                 "code" },
-    { "description",    CT_STRING,       ACCOUNT_MAX_DESCRIPTION_LEN, 0,                 "description" },
-    { NULL }
-};
-static col_cvt_t parent_col_table[] =
-{
-    { "parent_guid", CT_GUID, 0, 0, NULL, NULL, NULL, set_parent_guid },
-    { NULL }
-};
-
-typedef struct {
-	Account* pAccount;
-	GUID guid;
-} account_parent_guid_struct;
-
-/* ================================================================= */
-
-static gpointer
-get_parent( gpointer pObject, const QofParam* param )
-{
-    const Account* pAccount;
-    const Account* pParent;
-    const GUID* parent_guid;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( GNC_IS_ACCOUNT(pObject), NULL );
-
-    pAccount = GNC_ACCOUNT(pObject);
-    pParent = gnc_account_get_parent( pAccount );
-    if( pParent == NULL ) {
-        parent_guid = NULL;
-    } else {
-        parent_guid = qof_instance_get_guid( QOF_INSTANCE(pParent) );
-    }
-
-    return (gpointer)parent_guid;
-}
-
-static void 
-set_parent( gpointer pObject, gpointer pValue )
-{
-    Account* pAccount;
-    QofBook* pBook;
-    GUID* guid = (GUID*)pValue;
-    Account* pParent;
-    
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( GNC_IS_ACCOUNT(pObject) );
-
-    pAccount = GNC_ACCOUNT(pObject);
-    pBook = qof_instance_get_book( QOF_INSTANCE(pAccount) );
-    if( guid != NULL ) {
-        pParent = xaccAccountLookup( guid, pBook );
-        if( pParent != NULL ) {
-            gnc_account_append_child( pParent, pAccount );
-        }
-    }
-}
-
-static void
-set_parent_guid( gpointer pObject, gpointer pValue )
-{
-	account_parent_guid_struct* s = (account_parent_guid_struct*)pObject;
-    GUID* guid = (GUID*)pValue;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( pValue != NULL );
-
-	s->guid = *guid;
-}
-
-static void
-load_balances( GncGdaBackend* be, Account* pAccount )
-{
-    gnc_numeric start_balance;
-	gnc_numeric cleared_balance;
-	gnc_numeric reconciled_balance;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pAccount != NULL );
-
-	gnc_gda_get_account_balances( be, pAccount, &start_balance, &cleared_balance, &reconciled_balance );
-
-    g_object_set( pAccount,
-				"end-balance", &start_balance,
-                "end-cleared-balance", &cleared_balance,
-                "end-reconciled-balance", &reconciled_balance,
-                NULL);
-}
-
-static void
-load_account_balances_for_list( GncGdaBackend* be, GList* list )
-{
-	GList* balance_list;
-
-	g_return_if_fail( be != NULL );
-
-	if( list == NULL ) return;
-
-	balance_list = gnc_gda_get_account_balances_for_list( be, list );
-	for( ; balance_list != NULL; balance_list = balance_list->next ) {
-		acct_balances_t* acct_balances = (acct_balances_t*)balance_list->data;
-
-    	g_object_set( acct_balances->acct,
-					"end-balance", &acct_balances->start_balance,
-                	"end-cleared-balance", &acct_balances->cleared_balance,
-                	"end-reconciled-balance", &acct_balances->reconciled_balance,
-                	NULL);
-	}
-}
-
-static void
-load_single_account( GncGdaBackend* be, GdaDataModel* pModel, int row, GList** pList,
-				GList** l_accounts_needing_parents )
-{
-    const GUID* guid;
-    GUID acc_guid;
-	Account* pAccount;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( l_accounts_needing_parents != NULL );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    acc_guid = *guid;
-
-    pAccount = xaccAccountLookup( &acc_guid, be->primary_book );
-    if( pAccount == NULL ) {
-        pAccount = xaccMallocAccount( be->primary_book );
-    }
-	xaccAccountBeginEdit( pAccount );
-    gnc_gda_load_object( be, pModel, row, GNC_ID_ACCOUNT, pAccount, col_table );
-	*pList = g_list_append( *pList, pAccount );
-//    gnc_gda_slots_load( be, QOF_INSTANCE(pAccount) );
-//    load_balances( be, pAccount );
-
-	xaccAccountCommitEdit( pAccount );
-    qof_instance_mark_clean( QOF_INSTANCE(pAccount) );
-
-	/* If we don't have a parent, it might be because the parent account hasn't
-	   been loaded yet.  Remember the account and its parent guid for later. */
-	if( gnc_account_get_parent( pAccount ) == NULL ) {
-		account_parent_guid_struct* s = g_slice_new( account_parent_guid_struct );
-		s->pAccount = pAccount;
-		gnc_gda_load_object( be, pModel, row, GNC_ID_ACCOUNT, s, parent_col_table );
-		*l_accounts_needing_parents = g_list_prepend( *l_accounts_needing_parents, s );
-	}
-}
-
-static void
-load_all_accounts( GncGdaBackend* be )
-{
-    static GdaQuery* query = NULL;
-    GdaObject* ret;
-    QofBook* pBook;
-    gnc_commodity_table* pTable;
-
-	g_return_if_fail( be != NULL );
-
-    pBook = be->primary_book;
-    pTable = gnc_commodity_table_get_table( pBook );
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-        Account* parent;
-		GList* l_accounts_needing_parents = NULL;
-		GList* list = NULL;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_account( be, pModel, r, &list, &l_accounts_needing_parents );
-        }
-
-		if( list != NULL ) {
-//			load_account_balances_for_list( be, list );
-			gnc_gda_slots_load_for_list( be, list );
-		}
-
-		/* While there are items on the list of accounts needing parents,
-		   try to see if the parent has now been loaded.  Theory says that if
-		   items are removed from the front and added to the back if the
-		   parent is still not available, then eventually, the list will
-		   shrink to size 0. */
-		if( l_accounts_needing_parents != NULL ) {
-			gboolean progress_made = TRUE;
-
-			Account* pParent;
-			GList* elem;
-			
-			while( progress_made ) {
-				progress_made = FALSE;
-				for( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) ) {
-					account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data;
-					const gchar* name = xaccAccountGetName( s->pAccount );
-    				pParent = xaccAccountLookup( &s->guid, be->primary_book );
-					if( pParent != NULL ) {
-						gnc_account_append_child( pParent, s->pAccount );
-						l_accounts_needing_parents = g_list_delete_link( l_accounts_needing_parents, elem );
-						progress_made = TRUE;
-					}
-				}
-			}
-
-			/* Any accounts left over must be parented by the root account */
-			for( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) ) {
-				account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data;
-                Account* root;
-                root = gnc_book_get_root_account( pBook );
-                if( root == NULL ) {
-                    root = gnc_account_create_root( pBook );
-                }
-                gnc_account_append_child( root, s->pAccount ); 
-			}
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_account_tables( GncGdaBackend* be )
-{
-	gint version;
-
-	g_return_if_fail( be != NULL );
-
-	version = gnc_gda_get_table_version( be, TABLE_NAME );
-    if( version == 0 ) {
-    	GError* error = NULL;
-
-        gnc_gda_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
-        if( error != NULL ) {
-            PERR( "Error creating table: %s\n", error->message );
-        }
-    }
-}
-
-/* ================================================================= */
-void
-gnc_gda_save_account( QofInstance* inst, GncGdaBackend* be )
-{
-    Account* pAcc = GNC_ACCOUNT(inst);
-    const GUID* guid;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_ACCOUNT(inst) );
-
-    // If there is no commodity yet, this might be because a new account name has been entered directly
-    // into the register and an account window will be opened.  The account info is not complete yet,
-    // but the name has been set, triggering this commit
-    if( xaccAccountGetCommodity( pAcc ) != NULL ) {
-		gint op;
-
-        // Ensure the commodity is in the db
-        gnc_gda_save_commodity( be, xaccAccountGetCommodity( pAcc ) );
-
-		if( qof_instance_get_destroying( inst ) ) {
-			op = OP_DB_DELETE;
-		} else if( be->is_pristine_db ) {
-			op = OP_DB_ADD;
-		} else {
-			op = OP_DB_ADD_OR_UPDATE;
-		}
-
-        (void)gnc_gda_do_db_operation( be, op, TABLE_NAME, GNC_ID_ACCOUNT, pAcc, col_table );
-
-        // Now, commit or delete any slots
-        guid = qof_instance_get_guid( inst );
-        if( !qof_instance_get_destroying(inst) ) {
-            gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-        } else {
-            gnc_gda_slots_delete( be, guid );
-        }
-    }
-}
-
-/* ================================================================= */
-static void
-load_account_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GUID guid;
-    const GUID* pGuid;
-	Account* account = NULL;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        pGuid = NULL;
-    } else {
-        string_to_guid( g_value_get_string( val ), &guid );
-        pGuid = &guid;
-    }
-	if( pGuid != NULL ) {
-		account = xaccAccountLookup( pGuid, be->primary_book );
-	}
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, account, NULL );
-    } else {
-		(*setter)( pObject, (const gpointer)account );
-    }
-}
-
-static col_type_handler_t account_guid_handler =
-        { load_account_guid, gnc_gda_create_objectref_guid_col,
-            gnc_gda_get_gvalue_objectref_guid_for_query, gnc_gda_get_gvalue_objectref_guid_cond };
-/* ================================================================= */
-void
-gnc_gda_init_account_handler( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_ACCOUNT,
-        gnc_gda_save_account,		/* commit */
-        load_all_accounts,			/* initial_load */
-        create_account_tables		/* create_tables */
-    };
-
-    qof_object_register_backend( GNC_ID_ACCOUNT, GNC_GDA_BACKEND, &be_data );
-
-	gnc_gda_register_col_type_handler( CT_ACCOUNTREF, &account_guid_handler );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-account-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-account-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-account-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,38 +0,0 @@
-/********************************************************************
- * gnc-account-gda.h: load and save data to SQL via libgda          *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-account-gda.h
- *  @brief load and save accounts data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_ACCOUNT_GDA_H_
-#define GNC_ACCOUNT_GDA_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-void gnc_gda_init_account_handler( void );
-void gnc_gda_save_account( QofInstance* inst, GncGdaBackend* be );
-
-#endif /* GNC_ACCOUNT_GDA_H_ */

Modified: gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -20,7 +20,7 @@
 \********************************************************************/
 /** @file gnc-backend-gda.c
  *  @brief load and save data to SQL 
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
  *
  * This file implements the top-level QofBackend API for saving/
  * restoring data to/from an SQL db using libgda
@@ -34,7 +34,12 @@
 #include <glib/gstdio.h>
 
 #include <libgda/libgda.h>
+#include <libgda/gda-easy.h>
+#include <sql-parser/gda-sql-parser.h>
 
+#include "gnc-backend-util-sql.h"
+#include "gnc-backend-sql.h"
+
 #include "qof.h"
 #include "qofquery-p.h"
 #include "qofquerycore-p.h"
@@ -44,65 +49,31 @@
 #include "SX-book.h"
 #include "Recurrence.h"
 
-#include "gnc-backend-util-gda.h"
 #include "gnc-gconf-utils.h"
 
-#include "gnc-account-gda.h"
-#include "gnc-book-gda.h"
-#include "gnc-budget-gda.h"
-#include "gnc-commodity-gda.h"
-#include "gnc-lots-gda.h"
-#include "gnc-price-gda.h"
-#include "gnc-pricedb.h"
-#include "gnc-recurrence-gda.h"
-#include "gnc-schedxaction-gda.h"
-#include "gnc-slots-gda.h"
-#include "gnc-transaction-gda.h"
-
 #include "gnc-backend-gda.h"
 
-static const gchar* convert_search_obj( QofIdType objType );
-static void gnc_gda_init_object_handlers( void );
-static void add_table_column( GdaServerProvider* server, GdaConnection* cnn,
-            xmlNodePtr array_data, const gchar* arg, const gchar* dbms_type,
-            gint size, gint flags );
-static void update_save_progress( GncGdaBackend* be );
-
 #define TRANSACTION_NAME "trans"
 
-typedef struct {
-    QofIdType searchObj;
-    gpointer pCompiledQuery;
-} gnc_gda_query_info;
-
-/* callback structure */
-typedef struct {
-    gboolean ok;
-    GncGdaBackend* be;
-    QofInstance* inst;
-    QofQuery* pQuery;
-    gpointer pCompiledQuery;
-    gnc_gda_query_info* pQueryInfo;
-} gda_backend;
-
 static QofLogModule log_module = G_LOG_DOMAIN;
 
 #define SQLITE_PROVIDER_NAME "SQLite"
 #define URI_PREFIX "gda://"
+static GncSqlConnection* create_gda_connection( GdaConnection* conn );
 
 /* ================================================================= */
 
 static void
 create_tables_cb( const gchar* type, gpointer data_p, gpointer be_p )
 {
-    GncGdaDataType_t* pData = data_p;
+    GncSqlDataType_t* pData = data_p;
     GncGdaBackend* be = be_p;
 
     g_return_if_fail( type != NULL && data_p != NULL && be_p != NULL );
-    g_return_if_fail( pData->version == GNC_GDA_BACKEND_VERSION );
+    g_return_if_fail( pData->version == GNC_SQL_BACKEND_VERSION );
 
     if( pData->create_tables != NULL ) {
-        (pData->create_tables)( be );
+        (pData->create_tables)( &be->sql_be );
     }
 }
 
@@ -115,10 +86,12 @@
 {
 	gchar* uri_id;
 	gchar* book_info;
-	gchar* provider;
-	GList* provider_list;
+	const gchar* provider;
+	GdaDataModel* providers;
+	gint numProviders;
 	gboolean provider_found;
 	gchar* dsn;
+	gint i;
 
 	*pProvider = NULL;
 	*pDsn = NULL;
@@ -161,15 +134,17 @@
 
 	// Get a list of all of the providers.  If the requested provider is on the list, use it.
 	// Note that we need a case insensitive comparison here
-	provider_list = gda_config_get_provider_list();
+	providers = gda_config_list_providers();
+	numProviders = gda_data_model_get_n_rows( providers );
 
 	provider_found = FALSE;
-	for( ; provider_list != NULL; provider_list = provider_list->next ) {
-		GdaProviderInfo* provider_info = (GdaProviderInfo*)provider_list->data;
+	for( i = 0; i < numProviders; i++ ) {
+		const GValue* providerValue = gda_data_model_get_value_at( providers, 0, i );
+		const gchar* s = g_value_get_string( providerValue );
 
-		if( provider_info != NULL && g_ascii_strcasecmp( provider_info->id, provider ) == 0 ) {
+		if( g_ascii_strcasecmp( s, provider ) == 0 ) {
 			provider_found = TRUE;
-			provider = provider_info->id;
+			provider = s;
 			break;
 		}
 	}
@@ -234,7 +209,6 @@
 
     ENTER (" ");
 
-    be->pClient = gda_client_new();
 	be->pConnection = NULL;
 
 	// FIXME: better username/password handling
@@ -250,38 +224,30 @@
 	}
 
 	if( provider == NULL ) {
-	    be->pConnection = gda_client_open_connection( be->pClient,
-													dsn,
-													username, password,
-													0,
-													&error );
+		be->pConnection = gda_connection_open_from_dsn( dsn, "", 0, &error );
 	} else {
-		be->pConnection = gda_client_open_connection_from_string( be->pClient,
-									provider, 
-									dsn,
-									username, password,
-									0,
-									&error );
+		be->pConnection = gda_connection_open_from_string(
+											provider,
+											dsn,
+											"",
+											0,
+											&error );
 
 		if( be->pConnection == NULL ) {
-			GdaServerOperation* op = gda_client_prepare_create_database(
-													be->pClient,
+			GdaServerOperation* op = gda_prepare_create_database(
+													provider,
 													dsn,
-													provider );
+													&error );
 			if( op != NULL ) {
 				gboolean isOK;
-				isOK = gda_client_perform_create_database(
-													be->pClient,
-													op,
-													&error );
+				isOK = gda_perform_create_database( op, &error );
 				if( isOK ) {
-					be->pConnection = gda_client_open_connection_from_string(
-													be->pClient,
-													provider, 
-													dsn,
-													username, password,
-													0,
-													&error );
+					be->pConnection = gda_connection_open_from_string(
+											provider,
+											dsn,
+											"",
+											0,
+											&error );
 				}
 			}
 		}
@@ -303,21 +269,19 @@
 	be->supports_transactions = gda_connection_supports_feature( be->pConnection, GDA_CONNECTION_FEATURE_TRANSACTIONS );
 
     // Set up the dictionary
-    be->pDict = gda_dict_new();
-    gda_dict_set_connection( be->pDict, be->pConnection );
-    gda_dict_update_dbms_meta_data( be->pDict, 0, NULL, &error );
-	gda_dict_extend_with_functions( be->pDict );
-    if( error != NULL ) {
-        PERR( "gda_dict_update_dbms_meta_data() error: %s\n", error->message );
-    }
+	gda_connection_update_meta_store( be->pConnection, NULL, &error );
 
+	be->parser = gda_sql_parser_new();
+
+	be->sql_be.conn = create_gda_connection( be->pConnection );
+
     LEAVE (" ");
 }
 
 /* ================================================================= */
 
 static void
-gnc_gda_session_end(QofBackend *be_start)
+gnc_gda_session_end( QofBackend *be_start )
 {
     GncGdaBackend *be = (GncGdaBackend*)be_start;
 
@@ -325,25 +289,24 @@
 
     ENTER (" ");
 
-    if( be->pDict != NULL ) {
-        g_object_unref( G_OBJECT(be->pDict) );
-        be->pDict = NULL;
+	if( be->parser != NULL ) {
+		g_object_unref( be->parser );
+		be->parser = NULL;
+	}
+    if( be->pConnection != NULL ) {
+		if( gda_connection_is_opened( be->pConnection ) ) {
+        	gda_connection_close( be->pConnection );
+		}
+		g_object_unref( be->pConnection );
+    	be->pConnection = NULL;
     }
-    if( be->pConnection != NULL && gda_connection_is_opened( be->pConnection ) ) {
-        gda_connection_close( be->pConnection );
-    }
-    be->pConnection = NULL;
-    if( be->pClient != NULL ) {
-        g_object_unref( G_OBJECT(be->pClient ) );
-        be->pClient = NULL;
-    }
-	_finalize_version_info( be );
+	gnc_sql_finalize_version_info( &be->sql_be );
 
     LEAVE (" ");
 }
 
 static void
-gnc_gda_destroy_backend(QofBackend *be)
+gnc_gda_destroy_backend( QofBackend *be )
 {
 	g_return_if_fail( be != NULL );
 
@@ -352,39 +315,16 @@
 
 /* ================================================================= */
 
-static const gchar* fixed_load_order[] =
-{ GNC_ID_BOOK, GNC_ID_COMMODITY, GNC_ID_ACCOUNT, NULL };
-
 static void
-initial_load_cb( const gchar* type, gpointer data_p, gpointer be_p )
+gnc_gda_load( QofBackend* qbe, QofBook *book )
 {
-    GncGdaDataType_t* pData = data_p;
-    GncGdaBackend* be = be_p;
+    GncGdaBackend *be = (GncGdaBackend*)qbe;
+    GncSqlDataType_t* pData;
 	int i;
-
-    g_return_if_fail( type != NULL && data_p != NULL && be_p != NULL );
-    g_return_if_fail( pData->version == GNC_GDA_BACKEND_VERSION );
-
-	// Don't need to load anything if it has already been loaded with the fixed order
-	for( i = 0; fixed_load_order[i] != NULL; i++ ) {
-    	if( g_ascii_strcasecmp( type, fixed_load_order[i] ) == 0 ) return;
-	}
-
-    if( pData->initial_load != NULL ) {
-        (pData->initial_load)( be );
-    }
-}
-
-static void
-gnc_gda_load(QofBackend* be_start, QofBook *book)
-{
-    GncGdaBackend *be = (GncGdaBackend*)be_start;
-    GncGdaDataType_t* pData;
-	int i;
 	Account* root;
 	GError* error = NULL;
 
-	g_return_if_fail( be_start != NULL );
+	g_return_if_fail( qbe != NULL );
 	g_return_if_fail( book != NULL );
 
     ENTER( "be=%p, book=%p", be, book );
@@ -393,200 +333,18 @@
     be->primary_book = book;
 
 	// Set up table version information
-	_init_version_info( be );
+	gnc_sql_init_version_info( &be->sql_be );
 
     // Call all object backends to create any required tables
-    qof_object_foreach_backend( GNC_GDA_BACKEND, create_tables_cb, be );
+    qof_object_foreach_backend( GNC_SQL_BACKEND, create_tables_cb, be );
 
-    // Update the dictionary because new tables may exist
-    gda_dict_update_dbms_meta_data( be->pDict, 0, NULL, &error );
-    if( error != NULL ) {
-        PERR( "gda_dict_update_dbms_meta_data() error: %s\n", error->message );
-    }
+	gnc_sql_load( &be->sql_be, book );
 
-    /* Load any initial stuff */
-    be->loading = TRUE;
-    
-    /* Some of this needs to happen in a certain order */
-	for( i = 0; fixed_load_order[i] != NULL; i++ ) {
-    	pData = qof_object_lookup_backend( fixed_load_order[i], GNC_GDA_BACKEND );
-    	if( pData->initial_load != NULL ) {
-        	(pData->initial_load)( be );
-		}
-    }
-
-	root = gnc_book_get_root_account( book );
-	gnc_account_foreach_descendant( root, (AccountCb)xaccAccountBeginEdit, NULL );
-
-    qof_object_foreach_backend( GNC_GDA_BACKEND, initial_load_cb, be );
-
-	gnc_account_foreach_descendant( root, (AccountCb)xaccAccountCommitEdit, NULL );
-
-    be->loading = FALSE;
-
-	// Mark the book as clean
-	qof_book_mark_saved( book );
-
     LEAVE( "" );
 }
 
 /* ================================================================= */
 
-static gint
-compare_namespaces(gconstpointer a, gconstpointer b)
-{
-    const gchar *sa = (const gchar *) a;
-    const gchar *sb = (const gchar *) b;
-
-    return( safe_strcmp( sa, sb ) );
-}
-
-static gint
-compare_commodity_ids(gconstpointer a, gconstpointer b)
-{
-    const gnc_commodity *ca = (const gnc_commodity *) a;
-    const gnc_commodity *cb = (const gnc_commodity *) b;
-  
-    return( safe_strcmp( gnc_commodity_get_mnemonic( ca ),
-                     	 gnc_commodity_get_mnemonic( cb ) ) );
-}
-
-static void
-write_commodities( GncGdaBackend* be, QofBook* book )
-{
-    gnc_commodity_table* tbl;
-    GList* namespaces;
-    GList* lp;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( book != NULL );
-
-    tbl = gnc_book_get_commodity_table( book );
-    namespaces = gnc_commodity_table_get_namespaces( tbl );
-    if( namespaces != NULL ) {
-        namespaces = g_list_sort( namespaces, compare_namespaces );
-    }
-    for( lp = namespaces; lp != NULL; lp = lp->next ) {
-        GList* comms;
-        GList* lp2;
-        
-        comms = gnc_commodity_table_get_commodities( tbl, lp->data );
-        comms = g_list_sort( comms, compare_commodity_ids );
-
-        for( lp2 = comms; lp2 != NULL; lp2 = lp2->next ) {
-	    	gnc_gda_save_commodity( be, GNC_COMMODITY(lp2->data) );
-        }
-    }
-}
-
-static void
-write_account_tree( GncGdaBackend* be, Account* root )
-{
-    GList* descendants;
-    GList* node;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( root != NULL );
-
-    descendants = gnc_account_get_descendants( root );
-    for( node = descendants; node != NULL; node = g_list_next(node) ) {
-        gnc_gda_save_account( QOF_INSTANCE(GNC_ACCOUNT(node->data)), be );
-		update_save_progress( be );
-    }
-    g_list_free( descendants );
-}
-
-static void
-write_accounts( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    write_account_tree( be, gnc_book_get_root_account( be->primary_book ) );
-}
-
-static int
-write_tx( Transaction* tx, gpointer data )
-{
-    GncGdaBackend* be = (GncGdaBackend*)data;
-
-	g_return_val_if_fail( tx != NULL, 0 );
-	g_return_val_if_fail( data != NULL, 0 );
-
-    gnc_gda_save_transaction( QOF_INSTANCE(tx), be );
-	update_save_progress( be );
-
-    return 0;
-}
-
-static void
-write_transactions( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-	
-    xaccAccountTreeForEachTransaction( gnc_book_get_root_account( be->primary_book ),
-                                       write_tx,
-                                       (gpointer)be );
-}
-
-static void
-write_template_transactions( GncGdaBackend* be )
-{
-    Account* ra;
-
-	g_return_if_fail( be != NULL );
-
-    ra = gnc_book_get_template_root( be->primary_book );
-    if( gnc_account_n_descendants( ra ) > 0 ) {
-        write_account_tree( be, ra );
-        xaccAccountTreeForEachTransaction( ra, write_tx, (gpointer)be );
-    }
-}
-
-static void
-write_schedXactions( GncGdaBackend* be )
-{
-    GList* schedXactions;
-    SchedXaction* tmpSX;
-
-	g_return_if_fail( be != NULL );
-
-    schedXactions = gnc_book_get_schedxactions( be->primary_book )->sx_list;
-
-    for( ; schedXactions != NULL; schedXactions = schedXactions->next ) {
-        tmpSX = schedXactions->data;
-		gnc_gda_save_schedxaction( QOF_INSTANCE( tmpSX ), be );
-    }
-}
-
-static void
-write_cb( const gchar* type, gpointer data_p, gpointer be_p )
-{
-    GncGdaDataType_t* pData = data_p;
-    GncGdaBackend* be = (GncGdaBackend*)be_p;
-
-    g_return_if_fail( type != NULL && data_p != NULL && be_p != NULL );
-    g_return_if_fail( pData->version == GNC_GDA_BACKEND_VERSION );
-
-    if( pData->write != NULL ) {
-        (pData->write)( be );
-    }
-}
-
-static void
-update_save_progress( GncGdaBackend* be )
-{
-	if( be->be.percentage != NULL ) {
-		gint percent_done;
-
-		be->operations_done++;
-		percent_done = be->operations_done * 100 / be->obj_total;
-		if( percent_done > 100 ) {
-			percent_done = 100;
-		}
-		(be->be.percentage)( NULL, percent_done );
-	}
-}
-
 static gboolean
 gnc_gda_save_may_clobber_data( QofBackend* qbe )
 {
@@ -594,12 +352,17 @@
     GdaDataModel* tables;
     GError* error = NULL;
 	gint numTables;
+	GdaMetaStore* mstore;
 
 	/* Data may be clobbered iff the number of tables != 0 */
+	mstore = gda_connection_get_meta_store( be->pConnection );
+	tables = gda_connection_get_meta_store_data( be->pConnection, GDA_CONNECTION_META_TABLES, &error, 0 );
+#if 0
     tables = gda_connection_get_schema( be->pConnection,
                                         GDA_CONNECTION_SCHEMA_TABLES,
                                         NULL,
                                         &error );
+#endif
     if( error != NULL ) {
         PERR( "SQL error: %s\n", error->message );
     }
@@ -617,6 +380,7 @@
     gint row;
     gint numTables;
 	gboolean status;
+	GdaMetaStore* mstore;
 
 	g_return_if_fail( be != NULL );
 	g_return_if_fail( book != NULL );
@@ -624,10 +388,8 @@
     ENTER( "book=%p, primary=%p", book, be->primary_book );
 
     /* Destroy the current contents of the database */
-    tables = gda_connection_get_schema( be->pConnection,
-                                        GDA_CONNECTION_SCHEMA_TABLES,
-                                        NULL,
-                                        &error );
+	mstore = gda_connection_get_meta_store( be->pConnection );
+	tables = gda_connection_get_meta_store_data( be->pConnection, GDA_CONNECTION_META_TABLES, &error, 0 );
     if( error != NULL ) {
         PERR( "SQL error: %s\n", error->message );
     }
@@ -635,524 +397,39 @@
     for( row = 0; row < numTables; row++ ) {
         const GValue* row_value;
         const gchar* table_name;
+		GdaServerOperation* op;
 
         row_value = gda_data_model_get_value_at( tables, 0, row );
         table_name = g_value_get_string( row_value );
         error = NULL;
-        if( !gda_drop_table( be->pConnection, table_name, &error ) ) {
-            PERR( "Unable to drop table %s\n", table_name );
+		op = gda_prepare_drop_table( be->pConnection, table_name, &error );
+		if( error != NULL ) {
+			PERR( "Unable to create op: %s\n", error->message );
+		}
+		if( op != NULL ) {
+			error = NULL;
+			gda_perform_drop_table( op, &error );
             if( error != NULL ) {
                 PERR( "SQL error: %s\n", error->message );
             }
         }
     }
 
-	_reset_version_info( be );
+	gnc_sql_reset_version_info( &be->sql_be );
 
-    // Update the dictionary because new tables may exist
-    gda_dict_update_dbms_meta_data( be->pDict, 0, NULL, &error );
-    if( error != NULL ) {
-        PERR( "gda_dict_update_dbms_meta_data() error: %s\n", error->message );
-    }
-
     /* Create new tables */
 	be->is_pristine_db = TRUE;
-    qof_object_foreach_backend( GNC_GDA_BACKEND, create_tables_cb, be );
+    qof_object_foreach_backend( GNC_SQL_BACKEND, create_tables_cb, be );
 
-    // Update the dictionary because new tables may exist
-    gda_dict_update_dbms_meta_data( be->pDict, 0, NULL, &error );
-    if( error != NULL ) {
-        PERR( "gda_dict_update_dbms_meta_data() error: %s\n", error->message );
-    }
-
     /* Save all contents */
 	be->primary_book = book;
-	be->obj_total = 0;
-    be->obj_total += 1 + gnc_account_n_descendants( gnc_book_get_root_account( book ) );
-	be->obj_total += gnc_book_count_transactions( book );
-	be->operations_done = 0;
+	gnc_sql_sync_all( &be->sql_be, book );
 
-	error = NULL;
-	if( be->supports_transactions ) {
-		status = gda_connection_begin_transaction( be->pConnection, TRANSACTION_NAME,
-										GDA_TRANSACTION_ISOLATION_UNKNOWN, &error );
-		if( !status ) {
-			if( error != NULL ) {
-				PWARN( "Unable to begin transaction: %s\n", error->message );
-			} else {
-				PWARN( "Unable to begin transaction\n" );
-			}
-		}
-	}
-
-	// FIXME: should write the set of commodities that are used 
-    //write_commodities( be, book );
-	gnc_gda_save_book( QOF_INSTANCE(book), be );
-    write_accounts( be );
-    write_transactions( be );
-    write_template_transactions( be );
-    write_schedXactions( be );
-    qof_object_foreach_backend( GNC_GDA_BACKEND, write_cb, be );
-	if( be->supports_transactions ) {
-		status = gda_connection_commit_transaction( be->pConnection, TRANSACTION_NAME, &error );
-		if( !status ) {
-			if( error != NULL ) {
-				PWARN( "Unable to commit transaction: %s\n", error->message );
-			} else {
-				PWARN( "Unable to commit transaction\n" );
-			}
-		}
-	}
-	be->is_pristine_db = FALSE;
-
-	// Mark the book as clean
-	qof_book_mark_saved( book );
-
     LEAVE( "book=%p", book );
 }
 
 /* ================================================================= */
-/* Routines to deal with the creation of multiple books. */
 
-
-static void
-gnc_gda_begin_edit (QofBackend *be, QofInstance *inst)
-{
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( inst != NULL );
-}
-
-static void
-gnc_gda_rollback_edit (QofBackend *be, QofInstance *inst)
-{
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( inst != NULL );
-}
-
-static void
-commit_cb( const gchar* type, gpointer data_p, gpointer be_data_p )
-{
-    GncGdaDataType_t* pData = data_p;
-    gda_backend* be_data = be_data_p;
-
-    g_return_if_fail( type != NULL && pData != NULL && be_data != NULL );
-    g_return_if_fail( pData->version == GNC_GDA_BACKEND_VERSION );
-
-    /* If this has already been handled, or is not the correct handler, return */
-    if( strcmp( pData->type_name, be_data->inst->e_type ) != 0 ) return;
-    if( be_data->ok ) return;
-
-    if( pData->commit != NULL ) {
-        (pData->commit)( be_data->inst, be_data->be );
-        be_data->ok = TRUE;
-    }
-}
-
-/* Commit_edit handler - find the correct backend handler for this object
- * type and call its commit handler
- */
-static void
-gnc_gda_commit_edit (QofBackend *be_start, QofInstance *inst)
-{
-    GncGdaBackend *be = (GncGdaBackend*)be_start;
-    gda_backend be_data;
-	GError* error;
-	gboolean status;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( inst != NULL );
-
-    /* During initial load where objects are being created, don't commit
-    anything */
-    if( be->loading ) {
-	    return;
-	}
-
-	// The engine has a PriceDB object but it isn't in the database
-	if( strcmp( inst->e_type, "PriceDB" ) == 0 ) {
-    	qof_instance_mark_clean(inst);
-    	qof_book_mark_saved( be->primary_book );
-		return;
-	}
-
-    ENTER( " " );
-
-    DEBUG( "gda_commit_edit(): %s dirty = %d, do_free=%d\n",
-             (inst->e_type ? inst->e_type : "(null)"),
-             qof_instance_get_dirty_flag(inst), qof_instance_get_destroying(inst) );
-
-    if( !qof_instance_get_dirty_flag(inst) && !qof_instance_get_destroying(inst) && GNC_IS_TRANS(inst) ) {
-        gnc_gda_transaction_commit_splits( be, GNC_TRANS(inst) );
-    }
-
-    if( !qof_instance_get_dirty_flag(inst) && !qof_instance_get_destroying(inst) ) return;
-
-	error = NULL;
-	if( be->supports_transactions ) {
-		status = gda_connection_begin_transaction( be->pConnection, TRANSACTION_NAME,
-										GDA_TRANSACTION_ISOLATION_UNKNOWN, &error );
-		if( !status ) {
-			if( error != NULL ) {
-				PWARN( "Unable to begin transaction: %s\n", error->message );
-			} else {
-				PWARN( "Unable to begin transaction\n" );
-			}
-		}
-	}
-
-    be_data.ok = FALSE;
-    be_data.be = be;
-    be_data.inst = inst;
-    qof_object_foreach_backend( GNC_GDA_BACKEND, commit_cb, &be_data );
-
-    if( !be_data.ok ) {
-        PERR( "gnc_gda_commit_edit(): Unknown object type '%s'\n", inst->e_type );
-		if( be->supports_transactions ) {
-			status = gda_connection_rollback_transaction( be->pConnection, TRANSACTION_NAME, &error );
-			if( !status ) {
-				if( error != NULL ) {
-					PWARN( "Unable to roll back transaction: %s\n", error->message );
-				} else {
-					PWARN( "Unable to roll back transaction\n" );
-				}
-			}
-		}
-
-		// Don't let unknown items still mark the book as being dirty
-    	qof_instance_mark_clean(inst);
-    	qof_book_mark_saved( be->primary_book );
-        return;
-    }
-	if( be->supports_transactions ) {
-		status = gda_connection_commit_transaction( be->pConnection, TRANSACTION_NAME, &error );
-		if( !status ) {
-			if( error != NULL ) {
-				PWARN( "Unable to commit transaction: %s\n", error->message );
-			} else {
-				PWARN( "Unable to commit transaction\n" );
-			}
-		}
-	}
-
-    qof_instance_mark_clean(inst);
-    qof_book_mark_saved( be->primary_book );
-
-	LEAVE( "" );
-}
-/* ---------------------------------------------------------------------- */
-
-/* Query processing */
-
-static const gchar*
-convert_search_obj( QofIdType objType )
-{
-    return (gchar*)objType;
-}
-
-static void
-handle_and_term( QofQueryTerm* pTerm, gchar* sql )
-{
-    GSList* pParamPath;
-    QofQueryPredData* pPredData;
-    gboolean isInverted;
-    GSList* name;
-    gchar val[GUID_ENCODING_LENGTH+1];
-
-	g_return_if_fail( pTerm != NULL );
-	g_return_if_fail( sql != NULL );
-
-    pParamPath = qof_query_term_get_param_path( pTerm );
-    pPredData = qof_query_term_get_pred_data( pTerm );
-    isInverted = qof_query_term_is_inverted( pTerm );
-
-    strcat( sql, "(" );
-    if( isInverted ) {
-        strcat( sql, "!" );
-    }
-
-    for( name = pParamPath; name != NULL; name = name->next ) {
-        if( name != pParamPath ) strcat( sql, "." );
-        strcat( sql, name->data );
-    }
-
-    if( pPredData->how == QOF_COMPARE_LT ) {
-        strcat( sql, "<" );
-    } else if( pPredData->how == QOF_COMPARE_LTE ) {
-        strcat( sql, "<=" );
-    } else if( pPredData->how == QOF_COMPARE_EQUAL ) {
-        strcat( sql, "=" );
-    } else if( pPredData->how == QOF_COMPARE_GT ) {
-        strcat( sql, ">" );
-    } else if( pPredData->how == QOF_COMPARE_GTE ) {
-        strcat( sql, ">=" );
-    } else if( pPredData->how == QOF_COMPARE_NEQ ) {
-        strcat( sql, "~=" );
-    } else {
-        strcat( sql, "??" );
-    }
-
-    if( strcmp( pPredData->type_name, "string" ) == 0 ) {
-        query_string_t pData = (query_string_t)pPredData;
-        strcat( sql, "'" );
-        strcat( sql, pData->matchstring );
-        strcat( sql, "'" );
-    } else if( strcmp( pPredData->type_name, "date" ) == 0 ) {
-        query_date_t pData = (query_date_t)pPredData;
-
-        (void)gnc_timespec_to_iso8601_buff( pData->date, val );
-        strcat( sql, "'" );
-        strncat( sql, val, 4+1+2+1+2 );
-        strcat( sql, "'" );
-    } else if( strcmp( pPredData->type_name, "numeric" ) == 0 ) {
-        query_numeric_t pData = (query_numeric_t)pPredData;
-    
-        strcat( sql, "numeric" );
-    } else if( strcmp( pPredData->type_name, "guid" ) == 0 ) {
-        query_guid_t pData = (query_guid_t)pPredData;
-        (void)guid_to_string_buff( pData->guids->data, val );
-        strcat( sql, "'" );
-        strcat( sql, val );
-        strcat( sql, "'" );
-    } else if( strcmp( pPredData->type_name, "gint32" ) == 0 ) {
-        query_int32_t pData = (query_int32_t)pPredData;
-
-        sprintf( val, "%d", pData->val );
-        strcat( sql, val );
-    } else if( strcmp( pPredData->type_name, "gint64" ) == 0 ) {
-        query_int64_t pData = (query_int64_t)pPredData;
-    
-        sprintf( val, "%" G_GINT64_FORMAT, pData->val );
-        strcat( sql, val );
-    } else if( strcmp( pPredData->type_name, "double" ) == 0 ) {
-        query_double_t pData = (query_double_t)pPredData;
-
-        sprintf( val, "%f", pData->val );
-        strcat( sql, val );
-    } else if( strcmp( pPredData->type_name, "boolean" ) == 0 ) {
-        query_boolean_t pData = (query_boolean_t)pPredData;
-
-        sprintf( val, "%d", pData->val );
-        strcat( sql, val );
-    } else {
-        g_assert( FALSE );
-    }
-
-    strcat( sql, ")" );
-}
-
-static void
-compile_query_cb( const gchar* type, gpointer data_p, gpointer be_data_p )
-{
-    GncGdaDataType_t* pData = data_p;
-    gda_backend* be_data = be_data_p;
-
-    g_return_if_fail( type != NULL && pData != NULL && be_data != NULL );
-    g_return_if_fail( pData->version == GNC_GDA_BACKEND_VERSION );
-
-	// Is this the right item?
-    if( strcmp( type, be_data->pQueryInfo->searchObj ) != 0 ) return;
-    if( be_data->ok ) return;
-
-    if( pData->compile_query != NULL ) {
-        be_data->pQueryInfo->pCompiledQuery = (pData->compile_query)(
-                                                            be_data->be,
-                                                            be_data->pQuery );
-        be_data->ok = TRUE;
-    }
-}
-
-static gpointer
-gnc_gda_compile_query(QofBackend* pBEnd, QofQuery* pQuery)
-{
-    GncGdaBackend *be = (GncGdaBackend*)pBEnd;
-    GList* pBookList;
-    QofIdType searchObj;
-    gchar sql[1000];
-    gda_backend be_data;
-    gnc_gda_query_info* pQueryInfo;
-
-	g_return_val_if_fail( pBEnd != NULL, NULL );
-	g_return_val_if_fail( pQuery != NULL, NULL );
-
-	ENTER( " " );
-
-    searchObj = qof_query_get_search_for( pQuery );
-
-    pQueryInfo = g_malloc( sizeof( gnc_gda_query_info ) );
-
-    // Try various objects first
-    be_data.ok = FALSE;
-    be_data.be = be;
-    be_data.pQuery = pQuery;
-    pQueryInfo->searchObj = searchObj;
-    be_data.pQueryInfo = pQueryInfo;
-
-    qof_object_foreach_backend( GNC_GDA_BACKEND, compile_query_cb, &be_data );
-    if( be_data.ok ) {
-		LEAVE( "" );
-        return be_data.pQueryInfo;
-    }
-
-    pBookList = qof_query_get_books( pQuery );
-
-    /* Convert search object type to table name */
-    sprintf( sql, "SELECT * from %s", convert_search_obj( searchObj ) );
-    if( !qof_query_has_terms( pQuery ) ) {
-        strcat( sql, ";" );
-    } else {
-        GList* pOrTerms = qof_query_get_terms( pQuery );
-        GList* orTerm;
-
-        strcat( sql, " WHERE " );
-
-        for( orTerm = pOrTerms; orTerm != NULL; orTerm = orTerm->next ) {
-            GList* pAndTerms = (GList*)orTerm->data;
-            GList* andTerm;
-
-            if( orTerm != pOrTerms ) strcat( sql, " OR " );
-            strcat( sql, "(" );
-            for( andTerm = pAndTerms; andTerm != NULL; andTerm = andTerm->next ) {
-                if( andTerm != pAndTerms ) strcat( sql, " AND " );
-                handle_and_term( (QofQueryTerm*)andTerm->data, sql );
-            }
-            strcat( sql, ")" );
-        }
-    }
-
-    DEBUG( "Compiled: %s\n", sql );
-    pQueryInfo->pCompiledQuery =  g_strdup( sql );
-
-	LEAVE( "" );
-
-    return pQueryInfo;
-}
-
-static void
-free_query_cb( const gchar* type, gpointer data_p, gpointer be_data_p )
-{
-    GncGdaDataType_t* pData = data_p;
-    gda_backend* be_data = be_data_p;
-
-    g_return_if_fail( type != NULL && pData != NULL && be_data != NULL );
-    g_return_if_fail( pData->version == GNC_GDA_BACKEND_VERSION );
-    if( be_data->ok ) return;
-    if( strcmp( type, be_data->pQueryInfo->searchObj ) != 0 ) return;
-
-    if( pData->free_query != NULL ) {
-        (pData->free_query)( be_data->be, be_data->pCompiledQuery );
-        be_data->ok = TRUE;
-    }
-}
-
-static void
-gnc_gda_free_query(QofBackend* pBEnd, gpointer pQuery)
-{
-    GncGdaBackend *be = (GncGdaBackend*)pBEnd;
-    gnc_gda_query_info* pQueryInfo = (gnc_gda_query_info*)pQuery;
-    gda_backend be_data;
-
-	g_return_if_fail( pBEnd != NULL );
-	g_return_if_fail( pQuery != NULL );
-
-	ENTER( " " );
-
-    // Try various objects first
-    be_data.ok = FALSE;
-    be_data.be = be;
-    be_data.pCompiledQuery = pQuery;
-    be_data.pQueryInfo = pQueryInfo;
-
-    qof_object_foreach_backend( GNC_GDA_BACKEND, free_query_cb, &be_data );
-    if( be_data.ok ) {
-		LEAVE( "" );
-        return;
-    }
-
-    DEBUG( "gda_free_query(): %s\n", (gchar*)pQueryInfo->pCompiledQuery );
-    g_free( pQueryInfo->pCompiledQuery );
-    g_free( pQueryInfo );
-
-	LEAVE( "" );
-}
-
-static void
-run_query_cb( const gchar* type, gpointer data_p, gpointer be_data_p )
-{
-    GncGdaDataType_t* pData = data_p;
-    gda_backend* be_data = be_data_p;
-
-    g_return_if_fail( type != NULL && pData != NULL && be_data != NULL );
-    g_return_if_fail( pData->version == GNC_GDA_BACKEND_VERSION );
-    if( be_data->ok ) return;
-
-	// Is this the right item?
-    if( strcmp( type, be_data->pQueryInfo->searchObj ) != 0 ) return;
-
-    if( pData->run_query != NULL ) {
-        (pData->run_query)( be_data->be, be_data->pCompiledQuery );
-        be_data->ok = TRUE;
-    }
-}
-
-static void
-gnc_gda_run_query(QofBackend* pBEnd, gpointer pQuery)
-{
-    GncGdaBackend *be = (GncGdaBackend*)pBEnd;
-    gnc_gda_query_info* pQueryInfo = (gnc_gda_query_info*)pQuery;
-    gda_backend be_data;
-
-	g_return_if_fail( pBEnd != NULL );
-	g_return_if_fail( pQuery != NULL );
-    g_return_if_fail( !be->in_query );
-
-	ENTER( " " );
-
-    be->loading = TRUE;
-    be->in_query = TRUE;
-
-    qof_event_suspend();
-
-    // Try various objects first
-    be_data.ok = FALSE;
-    be_data.be = be;
-    be_data.pCompiledQuery = pQueryInfo->pCompiledQuery;
-    be_data.pQueryInfo = pQueryInfo;
-
-    qof_object_foreach_backend( GNC_GDA_BACKEND, run_query_cb, &be_data );
-    be->loading = FALSE;
-    be->in_query = FALSE;
-    qof_event_resume();
-//    if( be_data.ok ) {
-//		LEAVE( "" );
-//       	return;
-//    }
-
-	// Mark the book as clean
-	qof_instance_mark_clean( QOF_INSTANCE(be->primary_book) );
-
-//    DEBUG( "gda_run_query(): %s\n", (gchar*)pQueryInfo->pCompiledQuery );
-
-	LEAVE( "" );
-}
-
-/* ================================================================= */
-static void
-gnc_gda_init_object_handlers( void )
-{
-    gnc_gda_init_book_handler();
-    gnc_gda_init_commodity_handler();
-    gnc_gda_init_account_handler();
-    gnc_gda_init_budget_handler();
-    gnc_gda_init_price_handler();
-    gnc_gda_init_transaction_handler();
-    gnc_gda_init_slots_handler();
-	gnc_gda_init_recurrence_handler();
-    gnc_gda_init_schedxaction_handler();
-    gnc_gda_init_lot_handler();
-}
-
-/* ================================================================= */
-
 static QofBackend*
 gnc_gda_backend_new(void)
 {
@@ -1172,9 +449,9 @@
     be->save_may_clobber_data = gnc_gda_save_may_clobber_data;
 
     /* The gda backend treats accounting periods transactionally. */
-    be->begin = gnc_gda_begin_edit;
-    be->commit = gnc_gda_commit_edit;
-    be->rollback = gnc_gda_rollback_edit;
+    be->begin = gnc_sql_begin_edit;
+    be->commit = gnc_sql_commit_edit;
+    be->rollback = gnc_sql_rollback_edit;
 
     /* The gda backend uses queries to load data ... */
 #if 0
@@ -1198,9 +475,8 @@
     gnc_be->primary_book = NULL;
 
     if( !initialized ) {
-        gda_init( "gnucash", "2.0", 0, NULL );
-        gnc_gda_init_object_handlers();
-		gnc_gda_register_standard_col_type_handlers();
+        gda_init();
+		gnc_sql_init( &gnc_be->sql_be );
         initialized = TRUE;
     }
 
@@ -1290,4 +566,555 @@
     qof_backend_register_provider (prov);
 }
 
+/* --------------------------------------------------------- */
+typedef struct
+{
+	GncSqlRow base;
+
+	GdaDataModel* model;
+	int row_num;
+} GncGdaSqlRow;
+
+static void
+row_dispose( GncSqlRow* row )
+{
+	GncGdaSqlRow* gda_row = (GncGdaSqlRow*)row;
+
+	g_object_unref( gda_row->model );
+	gda_row->model = NULL;
+	g_free( gda_row );
+}
+
+static const GValue*
+row_get_value_at_col_name( GncSqlRow* row, const gchar* col_name )
+{
+	GncGdaSqlRow* gda_row = (GncGdaSqlRow*)row;
+
+	return gda_data_model_get_value_at_col_name( gda_row->model, col_name, gda_row->row_num );
+}
+
+static GncSqlRow*
+create_gda_row( GdaDataModel* model, int rowNum )
+{
+	GncGdaSqlRow* row;
+
+	row = g_new0( GncGdaSqlRow, 1 );
+	row->base.getValueAtColName = row_get_value_at_col_name;
+	row->base.dispose = row_dispose;
+	row->model = model;
+	g_object_ref( model );
+	row->row_num = rowNum;
+
+	return (GncSqlRow*)row;
+}
+/* --------------------------------------------------------- */
+typedef struct
+{
+	GncSqlResult base;
+
+	GdaDataModel* model;
+	gint next_row;
+	gint num_rows;
+} GncGdaSqlResult;
+
+static void
+result_dispose( GncSqlResult* result )
+{
+	GncGdaSqlResult* gda_result = (GncGdaSqlResult*)result;
+
+	g_object_unref( gda_result->model );
+	gda_result->model = NULL;
+	g_free( result );
+}
+
+static gint
+result_get_num_rows( GncSqlResult* result )
+{
+	GncGdaSqlResult* gda_result = (GncGdaSqlResult*)result;
+
+	return gda_result->num_rows;
+}
+
+static GncSqlRow*
+result_get_first_row( GncSqlResult* result )
+{
+	GncGdaSqlResult* gda_result = (GncGdaSqlResult*)result;
+
+	if( gda_result->num_rows > 0 ) {
+		gda_result->next_row = 1;
+		return create_gda_row( gda_result->model, 0 );
+	} else {
+		return NULL;
+	}
+}
+
+static GncSqlRow*
+result_get_next_row( GncSqlResult* result )
+{
+	GncGdaSqlResult* gda_result = (GncGdaSqlResult*)result;
+
+	if( gda_result->next_row < gda_result->num_rows ) {
+		return create_gda_row( gda_result->model, gda_result->next_row++ );
+	} else {
+		return NULL;
+	}
+}
+
+static GncSqlResult*
+create_gda_result( GdaDataModel* model )
+{
+	GncGdaSqlResult* result;
+
+	result = g_new0( GncGdaSqlResult, 1 );
+	result->base.dispose = result_dispose;
+	result->base.getNumRows = result_get_num_rows;
+	result->base.getFirstRow = result_get_first_row;
+	result->base.getNextRow = result_get_next_row;
+	result->model = model;
+	result->num_rows = gda_data_model_get_n_rows( model );
+	result->next_row = 0;
+
+	return (GncSqlResult*)result;
+}
+/* --------------------------------------------------------- */
+typedef struct
+{
+	GncSqlStatement base;
+
+	GString* sql;
+} GncGdaSqlStatement;
+
+static void
+stmt_dispose( GncSqlStatement* stmt )
+{
+	GncGdaSqlStatement* gda_stmt = (GncGdaSqlStatement*)stmt;
+
+	if( gda_stmt->sql != NULL ) {
+		g_string_free( gda_stmt->sql, TRUE );
+	}
+	g_free( stmt );
+}
+
+static gchar*
+stmt_to_sql( GncSqlStatement* stmt )
+{
+	GncGdaSqlStatement* gda_stmt = (GncGdaSqlStatement*)stmt;
+
+	return gda_stmt->sql->str;
+}
+
+static void
+stmt_add_where_cond( GncSqlStatement* stmt, QofIdTypeConst type_name,
+					gpointer obj, const col_cvt_t* table_row, GValue* value )
+{
+	GncGdaSqlStatement* gda_stmt = (GncGdaSqlStatement*)stmt;
+	gchar* buf;
+
+	buf = g_strdup_printf( " WHERE %s = %s", table_row->col_name,
+						gnc_sql_get_sql_value( value ) );
+	g_string_append( gda_stmt->sql, buf );
+	g_free( buf );
+}
+
+static GncSqlStatement*
+create_gda_statement( const gchar* sql )
+{
+	GncGdaSqlStatement* stmt;
+
+	stmt = g_new0( GncGdaSqlStatement, 1 );
+	stmt->base.dispose = stmt_dispose;
+	stmt->base.toSql = stmt_to_sql;
+	stmt->base.addWhereCond = stmt_add_where_cond;
+	stmt->sql = g_string_new( sql );
+
+	return (GncSqlStatement*)stmt;
+}
+/* --------------------------------------------------------- */
+typedef struct
+{
+	GncSqlConnection base;
+
+	GdaConnection* conn;
+	GdaSqlParser* parser;
+	GdaServerProvider* server;
+} GncGdaSqlConnection;
+
+static void
+conn_dispose( GncSqlConnection* conn )
+{
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+
+	gda_connection_close( gda_conn->conn );
+	g_object_unref( gda_conn->conn );
+	g_free( conn );
+}
+
+static GncSqlResult*
+conn_execute_select_statement( GncSqlConnection* conn, GncSqlStatement* stmt )
+{
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+	GncGdaSqlStatement* gda_stmt = (GncGdaSqlStatement*)stmt;
+	GdaDataModel* model;
+	GdaStatement* real_stmt;
+	GError* error = NULL;
+														
+	real_stmt = gda_sql_parser_parse_string( gda_conn->parser, gda_stmt->sql->str, NULL, &error );
+	if( error != NULL ) {
+		PERR( "Error parsing SQL %s\n%s\n", gda_stmt->sql->str, error->message );
+		return NULL;
+	}
+	model = gda_connection_statement_execute_select( gda_conn->conn,
+												real_stmt, NULL, &error );
+	if( error != NULL ) {
+		PERR( "Error executing SQL %s\n%s\n", gda_stmt->sql->str, error->message );
+		return NULL;
+	}
+	return create_gda_result( model );
+}
+
+static gint
+conn_execute_nonselect_statement( GncSqlConnection* conn, GncSqlStatement* stmt )
+{
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+	GncGdaSqlStatement* gda_stmt = (GncGdaSqlStatement*)stmt;
+	GdaStatement* real_stmt;
+	gint result;
+	GError* error = NULL;
+
+	real_stmt = gda_sql_parser_parse_string( gda_conn->parser, gda_stmt->sql->str, NULL, &error );
+	if( error != NULL ) {
+		PERR( "Error parsing SQL %s\n%s\n", gda_stmt->sql->str, error->message );
+		return 0;
+	}
+	result = gda_connection_statement_execute_non_select( gda_conn->conn,
+												real_stmt, NULL, NULL, &error );
+	if( error != NULL ) {
+		PERR( "Error executing SQL %s\n%s\n", gda_stmt->sql->str, error->message );
+		return 0;
+	}
+	return result;
+}
+
+static GncSqlStatement*
+conn_create_statement_from_sql( GncSqlConnection* conn, const gchar* sql )
+{
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+
+	return create_gda_statement( sql );
+}
+
+static GValue*
+create_gvalue_from_string( gchar* s )
+{
+	GValue* s_gval;
+
+	s_gval = g_new0( GValue, 1 );
+	g_value_init( s_gval, G_TYPE_STRING );
+	g_value_take_string( s_gval, s );
+
+	return s_gval;
+}
+
+static gboolean
+conn_does_table_exist( GncSqlConnection* conn, const gchar* table_name )
+{
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+	GError* error = NULL;
+	GValue* table_name_value;
+	GdaDataModel* model;
+	gint nTables;
+	GdaMetaStore* mstore;
+
+	g_return_val_if_fail( conn != NULL, FALSE );
+	g_return_val_if_fail( table_name != NULL, FALSE );
+
+#if 0
+	/* If the db is pristine because it's being saved, the table does not
+	 * exist.
+	 */
+	if( conn->is_pristine_db ) {
+		return FALSE;
+	}
+#endif
+
+	table_name_value = create_gvalue_from_string( g_strdup( table_name ) );
+	mstore = gda_connection_get_meta_store( gda_conn->conn );
+	model = gda_connection_get_meta_store_data( gda_conn->conn, GDA_CONNECTION_META_TABLES, &error, 1, "name", table_name_value );
+	g_free( table_name_value );
+	nTables = gda_data_model_get_n_rows( model );
+	g_object_unref( model );
+
+	if( nTables == 1 ) {
+		return TRUE;
+	} else {
+		return FALSE;
+	}
+}
+
+static void
+conn_begin_transaction( GncSqlConnection* conn )
+{
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+}
+
+static void
+conn_rollback_transaction( GncSqlConnection* conn )
+{
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+}
+
+static void
+conn_commit_transaction( GncSqlConnection* conn )
+{
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+}
+
+static const gchar*
+conn_get_column_type_name( GncSqlConnection* conn, GType type, gint size )
+{
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+
+	return gda_server_provider_get_default_dbms_type(
+									gda_conn->server, gda_conn->conn, type );
+}
+
+static void
+add_table_column( GdaServerOperation* op, const GncSqlColumnInfo* info,
+					guint col_num )
+{
+    gchar* buf;
+	GError* error = NULL;
+	gboolean ok;
+
+	g_return_if_fail( op != NULL );
+	g_return_if_fail( info != NULL );
+
+	ok = gda_server_operation_set_value_at( op, info->name, &error,
+									"/FIELDS_A/@COLUMN_NAME/%d", col_num );
+	if( error != NULL ) {
+		PWARN( "Error setting NAME for %s: %s\n", info->name, error->message );
+	}
+	if( !ok ) return;
+	ok = gda_server_operation_set_value_at( op, info->type_name, &error,
+									"/FIELDS_A/@COLUMN_TYPE/%d", col_num );
+	if( error != NULL ) {
+		PWARN( "Error setting TYPE for %s: %s\n", info->name, error->message );
+	}
+	if( !ok ) return;
+    if( info->size != 0 ) {
+        buf = g_strdup_printf( "%d", info->size );
+		ok = gda_server_operation_set_value_at( op, buf, &error,
+									"/FIELDS_A/@COLUMN_SIZE/%d", col_num );
+		if( error != NULL ) {
+			PWARN( "Error setting SIZE for %s: %s\n", info->name, error->message );
+		}
+        g_free( buf );
+		if( !ok ) return;
+    }
+	ok = gda_server_operation_set_value_at( op,
+									(info->is_primary_key) ? "TRUE" : "FALSE",
+									&error,
+									"/FIELDS_A/@COLUMN_PKEY/%d", col_num );
+	if( error != NULL ) {
+		PWARN( "Error setting PKEY for %s: %s\n", info->name, error->message );
+	}
+	if( !ok ) return;
+	ok = gda_server_operation_set_value_at( op,
+									(info->null_allowed) ? "FALSE" : "TRUE",
+									&error,
+									"/FIELDS_A/@COLUMN_NNUL/%d", col_num );
+	if( error != NULL ) {
+		PWARN( "Error setting NNUL for %s: %s\n", info->name, error->message );
+	}
+	if( !ok ) return;
+#if 0
+	ok = gda_server_operation_set_value_at( op,
+									(flags & COL_AUTOINC) ? "TRUE" : "FALSE",
+									&error,
+									"/FIELDS_A/@COLUMN_AUTOINC/%d", col_num );
+	if( error != NULL ) {
+		PWARN( "Error setting AUTOINC for %s: %s\n", arg, error->message );
+	}
+	if( !ok ) return;
+	ok = gda_server_operation_set_value_at( op,
+									(flags & COL_UNIQUE) ? "TRUE" : "FALSE",
+									&error,
+									"/FIELDS_A/@COLUMN_UNIQUE/%d", col_num );
+	if( error != NULL ) {
+		PWARN( "Error setting UNIQUE for %s: %s\n", arg, error->message );
+	}
+#endif
+}
+
+static void
+conn_create_table( GncSqlConnection* conn, const gchar* table_name,
+				const GList* col_info_list )
+{
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+    GdaServerOperation *op;
+	GdaConnection* cnn;
+	GError* error = NULL;
+	const GList* list_node;
+	guint col_num;
+
+	g_return_if_fail( conn != NULL );
+	g_return_if_fail( table_name != NULL );
+	g_return_if_fail( col_info_list != NULL );
+    
+	cnn = gda_conn->conn;
+    
+    op = gda_server_provider_create_operation( gda_conn->server, cnn, 
+                           GDA_SERVER_OPERATION_CREATE_TABLE, NULL, &error );
+    if( op != NULL && GDA_IS_SERVER_OPERATION(op) ) {
+        gint col;
+		gboolean ok;
+		GdaServerOperationNode* node;
+		gint i;
+		GdaDataModel* model;
+		GError* error = NULL;
+		gint numRows;
+
+		ok = gda_server_operation_set_value_at( op, table_name, &error,
+								"/TABLE_DEF_P/TABLE_NAME" );
+		if( error != NULL ) {
+			PWARN( "Setting TABLE_NAME %s\n%s\n", table_name, error->message );
+		}
+		if( !ok ) return;
+
+		/* Remove any pre-created colums */
+		node = gda_server_operation_get_node_info( op, "/FIELDS_A" );
+		model = node->model;
+		if( model != NULL ) {
+			numRows = gda_data_model_get_n_rows( model );
+			for( i = 0; i < numRows; i++ ) {
+				gda_data_model_remove_row( model, i, &error );
+				if( error != NULL ) {
+					PWARN( "Removing server op row: %s\n", error->message );
+				}
+			}
+		}
+        
+        for( list_node = col_info_list, col_num = 0; list_node != NULL;
+				list_node = list_node->next, col_num++ ) {
+			GncSqlColumnInfo* info = (GncSqlColumnInfo*)(list_node->data);
+
+			add_table_column( op, info, col_num );
+        }
+        
+        if( !gda_server_provider_perform_operation( gda_conn->server, cnn, op, &error ) ) {
+			PWARN( "gda_server_provider_perform_operation(): %s\n%s\n",
+					table_name, error->message );
+            g_object_unref( op );
+            return;
+        }
+
+        g_object_unref( op );
+    }
+}
+
+static void
+conn_create_index( GncSqlConnection* conn, const gchar* index_name,
+					const gchar* table_name, const col_cvt_t* col_table )
+{
+    GdaServerOperation *op;
+    GdaServerProvider *server;
+	GdaConnection* cnn;
+	GncGdaSqlConnection* gda_conn = (GncGdaSqlConnection*)conn;
+	GError* error = NULL;
+    
+    g_return_if_fail( conn != NULL );
+	g_return_if_fail( index_name != NULL );
+	g_return_if_fail( table_name != NULL );
+	g_return_if_fail( col_table != NULL );
+    
+	cnn = gda_conn->conn;
+	g_return_if_fail( cnn != NULL );
+    g_return_if_fail( GDA_IS_CONNECTION(cnn) );
+    g_return_if_fail( gda_connection_is_opened(cnn) );
+
+    server = gda_conn->server;
+	g_return_if_fail( server != NULL );
+    
+    op = gda_server_provider_create_operation( server, cnn, 
+                           GDA_SERVER_OPERATION_CREATE_INDEX, NULL, &error );
+    if( error != NULL ) {
+		PERR( "gda_server_provider_create_operation(): %s\n", error->message );
+	}
+    if( op != NULL && GDA_IS_SERVER_OPERATION(op) ) {
+        gint col;
+		gboolean ok;
+        
+		ok = gda_server_operation_set_value_at( op, index_name, &error,
+								"/INDEX_DEF_P/INDEX_NAME" );
+    	if( error != NULL ) {
+			PERR( "set INDEX_NAME: %s\n", error->message );
+		}
+		if( !ok ) return;
+		ok = gda_server_operation_set_value_at( op, "", &error,
+								"/INDEX_DEF_P/INDEX_TYPE" );
+    	if( error != NULL ) {
+			PERR( "set INDEX_TYPE: %s\n", error->message );
+		}
+		if( !ok ) return;
+		ok = gda_server_operation_set_value_at( op, "TRUE", &error,
+								"/INDEX_DEF_P/INDEX_IFNOTEXISTS" );
+    	if( error != NULL ) {
+			PERR( "set INDEX_IFNOTEXISTS: %s\n", error->message );
+		}
+		if( !ok ) return;
+		ok = gda_server_operation_set_value_at( op, table_name, &error,
+								"/INDEX_DEF_P/INDEX_ON_TABLE" );
+    	if( error != NULL ) {
+			PERR( "set INDEX_ON_TABLE: %s\n", error->message );
+		}
+		if( !ok ) return;
+
+        for( col = 0; col_table[col].col_name != NULL; col++ ) {
+			guint item;
+
+			if( col != 0 ) {
+				item = gda_server_operation_add_item_to_sequence( op, "/INDEX_FIELDS_S" );
+				g_assert( item == col );
+			}
+			ok = gda_server_operation_set_value_at( op, col_table->col_name, &error,
+													"/INDEX_FIELDS_S/%d/INDEX_FIELD", col );
+			if( error != NULL ) {
+				PERR( "set INDEX_FIELD %s: %s\n", col_table->col_name, error->message );
+			}
+			if( !ok ) break;
+        }
+        
+        if( !gda_server_provider_perform_operation( server, cnn, op, &error ) ) {
+            g_object_unref( op );
+            return;
+        }
+
+        g_object_unref( op );
+    }
+}
+
+static GncSqlConnection*
+create_gda_connection( GdaConnection* conn )
+{
+	GncGdaSqlConnection* gda_conn;
+
+	gda_conn = g_new0( GncGdaSqlConnection, 1 );
+	gda_conn->base.dispose = conn_dispose;
+	gda_conn->base.executeSelectStatement = conn_execute_select_statement;
+	gda_conn->base.executeNonSelectStatement = conn_execute_nonselect_statement;
+	gda_conn->base.createStatementFromSql = conn_create_statement_from_sql;
+	gda_conn->base.doesTableExist = conn_does_table_exist;
+	gda_conn->base.beginTransaction = conn_begin_transaction;
+	gda_conn->base.rollbackTransaction = conn_rollback_transaction;
+	gda_conn->base.commitTransaction = conn_commit_transaction;
+	gda_conn->base.getColumnTypeName = conn_get_column_type_name;
+	gda_conn->base.createTable = conn_create_table;
+	gda_conn->base.createIndex = conn_create_index;
+	gda_conn->conn = conn;
+	gda_conn->parser = gda_sql_parser_new();
+	gda_conn->server = gda_connection_get_provider_obj( conn );
+
+	return (GncSqlConnection*)gda_conn;
+}
+
 /* ========================== END OF FILE ===================== */

Modified: gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -20,7 +20,7 @@
 \********************************************************************/
 /** @file gnc-backend-gda.h
  *  @brief load and save data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
  *
  * This file implements the top-level QofBackend API for saving/
  * restoring data to/from an SQL database via libgda
@@ -34,4 +34,25 @@
 G_MODULE_EXPORT void
 qof_backend_module_init(void);
 
+struct GncGdaBackend_struct
+{
+  GncSqlBackend sql_be;
+
+  GdaConnection* pConnection;
+  GdaSqlParser* parser;
+  GdaDataHandler* timespecDH;
+  GdaDataHandler* dateDH;
+
+  QofBook *primary_book;	/* The primary, main open book */
+  gboolean	loading;		/* We are performing an initial load */
+  gboolean  in_query;
+  gboolean  supports_transactions;
+  gboolean  is_pristine_db;	// Are we saving to a new pristine db?
+
+  gint obj_total;			// Total # of objects (for percentage calculation)
+  gint operations_done;		// Number of operations (save/load) done
+  GHashTable* versions;		// Version number for each table
+};
+typedef struct GncGdaBackend_struct GncGdaBackend;
+
 #endif /* GNC_BACKEND_GDA_H_ */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-util-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-util-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-util-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,2290 +0,0 @@
-/********************************************************************
- * gnc-backend-util-gda.c: load and save data to SQL via libgda     *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-backend-util-gda.c
- *  @brief load and save data to SQL - utility functions
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file contains utility routines to support saving/restoring
- * data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <glib/gi18n.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-#include "qofquery-p.h"
-#include "qofquerycore-p.h"
-#include "TransLog.h"
-#include "gnc-engine.h"
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-gconf-utils.h"
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-typedef struct {
-    QofIdType searchObj;
-    gpointer pCompiledQuery;
-} gnc_gda_query_info;
-
-/* callback structure */
-typedef struct {
-    gboolean ok;
-    GncGdaBackend* be;
-    QofInstance* inst;
-    QofQuery* pQuery;
-    gpointer pCompiledQuery;
-    gnc_gda_query_info* pQueryInfo;
-} gda_backend;
-
-static void register_table_version( const GncGdaBackend* be, const gchar* table_name, gint version );
-static gint get_table_version( const GncGdaBackend* be, const gchar* table_name );
-
-/* ================================================================= */
-void
-gnc_gda_add_field_to_query( GdaQuery* query, const gchar* col_name, const GValue* value )
-{
-    GdaQueryField* field;
-    GdaQueryField* field_value;
-
-	g_return_if_fail( query != NULL );
-	g_return_if_fail( col_name != NULL );
-	g_return_if_fail( value != NULL );
-
-    field = gda_query_field_field_new( query, col_name );
-    gda_query_field_set_visible( field, TRUE );
-
-    field_value = gda_query_field_value_new( query, G_VALUE_TYPE(value) );
-    gda_query_field_set_visible( field_value, TRUE );
-    gda_query_field_value_set_value( GDA_QUERY_FIELD_VALUE(field_value), value );
-    g_object_set( field, "value-provider", field_value, NULL );
-    g_object_unref( G_OBJECT(field_value) );
-
-    gda_entity_add_field( GDA_ENTITY(query), GDA_ENTITY_FIELD(field) );
-    g_object_unref( G_OBJECT(field) );
-}
-
-GdaQueryCondition*
-gnc_gda_create_condition_from_field( GdaQuery* query, const gchar* col_name,
-                                const GValue* value )
-{
-    GdaQueryCondition* cond;
-    GdaQueryField* key;
-    GdaQueryField* key_value;
-
-	g_return_val_if_fail( query != NULL, NULL );
-	g_return_val_if_fail( col_name != NULL, NULL );
-	g_return_val_if_fail( value != NULL, NULL );
-
-    cond = gda_query_condition_new( query, GDA_QUERY_CONDITION_LEAF_EQUAL );
-
-    key = gda_query_field_field_new( query, col_name );
-    gda_query_field_set_visible( key, TRUE );
-    gda_query_condition_leaf_set_operator( cond,
-                                            GDA_QUERY_CONDITION_OP_LEFT,
-                                            GDA_QUERY_FIELD(key) );
-    g_object_unref( G_OBJECT(key) );
-
-    key_value = gda_query_field_value_new( query, G_VALUE_TYPE(value) );
-    gda_query_field_set_visible( key_value, TRUE );
-    gda_query_condition_leaf_set_operator( cond, GDA_QUERY_CONDITION_OP_RIGHT,
-                                                GDA_QUERY_FIELD(key_value) );
-    g_object_unref( G_OBJECT(key_value) );
-
-    gda_query_field_value_set_value( GDA_QUERY_FIELD_VALUE(key_value), value );
-
-    return cond;
-}
-/* ----------------------------------------------------------------- */
-static gpointer
-get_autoinc_id( gpointer pObject, const QofParam* param )
-{
-    // Just need a 0 to force a new recurrence id
-    return (gpointer)0;
-}
-
-static void
-set_autoinc_id( gpointer pObject, gpointer pValue )
-{
-    // Nowhere to put the ID
-}
-
-QofAccessFunc
-gnc_gda_get_getter( QofIdTypeConst obj_name, const col_cvt_t* table_row )
-{
-    QofAccessFunc getter;
-
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-
-	if( (table_row->flags & COL_AUTOINC) != 0 ) {
-		getter = get_autoinc_id;
-    } else if( table_row->param_name != NULL ) {
-        getter = qof_class_get_parameter_getter( obj_name,
-                                                table_row->param_name );
-    } else {
-        getter = table_row->getter;
-    }
-
-    return getter;
-}
-
-static void
-load_string( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    const gchar* s;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        s = NULL;
-    } else {
-        s = g_value_get_string( val );
-    }
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, s, NULL );
-    } else {
-		(*setter)( pObject, (const gpointer)s );
-    }
-}
-
-static void
-get_gvalue_string( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GValue* value )
-{
-    QofAccessFunc getter;
-    gchar* s;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-	if( table_row->gobj_param_name != NULL ) {
-		g_object_get( pObject, table_row->gobj_param_name, &s, NULL );
-	} else {
-    	getter = gnc_gda_get_getter( obj_name, table_row );
-    	s = (gchar*)(*getter)( pObject, NULL );
-	}
-    if( s ) {
-        g_value_init( value, G_TYPE_STRING );
-        g_value_set_string( value, s );
-    }
-}
-
-static void
-get_gvalue_string_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    get_gvalue_string( be, obj_name, pObject, table_row, &value );
-    gnc_gda_add_field_to_query( query, table_row->col_name, &value );
-}
-
-static GdaQueryCondition*
-get_gvalue_string_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    get_gvalue_string( be, obj_name, pObject, table_row, &value );
-    return gnc_gda_create_condition_from_field( query, table_row->col_name,
-                                            &value );
-}
-
-static void
-create_string_col( GdaServerProvider* server, GdaConnection* cnn,
-	                GdaServerOperation* op, const col_cvt_t* table_row )
-{
-    const gchar* dbms_type;
-
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    dbms_type = gda_server_provider_get_default_dbms_type( server,
-                                                        cnn, G_TYPE_STRING );
-    gnc_gda_add_table_column( op, table_row->col_name,
-                    dbms_type, table_row->size, table_row->flags );
-}
-
-static col_type_handler_t string_handler
-    = { load_string, create_string_col,
-        get_gvalue_string_for_query, get_gvalue_string_cond };
-/* ----------------------------------------------------------------- */
-typedef gint (*IntAccessFunc)( const gpointer );
-typedef void (*IntSetterFunc)( const gpointer, gint );
-
-static void
-load_int( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    gint int_value;
-	IntSetterFunc i_setter;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        int_value = 0;
-    } else {
-        int_value = g_value_get_int( val );
-    }
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, int_value, NULL );
-    } else {
-		i_setter = (IntSetterFunc)setter;
-    	(*i_setter)( pObject, int_value );
-    }
-}
-
-static void
-get_gvalue_int( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GValue* value )
-{
-    gint int_value;
-    IntAccessFunc i_getter;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-
-    i_getter = (IntAccessFunc)gnc_gda_get_getter( obj_name, table_row );
-    int_value = (*i_getter)( pObject );
-    g_value_init( value, G_TYPE_INT );
-    g_value_set_int( value, int_value );
-}
-
-static void
-get_gvalue_int_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    get_gvalue_int( be, obj_name, pObject, table_row, &value );
-    gnc_gda_add_field_to_query( query, table_row->col_name, &value );
-}
-
-static GdaQueryCondition*
-get_gvalue_int_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    get_gvalue_int( be, obj_name, pObject, table_row, &value );
-    return gnc_gda_create_condition_from_field( query, table_row->col_name, &value );
-}
-
-static void
-create_int_col( GdaServerProvider* server, GdaConnection* cnn,
-            GdaServerOperation* op, const col_cvt_t* table_row )
-{
-    const gchar* dbms_type;
-
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    dbms_type = gda_server_provider_get_default_dbms_type( server,
-                                                        cnn, G_TYPE_INT );
-    gnc_gda_add_table_column( op, table_row->col_name,
-                    dbms_type, table_row->size, table_row->flags );
-}
-
-static col_type_handler_t int_handler =
-        { load_int, create_int_col,
-            get_gvalue_int_for_query, get_gvalue_int_cond };
-/* ----------------------------------------------------------------- */
-typedef gboolean (*BooleanAccessFunc)( const gpointer );
-typedef void (*BooleanSetterFunc)( const gpointer, gboolean );
-
-static void
-load_boolean( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    gint int_value;
-	BooleanSetterFunc b_setter;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        int_value = 0;
-    } else {
-        int_value = g_value_get_int( val );
-    }
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, int_value, NULL );
-    } else {
-		b_setter = (BooleanSetterFunc)setter;
-    	(*b_setter)( pObject, int_value ? TRUE : FALSE );
-    }
-}
-
-static void
-get_gvalue_boolean( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GValue* value )
-{
-    gint int_value;
-    BooleanAccessFunc b_getter;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-
-    b_getter = (BooleanAccessFunc)gnc_gda_get_getter( obj_name, table_row );
-    int_value = ((*b_getter)( pObject )) ? 1 : 0;
-    g_value_init( value, G_TYPE_INT );
-    g_value_set_int( value, int_value );
-}
-
-static void
-get_gvalue_boolean_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    get_gvalue_int( be, obj_name, pObject, table_row, &value );
-    gnc_gda_add_field_to_query( query, table_row->col_name, &value );
-}
-
-static GdaQueryCondition*
-get_gvalue_boolean_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    get_gvalue_int( be, obj_name, pObject, table_row, &value );
-    return gnc_gda_create_condition_from_field( query, table_row->col_name, &value );
-}
-
-static void
-create_boolean_col( GdaServerProvider* server, GdaConnection* cnn,
-            		GdaServerOperation* op, const col_cvt_t* table_row )
-{
-    const gchar* dbms_type;
-
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    dbms_type = gda_server_provider_get_default_dbms_type( server,
-                                                        cnn, G_TYPE_INT );
-    gnc_gda_add_table_column( op, table_row->col_name,
-                    dbms_type, table_row->size, table_row->flags );
-}
-
-static col_type_handler_t boolean_handler =
-        { load_boolean, create_boolean_col,
-            get_gvalue_boolean_for_query, get_gvalue_boolean_cond };
-/* ----------------------------------------------------------------- */
-typedef gint64 (*Int64AccessFunc)( const gpointer );
-typedef void (*Int64SetterFunc)( const gpointer, gint64 );
-
-static void
-load_int64( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    gint64 i64_value = 0;
-	Int64SetterFunc i64_setter = (Int64SetterFunc)setter;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( setter != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( !gda_value_is_null( val ) ) {
-        i64_value = g_value_get_int64( val );
-    }
-    (*i64_setter)( pObject, i64_value );
-}
-
-static void
-get_gvalue_int64( const GncGdaBackend* be, QofIdTypeConst obj_name, const gpointer pObject,
-                const col_cvt_t* table_row, GValue* value )
-{
-    gint64 i64_value;
-    Int64AccessFunc getter;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-    getter = (Int64AccessFunc)gnc_gda_get_getter( obj_name, table_row );
-    i64_value = (*getter)( pObject );
-    g_value_init( value, G_TYPE_INT64 );
-    g_value_set_int64( value, i64_value );
-}
-
-static void
-get_gvalue_int64_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    get_gvalue_int64( be, obj_name, pObject, table_row, &value );
-    gnc_gda_add_field_to_query( query, table_row->col_name, &value );
-}
-
-static GdaQueryCondition*
-get_gvalue_int64_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    get_gvalue_int64( be, obj_name, pObject, table_row, &value );
-    return gnc_gda_create_condition_from_field( query, table_row->col_name, &value );
-}
-
-static void
-create_int64_col( GdaServerProvider* server, GdaConnection* cnn,
-            		GdaServerOperation* op, const col_cvt_t* table_row )
-{
-    const gchar* dbms_type;
-
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    dbms_type = gda_server_provider_get_default_dbms_type( server,
-                                                        cnn, G_TYPE_INT64 );
-    gnc_gda_add_table_column( op, table_row->col_name,
-                    dbms_type, table_row->size, table_row->flags );
-}
-
-static col_type_handler_t int64_handler =
-        { load_int64, create_int64_col,
-            get_gvalue_int64_for_query, get_gvalue_int64_cond };
-/* ----------------------------------------------------------------- */
-
-static void
-load_double( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    gdouble d_value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        (*setter)( pObject, (gpointer)NULL );
-    } else {
-        d_value = g_value_get_double( val );
-        (*setter)( pObject, (gpointer)&d_value );
-    }
-}
-
-static void
-get_gvalue_double( const GncGdaBackend* be, QofIdTypeConst obj_name, const gpointer pObject,
-                const col_cvt_t* table_row, GValue* value )
-{
-    QofAccessFunc getter;
-    gdouble* pDouble;
-    gdouble d_value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-
-    getter = gnc_gda_get_getter( obj_name, table_row );
-    pDouble = (*getter)( pObject, NULL );
-    if( pDouble != NULL ) {
-        d_value = *pDouble;
-        g_value_init( value, G_TYPE_DOUBLE );
-        g_value_set_double( value, d_value );
-    }
-}
-
-static void
-get_gvalue_double_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    get_gvalue_double( be, obj_name, pObject, table_row, &value );
-    gnc_gda_add_field_to_query( query, table_row->col_name, &value );
-}
-
-static GdaQueryCondition*
-get_gvalue_double_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    get_gvalue_double( be, obj_name, pObject, table_row, &value );
-    return gnc_gda_create_condition_from_field( query, table_row->col_name, &value );
-}
-
-static void
-create_double_col( GdaServerProvider* server, GdaConnection* cnn,
-            		GdaServerOperation* op, const col_cvt_t* table_row )
-{
-    const gchar* dbms_type;
-
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    dbms_type = gda_server_provider_get_default_dbms_type( server,
-                                                        cnn, G_TYPE_INT64 );
-    gnc_gda_add_table_column( op, table_row->col_name,
-                    dbms_type, table_row->size, table_row->flags );
-}
-
-static col_type_handler_t double_handler =
-        { load_double, create_double_col,
-            get_gvalue_double_for_query, get_gvalue_double_cond };
-/* ----------------------------------------------------------------- */
-
-static void
-load_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GUID guid;
-    const GUID* pGuid;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        pGuid = NULL;
-    } else {
-        string_to_guid( g_value_get_string( val ), &guid );
-        pGuid = &guid;
-    }
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, pGuid, NULL );
-    } else {
-		(*setter)( pObject, (const gpointer)pGuid );
-    }
-}
-
-static void
-get_gvalue_guid( const GncGdaBackend* be, QofIdTypeConst obj_name, const gpointer pObject,
-                const col_cvt_t* table_row, GValue* value )
-{
-    QofAccessFunc getter;
-    const GUID* guid;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-
-	if( table_row->gobj_param_name != NULL ) {
-		g_object_get( pObject, table_row->gobj_param_name, &guid, NULL );
-	} else {
-    	getter = gnc_gda_get_getter( obj_name, table_row );
-    	guid = (*getter)( pObject, NULL );
-	}
-    if( guid != NULL ) {
-        (void)guid_to_string_buff( guid, guid_buf );
-        g_value_init( value, G_TYPE_STRING );
-        g_value_set_string( value, guid_buf );
-    }
-}
-
-static void
-get_gvalue_guid_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    get_gvalue_guid( be, obj_name, pObject, table_row, &value );
-    gnc_gda_add_field_to_query( query, table_row->col_name, &value );
-}
-
-static GdaQueryCondition*
-get_gvalue_guid_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    get_gvalue_guid( be, obj_name, pObject, table_row, &value );
-    return gnc_gda_create_condition_from_field( query, table_row->col_name, &value );
-}
-
-static void
-create_guid_col( GdaServerProvider* server, GdaConnection* cnn,
-	            GdaServerOperation* op, const col_cvt_t* table_row )
-{
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    gnc_gda_add_table_column( op, table_row->col_name,
-                    "char", GUID_ENCODING_LENGTH, table_row->flags );
-}
-
-static col_type_handler_t guid_handler =
-        { load_guid, create_guid_col,
-            get_gvalue_guid_for_query, get_gvalue_guid_cond };
-/* ----------------------------------------------------------------- */
-
-static void
-get_gvalue_objectref_guid( const GncGdaBackend* be, QofIdTypeConst obj_name, const gpointer pObject,
-                const col_cvt_t* table_row, GValue* value )
-{
-    QofAccessFunc getter;
-    const GUID* guid = NULL;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-	QofInstance* inst;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-
-	if( table_row->gobj_param_name != NULL ) {
-		g_object_get( pObject, table_row->gobj_param_name, &inst, NULL );
-	} else {
-    	getter = gnc_gda_get_getter( obj_name, table_row );
-    	inst = (*getter)( pObject, NULL );
-	}
-	if( inst != NULL ) {
-		guid = qof_instance_get_guid( inst );
-	}
-    if( guid != NULL ) {
-        (void)guid_to_string_buff( guid, guid_buf );
-        g_value_init( value, G_TYPE_STRING );
-        g_value_set_string( value, guid_buf );
-    }
-}
-
-void
-gnc_gda_get_gvalue_objectref_guid_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    get_gvalue_objectref_guid( be, obj_name, pObject, table_row, &value );
-    gnc_gda_add_field_to_query( query, table_row->col_name, &value );
-}
-
-GdaQueryCondition*
-gnc_gda_get_gvalue_objectref_guid_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    get_gvalue_objectref_guid( be, obj_name, pObject, table_row, &value );
-    return gnc_gda_create_condition_from_field( query, table_row->col_name, &value );
-}
-
-void
-gnc_gda_create_objectref_guid_col( GdaServerProvider* server, GdaConnection* cnn,
-            					GdaServerOperation* op, const col_cvt_t* table_row )
-{
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    gnc_gda_add_table_column( op, table_row->col_name,
-                    "char", GUID_ENCODING_LENGTH, table_row->flags );
-}
-
-/* ----------------------------------------------------------------- */
-typedef Timespec (*TimespecAccessFunc)( const gpointer );
-typedef void (*TimespecSetterFunc)( const gpointer, Timespec );
-
-static void
-load_timespec( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GDate* date;
-    Timespec ts = {0, 0};
-	TimespecSetterFunc ts_setter;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-	ts_setter = (TimespecSetterFunc)setter;
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        (*ts_setter)( pObject, ts );
-    } else {
-		if( G_VALUE_HOLDS_STRING( val ) ) {
-			const gchar* s = g_value_get_string( val );
-			gchar* buf;
-			buf = g_strdup_printf( "%c%c%c%c-%c%c-%c%c 00:00:00",
-									s[6], s[7], s[8], s[9],
-									s[0], s[1],
-									s[3], s[4] );
-		    ts = gnc_iso8601_to_timespec_gmt( buf );
-			(*ts_setter)( pObject, ts );
-			g_free( buf );
-
-		} else if( G_VALUE_HOLDS_BOXED( val ) ) {
-        	date = (GDate*)g_value_get_boxed( val );
-        	if( date != NULL ) {
-            	ts = gnc_dmy2timespec( g_date_get_day( date ),
-                                g_date_get_month( date ),
-                                g_date_get_year( date ) );
-            	(*ts_setter)( pObject, ts );
-			}
-		} else {
-			PWARN( "Unknown timespec type: %s", G_VALUE_TYPE_NAME( val ) );
-        }
-    }
-}
-
-static void
-get_gvalue_timespec( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GValue* value )
-{
-    TimespecAccessFunc getter;
-    Timespec ts;
-    GDate* date;
-    gint y, m, d;
-    gchar iso8601_buf[33];
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-
-    getter = (TimespecAccessFunc)gnc_gda_get_getter( obj_name, table_row );
-    ts = (*getter)( pObject );
-
-    date = g_date_new();
-    (void)gnc_timespec_to_iso8601_buff( ts, iso8601_buf );
-    sscanf( iso8601_buf, "%d-%d-%d", &y, &m, &d );
-    g_date_set_dmy( date, d, m, y );
-    g_value_init( value, G_TYPE_DATE );
-    g_value_take_boxed( value, date );
-}
-
-static void
-get_gvalue_timespec_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    get_gvalue_timespec( be, obj_name, pObject, table_row, &value );
-    gnc_gda_add_field_to_query( query, table_row->col_name, &value );
-}
-
-static GdaQueryCondition*
-get_gvalue_timespec_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    get_gvalue_timespec( be, obj_name, pObject, table_row, &value );
-    return gnc_gda_create_condition_from_field( query, table_row->col_name, &value );
-}
-
-static void
-create_timespec_col( GdaServerProvider* server, GdaConnection* cnn,
-		            GdaServerOperation* op, const col_cvt_t* table_row )
-{
-    const gchar* dbms_type;
-
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    dbms_type = gda_server_provider_get_default_dbms_type( server,
-                                                        cnn, G_TYPE_DATE );
-    gnc_gda_add_table_column( op, table_row->col_name,
-                    dbms_type, table_row->size, table_row->flags );
-}
-
-static col_type_handler_t timespec_handler =
-        { load_timespec, create_timespec_col,
-            get_gvalue_timespec_for_query, get_gvalue_timespec_cond };
-/* ----------------------------------------------------------------- */
-static void
-load_date( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GDate* date;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-		date = g_date_new_dmy( 1, 1, 1970 );
-        (*setter)( pObject, date );
-		g_date_free( date );
-    } else {
-		if( G_VALUE_HOLDS_STRING( val ) ) {
-			const gchar* s = g_value_get_string( val );
-			guint year = atoi( &s[6] );
-			guint month = atoi( &s[0] );
-			guint day = atoi( &s[3] );
-
-			date = g_date_new_dmy( day, month, year );
-			(*setter)( pObject, date );
-			g_date_free( date );
-
-		} else if( G_VALUE_HOLDS_BOXED( val ) ) {
-        	date = (GDate*)g_value_get_boxed( val );
-        	if( date != NULL ) {
-            	(*setter)( pObject, date );
-				g_date_free( date );
-			}
-		} else {
-			PWARN( "Unknown timespec type: %s", G_VALUE_TYPE_NAME( val ) );
-        }
-    }
-}
-
-static void
-get_gvalue_date( const GncGdaBackend* be, QofIdTypeConst obj_name, const gpointer pObject,
-                const col_cvt_t* table_row, GValue* value )
-{
-    GDate* date;
-    QofAccessFunc getter;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-
-    getter = gnc_gda_get_getter( obj_name, table_row );
-    date = (GDate*)(*getter)( pObject, NULL );
-    if( date != NULL ) {
-        g_value_init( value, G_TYPE_DATE );
-        g_value_set_boxed( value, date );
-    }
-}
-
-static void
-get_gvalue_date_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    get_gvalue_date( be, obj_name, pObject, table_row, &value );
-    gnc_gda_add_field_to_query( query, table_row->col_name, &value );
-}
-
-static GdaQueryCondition*
-get_gvalue_date_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    get_gvalue_date( be, obj_name, pObject, table_row, &value );
-    return gnc_gda_create_condition_from_field( query, table_row->col_name, &value );
-}
-
-static col_type_handler_t date_handler =
-        { load_date, create_timespec_col,
-            get_gvalue_date_for_query, get_gvalue_date_cond };
-/* ----------------------------------------------------------------- */
-static gint64
-get_integer_value( const GValue* value )
-{
-	g_return_val_if_fail( value != NULL, 0 );
-
-	if( G_VALUE_HOLDS_INT(value) ) {
-		return g_value_get_int( value );
-	} else if( G_VALUE_HOLDS_UINT(value) ) {
-		return g_value_get_uint( value );
-	} else if( G_VALUE_HOLDS_LONG(value) ) {
-		return g_value_get_long( value );
-	} else if( G_VALUE_HOLDS_ULONG(value) ) {
-		return g_value_get_ulong( value );
-	} else if( G_VALUE_HOLDS_INT64(value) ) {
-		return g_value_get_int64( value );
-	} else if( G_VALUE_HOLDS_UINT64(value) ) {
-		return g_value_get_uint64( value );
-	} else {
-		PWARN( "Unknown type: %s", G_VALUE_TYPE_NAME( value ) );
-	}
-
-	return 0;
-}
-
-typedef gnc_numeric (*NumericGetterFunc)( const gpointer );
-typedef void (*NumericSetterFunc)( gpointer, gnc_numeric );
-
-static void
-load_numeric( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    gchar* buf;
-    gint64 num, denom;
-    gnc_numeric n;
-    gboolean isNull = FALSE;
-	NumericSetterFunc n_setter = (NumericSetterFunc)setter;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    buf = g_strdup_printf( "%s_num", table_row->col_name );
-    val = gda_data_model_get_value_at_col_name( pModel, buf, row );
-    g_free( buf );
-    if( gda_value_is_null( val ) ) {
-        isNull = TRUE;
-        num = 0;
-    } else {
-        num = get_integer_value( val );
-    }
-    buf = g_strdup_printf( "%s_denom", table_row->col_name );
-    val = gda_data_model_get_value_at_col_name( pModel, buf, row );
-    g_free( buf );
-    if( gda_value_is_null( val ) ) {
-        isNull = TRUE;
-        denom = 1;
-    } else {
-        denom = get_integer_value( val );
-    }
-    n = gnc_numeric_create( num, denom );
-    if( !isNull ) {
-        (*n_setter)( pObject, n );
-    }
-}
-
-static void
-get_gvalue_numeric( const GncGdaBackend* be, QofIdTypeConst obj_name, const gpointer pObject,
-                const col_cvt_t* table_row, GValue* value )
-{
-    NumericGetterFunc getter;
-    gnc_numeric n;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-
-    getter = (NumericGetterFunc)gnc_gda_get_getter( obj_name, table_row );
-    n = (*getter)( pObject );
-    g_value_init( value, gnc_numeric_get_type() );
-    g_value_set_boxed( value, &n );
-}
-
-static void
-get_gvalue_numeric_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-    GValue num_value;
-    GValue denom_value;
-    gnc_numeric* n;
-    gchar* s;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    memset( &value, 0, sizeof( GValue ) );
-    memset( &num_value, 0, sizeof( GValue ) );
-    memset( &denom_value, 0, sizeof( GValue ) );
-
-    get_gvalue_numeric( be, obj_name, pObject, table_row, &value );
-    if( G_VALUE_TYPE(&value) != 0 ) {
-        n = g_value_get_boxed( &value );
-        g_value_init( &num_value, G_TYPE_INT64 );
-        g_value_set_int64( &num_value, gnc_numeric_num( *n ) );
-        g_value_init( &denom_value, G_TYPE_INT64 );
-        g_value_set_int64( &denom_value, gnc_numeric_denom( *n ) );
-    }
-
-    s = g_strdup_printf( "%s_num", table_row->col_name );
-    gnc_gda_add_field_to_query( query, s, &num_value );
-    g_free( s );
-    s = g_strdup_printf( "%s_denom", table_row->col_name );
-    gnc_gda_add_field_to_query( query, s, &denom_value );
-    g_free( s );
-}
-
-static GdaQueryCondition*
-get_gvalue_numeric_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-    GValue num_value;
-    GValue denom_value;
-    gnc_numeric* n;
-    gchar* s;
-    GdaQueryCondition* num_cond;
-    GdaQueryCondition* denom_cond;
-    GdaQueryCondition* cond;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    memset( &value, 0, sizeof( GValue ) );
-    memset( &num_value, 0, sizeof( GValue ) );
-    memset( &denom_value, 0, sizeof( GValue ) );
-
-    get_gvalue_numeric( be, obj_name, pObject, table_row, &value );
-    if( G_VALUE_TYPE(&value) != 0 ) {
-        n = g_value_get_boxed( &value );
-        g_value_init( &num_value, G_TYPE_INT64 );
-        g_value_set_int64( &num_value, gnc_numeric_num( *n ) );
-        g_value_init( &denom_value, G_TYPE_INT64 );
-        g_value_set_int64( &denom_value, gnc_numeric_denom( *n ) );
-    }
-
-    s = g_strdup_printf( "%s_num", table_row->col_name );
-    num_cond = gnc_gda_create_condition_from_field( query, s, &value );
-    g_free( s );
-    s = g_strdup_printf( "%s_denom", table_row->col_name );
-    denom_cond = gnc_gda_create_condition_from_field( query, s, &value );
-    g_free( s );
-
-    cond = gda_query_condition_new( query, GDA_QUERY_CONDITION_NODE_AND );
-    gda_query_condition_node_add_child( cond, num_cond, NULL );
-    gda_query_condition_node_add_child( cond, denom_cond, NULL );
-
-    return cond;
-}
-
-static void
-create_numeric_col( GdaServerProvider* server, GdaConnection* cnn,
-            	GdaServerOperation* op, const col_cvt_t* table_row )
-{
-    const gchar* dbms_type;
-    gchar* buf;
-
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    dbms_type = gda_server_provider_get_default_dbms_type( server, cnn,
-                                                            G_TYPE_INT64 );
-    buf = g_strdup_printf( "%s_num", table_row->col_name );
-    gnc_gda_add_table_column( op, buf, dbms_type,
-                        table_row->size, table_row->flags );
-    g_free( buf );
-    buf = g_strdup_printf( "%s_denom", table_row->col_name );
-    gnc_gda_add_table_column( op, buf, dbms_type,
-                        table_row->size, table_row->flags );
-    g_free( buf );
-}
-
-static col_type_handler_t numeric_handler =
-        { load_numeric, create_numeric_col,
-            get_gvalue_numeric_for_query, get_gvalue_numeric_cond };
-/* ================================================================= */
-
-static GHashTable* g_columnTypeHash = NULL;
-
-void
-gnc_gda_register_col_type_handler( const gchar* colType, const col_type_handler_t* handler )
-{
-	g_return_if_fail( colType != NULL );
-	g_return_if_fail( handler != NULL );
-
-	if( g_columnTypeHash == NULL ) {
-		g_columnTypeHash = g_hash_table_new( g_str_hash, g_str_equal );
-	}
-
-	g_hash_table_insert( g_columnTypeHash, (gpointer)colType, (gpointer)handler );
-	DEBUG( "Col type %s registered\n", colType );
-}
-
-static col_type_handler_t*
-get_handler( const gchar* col_type )
-{
-    col_type_handler_t* pHandler;
-
-	g_return_val_if_fail( col_type != NULL, NULL );
-
-	pHandler = g_hash_table_lookup( g_columnTypeHash, col_type );
-	if( pHandler == NULL ) {
-        g_assert( FALSE );
-    }
-
-    return pHandler;
-}
-
-void
-gnc_gda_register_standard_col_type_handlers( void )
-{
-	gnc_gda_register_col_type_handler( CT_STRING, &string_handler );
-    gnc_gda_register_col_type_handler( CT_BOOLEAN, &boolean_handler );
-    gnc_gda_register_col_type_handler( CT_INT, &int_handler );
-    gnc_gda_register_col_type_handler( CT_INT64, &int64_handler );
-    gnc_gda_register_col_type_handler( CT_DOUBLE, &double_handler );
-    gnc_gda_register_col_type_handler( CT_GUID, &guid_handler );
-    gnc_gda_register_col_type_handler( CT_TIMESPEC, &timespec_handler );
-    gnc_gda_register_col_type_handler( CT_GDATE, &date_handler );
-    gnc_gda_register_col_type_handler( CT_NUMERIC, &numeric_handler );
-}
-
-void 
-_retrieve_guid_( gpointer pObject, gpointer pValue )
-{
-    GUID* pGuid = (GUID*)pObject;
-    GUID* guid = (GUID*)pValue;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( pValue != NULL );
-
-	memcpy( pGuid, guid, sizeof( GUID ) );
-}
-
-
-// Table to retrieve just the guid
-static col_cvt_t guid_table[] =
-{
-    { "guid", CT_GUID, 0, 0, NULL, NULL, NULL, _retrieve_guid_ },
-    { NULL }
-};
-
-const GUID*
-gnc_gda_load_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row )
-{
-	static GUID guid;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( pModel != NULL, NULL );
-	g_return_val_if_fail( row >= 0, NULL );
-
-    gnc_gda_load_object( be, pModel, row, NULL, &guid, guid_table );
-
-    return &guid;
-}
-
-// Table to retrieve just the guid
-static col_cvt_t tx_guid_table[] =
-{
-    { "tx_guid", CT_GUID, 0, 0, NULL, NULL, NULL, _retrieve_guid_ },
-    { NULL }
-};
-
-const GUID*
-gnc_gda_load_tx_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row )
-{
-    static GUID guid;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( pModel != NULL, NULL );
-	g_return_val_if_fail( row >= 0, NULL );
-
-    gnc_gda_load_object( be, pModel, row, NULL, &guid, tx_guid_table );
-
-    return &guid;
-}
-
-void
-gnc_gda_load_object( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-                    QofIdTypeConst obj_name, gpointer pObject,
-                    const col_cvt_t* table_row )
-{
-    int col;
-    QofSetterFunc setter;
-    col_type_handler_t* pHandler;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    for( col = 0; table_row[col].col_name != NULL; col++ ) {
-		if( (table_row[col].flags & COL_AUTOINC) != 0 ) {
-			setter = set_autoinc_id;
-        } else if( table_row[col].param_name != NULL ) {
-            setter = qof_class_get_parameter_setter( obj_name,
-                                                    table_row[col].param_name );
-        } else {
-            setter = table_row[col].setter;
-        }
-        pHandler = get_handler( table_row[col].col_type );
-        pHandler->load_fn( be, pModel, row, setter, pObject, &table_row[col] );
-    }
-}
-
-/* ================================================================= */
-GdaQuery*
-gnc_gda_create_select_query( const GncGdaBackend* be, const gchar* table_name )
-{
-    GdaQuery* query;
-    GdaQueryTarget* target;
-    GdaQueryField* allFields;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( table_name != NULL, NULL );
-
-    /* SELECT */
-    query = gda_query_new( be->pDict );
-    gda_query_set_query_type( query, GDA_QUERY_TYPE_SELECT );
-
-    /* FROM */
-    target = gda_query_target_new( query, table_name );
-    gda_query_add_target( query, target, NULL );
-    g_object_unref( G_OBJECT(target) );
-
-    /* all fields */
-    allFields = gda_query_field_all_new( query, table_name );
-    gda_query_field_set_visible( allFields, TRUE );
-    gda_entity_add_field( GDA_ENTITY(query), GDA_ENTITY_FIELD(allFields) );
-    g_object_unref( G_OBJECT(allFields) );
-
-    return query;
-}
-
-/* ================================================================= */
-GdaObject*
-gnc_gda_execute_query( GncGdaBackend* be, GdaQuery* query )
-{
-    GError* error = NULL;
-    GdaObject* ret;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    ret = gda_query_execute( query, NULL, FALSE, &error );
-
-    if( error != NULL ) {
-        PERR( "SQL error: %s\n", error->message );
-		qof_backend_set_error( &be->be, ERR_BACKEND_SERVER_ERR );
-    }
-
-    return ret;
-}
-
-GdaQuery*
-gnc_gda_create_query_from_sql( const GncGdaBackend* be, const gchar* sql )
-{
-    GError* error = NULL;
-    GdaQuery* query;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( sql != NULL, NULL );
-
-    query = gda_query_new_from_sql( be->pDict, sql, &error );
-    if( query == NULL ) {
-        PERR( "SQL error: %s\n", error->message );
-    }
-
-	return query;
-}
-
-GdaDataModel*
-gnc_gda_execute_select_sql( const GncGdaBackend* be, const gchar* sql )
-{
-	GdaCommand* cmd;
-    GError* error = NULL;
-	GdaDataModel* model;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( sql != NULL, NULL );
-
-	cmd = gda_command_new( sql, GDA_COMMAND_TYPE_SQL, 0 );
-    model = gda_connection_execute_select_command( be->pConnection, cmd, NULL, &error );
-	gda_command_free( cmd );
-    if( error != NULL ) {
-        PERR( "SQL error: %s\n", error->message );
-    }
-
-	return model;
-}
-
-gint
-gnc_gda_execute_nonselect_sql( const GncGdaBackend* be, const gchar* sql )
-{
-	GdaCommand* cmd;
-    GError* error = NULL;
-	gint ret;
-
-	g_return_val_if_fail( be != NULL, 0 );
-	g_return_val_if_fail( sql != NULL, 0 );
-
-	cmd = gda_command_new( sql, GDA_COMMAND_TYPE_SQL, 0 );
-    ret = gda_connection_execute_non_select_command( be->pConnection, cmd, NULL, &error );
-	gda_command_free( cmd );
-    if( error != NULL ) {
-        PERR( "SQL error: %s\n", error->message );
-    }
-
-	return ret;
-}
-
-int
-gnc_gda_execute_select_get_count( const GncGdaBackend* be, const gchar* sql )
-{
-    int count = 0;
-    GdaDataModel* model;
-
-	g_return_val_if_fail( be != NULL, 0 );
-	g_return_val_if_fail( sql != NULL, 0 );
-
-    model = gnc_gda_execute_select_sql( be, sql );
-    if( model != NULL ) {
-        count = gda_data_model_get_n_rows( model );
-    }
-
-    return count;
-}
-
-int
-gnc_gda_execute_query_get_count( GncGdaBackend* be, GdaQuery* query )
-{
-    int count = 0;
-    GdaObject* ret;
-
-	g_return_val_if_fail( be != NULL, 0 );
-	g_return_val_if_fail( query != NULL, 0 );
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL(ret) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        count = gda_data_model_get_n_rows( pModel );
-    }
-
-    return count;
-}
-
-guint
-gnc_gda_append_guid_list_to_sql( GString* sql, GList* list, guint maxCount )
-{
-	gchar guid_buf[GUID_ENCODING_LENGTH+1];
-	gboolean first_guid = TRUE;
-	guint count;
-
-	g_return_val_if_fail( sql != NULL, 0 );
-
-	if( list == NULL ) return 0;
-
-	for( count = 0; list != NULL && count < maxCount; list = list->next, count++ ) {
-		QofInstance* inst = QOF_INSTANCE(list->data);
-    	guid_to_string_buff( qof_instance_get_guid( inst ), guid_buf );
-
-		if( !first_guid ) {
-			g_string_append( sql, "," );
-		}
-		g_string_append( sql, "'" );
-		g_string_append( sql, guid_buf );
-		g_string_append( sql, "'" );
-		first_guid = FALSE;
-    }
-
-	return count;
-}
-/* ================================================================= */
-static void
-get_col_gvalue_for_query( GncGdaBackend* be, QofIdTypeConst obj_name,
-                        gpointer pObject, const col_cvt_t* table_row,
-                        GdaQuery* query )
-{
-    col_type_handler_t* pHandler;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    pHandler = get_handler( table_row->col_type );
-    pHandler->get_gvalue_query_fn( be, obj_name, pObject, table_row, query );
-}
-
-static void
-get_col_gvalue_for_condition( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                        gpointer pObject, const col_cvt_t* table_row,
-                        GdaQuery* query )
-{
-    col_type_handler_t* pHandler;
-    GdaQueryCondition* cond;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    pHandler = get_handler( table_row->col_type );
-    cond = pHandler->get_gvalue_cond_fn( be, obj_name, pObject, table_row, query );
-
-    gda_query_set_condition( query, cond );
-    g_object_unref( G_OBJECT(cond) );
-}
-
-gboolean
-gnc_gda_object_is_it_in_db( GncGdaBackend* be, const gchar* table_name,
-                    QofIdTypeConst obj_name, gpointer pObject,
-                    const col_cvt_t* table )
-{
-    GdaQuery* query;
-    int count;
-
-	g_return_val_if_fail( be != NULL, FALSE );
-	g_return_val_if_fail( table_name != NULL, FALSE );
-	g_return_val_if_fail( obj_name != NULL, FALSE );
-	g_return_val_if_fail( pObject != NULL, FALSE );
-	g_return_val_if_fail( table != NULL, FALSE );
-
-    /* SELECT * FROM */
-    query = gnc_gda_create_select_query( be, table_name );
-
-    /* WHERE */
-    get_col_gvalue_for_condition( be, obj_name, pObject, &table[0], query );
-
-    count = gnc_gda_execute_query_get_count( be, query );
-    if( count == 0 ) {
-        return FALSE;
-    } else {
-        return TRUE;
-    }
-}
-
-gboolean
-gnc_gda_do_db_operation( GncGdaBackend* be,
-                        E_DB_OPERATION op,
-                        const gchar* table_name,
-                        QofIdTypeConst obj_name, gpointer pObject,
-                        const col_cvt_t* table )
-{
-    GdaQuery* pQuery;
-
-	g_return_val_if_fail( be != NULL, FALSE );
-	g_return_val_if_fail( table_name != NULL, FALSE );
-	g_return_val_if_fail( obj_name != NULL, FALSE );
-	g_return_val_if_fail( pObject != NULL, FALSE );
-	g_return_val_if_fail( table != NULL, FALSE );
-
-    if( op == OP_DB_ADD_OR_UPDATE ) {
-        if( gnc_gda_object_is_it_in_db( be, table_name, obj_name, pObject, table ) ) {
-            pQuery = gnc_gda_build_update_query( be, table_name, obj_name, pObject, table );
-        } else {
-            pQuery = gnc_gda_build_insert_query( be, table_name, obj_name, pObject, table );
-        }
-    } else if( op == OP_DB_DELETE ) {
-        pQuery = gnc_gda_build_delete_query( be, table_name, obj_name, pObject, table );
-    } else if( op == OP_DB_ADD ) {
-        pQuery = gnc_gda_build_insert_query( be, table_name, obj_name, pObject, table );
-    } else {
-        g_assert( FALSE );
-    }
-    if( pQuery != NULL ) {
-        gnc_gda_execute_query( be, pQuery );
-        g_object_unref( G_OBJECT(pQuery) );
-
-        return TRUE;
-    } else {
-        return FALSE;
-    }
-}
-
-#define INITIAL_SQL_BUF_LEN 500
-
-GdaQuery*
-gnc_gda_build_insert_query( GncGdaBackend* be,
-                            const gchar* table_name,
-                            QofIdTypeConst obj_name, gpointer pObject,
-                            const col_cvt_t* table )
-{
-    GdaQuery* query;
-    int col;
-    GdaQueryTarget* target;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( table_name != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table != NULL, NULL );
-
-
-    /* INSERT */
-    query = gda_query_new( be->pDict );
-    gda_query_set_query_type( query, GDA_QUERY_TYPE_INSERT );
-
-    /* INTO */
-    target = gda_query_target_new( query, table_name );
-    gda_query_add_target( query, target, NULL );
-
-    /* VALUES */
-    for( col = 0; table[col].col_name != NULL; col++ ) {
-		if(( table[col].flags & COL_AUTOINC ) == 0 ) {
-	        get_col_gvalue_for_query( be, obj_name, pObject, &table[col], query );
-		}
-    }
-
-    return query;
-}
-
-GdaQuery*
-gnc_gda_build_update_query( GncGdaBackend* be,
-                            const gchar* table_name,
-                            QofIdTypeConst obj_name, gpointer pObject,
-                            const col_cvt_t* table )
-{
-    GdaQuery* query;
-    int col;
-    GdaQueryTarget* target;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( table_name != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table != NULL, NULL );
-
-    /* UPDATE */
-    query = gda_query_new( be->pDict );
-    gda_query_set_query_type( query, GDA_QUERY_TYPE_UPDATE );
-    target = gda_query_target_new( query, table_name );
-    gda_query_add_target( query, target, NULL );
-
-    /* SET */
-    for( col = 1; table[col].col_name != NULL; col++ ) {
-        get_col_gvalue_for_query( be, obj_name, pObject, &table[col], query );
-    }
-
-    /* WHERE */
-    get_col_gvalue_for_condition( be, obj_name, pObject, &table[0], query );
-
-    return query;
-}
-
-GdaQuery*
-gnc_gda_build_delete_query( GncGdaBackend* be,
-                            const gchar* table_name,
-                            QofIdTypeConst obj_name, gpointer pObject,
-                            const col_cvt_t* table )
-{
-    GdaQuery* query;
-    GdaQueryTarget* target;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( table_name != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table != NULL, NULL );
-
-    /* DELETE */
-    query = gda_query_new( be->pDict );
-    gda_query_set_query_type( query, GDA_QUERY_TYPE_DELETE );
-
-    /* FROM */
-    target = gda_query_target_new( query, table_name );
-    gda_query_add_target( query, target, NULL );
-    g_object_unref( G_OBJECT(target) );
-
-    /* WHERE */
-    get_col_gvalue_for_condition( be, obj_name, pObject, &table[0], query );
-
-    return query;
-}
-
-/* ================================================================= */
-void
-gnc_gda_add_table_column( GdaServerOperation* op, const gchar* arg, const gchar* dbms_type,
-            gint size, gint flags )
-{
-    gchar* buf;
-	GdaServerOperationNode* node;
-	GdaDataModel* model;
-	gint col_num;
-	GError* error = NULL;
-	gboolean ok;
-
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( arg != NULL );
-	g_return_if_fail( dbms_type != NULL );
-
-	node = gda_server_operation_get_node_info( op, "/FIELDS_A" );
-	model = node->model;
-	col_num = gda_data_model_get_n_rows( model );
-
-	ok = gda_server_operation_set_value_at( op, arg, &error, "/FIELDS_A/@COLUMN_NAME/%d", col_num );
-	if( !ok ) return;
-	ok = gda_server_operation_set_value_at( op, dbms_type, &error, "/FIELDS_A/@COLUMN_TYPE/%d", col_num );
-	if( !ok ) return;
-    if( size != 0 ) {
-        buf = g_strdup_printf( "%d", size );
-		ok = gda_server_operation_set_value_at( op, buf, &error, "/FIELDS_A/@COLUMN_SIZE/%d", col_num );
-        g_free( buf );
-		if( !ok ) return;
-    }
-    if( (flags & COL_PKEY) != 0 ) {
-		ok = gda_server_operation_set_value_at( op, "TRUE", &error, "/FIELDS_A/@COLUMN_PKEY/%d", col_num );
-		if( !ok ) return;
-    }
-    if( (flags & COL_NNUL) != 0 ) {
-		ok = gda_server_operation_set_value_at( op, "TRUE", &error, "/FIELDS_A/@COLUMN_NNUL/%d", col_num );
-		if( !ok ) return;
-    }
-    if( (flags & COL_AUTOINC) != 0 ) {
-		ok = gda_server_operation_set_value_at( op, "TRUE", &error, "/FIELDS_A/@COLUMN_AUTOINC/%d", col_num );
-		if( !ok ) return;
-    }
-    if( (flags & COL_UNIQUE) != 0 ) {
-		ok = gda_server_operation_set_value_at( op, "TRUE", &error, "/FIELDS_A/@COLUMN_UNIQUE/%d", col_num );
-		if( !ok ) return;
-    }
-}
-
-static gboolean
-create_table( const GncGdaBackend* be, const gchar* table_name,
-				const col_cvt_t* col_table, GError** error )
-{
-    GdaServerOperation *op;
-    GdaServerProvider *server;
-	GdaConnection* cnn;
-    
-	g_return_val_if_fail( be != NULL, FALSE );
-	g_return_val_if_fail( table_name != NULL, FALSE );
-	g_return_val_if_fail( col_table != NULL, FALSE );
-	g_return_val_if_fail( error != NULL, FALSE );
-    
-	cnn = be->pConnection;
-	g_return_val_if_fail( cnn != NULL, FALSE );
-    g_return_val_if_fail( GDA_IS_CONNECTION(cnn), FALSE );
-    g_return_val_if_fail( gda_connection_is_opened(cnn), FALSE );
-
-    server = gda_connection_get_provider_obj( cnn );
-    
-    op = gda_server_provider_create_operation( server, cnn, 
-                           GDA_SERVER_OPERATION_CREATE_TABLE, NULL, error );
-    if( GDA_IS_SERVER_OPERATION(op) ) {
-        gint col;
-		gboolean ok;
-        
-        if( table_name == NULL ) {
-            g_message( "Table name is NULL!" );      
-            g_set_error( error,
-                    GDA_GENERAL_ERROR, GDA_GENERAL_OBJECT_NAME_ERROR, 
-                    "Couldn't create table with a NULL string" );
-            return FALSE;    
-        }
-        
-		ok = gda_server_operation_set_value_at( op, table_name, error, "/TABLE_DEF_P/TABLE_NAME" );
-		if( !ok ) return FALSE;
-
-        for( col = 0; col_table[col].col_name != NULL; col++ ) {
-            col_type_handler_t* pHandler;
-
-            pHandler = get_handler( col_table[col].col_type );
-            pHandler->create_col_fn( server, cnn, op, &col_table[col] );
-        }
-        
-        if( !gda_server_provider_perform_operation( server, cnn, op, error ) ) {
-            /* error */
-            g_set_error( error,
-                    GDA_GENERAL_ERROR, GDA_GENERAL_OPERATION_ERROR, 
-                    "The Server couldn't perform the CREATE TABLE operation!" );
-            g_object_unref( op );
-            return FALSE;
-        }
-
-        g_object_unref( op );
-    } else {
-        g_set_error( error, GDA_GENERAL_ERROR, GDA_GENERAL_OBJECT_NAME_ERROR, 
-                "The Server doesn't support the CREATE TABLE operation!" );
-        return FALSE;
-    }
-    return TRUE;
-}
-
-gboolean
-gnc_gda_create_table( const GncGdaBackend* be, const gchar* table_name,
-					gint table_version, const col_cvt_t* col_table, GError** error )
-{
-	gboolean ok;
-
-	ok = create_table( be, table_name, col_table, error );
-	if( ok ) {
-		register_table_version( be, table_name, table_version );
-	}
-	return ok;
-}
-
-gboolean
-gnc_gda_create_index( const GncGdaBackend* be, const gchar* index_name,
-					const gchar* table_name,
-                    const col_cvt_t* col_table, GError** error )
-{
-    GdaServerOperation *op;
-    GdaServerProvider *server;
-	GdaConnection* cnn;
-    
-    g_return_val_if_fail( be != NULL, FALSE );
-	g_return_val_if_fail( index_name != NULL, FALSE );
-	g_return_val_if_fail( table_name != NULL, FALSE );
-	g_return_val_if_fail( col_table != NULL, FALSE );
-	g_return_val_if_fail( error != NULL, FALSE );
-    
-	cnn = be->pConnection;
-	g_return_val_if_fail( cnn != NULL, FALSE );
-    g_return_val_if_fail( GDA_IS_CONNECTION(cnn), FALSE );
-    g_return_val_if_fail( gda_connection_is_opened(cnn), FALSE );
-
-    server = gda_connection_get_provider_obj( cnn );
-	g_return_val_if_fail( server != NULL, FALSE );
-    
-    op = gda_server_provider_create_operation( server, cnn, 
-                           GDA_SERVER_OPERATION_CREATE_INDEX, NULL, error );
-    if( GDA_IS_SERVER_OPERATION(op) ) {
-        gint col;
-		gboolean ok;
-        
-		if( index_name == NULL ) {
-            g_message( "Index name is NULL!" );      
-            g_set_error( error,
-                    GDA_GENERAL_ERROR, GDA_GENERAL_OBJECT_NAME_ERROR, 
-                    "Couldn't create index with a NULL string" );
-            return FALSE;    
-		}
-        if( table_name == NULL ) {
-            g_message( "Table name is NULL!" );      
-            g_set_error( error,
-                    GDA_GENERAL_ERROR, GDA_GENERAL_OBJECT_NAME_ERROR, 
-                    "Couldn't create index with a NULL string" );
-            return FALSE;    
-        }
-        
-    
-		ok = gda_server_operation_set_value_at( op, index_name, error, "/INDEX_DEF_P/INDEX_NAME" );
-		if( !ok ) return FALSE;
-		ok = gda_server_operation_set_value_at( op, "", error, "/INDEX_DEF_P/INDEX_TYPE" );
-		if( !ok ) return FALSE;
-		ok = gda_server_operation_set_value_at( op, "TRUE", error, "/INDEX_DEF_P/INDEX_IFNOTEXISTS" );
-		if( !ok ) return FALSE;
-		ok = gda_server_operation_set_value_at( op, table_name, error, "/INDEX_DEF_P/INDEX_ON_TABLE" );
-		if( !ok ) return FALSE;
-
-        for( col = 0; col_table[col].col_name != NULL; col++ ) {
-			guint item;
-
-			if( col != 0 ) {
-				item = gda_server_operation_add_item_to_sequence( op, "/INDEX_FIELDS_S" );
-				g_assert( item == col );
-			}
-			ok = gda_server_operation_set_value_at( op, col_table->col_name, error,
-													"/INDEX_FIELDS_S/%d/INDEX_FIELD", col );
-			if( !ok ) break;
-        }
-        
-        if( !gda_server_provider_perform_operation( server, cnn, op, error ) ) {
-            /* error */
-            g_set_error( error,
-                    	GDA_GENERAL_ERROR, GDA_GENERAL_OPERATION_ERROR, 
-                    	"The Server couldn't perform the CREATE INDEX operation!" );
-            g_object_unref( op );
-            return FALSE;
-        }
-
-        g_object_unref( op );
-    } else {
-        g_set_error( error, GDA_GENERAL_ERROR, GDA_GENERAL_OBJECT_NAME_ERROR, 
-                "The Server doesn't support the CREATE INDEX operation!" );
-        return FALSE;
-    }
-    return TRUE;
-}
-
-gint
-gnc_gda_get_table_version( const GncGdaBackend* be, const gchar* table_name )
-{
-    GdaDictTable* table;
-    GdaDictDatabase* db;
-
-	g_return_val_if_fail( be != NULL, 0 );
-	g_return_val_if_fail( table_name != NULL, 0 );
-
-	/* If the db is pristine because it's being saved, the table does not
-	 * exist.  This gets around a GDA-3 bug where deleting all tables and
-	 * updating the meta-data leaves the meta-data still thinking 1 table
-	 * exists.
-	 */
-	if( be->is_pristine_db ) {
-		return 0;
-	}
-
-	return get_table_version( be, table_name );
-}
-
-static gboolean
-does_table_exist( const GncGdaBackend* be, const gchar* table_name )
-{
-    GdaDictTable* table;
-    GdaDictDatabase* db;
-
-	g_return_val_if_fail( be != NULL, FALSE );
-	g_return_val_if_fail( table_name != NULL, FALSE );
-
-	/* If the db is pristine because it's being saved, the table does not
-	 * exist.  This gets around a GDA-3 bug where deleting all tables and
-	 * updating the meta-data leaves the meta-data still thinking 1 table
-	 * exists.
-	 */
-	if( be->is_pristine_db ) {
-		return FALSE;
-	}
-
-    db = gda_dict_get_database( be->pDict );
-	g_return_val_if_fail( db != NULL, FALSE );
-
-    table = gda_dict_database_get_table_by_name( db, table_name );
-	if( table != NULL && GDA_IS_DICT_TABLE(table) ) {
-		return TRUE;
-	} else {
-		return FALSE;
-	}
-}
-
-/* ================================================================= */
-#if 0
-static gboolean
-create_or_drop_db( GdaConnection* cnn, GdaServerOperationType opType,
-                const gchar* db_name, GError** error )
-{
-    typedef struct {
-        GdaServerOperationType opType;
-        const gchar* op_name;
-        const gchar* op_path_name;
-    } S_ServerOpInfo;
-#define NUMOF(X) (sizeof(X)/sizeof(X[0]))
-    static S_ServerOpInfo s_op_info[] =
-    {
-        { GDA_SERVER_OPERATION_CREATE_DB, "CREATE DB", "DB_DEF_P" },
-        { GDA_SERVER_OPERATION_DROP_DB,   "DROP DB",   "DB_DESC_P" }
-    };
-#define NUM_OPS NUMOF(s_op_info)
-    S_ServerOpInfo* op_info;
-    GdaServerOperation *op;
-    GdaServerProvider *server;
-    gchar* buf;
-    gint i;
-    
-    g_return_val_if_fail( GDA_IS_CONNECTION(cnn), FALSE );
-    g_return_val_if_fail( gda_connection_is_opened(cnn), FALSE );
-    
-    if( db_name == NULL ) {
-        g_message( "Database name is NULL!" );      
-        g_set_error( error,
-                GDA_GENERAL_ERROR, GDA_GENERAL_OBJECT_NAME_ERROR, 
-                "Couldn't create or drop database with a NULL string" );
-        return FALSE;    
-    }
-
-    for( i = 0, op_info = NULL; i < NUM_OPS; i++ ) {
-        if( s_op_info[i].opType == opType ) {
-            op_info = &s_op_info[i];
-            break;
-        }
-    }
-
-    g_return_val_if_fail( op_info != NULL, FALSE );
-    
-    server = gda_connection_get_provider_obj( cnn );
-    op = gda_server_provider_create_operation( server, cnn, opType, NULL,
-                                            error );
-    if( GDA_IS_SERVER_OPERATION(op) ) {
-		gboolean ok;
-		GError* error = NULL;
-
-        buf = g_strdup_printf( "/%s/DB_NAME", op_info->op_path_name );
-		ok = gda_server_operation_set_value_at( op, db_name, &error, buf );
-        g_free( buf );
-		if( !ok ) return FALSE;
-
-        if( !gda_server_provider_perform_operation( server, cnn, op, error ) ) {
-            /* error */
-            buf = g_strdup_printf( "The server couldn't perform the %s operation",
-                                        op_info->op_name );
-            g_set_error( error,
-                    GDA_GENERAL_ERROR, GDA_GENERAL_OPERATION_ERROR, 
-                    buf );
-            g_free( buf );
-            g_object_unref( op );
-            return FALSE;
-        }
-
-        g_object_unref( op );
-    } else {
-        buf = g_strdup_printf( "The server doesn't support the %s operation",
-                                op_info->op_name );
-        g_set_error( error, GDA_GENERAL_ERROR, GDA_GENERAL_OBJECT_NAME_ERROR, 
-                    buf );
-        g_free( buf );
-        return FALSE;
-    }
-    return TRUE;
-}
-
-static gboolean
-drop_db( GncGdaBackend* be, const gchar* db_name, GError** error )
-{
-    return create_or_drop_db( be->pConnection, GDA_SERVER_OPERATION_DROP_DB,
-                                db_name, error );
-}
-
-static gboolean
-create_db( GncGdaBackend* be, const gchar* db_name, GError** error )
-{
-    return create_or_drop_db( be->pConnection, GDA_SERVER_OPERATION_CREATE_DB,
-                                db_name, error );
-}
-#endif
-/* ================================================================= */
-#define VERSION_TABLE_NAME "versions"
-#define MAX_TABLE_NAME_LEN 50
-#define TABLE_COL_NAME "table_name"
-#define VERSION_COL_NAME "table_version"
-
-static col_cvt_t version_table[] =
-{
-    { TABLE_COL_NAME,   CT_STRING, MAX_TABLE_NAME_LEN },
-	{ VERSION_COL_NAME, CT_INT },
-    { NULL }
-};
-
-/**
- * Sees if the version table exists, and if it does, loads the info into
- * the version hash table.  Otherwise, it creates an empty version table.
- *
- * @param be Backend struct
- */
-void
-_init_version_info( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-	be->versions = g_hash_table_new( g_str_hash, g_str_equal );
-
-	if( does_table_exist( be, VERSION_TABLE_NAME ) ) {
-		GdaDataModel* model;
-		gchar* sql;
-
-		sql = g_strdup_printf( "SELECT * FROM %s", VERSION_TABLE_NAME );
-		model = gnc_gda_execute_select_sql( be, sql );
-		g_free( sql );
-		if( model != NULL ) {
-			gint numRows = gda_data_model_get_n_rows( model );
-			const GValue* name;
-			const GValue* version;
-			gint row;
-
-			for( row = 0; row < numRows; row++ ) {
-    			name = gda_data_model_get_value_at_col_name( model, TABLE_COL_NAME, row );
-				version = gda_data_model_get_value_at_col_name( model, VERSION_COL_NAME,
-																row );
-				g_hash_table_insert( be->versions,
-									(gpointer)g_value_get_string( name ),
-									GINT_TO_POINTER(g_value_get_int( version )) );
-			}
-		}
-	} else {
-		gboolean ok;
-		GError* error = NULL;
-
-		ok = create_table( be, VERSION_TABLE_NAME, version_table, &error );
-		if( error != NULL ) {
-			PERR( "Error creating versions table: %s\n", error->message );
-		}
-	}
-}
-
-/**
- * Resets the version table information by removing all version table info.
- * It also recreates the version table in the db.
- *
- * @param be Backend struct
- */
-void
-_reset_version_info( const GncGdaBackend* be )
-{
-	gboolean ok;
-	GError* error = NULL;
-
-	g_return_if_fail( be != NULL );
-
-	ok = create_table( be, VERSION_TABLE_NAME, version_table, &error );
-	if( error != NULL ) {
-		PERR( "Error creating versions table: %s\n", error->message );
-	}
-	g_hash_table_remove_all( be->versions );
-}
-
-/**
- * Finalizes the version table info by destroying the hash table.
- *
- * @param be Backend struct
- */
-void
-_finalize_version_info( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-	g_hash_table_destroy( be->versions );
-}
-
-/**
- * Registers the version for a table.  Registering involves updating the
- * db version table and also the hash table.
- *
- * @param be Backend struct
- * @param table_name Table name
- * @param version Version number
- */
-static void
-register_table_version( const GncGdaBackend* be, const gchar* table_name, gint version )
-{
-	gchar* sql;
-	gint cur_version;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( table_name != NULL );
-	g_return_if_fail( version > 0 );
-
-	cur_version = get_table_version( be, table_name );
-	if( cur_version != version ) {
-		if( cur_version == 0 ) {
-			sql = g_strdup_printf( "INSERT INTO %s VALUES('%s',%d)", VERSION_TABLE_NAME,
-								table_name, version );
-		} else {
-			sql = g_strdup_printf( "UPDATE %s SET %s=%d WHERE %s='%s'", VERSION_TABLE_NAME,
-								VERSION_COL_NAME, version,
-								TABLE_COL_NAME, table_name );
-		}
-		(void)gnc_gda_execute_nonselect_sql( be, sql );
-		g_free( sql );
-	}
-
-	g_hash_table_insert( be->versions, (gpointer)table_name, GINT_TO_POINTER(version) );
-}
-
-/**
- * Returns the registered version number for a table.
- *
- * @param be Backend struct
- * @param table_name Table name
- * @return Version number
- */
-static gint
-get_table_version( const GncGdaBackend* be, const gchar* table_name )
-{
-	g_return_val_if_fail( be != NULL, 0 );
-	g_return_val_if_fail( table_name != NULL, 0 );
-
-	return GPOINTER_TO_INT(g_hash_table_lookup( be->versions, table_name ));
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-util-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-util-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-backend-util-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,212 +0,0 @@
-/********************************************************************
- * gnc-backend-gda.h: load and save data to SQL via libgda          *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-backend-gda.h
- *  @brief load and save data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_BACKEND_GDA_UTIL_H_
-#define GNC_BACKEND_GDA_UTIL_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-#include "qofbackend-p.h"
-struct GncGdaBackend_struct
-{
-  QofBackend be;
-
-  GdaClient* pClient;
-  GdaConnection* pConnection;
-  GdaDict* pDict;
-
-  QofBook *primary_book;	/* The primary, main open book */
-  gboolean	loading;		/* We are performing an initial load */
-  gboolean  in_query;
-  gboolean  supports_transactions;
-  gboolean  is_pristine_db;	// Are we saving to a new pristine db?
-
-  gint obj_total;			// Total # of objects (for percentage calculation)
-  gint operations_done;		// Number of operations (save/load) done
-  GHashTable* versions;		// Version number for each table
-};
-typedef struct GncGdaBackend_struct GncGdaBackend;
-
-/**
- * Struct used to pass in a new data type for GDA storage.  This contains
- * the set of callbacks to read and write GDA for new data objects..  New
- * types should register an instance of this object with the engine.
- *
- * commit()			- commit an object to the db
- * initial_load()	- load stuff when new db opened
- * create_tables()  - create any db tables
- * compile_query()  - compile a backend object query
- * run_query()      - run a compiled query
- * free_query()     - free a compiled query
- * write()          - write all objects
- */
-#define GNC_GDA_BACKEND	"gnc:gda:1"
-#define GNC_GDA_BACKEND_VERSION	1
-typedef struct
-{
-  int		version;	/* backend version number */
-  const gchar *	type_name;	/* The XML tag for this type */
-
-  void		(*commit)( QofInstance* inst, GncGdaBackend* be );
-  void		(*initial_load)( GncGdaBackend* pBackend );
-  void		(*create_tables)( GncGdaBackend* pBackend );
-  gpointer	(*compile_query)( GncGdaBackend* pBackend, QofQuery* pQuery );
-  void		(*run_query)( GncGdaBackend* pBackend, gpointer pQuery );
-  void		(*free_query)( GncGdaBackend* pBackend, gpointer pQuery );
-  void		(*write)( GncGdaBackend* pBackend );
-} GncGdaDataType_t;
-
-// Type for conversion of db row to object.
-#define CT_STRING "ct_string"
-#define CT_GUID "ct_guid"
-#define CT_INT "ct_int"
-#define CT_INT64 "ct_int64"
-#define CT_TIMESPEC "ct_timespec"
-#define CT_GDATE "ct_gdate"
-#define CT_NUMERIC "ct_numeric"
-#define CT_DOUBLE "ct_double"
-#define CT_BOOLEAN "ct_boolean"
-#define CT_ACCOUNTREF "ct_accountref"
-#define CT_COMMODITYREF "ct_commodityref"
-#define CT_TXREF "ct_txref"
-#define CT_LOTREF "ct_lotref"
-
-typedef struct {
-	const gchar* col_name;
-	const gchar* col_type;
-	gint size;
-#define COL_PKEY	0x01
-#define COL_NNUL	0x02
-#define COL_UNIQUE	0x04
-#define COL_AUTOINC	0x08
-	gint flags;
-	const gchar* gobj_param_name;	// If non-null, use g_object_get/g_object_set
-	const gchar* param_name;	// If non null, use qof getter/setter
-	QofAccessFunc getter;
-	QofSetterFunc setter;
-} col_cvt_t;
-
-typedef enum {
-	OP_DB_ADD,
-	OP_DB_ADD_OR_UPDATE,
-	OP_DB_DELETE
-} E_DB_OPERATION;
-
-typedef void (*GNC_GDA_LOAD_FN)( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-                                QofSetterFunc setter, gpointer pObject,
-                                const col_cvt_t* table );
-typedef void (*GNC_GDA_CREATE_COL_FN)( GdaServerProvider* server,
-				                        GdaConnection* cnn, GdaServerOperation* op,
-                        				const col_cvt_t* table_row );
-typedef void (*GNC_GDA_GET_GVALUE_QUERY_FN)( const GncGdaBackend* be,
-                QofIdTypeConst obj_name, const gpointer pObject,
-                const col_cvt_t* table_row, GdaQuery* query );
-typedef GdaQueryCondition* (*GNC_GDA_GET_GVALUE_COND_FN)( const GncGdaBackend* be,
-                QofIdTypeConst obj_name, const gpointer pObject,
-                const col_cvt_t* table_row, GdaQuery* query );
-
-typedef struct {
-    GNC_GDA_LOAD_FN             load_fn;
-    GNC_GDA_CREATE_COL_FN       create_col_fn;
-    GNC_GDA_GET_GVALUE_QUERY_FN get_gvalue_query_fn;
-    GNC_GDA_GET_GVALUE_COND_FN  get_gvalue_cond_fn;
-} col_type_handler_t;
-
-QofAccessFunc gnc_gda_get_getter( QofIdTypeConst obj_name, const col_cvt_t* table_row );
-void gnc_gda_add_table_column( GdaServerOperation* op, const gchar* arg, const gchar* dbms_type,
-            gint size, gint flags );
-
-gboolean gnc_gda_do_db_operation( GncGdaBackend* pBackend,
-									E_DB_OPERATION op,
-									const gchar* table_name,
-									QofIdTypeConst obj_name,
-									gpointer pObject,
-									const col_cvt_t* table );
-GdaQuery* gnc_gda_build_insert_query( GncGdaBackend* pBackend,
-									const gchar* table_name,
-									QofIdTypeConst obj_name,
-									gpointer pObject,
-									const col_cvt_t* table );
-GdaQuery* gnc_gda_build_update_query( GncGdaBackend* pBackend,
-									const gchar* table_name,
-									QofIdTypeConst obj_name,
-									gpointer pObject,
-									const col_cvt_t* table );
-GdaQuery* gnc_gda_build_delete_query( GncGdaBackend* pBackend,
-									const gchar* table_name,
-									QofIdTypeConst obj_name,
-									gpointer pObject,
-									const col_cvt_t* table );
-GdaObject* gnc_gda_execute_query( GncGdaBackend* pBackend, GdaQuery* pQuery );
-GdaDataModel* gnc_gda_execute_select_sql( const GncGdaBackend* pBackend, const gchar* sql );
-gint gnc_gda_execute_nonselect_sql( const GncGdaBackend* pBackend, const gchar* sql );
-GdaQuery* gnc_gda_create_query_from_sql( const GncGdaBackend* pBackend, const gchar* sql );
-int gnc_gda_execute_select_get_count( const GncGdaBackend* pBackend, const gchar* sql );
-int gnc_gda_execute_query_get_count( GncGdaBackend* pBackend, GdaQuery* query );
-void gnc_gda_load_object( const GncGdaBackend* be, GdaDataModel* pModel, int row,
-						QofIdTypeConst obj_name, gpointer pObject,
-						const col_cvt_t* table );
-gboolean gnc_gda_object_is_it_in_db( GncGdaBackend* be,
-									const gchar* table_name,
-									QofIdTypeConst obj_name, const gpointer pObject,
-									const col_cvt_t* table );
-gint gnc_gda_get_table_version( const GncGdaBackend* be, const gchar* table_name );
-gboolean gnc_gda_create_table( const GncGdaBackend* be, const gchar* table_name,
-								gint table_version, const col_cvt_t* col_table,
-								GError** error );
-gboolean gnc_gda_create_index( const GncGdaBackend* be, const gchar* index_name,
-						const gchar* table_name, const col_cvt_t* col_table,
-						GError** error );
-const GUID* gnc_gda_load_guid( const GncGdaBackend* be, GdaDataModel* pModel, int row );
-const GUID* gnc_gda_load_tx_guid( const GncGdaBackend* be, GdaDataModel* pModel, int row );
-GdaQuery* gnc_gda_create_select_query( const GncGdaBackend* be, const gchar* table_name );
-GdaQueryCondition* gnc_gda_create_condition_from_field( GdaQuery* query,
-														const gchar* col_name,
-														const GValue* value );
-void gnc_gda_register_col_type_handler( const gchar* colType, const col_type_handler_t* handler );
-void gnc_gda_register_standard_col_type_handlers( void );
-void gnc_gda_add_field_to_query( GdaQuery* query, const gchar* col_name, const GValue* value );
-
-void gnc_gda_get_gvalue_objectref_guid_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query );
-GdaQueryCondition* gnc_gda_get_gvalue_objectref_guid_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query );
-void gnc_gda_create_objectref_guid_col( GdaServerProvider* server, GdaConnection* cnn,
-	            GdaServerOperation* op, const col_cvt_t* table_row );
-guint gnc_gda_append_guid_list_to_sql( GString* str, GList* list, guint maxCount );
-
-void _retrieve_guid_( gpointer pObject, gpointer pValue );
-void _init_version_info( GncGdaBackend* be );
-void _reset_version_info( const GncGdaBackend* be );
-void _finalize_version_info( GncGdaBackend* be );
-
-G_MODULE_EXPORT const gchar *
-g_module_check_init( GModule *module );
-
-#endif /* GNC_BACKEND_GDA_UTIL_H_ */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-book-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-book-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-book-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,245 +0,0 @@
-/********************************************************************
- * gnc-book-gda.c: load and save data to SQL via libgda             *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-book-gda.c
- *  @brief load and save data to SQL 
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-
-#include "gnc-backend-util-gda.h"
-
-#include "gnc-book-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gnc-engine.h"
-#include "gnc-book.h"
-#include "SX-book.h"
-#include "SX-book-p.h"
-
-#define BOOK_TABLE "books"
-#define TABLE_VERSION 1
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-static gpointer get_root_account_guid( gpointer pObject, const QofParam* );
-static void set_root_account_guid( gpointer pObject, gpointer pValue );
-static gpointer get_root_template_guid( gpointer pObject, const QofParam* );
-static void set_root_template_guid( gpointer pObject, gpointer pValue );
-
-static const col_cvt_t col_table[] =
-{
-    { "guid",               CT_GUID, 0, COL_NNUL|COL_PKEY, "guid" },
-    { "root_account_guid",  CT_GUID, 0, COL_NNUL,          NULL, NULL, get_root_account_guid,  set_root_account_guid },
-    { "root_template_guid", CT_GUID, 0, COL_NNUL,          NULL, NULL, get_root_template_guid, set_root_template_guid },
-    { NULL }
-};
-
-/* ================================================================= */
-static gpointer
-get_root_account_guid( gpointer pObject, const QofParam* param )
-{
-    GNCBook* book = QOF_BOOK(pObject);
-    const Account* root;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL );
-
-    root = gnc_book_get_root_account( book );
-    return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) );
-}
-
-static void 
-set_root_account_guid( gpointer pObject, gpointer pValue )
-{
-    GNCBook* book = QOF_BOOK(pObject);
-    const Account* root;
-    GUID* guid = (GUID*)pValue;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( QOF_IS_BOOK(pObject) );
-	g_return_if_fail( pValue != NULL );
-
-    root = gnc_book_get_root_account( book );
-    qof_instance_set_guid( QOF_INSTANCE(root), guid );
-}
-
-static gpointer
-get_root_template_guid( gpointer pObject, const QofParam* param )
-{
-    const GNCBook* book = QOF_BOOK(pObject);
-    const Account* root;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL );
-
-    root = gnc_book_get_template_root( book );
-    return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) );
-}
-
-static void 
-set_root_template_guid( gpointer pObject, gpointer pValue )
-{
-    GNCBook* book = QOF_BOOK(pObject);
-    GUID* guid = (GUID*)pValue;
-    Account* root;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( QOF_IS_BOOK(pObject) );
-	g_return_if_fail( pValue != NULL );
-
-    root = gnc_book_get_template_root( book );
-    if( root == NULL ) {
-        root = xaccMallocAccount( book );
-        xaccAccountBeginEdit( root );
-        xaccAccountSetType( root, ACCT_TYPE_ROOT );
-        xaccAccountCommitEdit( root );
-        gnc_book_set_template_root( book, root );
-    }
-    qof_instance_set_guid( QOF_INSTANCE(root), guid );
-}
-
-/* ================================================================= */
-static void
-load_single_book( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    const GUID* guid;
-    GUID book_guid;
-	GNCBook* pBook;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    book_guid = *guid;
-
-	pBook = be->primary_book;
-	if( pBook == NULL ) {
-	    pBook = gnc_book_new();
-	}
-
-    gnc_gda_load_object( be, pModel, row, GNC_ID_BOOK, pBook, col_table );
-    gnc_gda_slots_load( be, QOF_INSTANCE(pBook) );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pBook) );
-}
-
-static void
-load_all_books( GncGdaBackend* be )
-{
-    static GdaQuery* query;
-    GdaObject* ret;
-
-	g_return_if_fail( be != NULL );
-
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, BOOK_TABLE );
-    }
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-
-		// If there are no rows, try committing the book
-		if( numRows == 0 ) {
-   	    	gnc_gda_save_book( QOF_INSTANCE(be->primary_book), be );
-		} else {
-			// Otherwise, load the 1st book.
-            load_single_book( be, pModel, 0 );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_book_tables( GncGdaBackend* be )
-{
-	gint version;
-
-	g_return_if_fail( be != NULL );
-
-	version = gnc_gda_get_table_version( be, BOOK_TABLE );
-    if( version == 0 ) {
-    	GError* error = NULL;
-
-        gnc_gda_create_table( be, BOOK_TABLE, TABLE_VERSION, col_table, &error );
-        if( error != NULL ) {
-            PERR( "Error creating table: %s\n", error->message );
-        }
-    }
-}
-
-/* ================================================================= */
-void
-gnc_gda_save_book( QofInstance* inst, GncGdaBackend* be )
-{
-    const GUID* guid;
-	gint op;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( QOF_IS_BOOK(inst) );
-
-	if( qof_instance_get_destroying( inst ) ) {
-		op = OP_DB_DELETE;
-	} else if( be->is_pristine_db ) {
-		op = OP_DB_ADD;
-	} else {
-		op = OP_DB_ADD_OR_UPDATE;
-	}
-    (void)gnc_gda_do_db_operation( be, op, BOOK_TABLE, GNC_ID_BOOK, inst, col_table );
-
-    // Delete old slot info
-    guid = qof_instance_get_guid( inst );
-
-    // Now, commit any slots
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-/* ================================================================= */
-void
-gnc_gda_init_book_handler( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_BOOK,
-        gnc_gda_save_book,                 /* commit */
-        load_all_books,                    /* initial_load */
-        create_book_tables 		           /* create_tables */
-    };
-
-    qof_object_register_backend( GNC_ID_BOOK, GNC_GDA_BACKEND, &be_data );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-book-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-book-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-book-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,38 +0,0 @@
-/********************************************************************
- * gnc-book-gda.h: load and save data to SQL via libgda             *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-book-gda.h
- *  @brief load and save data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_BOOK_GDA_H_
-#define GNC_BOOK_GDA_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-void gnc_gda_init_book_handler( void );
-void gnc_gda_save_book( QofInstance* inst, GncGdaBackend* be );
-
-#endif /* GNC_BOOK_GDA_H_ */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-budget-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-budget-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-budget-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,197 +0,0 @@
-/********************************************************************
- * gnc-budget-gda.c: load and save data to SQL via libgda           *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-budget-gda.c
- *  @brief load and save data to SQL 
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-
-#include "gnc-backend-util-gda.h"
-
-#include "Recurrence.h"
-
-#include "gnc-budget-gda.h"
-#include "gnc-slots-gda.h"
-#include "gnc-recurrence-gda.h"
-
-#include "gnc-budget.h"
-
-#define BUDGET_TABLE "budgets"
-#define TABLE_VERSION 1
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define BUDGET_MAX_NAME_LEN 2048
-#define BUDGET_MAX_DESCRIPTION_LEN 2048
-
-static const col_cvt_t col_table[] =
-{
-    { "guid",        CT_GUID,   0,                          COL_NNUL|COL_PKEY, "guid" },
-    { "name",        CT_STRING, BUDGET_MAX_NAME_LEN,        COL_NNUL,          NULL, "name" },
-    { "description", CT_STRING, BUDGET_MAX_DESCRIPTION_LEN, 0,                 NULL, "description" },
-    { "num_periods", CT_INT,    0,                          COL_NNUL,          NULL, "num_periods" },
-    { NULL }
-};
-
-/* ================================================================= */
-static void
-load_single_budget( GncGdaBackend* be, GdaDataModel* pModel, int row, GList** pList )
-{
-    const GUID* guid;
-    GUID budget_guid;
-	GncBudget* pBudget;
-	Recurrence* r;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    budget_guid = *guid;
-
-    pBudget = gnc_budget_lookup( &budget_guid, be->primary_book );
-    if( pBudget == NULL ) {
-        pBudget = gnc_budget_new( be->primary_book );
-    }
-
-    gnc_gda_load_object( be, pModel, row, GNC_ID_BUDGET, pBudget, col_table );
-	r = g_new0( Recurrence, 1 );
-	gnc_gda_recurrence_load( be, gnc_budget_get_guid( pBudget ), r );
-	*pList = g_list_append( *pList, pBudget );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pBudget) );
-}
-
-static void
-load_all_budgets( GncGdaBackend* be )
-{
-    static GdaQuery* query;
-    GdaObject* ret;
-
-	g_return_if_fail( be != NULL );
-
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, BUDGET_TABLE );
-    }
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-		GList* list = NULL;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_budget( be, pModel, r, &list );
-        }
-
-		if( list != NULL ) {
-			gnc_gda_slots_load_for_list( be, list );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_budget_tables( GncGdaBackend* be )
-{
-	gint version;
-
-	g_return_if_fail( be != NULL );
-
-	version = gnc_gda_get_table_version( be, BUDGET_TABLE );
-    if( version == 0 ) {
-    	GError* error = NULL;
-
-        gnc_gda_create_table( be, BUDGET_TABLE, TABLE_VERSION, col_table, &error );
-        if( error != NULL ) {
-            PERR( "Error creating table: %s\n", error->message );
-        }
-    }
-}
-
-/* ================================================================= */
-static void
-save_budget( QofInstance* inst, GncGdaBackend* be )
-{
-    GncBudget* pBudget = GNC_BUDGET(inst);
-    const GUID* guid;
-	gint op;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_BUDGET(inst) );
-
-	if( qof_instance_get_destroying( inst ) ) {
-		op = OP_DB_DELETE;
-	} else if( be->is_pristine_db ) {
-		op = OP_DB_ADD;
-	} else {
-		op = OP_DB_ADD_OR_UPDATE;
-	}
-    (void)gnc_gda_do_db_operation( be, op, BUDGET_TABLE, GNC_ID_BUDGET, pBudget, col_table );
-
-    // Now, commit any slots and recurrence
-    guid = qof_instance_get_guid( inst );
-    if( !qof_instance_get_destroying(inst) ) {
-		gnc_gda_recurrence_save( be, guid, gnc_budget_get_recurrence( pBudget ) );
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_recurrence_delete( be, guid );
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-static void
-write_budgets( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    qof_collection_foreach( qof_book_get_collection( be->primary_book, GNC_ID_BUDGET ),
-                            (QofInstanceForeachCB)save_budget, be );
-}
-
-/* ================================================================= */
-void
-gnc_gda_init_budget_handler( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_BUDGET,
-        save_budget,    		        /* commit */
-        load_all_budgets,               /* initial_load */
-        create_budget_tables,	        /* create_tables */
-		NULL, NULL, NULL,
-		write_budgets					/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_BUDGET, GNC_GDA_BACKEND, &be_data );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-budget-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-budget-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-budget-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,37 +0,0 @@
-/********************************************************************
- * gnc-budget-gda.h: load and save data to SQL via libgda           *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-budget-gda.h
- *  @brief load and save data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_BUDGET_GDA_H_
-#define GNC_BUDGET_GDA_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-void gnc_gda_init_budget_handler( void );
-
-#endif /* GNC_BUDGET_GDA_H_ */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-commodity-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-commodity-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-commodity-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,299 +0,0 @@
-/********************************************************************
- * gnc-commodity-gda.c: load and save data to SQL via libgda        *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-commodity-gda.c
- *  @brief load and save data to SQL 
- *  @author Copyright (c) 2000 Gnumatic Inc.
- *  @author Copyright (c) 2002 Derek Atkins <warlord at MIT.EDU>
- *  @author Copyright (c) 2003 Linas Vepstas <linas at linas.org>
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-commodity.h"
-
-#include "gnc-commodity-gda.h"
-#include "gnc-slots-gda.h"
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-static gpointer get_quote_source_name( gpointer pObject, const QofParam* );
-static void set_quote_source_name( gpointer pObject, gpointer pValue );
-
-#define COMMODITIES_TABLE "commodities"
-#define TABLE_VERSION 1
-
-#define COMMODITY_MAX_NAMESPACE_LEN 2048
-#define COMMODITY_MAX_MNEMONIC_LEN 2048
-#define COMMODITY_MAX_FULLNAME_LEN 2048
-#define COMMODITY_MAX_CUSIP_LEN 2048
-#define COMMODITY_MAX_QUOTESOURCE_LEN 2048
-#define COMMODITY_MAX_QUOTE_TZ_LEN 2048
-
-static const col_cvt_t col_table[] = {
-    { "guid",         CT_GUID,   0,                             COL_NNUL|COL_PKEY, "guid" },
-    { "namespace",    CT_STRING, COMMODITY_MAX_NAMESPACE_LEN,   COL_NNUL,          NULL, NULL,
-            (QofAccessFunc)gnc_commodity_get_namespace,
-            (QofSetterFunc)gnc_commodity_set_namespace },
-    { "mnemonic",     CT_STRING, COMMODITY_MAX_MNEMONIC_LEN,    COL_NNUL,          NULL, NULL,
-            (QofAccessFunc)gnc_commodity_get_mnemonic,
-            (QofSetterFunc)gnc_commodity_set_mnemonic },
-    { "fullname",     CT_STRING, COMMODITY_MAX_FULLNAME_LEN,    0,                 NULL, NULL,
-            (QofAccessFunc)gnc_commodity_get_fullname,
-            (QofSetterFunc)gnc_commodity_set_fullname },
-    { "cusip",        CT_STRING, COMMODITY_MAX_CUSIP_LEN,       0,                 NULL, NULL,
-            (QofAccessFunc)gnc_commodity_get_cusip,
-            (QofSetterFunc)gnc_commodity_set_cusip },
-    { "fraction",     CT_INT,    0,                             COL_NNUL,          NULL, NULL,
-            (QofAccessFunc)gnc_commodity_get_fraction,
-            (QofSetterFunc)gnc_commodity_set_fraction },
-    { "quote_flag",   CT_INT,    0,                             COL_NNUL,          NULL, NULL,
-            (QofAccessFunc)gnc_commodity_get_quote_flag,
-            (QofSetterFunc)gnc_commodity_set_quote_flag },
-    { "quote_source", CT_STRING, COMMODITY_MAX_QUOTESOURCE_LEN, 0,                 NULL, NULL,
-            get_quote_source_name, set_quote_source_name },
-    { "quote_tz",     CT_STRING, COMMODITY_MAX_QUOTE_TZ_LEN,    0,                 NULL, NULL,
-            (QofAccessFunc)gnc_commodity_get_quote_tz,
-            (QofSetterFunc)gnc_commodity_set_quote_tz },
-    { NULL }
-};
-
-/* ================================================================= */
-
-static gpointer
-get_quote_source_name( gpointer pObject, const QofParam* param )
-{
-    const gnc_commodity* pCommodity = GNC_COMMODITY(pObject);
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( GNC_IS_COMMODITY(pObject), NULL );
-
-    return (gpointer)gnc_quote_source_get_internal_name(
-                            gnc_commodity_get_quote_source(pCommodity));
-}
-
-static void 
-set_quote_source_name( gpointer pObject, gpointer pValue )
-{
-    gnc_commodity* pCommodity = GNC_COMMODITY(pObject);
-    const gchar* quote_source_name = (const gchar*)pValue;
-    gnc_quote_source* quote_source;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( GNC_IS_COMMODITY(pObject) );
-
-	if( pValue == NULL ) return;
-
-    quote_source = gnc_quote_source_lookup_by_internal( quote_source_name );
-    gnc_commodity_set_quote_source( pCommodity, quote_source );
-}
-
-static gnc_commodity*
-load_single_commodity( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    QofBook* pBook = be->primary_book;
-    int col;
-    const GValue* val;
-    gnc_commodity* pCommodity;
-
-    pCommodity = gnc_commodity_new( pBook, NULL, NULL, NULL, NULL, 100 );
-
-    gnc_gda_load_object( be, pModel, row, GNC_ID_COMMODITY, pCommodity, col_table );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pCommodity) );
-
-    return pCommodity;
-}
-
-static void
-load_all_commodities( GncGdaBackend* be )
-{
-    static GdaQuery* query;
-    GdaObject* ret;
-    gnc_commodity_table* pTable = gnc_commodity_table_get_table( be->primary_book );
-
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, COMMODITIES_TABLE );
-    }
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-        gnc_commodity* pCommodity;
-		GList* list = NULL;
-
-        for( r = 0; r < numRows; r++ ) {
-            gnc_commodity* c;
-
-            pCommodity = load_single_commodity( be, pModel, r );
-
-            if( pCommodity != NULL ) {
-                GUID guid;
-
-                guid = *qof_instance_get_guid( QOF_INSTANCE(pCommodity) );
-                pCommodity = gnc_commodity_table_insert( pTable, pCommodity );
-				list = g_list_append( list, pCommodity );
-                qof_instance_set_guid( QOF_INSTANCE(pCommodity), &guid );
-            }
-        }
-
-		if( list != NULL ) {
-			gnc_gda_slots_load_for_list( be, list );
-		}
-    }
-}
-/* ================================================================= */
-static void
-create_commodities_tables( GncGdaBackend* be )
-{
-	gint version;
-
-	g_return_if_fail( be != NULL );
-
-	version = gnc_gda_get_table_version( be, COMMODITIES_TABLE );
-    if( version == 0 ) {
-    	GError* error = NULL;
-
-        gnc_gda_create_table( be, COMMODITIES_TABLE, TABLE_VERSION, col_table, &error );
-        if( error != NULL ) {
-            PERR( "Error creating table: %s\n", error->message );
-        }
-    }
-}
-
-/* ================================================================= */
-static void
-commit_commodity( QofInstance* inst, GncGdaBackend* be )
-{
-    const GUID* guid;
-	gint op;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_COMMODITY(inst) );
-
-	if( qof_instance_get_destroying( inst ) ) {
-		op = OP_DB_DELETE;
-	} else if( be->is_pristine_db ) {
-		op = OP_DB_ADD;
-	} else {
-		op = OP_DB_ADD_OR_UPDATE;
-	}
-    (void)gnc_gda_do_db_operation( be, op, COMMODITIES_TABLE, GNC_ID_COMMODITY, inst, col_table );
-
-    // Delete old slot info
-    guid = qof_instance_get_guid( inst );
-
-    // Now, commit or delete any slots
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-static gboolean
-is_commodity_in_db( GncGdaBackend* be, gnc_commodity* pCommodity )
-{
-	g_return_val_if_fail( be != NULL, FALSE );
-	g_return_val_if_fail( pCommodity != NULL, FALSE );
-
-    return gnc_gda_object_is_it_in_db( be, COMMODITIES_TABLE, GNC_ID_COMMODITY,
-                                pCommodity, col_table );
-}
-
-void
-gnc_gda_save_commodity( GncGdaBackend* be, gnc_commodity* pCommodity )
-{
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pCommodity != NULL );
-
-    if( !is_commodity_in_db( be, pCommodity ) ) {
-        commit_commodity( QOF_INSTANCE(pCommodity), be );
-    }
-}
-
-/* ----------------------------------------------------------------- */
-
-static void
-load_commodity_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GUID guid;
-    const GUID* pGuid;
-	gnc_commodity* commodity = NULL;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        pGuid = NULL;
-    } else {
-        string_to_guid( g_value_get_string( val ), &guid );
-        pGuid = &guid;
-    }
-	if( pGuid != NULL ) {
-		commodity = gnc_commodity_find_commodity_by_guid( pGuid, be->primary_book );
-	}
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, commodity, NULL );
-    } else {
-		(*setter)( pObject, (const gpointer)commodity );
-    }
-}
-
-static col_type_handler_t commodity_guid_handler =
-        { load_commodity_guid, gnc_gda_create_objectref_guid_col,
-            gnc_gda_get_gvalue_objectref_guid_for_query, gnc_gda_get_gvalue_objectref_guid_cond };
-/* ================================================================= */
-void
-gnc_gda_init_commodity_handler( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_COMMODITY,
-        commit_commodity,            /* commit */
-        load_all_commodities,            /* initial_load */
-        create_commodities_tables    /* create_tables */
-    };
-
-    qof_object_register_backend( GNC_ID_COMMODITY, GNC_GDA_BACKEND, &be_data );
-
-	gnc_gda_register_col_type_handler( CT_COMMODITYREF, &commodity_guid_handler );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-commodity-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-commodity-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-commodity-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,38 +0,0 @@
-/********************************************************************
- * gnc-commodity-gda.h: load and save data to SQL via libgda        *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-commodity-gda.h
- *  @brief load and save data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_COMMODITY_GDA_H_
-#define GNC_COMMODITY_GDA_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-void gnc_gda_init_commodity_handler( void );
-void gnc_gda_save_commodity( GncGdaBackend* be, gnc_commodity* pCommodity );
-
-#endif /* GNC_COMMODITY_GDA_H_ */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-lots-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-lots-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-lots-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,246 +0,0 @@
-/********************************************************************
- * gnc-lots-gda.c: load and save data to SQL via libgda             *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-lots-gda.c
- *  @brief load and save data to SQL 
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-#include "gnc-lot.h"
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gnc-lots-gda.h"
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TABLE_NAME "lots"
-#define TABLE_VERSION 1
-
-static gpointer get_lot_account( gpointer pObject, const QofParam* param );
-static void set_lot_account( gpointer pObject, gpointer pValue );
-static void set_lot_is_closed( gpointer pObject, gboolean value );
-
-static const col_cvt_t col_table[] =
-{
-    { "guid",         CT_GUID,    0, COL_NNUL|COL_PKEY, "guid" },
-    { "account_guid", CT_GUID,    0, COL_NNUL,          NULL, NULL, get_lot_account,   set_lot_account },
-    { "is_closed",    CT_BOOLEAN, 0, COL_NNUL,          NULL, NULL,
-		(QofAccessFunc)gnc_lot_is_closed, (QofSetterFunc)set_lot_is_closed },
-    { NULL }
-};
-
-/* ================================================================= */
-static gpointer
-get_lot_account( gpointer pObject, const QofParam* param )
-{
-    const GNCLot* lot = GNC_LOT(pObject);
-    const Account* pAccount;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( GNC_IS_LOT(pObject), NULL );
-
-    pAccount = gnc_lot_get_account( lot );
-    return (gpointer)qof_instance_get_guid( QOF_INSTANCE(pAccount) );
-}
-
-static void 
-set_lot_account( gpointer pObject, gpointer pValue )
-{
-    GNCLot* lot = GNC_LOT(pObject);
-    QofBook* pBook;
-    GUID* guid = (GUID*)pValue;
-    Account* pAccount;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( GNC_IS_LOT(pObject) );
-	g_return_if_fail( pValue != NULL );
-
-    pBook = qof_instance_get_book( QOF_INSTANCE(lot) );
-    pAccount = xaccAccountLookup( guid, pBook );
-    xaccAccountInsertLot( pAccount, lot );
-}
-
-static void
-set_lot_is_closed( gpointer pObject, gboolean closed )
-{
-    GNCLot* lot = GNC_LOT(pObject);
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( GNC_IS_LOT(pObject) );
-
-    lot->is_closed = closed;
-}
-
-static void
-load_single_lot( GncGdaBackend* be, GdaDataModel* pModel, int row, GList** pList )
-{
-	GNCLot* lot;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    lot = gnc_lot_new( be->primary_book );
-
-    gnc_gda_load_object( be, pModel, row, GNC_ID_LOT, lot, col_table );
-	*pList = g_list_append( *pList, lot );
-
-    qof_instance_mark_clean( QOF_INSTANCE(lot) );
-}
-
-static void
-load_all_lots( GncGdaBackend* be )
-{
-    static GdaQuery* query;
-    GdaObject* ret;
-
-	g_return_if_fail( be != NULL );
-
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-		GList* list = NULL;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_lot( be, pModel, r, &list );
-        }
-
-		if( list != NULL ) {
-			gnc_gda_slots_load_for_list( be, list );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_lots_tables( GncGdaBackend* be )
-{
-	gint version;
-
-	g_return_if_fail( be != NULL );
-
-	version = gnc_gda_get_table_version( be, TABLE_NAME );
-    if( version == 0 ) {
-    	GError* error = NULL;
-
-        gnc_gda_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
-        if( error != NULL ) {
-            PERR( "Error creating table: %s\n", error->message );
-        }
-    }
-}
-
-/* ================================================================= */
-
-static void
-commit_lot( QofInstance* inst, GncGdaBackend* be )
-{
-	gint op;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_LOT(inst) );
-
-	if( qof_instance_get_destroying( inst ) ) {
-		op = OP_DB_DELETE;
-	} else if( be->is_pristine_db ) {
-		op = OP_DB_ADD;
-	} else {
-		op = OP_DB_ADD_OR_UPDATE;
-	}
-    (void)gnc_gda_do_db_operation( be, op, TABLE_NAME, GNC_ID_LOT, inst, col_table );
-
-    // Now, commit any slots
-    gnc_gda_slots_save( be, qof_instance_get_guid( inst ),
-                        qof_instance_get_slots( inst ) );
-}
-
-/* ----------------------------------------------------------------- */
-static void
-load_lot_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GUID guid;
-    const GUID* pGuid;
-	GNCLot* lot = NULL;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        pGuid = NULL;
-    } else {
-        string_to_guid( g_value_get_string( val ), &guid );
-        pGuid = &guid;
-    }
-	if( pGuid != NULL ) {
-		lot = gnc_lot_lookup( pGuid, be->primary_book );
-	}
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, lot, NULL );
-    } else {
-		(*setter)( pObject, (const gpointer)lot );
-    }
-}
-
-static col_type_handler_t lot_guid_handler =
-        { load_lot_guid, gnc_gda_create_objectref_guid_col,
-            gnc_gda_get_gvalue_objectref_guid_for_query, gnc_gda_get_gvalue_objectref_guid_cond };
-/* ================================================================= */
-void
-gnc_gda_init_lot_handler( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_LOT,
-        commit_lot,            /* commit */
-        load_all_lots,            /* initial_load */
-        create_lots_tables    /* create tables */
-    };
-
-    qof_object_register_backend( GNC_ID_LOT, GNC_GDA_BACKEND, &be_data );
-
-	gnc_gda_register_col_type_handler( CT_LOTREF, &lot_guid_handler );
-}
-
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-lots-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-lots-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-lots-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,37 +0,0 @@
-/********************************************************************
- * gnc-lots-gda.h: load and save data to SQL via libgda             *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-lots-gda.h
- *  @brief load and save data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_LOT_GDA_H_
-#define GNC_LOT_GDA_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-void gnc_gda_init_lot_handler( void );
-
-#endif /* GNC_LOT_GDA_H_ */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-price-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-price-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-price-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,209 +0,0 @@
-/********************************************************************
- * gnc-price-gda.c: load and save data to SQL via libgda            *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-price-gda.c
- *  @brief load and save data to SQL 
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-#include "gnc-pricedb.h"
-
-#include "gnc-backend-util-gda.h"
-
-#include "gnc-commodity-gda.h"
-#include "gnc-price-gda.h"
-#include "gnc-slots-gda.h"
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TABLE_NAME "prices"
-#define TABLE_VERSION 1
-
-#define PRICE_MAX_SOURCE_LEN 2048
-#define PRICE_MAX_TYPE_LEN 2048
-
-static const col_cvt_t col_table[] =
-{
-    { "guid",           CT_GUID,           0,                    COL_NNUL|COL_PKEY, "guid" },
-    { "commodity_guid", CT_COMMODITYREF,   0,                    COL_NNUL,          NULL, PRICE_COMMODITY },
-    { "currency_guid",  CT_COMMODITYREF,   0,                    COL_NNUL,          NULL, PRICE_CURRENCY },
-    { "date",           CT_TIMESPEC,       0,                    COL_NNUL,          NULL, PRICE_DATE },
-    { "source",         CT_STRING,         PRICE_MAX_SOURCE_LEN, 0,                 NULL, PRICE_SOURCE },
-    { "type",           CT_STRING,         PRICE_MAX_TYPE_LEN,   0,                 NULL, PRICE_TYPE },
-    { "value",          CT_NUMERIC,        0,                    COL_NNUL,          NULL, PRICE_VALUE },
-    { NULL }
-};
-
-/* ================================================================= */
-
-static GNCPrice*
-load_single_price( GncGdaBackend* be, GdaDataModel* pModel, int row, GList** pList )
-{
-	GNCPrice* pPrice;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( pModel != NULL, NULL );
-	g_return_val_if_fail( row >= 0, NULL );
-
-    pPrice = gnc_price_create( be->primary_book );
-
-    gnc_gda_load_object( be, pModel, row, GNC_ID_PRICE, pPrice, col_table );
-	*pList = g_list_append( *pList, pPrice );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pPrice) );
-
-    return pPrice;
-}
-
-static void
-load_all_prices( GncGdaBackend* be )
-{
-    static GdaQuery* query;
-    GdaObject* ret;
-    QofBook* pBook;
-    GNCPriceDB* pPriceDB;
-
-	g_return_if_fail( be != NULL );
-
-    pBook = be->primary_book;
-    pPriceDB = gnc_book_get_pricedb( pBook );
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-        GNCPrice* pPrice;
-		GList* list = NULL;
-
-        for( r = 0; r < numRows; r++ ) {
-            pPrice = load_single_price( be, pModel, r, &list );
-
-            if( pPrice != NULL ) {
-                gnc_pricedb_add_price( pPriceDB, pPrice );
-            }
-        }
-
-		if( list != NULL ) {
-			gnc_gda_slots_load_for_list( be, list );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_prices_tables( GncGdaBackend* be )
-{
-	gint version;
-
-	g_return_if_fail( be != NULL );
-
-	version = gnc_gda_get_table_version( be, TABLE_NAME );
-    if( version == 0 ) {
-    	GError* error = NULL;
-
-        gnc_gda_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
-        if( error != NULL ) {
-            PERR( "Error creating table: %s\n", error->message );
-        }
-    }
-}
-
-/* ================================================================= */
-
-static void
-save_price( QofInstance* inst, GncGdaBackend* be )
-{
-    GNCPrice* pPrice = GNC_PRICE(inst);
-	gint op;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_PRICE(inst) );
-
-    /* Ensure commodity and currency are in the db */
-    gnc_gda_save_commodity( be, gnc_price_get_commodity( pPrice ) );
-    gnc_gda_save_commodity( be, gnc_price_get_currency( pPrice ) );
-
-	if( qof_instance_get_destroying( inst ) ) {
-		op = OP_DB_DELETE;
-	} else if( be->is_pristine_db ) {
-		op = OP_DB_ADD;
-	} else {
-		op = OP_DB_ADD_OR_UPDATE;
-	}
-    (void)gnc_gda_do_db_operation( be, op, TABLE_NAME, GNC_ID_PRICE, pPrice, col_table );
-}
-
-static gboolean
-write_price( GNCPrice* p, gpointer data )
-{
-    GncGdaBackend* be = (GncGdaBackend*)data;
-
-	g_return_val_if_fail( p != NULL, FALSE );
-	g_return_val_if_fail( data != NULL, FALSE );
-
-    save_price( QOF_INSTANCE(p), be );
-
-    return TRUE;
-}
-
-static void
-write_prices( GncGdaBackend* be )
-{
-    GNCPriceDB* priceDB;
-
-	g_return_if_fail( be != NULL );
-
-    priceDB = gnc_book_get_pricedb( be->primary_book );
-
-    gnc_pricedb_foreach_price( priceDB, write_price, be, TRUE );
-}
-
-/* ================================================================= */
-void
-gnc_gda_init_price_handler( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_PRICE,
-        save_price,         		/* commit */
-        load_all_prices,            /* initial_load */
-        create_prices_tables,    	/* create tables */
-		NULL, NULL, NULL,
-		write_prices				/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_PRICE, GNC_GDA_BACKEND, &be_data );
-}
-
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-price-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-price-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-price-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,37 +0,0 @@
-/********************************************************************
- * gnc-price-gda.h: load and save data to SQL via libgda            *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-price-gda.h
- *  @brief load and save data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_PRICE_GDA_H_
-#define GNC_PRICE_GDA_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-void gnc_gda_init_price_handler( void );
-
-#endif /* GNC_PRICE_GDA_H_ */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-recurrence-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-recurrence-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-recurrence-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,388 +0,0 @@
-/********************************************************************
- * gnc-recurrence-gda.c: load and save data to SQL via libgda       *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-recurrence-gda.c
- *  @brief load and save data to SQL 
- *  @author Copyright (c) 2006, 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-#include "gnc-engine.h"
-#include "Recurrence.h"
-
-#include "gnc-backend-util-gda.h"
-
-#include "gnc-recurrence-gda.h"
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TABLE_NAME "recurrences"
-#define TABLE_VERSION 1
-
-#define BUDGET_MAX_RECURRENCE_PERIOD_TYPE_LEN 2048
-
-typedef struct {
-    GncGdaBackend* be;
-    const GUID* guid;
-	Recurrence* pRecurrence;
-} recurrence_info_t;
-
-static gpointer get_obj_guid( gpointer pObject, const QofParam* param );
-static void set_obj_guid( gpointer pObject, gpointer pValue );
-static gint get_recurrence_mult( gpointer pObject );
-static void set_recurrence_mult( gpointer pObject, gint value );
-static gpointer get_recurrence_period_type( gpointer pObject, const QofParam* );
-static void set_recurrence_period_type( gpointer pObject, gpointer pValue );
-static gpointer get_recurrence_period_start( gpointer pObject, const QofParam* );
-static void set_recurrence_period_start( gpointer pObject, gpointer pValue );
-
-static const col_cvt_t col_table[] =
-{
-    { "obj_guid",                CT_GUID,   0,                                     COL_NNUL, NULL, NULL,
-            get_obj_guid, set_obj_guid },
-    { "recurrence_mult",         CT_INT,    0,                                     COL_NNUL, NULL, NULL,
-            (QofAccessFunc)get_recurrence_mult, (QofSetterFunc)set_recurrence_mult },
-    { "recurrence_period_type",  CT_STRING, BUDGET_MAX_RECURRENCE_PERIOD_TYPE_LEN, COL_NNUL, NULL, NULL,
-			get_recurrence_period_type, set_recurrence_period_type },
-    { "recurrence_period_start", CT_GDATE,  0,                                     COL_NNUL, NULL, NULL,
-            get_recurrence_period_start, set_recurrence_period_start },
-    { NULL }
-};
-
-/* Special column table because we need to be able to access the table by
-a column other than the primary key */
-static const col_cvt_t guid_col_table[] =
-{
-    { "obj_guid", CT_GUID, 0, 0, NULL, NULL, get_obj_guid, set_obj_guid },
-    { NULL }
-};
-
-/* ================================================================= */
-
-static gpointer
-get_obj_guid( gpointer pObject, const QofParam* param )
-{
-    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-
-    return (gpointer)pInfo->guid;
-}
-
-static void
-set_obj_guid( gpointer pObject, gpointer pValue )
-{
-    // Nowhere to put the GUID
-}
-
-static gint
-get_recurrence_mult( gpointer pObject )
-{
-    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
-	
-	g_return_val_if_fail( pObject != NULL, 0 );
-	g_return_val_if_fail( pInfo->pRecurrence != NULL, 0 );
-
-	return pInfo->pRecurrence->mult;
-}
-
-static void
-set_recurrence_mult( gpointer pObject, gint value )
-{
-    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( pInfo->pRecurrence != NULL );
-
-    pInfo->pRecurrence->mult = value;
-}
-
-static gpointer
-get_recurrence_period_type( gpointer pObject, const QofParam* param )
-{
-    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( pInfo->pRecurrence != NULL, NULL );
-
-    return (gpointer)recurrencePeriodTypeToString(
-                            recurrenceGetPeriodType( pInfo->pRecurrence ) );
-}
-
-static void
-set_recurrence_period_type( gpointer pObject, gpointer pValue )
-{
-    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( pInfo->pRecurrence != NULL );
-	g_return_if_fail( pValue != NULL );
-
-    pInfo->pRecurrence->ptype = recurrencePeriodTypeFromString( (gchar*)pValue );
-}
-
-static gpointer
-get_recurrence_period_start( gpointer pObject, const QofParam* param )
-{
-    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
-    static GDate date;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( pInfo->pRecurrence != NULL, NULL );
-
-    date = recurrenceGetDate( pInfo->pRecurrence );
-    return (gpointer)&date;
-}
-
-static void
-set_recurrence_period_start( gpointer pObject, gpointer pValue )
-{
-    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
-    GDate* date = (GDate*)pValue;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( pInfo->pRecurrence != NULL );
-	g_return_if_fail( pValue != NULL );
-
-    pInfo->pRecurrence->start = *date;
-}
-
-/* ================================================================= */
-
-void
-gnc_gda_recurrence_save( GncGdaBackend* be, const GUID* guid, const Recurrence* r )
-{
-    recurrence_info_t recurrence_info;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( guid != NULL );
-	g_return_if_fail( r != NULL );
-
-	gnc_gda_recurrence_delete( be, guid );
-
-    recurrence_info.be = be;
-    recurrence_info.guid = guid;
-	recurrence_info.pRecurrence = (Recurrence*)r;
-    (void)gnc_gda_do_db_operation( be, OP_DB_ADD, TABLE_NAME,
-                                TABLE_NAME, &recurrence_info, col_table );
-}
-
-void
-gnc_gda_recurrence_save_list( GncGdaBackend* be, const GUID* guid, GList* schedule )
-{
-    recurrence_info_t recurrence_info;
-	GList* l;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( guid != NULL );
-
-	gnc_gda_recurrence_delete( be, guid );
-
-    recurrence_info.be = be;
-    recurrence_info.guid = guid;
-	for( l = schedule; l != NULL; l = g_list_next( l ) ) {
-		recurrence_info.pRecurrence = (Recurrence*)l->data;
-    	(void)gnc_gda_do_db_operation( be, OP_DB_ADD, TABLE_NAME,
-                                TABLE_NAME, &recurrence_info, col_table );
-	}
-}
-
-void
-gnc_gda_recurrence_delete( GncGdaBackend* be, const GUID* guid )
-{
-    recurrence_info_t recurrence_info;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( guid != NULL );
-
-    recurrence_info.be = be;
-    recurrence_info.guid = guid;
-    (void)gnc_gda_do_db_operation( be, OP_DB_DELETE, TABLE_NAME,
-                                TABLE_NAME, &recurrence_info, guid_col_table );
-}
-
-static void
-load_recurrence( GncGdaBackend* be, GdaDataModel* pModel, gint row, Recurrence* r )
-{
-    recurrence_info_t recurrence_info;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( r != NULL );
-
-    recurrence_info.be = be;
-	recurrence_info.pRecurrence = r;
-
-    gnc_gda_load_object( be, pModel, row, TABLE_NAME, &recurrence_info, col_table );
-}
-
-static GdaObject*
-gnc_gda_set_recurrences_from_db( GncGdaBackend* be, const GUID* guid )
-{
-    gchar* buf;
-    GdaObject* ret;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-    gchar* field_name;
-    static GdaQuery* query = NULL;
-    GdaQueryCondition* cond;
-    GdaQueryField* key_value;
-    GValue value;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( guid != NULL, NULL );
-
-    guid_to_string_buff( guid, guid_buf );
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        GdaQueryTarget* target;
-        GdaQueryField* key;
-
-        /* SELECT */
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-        target = gda_query_get_target_by_alias( query, TABLE_NAME );
-
-        /* WHERE */
-        cond = gda_query_condition_new( query, GDA_QUERY_CONDITION_LEAF_EQUAL );
-        gda_query_set_condition( query, cond );
-
-        field_name = g_strdup_printf( "%s.%s",
-                        gda_query_target_get_alias( target ), "obj_guid" );
-        key = gda_query_field_field_new( query, field_name );
-        g_free( field_name );
-        gda_query_field_set_visible( key, TRUE );
-        gda_query_condition_leaf_set_operator( cond,
-                                                GDA_QUERY_CONDITION_OP_LEFT,
-                                                GDA_QUERY_FIELD(key) );
-        g_object_unref( G_OBJECT(key) );
-
-        key_value = gda_query_field_value_new( query, G_TYPE_STRING );
-        gda_query_field_set_visible( key_value, TRUE );
-        gda_query_condition_leaf_set_operator( cond, GDA_QUERY_CONDITION_OP_RIGHT,
-                                                GDA_QUERY_FIELD(key_value) );
-        g_object_unref( G_OBJECT(key_value) );
-    }
-
-    /* Fill in the guid value */
-    cond = gda_query_get_condition( query );
-    key_value = gda_query_condition_leaf_get_operator( cond, 
-                                                GDA_QUERY_CONDITION_OP_RIGHT );
-    memset( &value, 0, sizeof( value ) );
-    g_value_init( &value, G_TYPE_STRING );
-    g_value_set_string( &value, guid_buf );
-    gda_query_field_value_set_value( GDA_QUERY_FIELD_VALUE(key_value), &value );
-
-    ret = gnc_gda_execute_query( be, query );
-
-	return ret;
-}
-
-void
-gnc_gda_recurrence_load( GncGdaBackend* be, const GUID* guid, Recurrence* pRecurrence )
-{
-	GdaObject* ret;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( guid != NULL );
-	g_return_if_fail( pRecurrence != NULL );
-
-	ret = gnc_gda_set_recurrences_from_db( be, guid );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-
-		if( numRows > 0 ) {
-			if( numRows > 1 ) {
-				g_warning( "More than 1 recurrence found: first one used" );
-			}
-			load_recurrence( be, pModel, 0, pRecurrence );
-		} else {
-			g_warning( "No recurrences found" );
-		}
-    }
-}
-
-void
-gnc_gda_recurrence_load_list( GncGdaBackend* be, const GUID* guid, GList** pSchedule )
-{
-	GdaObject* ret;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( guid != NULL );
-	g_return_if_fail( pSchedule != NULL );
-
-	ret = gnc_gda_set_recurrences_from_db( be, guid );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-		for( r = 0; r < numRows; r++ ) {
-			Recurrence* pRecurrence = g_new0( Recurrence, 1 );
-			load_recurrence( be, pModel, 0, pRecurrence );
-			*pSchedule = g_list_append( *pSchedule, pRecurrence );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_recurrence_tables( GncGdaBackend* be )
-{
-	gint version;
-
-	g_return_if_fail( be != NULL );
-
-	version = gnc_gda_get_table_version( be, TABLE_NAME );
-    if( version == 0 ) {
-    	GError* error = NULL;
-
-        gnc_gda_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
-        if( error != NULL ) {
-            PERR( "Error creating table: %s\n", error->message );
-        }
-    }
-}
-
-/* ================================================================= */
-void
-gnc_gda_init_recurrence_handler( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_ACCOUNT,
-        NULL,                    /* commit - cannot occur */
-        NULL,                    /* initial_load - cannot occur */
-        create_recurrence_tables        /* create_tables */
-    };
-
-    qof_object_register_backend( TABLE_NAME, GNC_GDA_BACKEND, &be_data );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-recurrence-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-recurrence-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-recurrence-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,43 +0,0 @@
-/********************************************************************
- * gnc-recurrence-gda.h: load and save data to SQL via libgda       *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-recurrence-gda.h
- *  @brief load and save accounts data to SQL via libgda
- *  @author Copyright (c) 2006-2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_RECURRENCE_GDA_H_
-#define GNC_RECURRENCE_GDA_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-void gnc_gda_recurrence_save( GncGdaBackend* be, const GUID* guid, const Recurrence* pRecurrence );
-void gnc_gda_recurrence_save_list( GncGdaBackend* be, const GUID* guid, GList* schedule );
-void gnc_gda_recurrence_delete( GncGdaBackend* be, const GUID* guid );
-void gnc_gda_recurrence_load( GncGdaBackend* be, const GUID* guid, Recurrence* pRecurrence );
-void gnc_gda_recurrence_load_list( GncGdaBackend* be, const GUID* guid, GList** pSchedule );
-
-void gnc_gda_init_recurrence_handler( void );
-
-#endif /* GNC_RECURRENCE_GDA_H_ */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-schedxaction-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-schedxaction-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-schedxaction-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,314 +0,0 @@
-/********************************************************************
- * gnc-schedxaction-gda.c: load and save data to SQL via libgda     *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-schedxaction-gda.c
- *  @brief load and save data to SQL 
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-
-#include "gnc-backend-util-gda.h"
-
-#include "gnc-schedxaction-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "SchedXaction.h"
-#include "SX-book.h"
-#include "Recurrence.h"
-
-#include "gnc-recurrence-gda.h"
-
-#define SCHEDXACTION_TABLE "schedxactions"
-#define TABLE_VERSION 1
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define SX_MAX_NAME_LEN 2048
-
-static gboolean get_autocreate( gpointer pObject );
-static void set_autocreate( gpointer pObject, gboolean value );
-static gboolean get_autonotify( gpointer pObject );
-static void set_autonotify( gpointer pObject, gboolean value );
-static gint get_instance_count( gpointer pObject );
-static gpointer get_template_act_guid( gpointer pObject, const QofParam* param );
-static void set_template_act_guid( gpointer pObject, gpointer pValue );
-
-static const col_cvt_t col_table[] =
-{
-    { "guid",              CT_GUID,    0,               COL_NNUL|COL_PKEY, "guid" },
-    { "name",              CT_STRING,  SX_MAX_NAME_LEN, 0,                 NULL, GNC_SX_NAME },
-	{ "enabled",           CT_BOOLEAN, 0,               COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)xaccSchedXactionGetEnabled, (QofSetterFunc)xaccSchedXactionSetEnabled },
-    { "start_date",        CT_GDATE,   0,               COL_NNUL,          NULL, GNC_SX_START_DATE },
-    { "last_occur",        CT_GDATE,   0,               0,                 NULL, GNC_SX_LAST_DATE },
-    { "num_occur",         CT_INT,     0,               COL_NNUL,          NULL, GNC_SX_NUM_OCCUR },
-    { "rem_occur",         CT_INT,     0,               COL_NNUL,          NULL, GNC_SX_REM_OCCUR },
-    { "auto_create",       CT_BOOLEAN, 0,               COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)get_autocreate,        (QofSetterFunc)set_autocreate },
-    { "auto_notify",       CT_BOOLEAN, 0,               COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)get_autonotify,        (QofSetterFunc)set_autonotify },
-    { "adv_creation",      CT_INT,     0,               COL_NNUL,          NULL, NULL,
-            (QofAccessFunc)xaccSchedXactionGetAdvanceCreation,
-            (QofSetterFunc)xaccSchedXactionSetAdvanceCreation },
-    { "adv_notify",        CT_INT,     0,               COL_NNUL,          NULL, NULL,
-            (QofAccessFunc)xaccSchedXactionGetAdvanceReminder,
-            (QofSetterFunc)xaccSchedXactionSetAdvanceReminder },
-	{ "instance_count",    CT_INT,     0,               COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)get_instance_count, (QofSetterFunc)gnc_sx_set_instance_count },
-    { "template_act_guid", CT_GUID,    0,               COL_NNUL,          NULL, NULL,
-			get_template_act_guid, set_template_act_guid },
-    { NULL }
-};
-
-/* ================================================================= */
-
-static gboolean
-get_autocreate( gpointer pObject )
-{
-    const SchedXaction* pSx = GNC_SX(pObject);
-    gboolean autoCreate;
-    gboolean autoNotify;
-
-	g_return_val_if_fail( pObject != NULL, FALSE );
-	g_return_val_if_fail( GNC_IS_SX(pObject), FALSE );
-
-    xaccSchedXactionGetAutoCreate( pSx, &autoCreate, &autoNotify );
-    return autoCreate;
-}
-
-static void 
-set_autocreate( gpointer pObject, gboolean value )
-{
-    SchedXaction* pSx = GNC_SX(pObject);
-    gboolean autoNotify;
-	gboolean dummy;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( GNC_IS_SX(pObject) );
-
-    xaccSchedXactionGetAutoCreate( pSx, &dummy, &autoNotify );
-    xaccSchedXactionSetAutoCreate( pSx, value, autoNotify );
-}
-
-static gboolean
-get_autonotify( gpointer pObject )
-{
-    const SchedXaction* pSx = GNC_SX(pObject);
-    gboolean autoCreate;
-    gboolean autoNotify;
-
-	g_return_val_if_fail( pObject != NULL, FALSE );
-	g_return_val_if_fail( GNC_IS_SX(pObject), FALSE );
-
-    xaccSchedXactionGetAutoCreate( pSx, &autoCreate, &autoNotify );
-    return autoNotify;
-}
-
-static void 
-set_autonotify( gpointer pObject, gboolean value )
-{
-    SchedXaction* pSx = GNC_SX(pObject);
-    gboolean autoCreate;
-    gboolean dummy;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( GNC_IS_SX(pObject) );
-
-    xaccSchedXactionGetAutoCreate( pSx, &autoCreate, &dummy );
-    xaccSchedXactionSetAutoCreate( pSx, autoCreate, value );
-}
-
-static gint
-get_instance_count( gpointer pObject )
-{
-    const SchedXaction* pSx = GNC_SX(pObject);
-
-	g_return_val_if_fail( pObject != NULL, FALSE );
-	g_return_val_if_fail( GNC_IS_SX(pObject), FALSE );
-
-    return gnc_sx_get_instance_count( pSx, NULL );
-}
-
-static gpointer
-get_template_act_guid( gpointer pObject, const QofParam* param )
-{
-    const SchedXaction* pSx = GNC_SX(pObject);
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( GNC_IS_SX(pObject), NULL );
-
-    return (gpointer)xaccAccountGetGUID( pSx->template_acct );
-}
-
-static void 
-set_template_act_guid( gpointer pObject, gpointer pValue )
-{
-    SchedXaction* pSx = GNC_SX(pObject);
-    QofBook* pBook;
-    GUID* guid = (GUID*)pValue;
-	Account* pAcct;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( GNC_IS_SX(pObject) );
-	g_return_if_fail( pValue != NULL );
-
-    pBook = qof_instance_get_book( QOF_INSTANCE(pSx) );
-	pAcct = xaccAccountLookup( guid, pBook );
-	sx_set_template_account( pSx, pAcct );
-}
-
-/* ================================================================= */
-static SchedXaction*
-load_single_sx( GncGdaBackend* be, GdaDataModel* pModel, int row, GList** pList )
-{
-    const GUID* guid;
-    GUID sx_guid;
-	SchedXaction* pSx;
-	GList* schedule = NULL;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( pModel != NULL, NULL );
-	g_return_val_if_fail( row >= 0, NULL );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    sx_guid = *guid;
-
-    pSx = xaccSchedXactionMalloc( be->primary_book );
-
-    gnc_gda_load_object( be, pModel, row, /*GNC_ID_SCHEDXACTION*/GNC_SX_ID, pSx, col_table );
-	gnc_gda_recurrence_load_list( be, guid, &schedule );
-	gnc_sx_set_schedule( pSx, schedule );
-	*pList = g_list_append( *pList, pSx );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pSx) );
-
-    return pSx;
-}
-
-static void
-load_all_sxes( GncGdaBackend* be )
-{
-    static GdaQuery* query;
-    GdaObject* ret;
-
-	g_return_if_fail( be != NULL );
-
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, SCHEDXACTION_TABLE );
-    }
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-     	SchedXactions *sxes;
-		GList* list = NULL;
-     	sxes = gnc_book_get_schedxactions( be->primary_book );
-
-        for( r = 0; r < numRows; r++ ) {
-            SchedXaction* sx;
-			
-			sx = load_single_sx( be, pModel, r, &list );
-			if( sx != NULL ) {
-		    	gnc_sxes_add_sx(sxes, sx);
-			}
-        }
-
-		if( list != NULL ) {
-			gnc_gda_slots_load_for_list( be, list );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_sx_tables( GncGdaBackend* be )
-{
-	gint version;
-
-	g_return_if_fail( be != NULL );
-
-	version = gnc_gda_get_table_version( be, SCHEDXACTION_TABLE );
-    if( version == 0 ) {
-    	GError* error = NULL;
-
-        gnc_gda_create_table( be, SCHEDXACTION_TABLE, TABLE_VERSION, col_table, &error );
-        if( error != NULL ) {
-            PERR( "Error creating table: %s\n", error->message );
-        }
-    }
-}
-
-/* ================================================================= */
-void
-gnc_gda_save_schedxaction( QofInstance* inst, GncGdaBackend* be )
-{
-    SchedXaction* pSx = GNC_SX(inst);
-    const GUID* guid;
-	gint op;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_SX(inst) );
-	g_return_if_fail( be != NULL );
-
-	if( qof_instance_get_destroying( inst ) ) {
-		op = OP_DB_DELETE;
-	} else if( be->is_pristine_db ) {
-		op = OP_DB_ADD;
-	} else {
-		op = OP_DB_ADD_OR_UPDATE;
-	}
-    (void)gnc_gda_do_db_operation( be, op, SCHEDXACTION_TABLE, /*GNC_ID_SCHEDXACTION*/GNC_SX_ID, pSx, col_table );
-    guid = qof_instance_get_guid( inst );
-	gnc_gda_recurrence_save_list( be, guid, gnc_sx_get_schedule( pSx ) );
-
-    // Now, commit any slots
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-/* ================================================================= */
-void
-gnc_gda_init_schedxaction_handler( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_SCHEDXACTION,
-        gnc_gda_save_schedxaction,                /* commit */
-        load_all_sxes,                /* initial_load */
-        create_sx_tables        /* create_tables */
-    };
-
-    qof_object_register_backend( GNC_ID_SCHEDXACTION, GNC_GDA_BACKEND, &be_data );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-schedxaction-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-schedxaction-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-schedxaction-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,38 +0,0 @@
-/********************************************************************
- * gnc-schedxaction-gda.h: load and save data to SQL via libgda     *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-backend-gda.h
- *  @brief load and save data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_SCHEDXACTION_GDA_H_
-#define GNC_SCHEDXACTION_GDA_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-void gnc_gda_init_schedxaction_handler( void );
-void gnc_gda_save_schedxaction( QofInstance* inst, GncGdaBackend* be );
-
-#endif /* GNC_SCHEDXACTION_GDA_H_ */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-slots-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-slots-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-slots-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,605 +0,0 @@
-/********************************************************************
- * gnc-slots-gda.c: load and save data to SQL via libgda            *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-slots-gda.c
- *  @brief load and save data to SQL 
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-#include "gnc-engine.h"
-
-#include "gnc-backend-util-gda.h"
-
-#include "gnc-slots-gda.h"
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TABLE_NAME "slots"
-#define TABLE_VERSION 1
-
-typedef struct {
-    GncGdaBackend* be;
-    const GUID* guid;
-    KvpFrame* pKvpFrame;
-    KvpValueType value_type;
-    KvpValue* pKvpValue;
-    GString* path;
-} slot_info_t;
-
-static gpointer get_obj_guid( gpointer pObject, const QofParam* param );
-static void set_obj_guid( gpointer pObject, gpointer pValue );
-static gpointer get_path( gpointer pObject, const QofParam* param );
-static void set_path( gpointer pObject, gpointer pValue );
-static gpointer get_slot_type( gpointer pObject, const QofParam* param );
-static void set_slot_type( gpointer pObject, gpointer pValue );
-static gint64 get_int64_val( gpointer pObject, const QofParam* param );
-static void set_int64_val( gpointer pObject, gint64 pValue );
-static gpointer get_string_val( gpointer pObject, const QofParam* param );
-static void set_string_val( gpointer pObject, gpointer pValue );
-static gpointer get_double_val( gpointer pObject, const QofParam* param );
-static void set_double_val( gpointer pObject, gpointer pValue );
-static Timespec get_timespec_val( gpointer pObject, const QofParam* param );
-static void set_timespec_val( gpointer pObject, Timespec ts );
-static gpointer get_guid_val( gpointer pObject, const QofParam* param );
-static void set_guid_val( gpointer pObject, gpointer pValue );
-static gnc_numeric get_numeric_val( gpointer pObject, const QofParam* param );
-static void set_numeric_val( gpointer pObject, gnc_numeric value );
-
-#define SLOT_MAX_PATHNAME_LEN 4096
-#define SLOT_MAX_STRINGVAL_LEN 4096
-
-static const col_cvt_t col_table[] =
-{
-    { "obj_guid",     CT_GUID,     0,                     COL_NNUL, NULL, NULL,
-			get_obj_guid,     set_obj_guid },
-    { "name",         CT_STRING,   SLOT_MAX_PATHNAME_LEN, COL_NNUL, NULL, NULL,
-			get_path,         set_path },
-    { "slot_type",    CT_INT,      0,                     COL_NNUL, NULL, NULL,
-			get_slot_type,    set_slot_type, },
-    { "int64_val",    CT_INT64,    0,                     0,        NULL, NULL,
-			(QofAccessFunc)get_int64_val,    (QofSetterFunc)set_int64_val },
-    { "string_val",   CT_STRING,   SLOT_MAX_PATHNAME_LEN, 0,        NULL, NULL,
-			get_string_val,   set_string_val },
-    { "double_val",   CT_DOUBLE,   0,                     0,        NULL, NULL,
-			get_double_val,   set_double_val },
-    { "timespec_val", CT_TIMESPEC, 0,                     0,        NULL, NULL,
-			(QofAccessFunc)get_timespec_val, (QofSetterFunc)set_timespec_val },
-    { "guid_val",     CT_GUID,     0,                     0,        NULL, NULL,
-			get_guid_val,     set_guid_val },
-    { "numeric_val",  CT_NUMERIC,  0,                     0,        NULL, NULL,
-			(QofAccessFunc)get_numeric_val, (QofSetterFunc)set_numeric_val },
-    { NULL }
-};
-
-/* Special column table because we need to be able to access the table by
-a column other than the primary key */
-static const col_cvt_t obj_guid_col_table[] =
-{
-    { "obj_guid", CT_GUID, 0, 0, NULL, NULL, get_obj_guid, _retrieve_guid_ },
-    { NULL }
-};
-
-/* ================================================================= */
-
-static gpointer
-get_obj_guid( gpointer pObject, const QofParam* param )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-
-    return (gpointer)pInfo->guid;
-}
-
-static void
-set_obj_guid( gpointer pObject, gpointer pValue )
-{
-    // Nowhere to put the GUID
-}
-
-static gpointer
-get_path( gpointer pObject, const QofParam* param )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-
-    return (gpointer)pInfo->path->str;
-}
-
-static void
-set_path( gpointer pObject, gpointer pValue )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( pValue != NULL );
-
-    pInfo->path = g_string_new( (gchar*)pValue );
-}
-
-static gpointer
-get_slot_type( gpointer pObject, const QofParam* param )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-
-    return (gpointer)kvp_value_get_type( pInfo->pKvpValue );
-}
-
-static void
-set_slot_type( gpointer pObject, gpointer pValue )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( pValue != NULL );
-
-    pInfo->value_type = (KvpValueType)pValue;
-}
-
-static gint64
-get_int64_val( gpointer pObject, const QofParam* param )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_val_if_fail( pObject != NULL, 0 );
-
-    if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_GINT64 ) {
-        return kvp_value_get_gint64( pInfo->pKvpValue );
-    } else {
-        return 0;
-    }
-}
-
-static void
-set_int64_val( gpointer pObject, gint64 value )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-
-    if( pInfo->value_type == KVP_TYPE_GINT64 ) {
-        kvp_frame_set_gint64( pInfo->pKvpFrame, pInfo->path->str, value );
-    }
-}
-
-static gpointer
-get_string_val( gpointer pObject, const QofParam* param )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-
-    if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_STRING ) {
-        return (gpointer)kvp_value_get_string( pInfo->pKvpValue );
-    } else {
-        return NULL;
-    }
-}
-
-static void
-set_string_val( gpointer pObject, gpointer pValue )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-
-    if( pInfo->value_type == KVP_TYPE_STRING && pValue != NULL ) {
-        kvp_frame_set_string( pInfo->pKvpFrame, pInfo->path->str, (const gchar*)pValue );
-    }
-}
-
-static gpointer
-get_double_val( gpointer pObject, const QofParam* param )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-    static double d_val;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-
-    if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_DOUBLE ) {
-        d_val = kvp_value_get_double( pInfo->pKvpValue );
-        return (gpointer)&d_val;
-    } else {
-        return NULL;
-    }
-}
-
-static void
-set_double_val( gpointer pObject, gpointer pValue )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-
-    if( pInfo->value_type == KVP_TYPE_DOUBLE && pValue != NULL ) {
-        kvp_frame_set_double( pInfo->pKvpFrame, pInfo->path->str, *(double*)pValue );
-    }
-}
-
-static Timespec
-get_timespec_val( gpointer pObject, const QofParam* param )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_val_if_fail( pObject != NULL, gnc_dmy2timespec( 1, 1, 1970 ) );
-
-//if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_TIMESPEC ) {
-    return kvp_value_get_timespec( pInfo->pKvpValue );
-}
-
-static void
-set_timespec_val( gpointer pObject, Timespec ts )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-
-    if( pInfo->value_type == KVP_TYPE_TIMESPEC ) {
-        kvp_frame_set_timespec( pInfo->pKvpFrame, pInfo->path->str, ts );
-    }
-}
-
-static gpointer
-get_guid_val( gpointer pObject, const QofParam* param )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-
-    if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_GUID ) {
-        return (gpointer)kvp_value_get_guid( pInfo->pKvpValue );
-    } else {
-        return NULL;
-    }
-}
-
-static void
-set_guid_val( gpointer pObject, gpointer pValue )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-
-    if( pInfo->value_type == KVP_TYPE_GUID && pValue != NULL ) {
-        kvp_frame_set_guid( pInfo->pKvpFrame, pInfo->path->str, (GUID*)pValue );
-    }
-}
-
-static gnc_numeric
-get_numeric_val( gpointer pObject, const QofParam* param )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_val_if_fail( pObject != NULL, gnc_numeric_zero() );
-
-    if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_NUMERIC ) {
-        return kvp_value_get_numeric( pInfo->pKvpValue );
-    } else {
-        return gnc_numeric_zero();
-    }
-}
-
-static void
-set_numeric_val( gpointer pObject, gnc_numeric value )
-{
-    slot_info_t* pInfo = (slot_info_t*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-
-    if( pInfo->value_type == KVP_TYPE_NUMERIC ) {
-        kvp_frame_set_numeric( pInfo->pKvpFrame, pInfo->path->str, value );
-    }
-}
-
-static void
-save_slot( const gchar* key, KvpValue* value, gpointer data )
-{
-    slot_info_t* pSlot_info = (slot_info_t*)data;
-    gint curlen;
-
-	g_return_if_fail( key != NULL );
-	g_return_if_fail( value != NULL );
-	g_return_if_fail( data != NULL );
-
-    curlen = pSlot_info->path->len;
-    pSlot_info->pKvpValue = value;
-    if( curlen != 0 ) {
-        g_string_append( pSlot_info->path, "/" );
-    }
-    g_string_append( pSlot_info->path, key );
-
-    if( kvp_value_get_type( value ) == KVP_TYPE_FRAME ) {
-        KvpFrame* pKvpFrame = kvp_value_get_frame( value );
-        kvp_frame_for_each_slot( pKvpFrame, save_slot, pSlot_info );
-    } else {
-        (void)gnc_gda_do_db_operation( pSlot_info->be, OP_DB_ADD, TABLE_NAME,
-                                        TABLE_NAME, pSlot_info, col_table );
-    }
-
-    g_string_truncate( pSlot_info->path, curlen );
-}
-
-void
-gnc_gda_slots_save( GncGdaBackend* be, const GUID* guid, KvpFrame* pFrame )
-{
-    slot_info_t slot_info;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( guid != NULL );
-	g_return_if_fail( pFrame != NULL );
-
-    // If this is not saving into a new db, clear out the old saved slots first
-	if( !be->is_pristine_db ) {
-    	gnc_gda_slots_delete( be, guid );
-	}
-
-    slot_info.be = be;
-    slot_info.guid = guid;
-    slot_info.path = g_string_new( "" );
-    kvp_frame_for_each_slot( pFrame, save_slot, &slot_info );
-    g_string_free( slot_info.path, FALSE );
-}
-
-void
-gnc_gda_slots_delete( GncGdaBackend* be, const GUID* guid )
-{
-    slot_info_t slot_info;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( guid != NULL );
-
-    slot_info.be = be;
-    slot_info.guid = guid;
-    (void)gnc_gda_do_db_operation( be, OP_DB_DELETE, TABLE_NAME,
-                                TABLE_NAME, &slot_info, obj_guid_col_table );
-}
-
-static void
-load_slot( GncGdaBackend* be, GdaDataModel* pModel, gint row, KvpFrame* pFrame )
-{
-    slot_info_t slot_info;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pFrame != NULL );
-
-    slot_info.be = be;
-    slot_info.pKvpFrame = pFrame;
-    slot_info.path = NULL;
-
-    gnc_gda_load_object( be, pModel, row, TABLE_NAME, &slot_info, col_table );
-
-    if( slot_info.path != NULL ) {
-        g_string_free( slot_info.path, TRUE );
-    }
-}
-
-void
-gnc_gda_slots_load( GncGdaBackend* be, QofInstance* inst )
-{
-    gchar* buf;
-    GdaObject* ret;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-    gchar* field_name;
-    static GdaQuery* query = NULL;
-    GdaQueryCondition* cond;
-    GdaQueryField* key_value;
-    GValue value;
-	const GUID* guid;
-	KvpFrame* pFrame;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( inst != NULL );
-
-	guid = qof_instance_get_guid( inst );
-	pFrame = qof_instance_get_slots( inst );
-    guid_to_string_buff( guid, guid_buf );
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        GdaQueryTarget* target;
-        GdaQueryField* key;
-
-        /* SELECT */
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-        target = gda_query_get_target_by_alias( query, TABLE_NAME );
-
-        /* WHERE */
-        cond = gda_query_condition_new( query, GDA_QUERY_CONDITION_LEAF_EQUAL );
-        gda_query_set_condition( query, cond );
-
-        field_name = g_strdup_printf( "%s.%s",
-                        gda_query_target_get_alias( target ), "obj_guid" );
-        key = gda_query_field_field_new( query, field_name );
-        g_free( field_name );
-        gda_query_field_set_visible( key, TRUE );
-        gda_query_condition_leaf_set_operator( cond,
-                                                GDA_QUERY_CONDITION_OP_LEFT,
-                                                GDA_QUERY_FIELD(key) );
-        g_object_unref( G_OBJECT(key) );
-
-        key_value = gda_query_field_value_new( query, G_TYPE_STRING );
-        gda_query_field_set_visible( key_value, TRUE );
-        gda_query_condition_leaf_set_operator( cond, GDA_QUERY_CONDITION_OP_RIGHT,
-                                                GDA_QUERY_FIELD(key_value) );
-        g_object_unref( G_OBJECT(key_value) );
-    }
-
-    /* Fill in the guid value */
-    cond = gda_query_get_condition( query );
-    key_value = gda_query_condition_leaf_get_operator( cond, 
-                                                GDA_QUERY_CONDITION_OP_RIGHT );
-    memset( &value, 0, sizeof( value ) );
-    g_value_init( &value, G_TYPE_STRING );
-    g_value_set_string( &value, guid_buf );
-    gda_query_field_value_set_value( GDA_QUERY_FIELD_VALUE(key_value), &value );
-
-	ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_slot( be, pModel, r, pFrame );
-        }
-    }
-}
-
-static const GUID*
-load_obj_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row )
-{
-    static GUID guid;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( pModel != NULL, NULL );
-	g_return_val_if_fail( row >= 0, NULL );
-
-    gnc_gda_load_object( be, pModel, row, NULL, &guid, obj_guid_col_table );
-
-    return &guid;
-}
-
-static void
-load_slot_for_list_item( GncGdaBackend* be, GdaDataModel* pModel, gint row, QofCollection* coll )
-{
-    slot_info_t slot_info;
-	const GUID* guid;
-	QofInstance* inst;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( coll != NULL );
-
-	guid = load_obj_guid( be, pModel, row );
-	inst = qof_collection_lookup_entity( coll, guid );
-
-    slot_info.be = be;
-    slot_info.pKvpFrame = qof_instance_get_slots( inst );
-    slot_info.path = NULL;
-
-    gnc_gda_load_object( be, pModel, row, TABLE_NAME, &slot_info, col_table );
-
-    if( slot_info.path != NULL ) {
-        g_string_free( slot_info.path, TRUE );
-    }
-}
-
-void
-gnc_gda_slots_load_for_list( GncGdaBackend* be, GList* list )
-{
-	QofCollection* coll;
-	GdaQuery* query;
-	GString* sql;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-	gboolean first_guid = TRUE;
-	GdaDataModel* model;
-	gboolean single_item;
-
-	g_return_if_fail( be != NULL );
-
-	// Ignore empty list
-	if( list == NULL ) return;
-
-	coll = qof_instance_get_collection( QOF_INSTANCE(list->data) );
-
-	// Create the query for all slots for all items on the list
-	sql = g_string_sized_new( 40+(GUID_ENCODING_LENGTH+3)*g_list_length( list ) );
-	g_string_append_printf( sql, "SELECT * FROM %s WHERE %s ", TABLE_NAME, obj_guid_col_table[0].col_name );
-	if( g_list_length( list ) != 1 ) {
-		g_string_append( sql, "IN (" );
-		single_item = FALSE;
-	} else {
-		g_string_append( sql, "= " );
-		single_item = TRUE;
-	}
-	(void)gnc_gda_append_guid_list_to_sql( sql, list, G_MAXUINT );
-	if( !single_item ) {
-		g_string_append( sql, ")" );
-	}
-
-	// Execute the query and load the slots
-	query = gnc_gda_create_query_from_sql( be, sql->str );
-	model = gnc_gda_execute_select_sql( be, sql->str );
-    if( model != NULL ) {
-        int numRows = gda_data_model_get_n_rows( model );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_slot_for_list_item( be, model, r, coll );
-        }
-		g_object_unref( model );
-    }
-	g_string_free( sql, TRUE );
-}
-
-/* ================================================================= */
-static void
-create_slots_tables( GncGdaBackend* be )
-{
-	GError* error = NULL;
-	gboolean ok;
-	gint version;
-
-	g_return_if_fail( be != NULL );
-
-	version = gnc_gda_get_table_version( be, TABLE_NAME );
-	if( version == 0 ) {
-    	gnc_gda_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
-		if( error != NULL ) {
-			g_critical( "GDA: unable to create SLOTS table: %s\n", error->message );
-		}
-		ok = gnc_gda_create_index( be, "slots_guid_index", TABLE_NAME, obj_guid_col_table, &error );
-		if( !ok ) {
-			g_critical( "GDA: unable to create index: %s\n", error->message );
-		}
-	}
-}
-
-/* ================================================================= */
-void
-gnc_gda_init_slots_handler( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_ACCOUNT,
-        NULL,                    /* commit - cannot occur */
-        NULL,                    /* initial_load - cannot occur */
-        create_slots_tables        /* create_tables */
-    };
-
-    qof_object_register_backend( TABLE_NAME, GNC_GDA_BACKEND, &be_data );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-slots-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-slots-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-slots-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,42 +0,0 @@
-/********************************************************************
- * gnc-slots-gda.h: load and save data to SQL via libgda            *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-slots-gda.h
- *  @brief load and save accounts data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_SLOTS_GDA_H_
-#define GNC_SLOTS_GDA_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-void gnc_gda_slots_save( GncGdaBackend* be, const GUID* guid, KvpFrame* pFrame );
-void gnc_gda_slots_delete( GncGdaBackend* be, const GUID* guid );
-void gnc_gda_slots_load( GncGdaBackend* be, QofInstance* inst );
-void gnc_gda_slots_load_for_list( GncGdaBackend* be, GList* list );
-
-void gnc_gda_init_slots_handler( void );
-
-#endif /* GNC_SLOTS_GDA_H_ */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-transaction-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-transaction-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-transaction-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,1084 +0,0 @@
-/********************************************************************
- * gnc-transaction-gda.c: load and save data to SQL via libgda      *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-transaction-gda.c
- *  @brief load and save data to SQL 
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL db using libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <libgda/libgda.h>
-
-#include "qof.h"
-#include "qofquery-p.h"
-#include "qofquerycore-p.h"
-
-#include "Account.h"
-#include "Transaction.h"
-#include "engine-helpers.h"
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-transaction-gda.h"
-#include "gnc-commodity.h"
-#include "gnc-commodity-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gnc-engine.h"
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TRANSACTION_TABLE "transactions"
-#define TX_TABLE_VERSION 1
-#define SPLIT_TABLE "splits"
-#define SPLIT_TABLE_VERSION 1
-
-typedef struct {
-    GncGdaBackend* be;
-    const GUID* guid;
-} split_info_t;
-
-#define TX_MAX_NUM_LEN 2048
-#define TX_MAX_DESCRIPTION_LEN 2048
-
-static const col_cvt_t tx_col_table[] =
-{
-    { "guid",          CT_GUID,           0,                      COL_NNUL|COL_PKEY, "guid" },
-    { "currency_guid", CT_COMMODITYREF,   0,                      COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)xaccTransGetCurrency, (QofSetterFunc)xaccTransSetCurrency },
-    { "num",           CT_STRING,         TX_MAX_NUM_LEN,         COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)xaccTransGetNum, (QofSetterFunc)xaccTransSetNum },
-    { "post_date",     CT_TIMESPEC,       0,                      COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)xaccTransRetDatePostedTS, (QofSetterFunc)gnc_transaction_set_date_posted },
-    { "enter_date",    CT_TIMESPEC,       0,                      COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)xaccTransRetDateEnteredTS, (QofSetterFunc)gnc_transaction_set_date_entered },
-    { "description",   CT_STRING,         TX_MAX_DESCRIPTION_LEN, 0,                 NULL, NULL,
-            (QofAccessFunc)xaccTransGetDescription, (QofSetterFunc)xaccTransSetDescription },
-    { NULL }
-};
-
-static gpointer get_split_reconcile_state( gpointer pObject, const QofParam* param );
-static void set_split_reconcile_state( gpointer pObject, gpointer pValue );
-static void set_split_reconcile_date( gpointer pObject, Timespec ts );
-
-#define SPLIT_MAX_MEMO_LEN 2048
-#define SPLIT_MAX_ACTION_LEN 2048
-
-static const col_cvt_t split_col_table[] =
-{
-    { "guid",            CT_GUID,         0,                    COL_NNUL|COL_PKEY, "guid" },
-    { "tx_guid",         CT_TXREF,        0,                    COL_NNUL,          NULL, SPLIT_TRANS },
-    { "account_guid",    CT_ACCOUNTREF,   0,                    COL_NNUL,          NULL, SPLIT_ACCOUNT },
-    { "memo",            CT_STRING,       SPLIT_MAX_MEMO_LEN,   COL_NNUL,          NULL, SPLIT_MEMO },
-    { "action",          CT_STRING,       SPLIT_MAX_ACTION_LEN, COL_NNUL,          NULL, SPLIT_ACTION },
-    { "reconcile_state", CT_STRING,       1,                    COL_NNUL,          NULL, NULL,
-			get_split_reconcile_state, set_split_reconcile_state },
-    { "reconcile_date",  CT_TIMESPEC,     0,                    COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)xaccSplitRetDateReconciledTS, (QofSetterFunc)set_split_reconcile_date },
-    { "value",           CT_NUMERIC,      0,                    COL_NNUL,          NULL, SPLIT_VALUE },
-    { "quantity",        CT_NUMERIC,      0,                    COL_NNUL,          NULL, SPLIT_AMOUNT },
-    { NULL }
-};
-
-static const col_cvt_t guid_col_table[] =
-{
-    { "tx_guid", CT_GUID, 0, 0, "guid" },
-    { NULL }
-};
-
-static void retrieve_numeric_value( gpointer pObject, gnc_numeric value );
-
-/* ================================================================= */
-
-static gpointer
-get_split_reconcile_state( gpointer pObject, const QofParam* param )
-{
-    const Split* pSplit = GNC_SPLIT(pObject);
-    static gchar c[2];
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( GNC_IS_SPLIT(pObject), NULL );
-
-    c[0] = xaccSplitGetReconcile( pSplit );
-    c[1] = '\0';
-    return (gpointer)c;
-}
-
-static void 
-set_split_reconcile_state( gpointer pObject, gpointer pValue )
-{
-    Split* pSplit = GNC_SPLIT(pObject);
-    const gchar* s = (const gchar*)pValue;
-
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( GNC_IS_SPLIT(pObject) );
-	g_return_if_fail( pValue != NULL );
-
-    xaccSplitSetReconcile( pSplit, s[0] );
-}
-
-static void 
-set_split_reconcile_date( gpointer pObject, Timespec ts )
-{
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( GNC_IS_SPLIT(pObject) );
-
-    xaccSplitSetDateReconciledTS( GNC_SPLIT(pObject), &ts );
-}
-
-static void 
-retrieve_numeric_value( gpointer pObject, gnc_numeric value )
-{
-    gnc_numeric* pResult = (gnc_numeric*)pObject;
-
-	g_return_if_fail( pObject != NULL );
-
-    *pResult = value;
-}
-
-
-// Table to retrieve just the quantity
-static col_cvt_t quantity_table[] =
-{
-    { "quantity", CT_NUMERIC, 0, COL_NNUL, NULL, NULL, NULL, (QofSetterFunc)retrieve_numeric_value },
-    { NULL }
-};
-
-static gnc_numeric
-get_gnc_numeric_from_row( GncGdaBackend* be, GdaDataModel* model, int row )
-{
-	gnc_numeric val = gnc_numeric_zero();
-
-	g_return_val_if_fail( be != NULL, val );
-	g_return_val_if_fail( model != NULL, val );
-	g_return_val_if_fail( row >= 0, val );
-
-    gnc_gda_load_object( be, model, row, NULL, &val, quantity_table );
-
-    return val;
-}
-
-/*
- * get_account_balance_from_query
- *
- * Given a GDA query which should return a number of rows of gnc_numeric num/denom pairs,
- * return the sum.
- */
-static gnc_numeric
-get_account_balance_from_query( GncGdaBackend* be, GdaQuery* query )
-{
-	gnc_numeric bal = gnc_numeric_zero();
-    GdaObject* ret;
-
-	g_return_val_if_fail( be != NULL, bal );
-	g_return_val_if_fail( query != NULL, bal );
-
-	/* Execute the query */
-    ret = gnc_gda_execute_query( be, query );
-
-	/* Loop for all rows, convert each to a gnc_numeric and sum them */
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-		    gnc_numeric val = get_gnc_numeric_from_row( be, pModel, r );
-			bal = gnc_numeric_add( bal, val, GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
-		}
-	}
-	g_object_unref( G_OBJECT(ret) );
-
-    return bal;
-}
-
-/*
- * get_account_balance_from_sql
- *
- * Given an SQL query which should return a number of rows of gnc_numeric num/denom pairs,
- * return the sum.
- */
-static gnc_numeric
-get_account_balance_from_sql( GncGdaBackend* be, const gchar* sql )
-{
-	gnc_numeric bal = gnc_numeric_zero();
-	GdaDataModel* model;
-
-	g_return_val_if_fail( be != NULL, bal );
-	g_return_val_if_fail( sql != NULL, bal );
-
-	model = gnc_gda_execute_select_sql( be, sql );
-	if( model != NULL ) {
-    	int numRows;
-    	int r;
-
-		// Loop for all rows, convert each to a gnc_numeric and sum them
-    	numRows = gda_data_model_get_n_rows( model );
-
-    	for( r = 0; r < numRows; r++ ) {
-			gnc_numeric val = get_gnc_numeric_from_row( be, model, r );
-			bal = gnc_numeric_add( bal, val, GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
-		}
-	}
-	g_object_unref( G_OBJECT(model) );
-
-    return bal;
-}
-
-static void
-get_account_balance_for_list_from_sql( GncGdaBackend* be, GList* result_list, const gchar* sql )
-{
-	GdaDataModel* model;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( sql != NULL );
-
-	model = gnc_gda_execute_select_sql( be, sql );
-	if( model != NULL ) {
-		for( ; result_list != NULL; result_list = result_list->next ) {
-			acct_balances_t* ab = (acct_balances_t*)result_list->data;
-			GdaDataModel* acct_values_model = gda_data_model_filter_sql_new();
-			gchar guid_buf[GUID_ENCODING_LENGTH+1];
-			gchar* filter_sql;
-			gboolean success;
-    		int numRows;
-    		int r;
-			gnc_numeric bal = gnc_numeric_zero();
-
-    		guid_to_string_buff( qof_instance_get_guid( ab->acct ), guid_buf );
-			filter_sql = g_strdup_printf( "SELECT quantity_num,quantity_denom FROM %s WHERE account_guid='%s'",
-											SPLIT_TABLE, guid_buf );
-			gda_data_model_filter_sql_set_sql( GDA_DATA_MODEL_FILTER_SQL(acct_values_model), filter_sql );
-			g_free( filter_sql );
-			success = gda_data_model_filter_sql_run( GDA_DATA_MODEL_FILTER_SQL(acct_values_model) );
-			if( !success ) {
-				PERR( "Unable to filter SQL data model" );
-			}
-
-			// Loop for all rows, convert each to a gnc_numeric and sum them
-    		numRows = gda_data_model_get_n_rows( acct_values_model );
-
-    		for( r = 0; r < numRows; r++ ) {
-				gnc_numeric val = get_gnc_numeric_from_row( be, acct_values_model, r );
-				bal = gnc_numeric_add( bal, val, GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
-			}
-			ab->start_balance = bal;
-		}
-	}
-}
-
-
-void
-gnc_gda_get_account_balances( GncGdaBackend* be, Account* pAccount, 
-								    gnc_numeric* start_balance,
-								    gnc_numeric* cleared_balance,
-									gnc_numeric* reconciled_balance )
-{
-	GdaQuery* query;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-	gchar* sql;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pAccount != NULL );
-	g_return_if_fail( start_balance != NULL );
-	g_return_if_fail( cleared_balance != NULL );
-	g_return_if_fail( reconciled_balance != NULL );
-
-    guid_to_string_buff( qof_instance_get_guid( pAccount ), guid_buf );
-
-	/*
-	 * For start balance,
-	 *    SELECT SUM(QUANTITY_NUM),QUANTITY_DENOM FROM SPLITS
-	 *        WHERE ACCOUNT_GUID=<guid> GROUP BY QUANTITY_DENOM
-	 *
-	 * This will return one entry per denom.  These can then be made into
-	 * gnc_numerics and then added.  With luck, there will only be one entry.
-	 */
-
-	sql = g_strdup_printf( "SELECT SUM(QUANTITY_NUM) AS quantity_num,quantity_denom FROM %s WHERE ACCOUNT_GUID='%s' GROUP BY QUANTITY_DENOM", SPLIT_TABLE, guid_buf );
-	//sql = g_strdup_printf( "SELECT QUANTITY_NUM,QUANTITY_DENOM FROM %s WHERE ACCOUNT_GUID='%s' GROUP BY QUANTITY_DENOM", SPLIT_TABLE, guid_buf );
-
-	/* Create the query */
-//	query = gnc_gda_create_query_from_sql( be, sql );
-//	*start_balance = get_account_balance_from_query( be, query );
-	*start_balance = get_account_balance_from_sql( be, sql );
-//	g_object_unref( G_OBJECT(query) );
-	g_free( sql );
-
-	/*
-	 * For cleared balance,
-	 *    SELECT SUM(QUANTITY_NUM),QUANTITY_DENOM FROM SPLITS
-	 *        WHERE ACCOUNT_GUID=<guid> AND RECONCILE_STATE='c'
-	 *        GROUP BY QUANTITY_DENOM
-	 *
-	 * This just requires a modification to the query
-	 */
-
-	sql = g_strdup_printf( "SELECT SUM(QUANTITY_NUM) as quantity_num,quantity_denom FROM %s WHERE ACCOUNT_GUID='%s' AND RECONCILE_STATE='%c' GROUP BY QUANTITY_DENOM", SPLIT_TABLE, guid_buf, CREC );
-	//sql = g_strdup_printf( "SELECT QUANTITY_NUM,QUANTITY_DENOM FROM %s WHERE ACCOUNT_GUID='%s' AND RECONCILE_STATE='%c' GROUP BY QUANTITY_DENOM", SPLIT_TABLE, guid_buf, CREC );
-
-	//query = gnc_gda_create_query_from_sql( be, sql );
-    //*cleared_balance = get_account_balance_from_query( be, query );
-    *cleared_balance = get_account_balance_from_sql( be, sql );
-	//g_object_unref( G_OBJECT(query) );
-
-	g_free( sql );
-
-	/*
-	 * For reconciled balance,
-	 *    SELECT SUM(QUANTITY_NUM),QUANTITY_DENOM FROM SPLITS
-	 *        WHERE ACCOUNT_GUID=<guid> AND RECONCILE_STATE='c'
-	 *        GROUP BY QUANTITY_DENOM
-	 *
-	 * This just requires a small modification to the cleared balance query
-	 */
-
-	sql = g_strdup_printf( "SELECT SUM(QUANTITY_NUM) as quantity_num,quantity_denom FROM %s WHERE ACCOUNT_GUID='%s' AND RECONCILE_STATE='%c' GROUP BY QUANTITY_DENOM", SPLIT_TABLE, guid_buf, YREC );
-	//sql = g_strdup_printf( "SELECT QUANTITY_NUM,QUANTITY_DENOM FROM %s WHERE ACCOUNT_GUID='%s' AND RECONCILE_STATE='%c' GROUP BY QUANTITY_DENOM", SPLIT_TABLE, guid_buf, YREC );
-
-	//query = gnc_gda_create_query_from_sql( be, sql );
-	//*reconciled_balance = get_account_balance_from_query( be, query );
-	*reconciled_balance = get_account_balance_from_sql( be, sql );
-	//g_object_unref( G_OBJECT(query) );
-
-	g_free( sql );
-}
-
-GList*
-gnc_gda_get_account_balances_for_list( GncGdaBackend* be, GList* list )
-{
-	GdaQuery* query;
-	GString* sql;
-	GList* result_list = NULL;
-	GList* orig_list = list;
-
-	g_return_val_if_fail( be != NULL, NULL );
-
-	if( list == NULL ) return NULL;
-
-	// Create the result list
-	for( ; list != NULL; list = list->next ) {
-		acct_balances_t* acct_balances = g_new0( acct_balances_t, 1 );
-		acct_balances->acct = GNC_ACCOUNT(list->data);
-		acct_balances->start_balance = gnc_numeric_zero();
-		acct_balances->cleared_balance = gnc_numeric_zero();
-		acct_balances->reconciled_balance = gnc_numeric_zero();
-		result_list = g_list_append( result_list, acct_balances );
-	}
-	list = orig_list;
-
-	//
-	// For start balance,
-	//    SELECT SUM(QUANTITY_NUM),QUANTITY_DENOM FROM SPLITS
-	//        WHERE ACCOUNT_GUID=<guid> GROUP BY QUANTITY_DENOM
-	//
-	// This will return one entry per denom.  These can then be made into
-	// gnc_numerics and then added.  With luck, there will only be one entry.
-	//
-
-	sql = g_string_new( "" );
-	g_string_printf( sql, "SELECT account_guid,SUM(QUANTITY_NUM) AS quantity_num,quantity_denom FROM %s WHERE ACCOUNT_GUID IN (", SPLIT_TABLE );
-	(void)gnc_gda_append_guid_list_to_sql( sql, list, G_MAXUINT );
-	g_string_append( sql, ") GROUP BY ACCOUNT_GUID" );
-
-	get_account_balance_for_list_from_sql( be, result_list, sql->str );
-
-#if 0
-	/*
-	 * For cleared balance,
-	 *    SELECT SUM(QUANTITY_NUM),QUANTITY_DENOM FROM SPLITS
-	 *        WHERE ACCOUNT_GUID=<guid> AND RECONCILE_STATE='c'
-	 *        GROUP BY QUANTITY_DENOM
-	 *
-	 * This just requires a modification to the query
-	 */
-
-	sql = g_strdup_printf( "SELECT SUM(QUANTITY_NUM) as quantity_num,quantity_denom FROM %s WHERE ACCOUNT_GUID='%s' AND RECONCILE_STATE='%c' GROUP BY QUANTITY_DENOM", SPLIT_TABLE, guid_buf, CREC );
-	//sql = g_strdup_printf( "SELECT QUANTITY_NUM,QUANTITY_DENOM FROM %s WHERE ACCOUNT_GUID='%s' AND RECONCILE_STATE='%c' GROUP BY QUANTITY_DENOM", SPLIT_TABLE, guid_buf, CREC );
-
-	//query = gnc_gda_create_query_from_sql( be, sql );
-    //*cleared_balance = get_account_balance_from_query( be, query );
-    *cleared_balance = get_account_balance_from_sql( be, sql );
-	//g_object_unref( G_OBJECT(query) );
-
-	g_free( sql );
-
-	/*
-	 * For reconciled balance,
-	 *    SELECT SUM(QUANTITY_NUM),QUANTITY_DENOM FROM SPLITS
-	 *        WHERE ACCOUNT_GUID=<guid> AND RECONCILE_STATE='c'
-	 *        GROUP BY QUANTITY_DENOM
-	 *
-	 * This just requires a small modification to the cleared balance query
-	 */
-
-	sql = g_strdup_printf( "SELECT SUM(QUANTITY_NUM) as quantity_num,quantity_denom FROM %s WHERE ACCOUNT_GUID='%s' AND RECONCILE_STATE='%c' GROUP BY QUANTITY_DENOM", SPLIT_TABLE, guid_buf, YREC );
-	//sql = g_strdup_printf( "SELECT QUANTITY_NUM,QUANTITY_DENOM FROM %s WHERE ACCOUNT_GUID='%s' AND RECONCILE_STATE='%c' GROUP BY QUANTITY_DENOM", SPLIT_TABLE, guid_buf, YREC );
-
-	//query = gnc_gda_create_query_from_sql( be, sql );
-	//*reconciled_balance = get_account_balance_from_query( be, query );
-	*reconciled_balance = get_account_balance_from_sql( be, sql );
-	//g_object_unref( G_OBJECT(query) );
-#endif
-
-	g_string_free( sql, TRUE );
-
-	return result_list;
-}
-
-static void
-load_single_split( GncGdaBackend* be, GdaDataModel* pModel, int row, GList** pList )
-{
-    const GUID* guid;
-    GUID split_guid;
-	Split* pSplit;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    split_guid = *guid;
-
-    pSplit = xaccSplitLookup( &split_guid, be->primary_book );
-    if( pSplit == NULL ) {
-        pSplit = xaccMallocSplit( be->primary_book );
-    }
-
-    /* If the split is dirty, don't overwrite it */
-    if( !qof_instance_is_dirty( QOF_INSTANCE(pSplit) ) ) {
-    	gnc_gda_load_object( be, pModel, row, GNC_ID_SPLIT, pSplit, split_col_table );
-		*pList = g_list_append( *pList, pSplit );
-//    	gnc_gda_slots_load( be, QOF_INSTANCE(pSplit) );
-	}
-
-    g_assert( pSplit == xaccSplitLookup( &split_guid, be->primary_book ) );
-}
-
-static void
-load_all_splits_for_tx( GncGdaBackend* be, const GUID* tx_guid )
-{
-    GdaObject* ret;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-    GdaQuery* query;
-    GdaQueryCondition* cond;
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( tx_guid != NULL );
-
-    guid_to_string_buff( tx_guid, guid_buf );
-    memset( &value, 0, sizeof( GValue ) );
-    g_value_init( &value, G_TYPE_STRING );
-    g_value_set_string( &value, guid_buf );
-    query = gnc_gda_create_select_query( be, SPLIT_TABLE );
-    cond = gnc_gda_create_condition_from_field( query, "tx_guid", &value );
-    gda_query_set_condition( query, cond );
-    g_object_unref( G_OBJECT(cond) );
-
-    ret = gnc_gda_execute_query( be, query );
-    g_object_unref( G_OBJECT(query) );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-		GList* list = NULL;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_split( be, pModel, r, &list );
-        }
-
-		if( list != NULL ) {
-			gnc_gda_slots_load_for_list( be, list );
-		}
-    }
-}
-
-static void
-load_splits_for_tx_list( GncGdaBackend* be, GList* list )
-{
-	GString* sql;
-	GdaQuery* query;
-	QofCollection* col;
-	GdaDataModel* model;
-	gboolean first_guid = TRUE;
-
-	g_return_if_fail( be != NULL );
-
-	if( list == NULL ) return;
-
-	sql = g_string_sized_new( 40+(GUID_ENCODING_LENGTH+3)*g_list_length( list ) );
-	g_string_append_printf( sql, "SELECT * FROM %s WHERE %s IN (", SPLIT_TABLE, guid_col_table[0].col_name );
-	(void)gnc_gda_append_guid_list_to_sql( sql, list, G_MAXUINT );
-	g_string_append( sql, ")" );
-
-	// Execute the query and load the splits
-	model = gnc_gda_execute_select_sql( be, sql->str );
-    if( model != NULL ) {
-        int numRows = gda_data_model_get_n_rows( model );
-        int r;
-		GList* list = NULL;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_split( be, model, r, &list );
-        }
-
-		if( list != NULL ) {
-			gnc_gda_slots_load_for_list( be, list );
-		}
-    }
-	g_string_free( sql, TRUE );
-	g_object_unref( model );
-}
-
-static void
-load_single_tx( GncGdaBackend* be, GdaDataModel* pModel, int row, GList** pList )
-{
-    const GUID* guid;
-    GUID tx_guid;
-	Transaction* pTx;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    tx_guid = *guid;
-
-    pTx = xaccTransLookup( &tx_guid, be->primary_book );
-    if( pTx == NULL ) {
-        pTx = xaccMallocTransaction( be->primary_book );
-    }
-    xaccTransBeginEdit( pTx );
-    gnc_gda_load_object( be, pModel, row, GNC_ID_TRANS, pTx, tx_col_table );
-//    gnc_gda_slots_load( be, QOF_INSTANCE(pTx) );
-	*pList = g_list_append( *pList, pTx );
-//    load_all_splits_for_tx( be, qof_instance_get_guid( QOF_INSTANCE(pTx) ) );
-
-	// Leave the transaction open for edit until all of the slots and splits are loaded
-//    qof_instance_mark_clean( QOF_INSTANCE(pTx) );
-//    xaccTransCommitEdit( pTx );
-
-    g_assert( pTx == xaccTransLookup( &tx_guid, be->primary_book ) );
-}
-
-static void
-query_transactions( GncGdaBackend* be, GdaQuery* query )
-{
-    GdaObject* ret;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( query != NULL );
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-		GList* tx_list = NULL;
-		GList* node;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_tx( be, pModel, r, &tx_list );
-        }
-
-		if( tx_list != NULL ) {
-			gnc_gda_slots_load_for_list( be, tx_list );
-			load_splits_for_tx_list( be, tx_list );
-		}
-
-		// Commit all of the transactions
-		for( node = tx_list; node != NULL; node = node->next ) {
-			Transaction* pTx = GNC_TRANSACTION(node->data);
-    		qof_instance_mark_clean( QOF_INSTANCE(pTx) );
-    		xaccTransCommitEdit( pTx );
-		}
-    }
-}
-
-static void
-load_tx_by_guid( GncGdaBackend* be, GUID* tx_guid )
-{
-    GdaQuery* query;
-	gchar* sql;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( tx_guid != NULL );
-
-    guid_to_string_buff( tx_guid, guid_buf );
-	sql = g_strdup_printf( "SELECT * FROM %s WHERE guid = %s", TRANSACTION_TABLE, guid_buf );
-	query = gnc_gda_create_query_from_sql( be, sql );
-	query_transactions( be, query );
-}
-
-/* ================================================================= */
-static void
-load_all_tx( GncGdaBackend* be )
-{
-	gchar* sql;
-	GdaQuery* query;
-
-	g_return_if_fail( be != NULL );
-
-	sql = g_strdup_printf( "SELECT * FROM %s", TRANSACTION_TABLE );
-	query = gnc_gda_create_query_from_sql( be, sql );
-	g_free( sql );
-	query_transactions( be, query );
-}
-
-/* ================================================================= */
-static void
-create_transaction_tables( GncGdaBackend* be )
-{
-	gint version;
-
-	g_return_if_fail( be != NULL );
-
-	version = gnc_gda_get_table_version( be, TRANSACTION_TABLE );
-    if( version == 0 ) {
-    	GError* error = NULL;
-
-        gnc_gda_create_table( be, TRANSACTION_TABLE, TX_TABLE_VERSION, tx_col_table, &error );
-        if( error != NULL ) {
-            PERR( "Error creating table: %s\n", error->message );
-        }
-    }
-
-	version = gnc_gda_get_table_version( be, SPLIT_TABLE );
-    if( version == 0 ) {
-    	GError* error = NULL;
-
-        gnc_gda_create_table( be, SPLIT_TABLE, SPLIT_TABLE_VERSION, split_col_table, &error );
-        if( error != NULL ) {
-            PERR( "Error creating table: %s\n", error->message );
-        }
-    }
-}
-/* ================================================================= */
-static void
-delete_split_slots_cb( gpointer data, gpointer user_data )
-{
-    split_info_t* split_info = (split_info_t*)user_data;
-    Split* pSplit = GNC_SPLIT(data);
-
-	g_return_if_fail( data != NULL );
-	g_return_if_fail( GNC_IS_SPLIT(data) );
-	g_return_if_fail( user_data != NULL );
-
-    gnc_gda_slots_delete( split_info->be,
-                    qof_instance_get_guid( QOF_INSTANCE(pSplit) ) );
-}
-
-static void
-delete_splits( GncGdaBackend* be, Transaction* pTx )
-{
-    split_info_t split_info;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pTx != NULL );
-
-    (void)gnc_gda_do_db_operation( be, OP_DB_DELETE, SPLIT_TABLE,
-                                SPLIT_TABLE, pTx, guid_col_table );
-    split_info.be = be;
-
-    g_list_foreach( xaccTransGetSplitList( pTx ), delete_split_slots_cb, &split_info );
-}
-
-static void
-commit_split( QofInstance* inst, GncGdaBackend* be )
-{
-	gint op;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( be != NULL );
-
-	if( qof_instance_get_destroying( inst ) ) {
-		op = OP_DB_DELETE;
-	} else if( be->is_pristine_db ) {
-		op = OP_DB_ADD;
-	} else {
-		op = OP_DB_ADD_OR_UPDATE;
-	}
-    (void)gnc_gda_do_db_operation( be, op, SPLIT_TABLE, GNC_ID_SPLIT, inst, split_col_table );
-    gnc_gda_slots_save( be,
-                        qof_instance_get_guid( inst ),
-                        qof_instance_get_slots( inst ) );
-}
-
-static void
-save_split_cb( gpointer data, gpointer user_data )
-{
-    split_info_t* split_info = (split_info_t*)user_data;
-    Split* pSplit = GNC_SPLIT(data);
-
-	g_return_if_fail( data != NULL );
-	g_return_if_fail( GNC_IS_SPLIT(data) );
-	g_return_if_fail( user_data != NULL );
-
-    commit_split( QOF_INSTANCE(pSplit), split_info->be );
-}
-
-static void
-save_splits( GncGdaBackend* be, const GUID* tx_guid, SplitList* pSplitList )
-{
-    split_info_t split_info;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( tx_guid != NULL );
-	g_return_if_fail( pSplitList != NULL );
-
-    split_info.be = be;
-    split_info.guid = tx_guid;
-    g_list_foreach( pSplitList, save_split_cb, &split_info );
-}
-
-void
-gnc_gda_save_transaction( QofInstance* inst, GncGdaBackend* be )
-{
-    Transaction* pTx = GNC_TRANS(inst);
-    const GUID* guid;
-	gint op;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_TRANS(inst) );
-	g_return_if_fail( be != NULL );
-
-    // Ensure the commodity is in the db
-    gnc_gda_save_commodity( be, xaccTransGetCurrency( pTx ) );
-
-	if( qof_instance_get_destroying( inst ) ) {
-		op = OP_DB_DELETE;
-	} else if( be->is_pristine_db ) {
-		op = OP_DB_ADD;
-	} else {
-		op = OP_DB_ADD_OR_UPDATE;
-	}
-
-    (void)gnc_gda_do_db_operation( be, op, TRANSACTION_TABLE, GNC_ID_TRANS, pTx, tx_col_table );
-
-    guid = qof_instance_get_guid( inst );
-
-    // Delete any old slots and splits for this transaction
-	if( !be->is_pristine_db ) {
-    	delete_splits( be, pTx );
-	}
-
-    if( !qof_instance_get_destroying(inst) ) {
-        SplitList* splits;
-
-        // Now, commit any slots and splits
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-        splits = xaccTransGetSplitList( pTx );
-        save_splits( be, guid, splits );
-
-        /* Mark the splits as clean */
-        splits = xaccTransGetSplitList( pTx );
-        for( ; splits != NULL; splits = splits->next ) {
-            QofInstance* inst = QOF_INSTANCE(splits->data);
-
-            qof_instance_mark_clean(inst);
-        }
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-void
-gnc_gda_transaction_commit_splits( GncGdaBackend* be, Transaction* pTx )
-{
-    SplitList* splits;
-    Split* s;
-    QofBackend* qbe = (QofBackend*)be;
-    
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pTx != NULL );
-
-    splits = xaccTransGetSplitList( pTx );
-    for( ; splits != NULL; splits = splits->next ) {
-        s = GNC_SPLIT(splits->data);
-
-        qbe->commit( qbe, QOF_INSTANCE(s) );
-    }
-}
-
-/* ================================================================= */
-static const GUID*
-get_guid_from_query( QofQuery* pQuery )
-{
-    GList* pOrTerms;
-    GList* pAndTerms;
-    GList* andTerm;
-    QofQueryTerm* pTerm;
-    QofQueryPredData* pPredData;
-    GSList* pParamPath;
-
-	g_return_val_if_fail( pQuery != NULL, NULL );
-
-    pOrTerms = qof_query_get_terms( pQuery );
-    pAndTerms = (GList*)pOrTerms->data;
-    andTerm = pAndTerms->next;
-    pTerm = (QofQueryTerm*)andTerm->data;
-
-    pPredData = qof_query_term_get_pred_data( pTerm );
-    pParamPath = qof_query_term_get_param_path( pTerm );
-
-    if( strcmp( pPredData->type_name, "guid" ) == 0 ) {
-        query_guid_t pData = (query_guid_t)pPredData;
-        return pData->guids->data;
-    } else {
-        return NULL;
-    }
-}
-
-static gpointer
-compile_split_query( GncGdaBackend* be, QofQuery* pQuery )
-{
-	GString* sql;
-    const GUID* acct_guid;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-	GdaQuery* query;
-	GdaDataModel* model;
-	gchar* buf;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( pQuery != NULL, NULL );
-
-#if 1
-    acct_guid = get_guid_from_query( pQuery );
-    guid_to_string_buff( acct_guid, guid_buf );
-	sql = g_string_new( "" );
-	g_string_printf( sql, "SELECT DISTINCT tx_guid FROM %s WHERE account_guid='%s'", SPLIT_TABLE, guid_buf );
-	model = gnc_gda_execute_select_sql( be, sql->str );
-    if( model != NULL ) {
-        int numRows = gda_data_model_get_n_rows( model );
-        int r;
-
-		sql = g_string_sized_new( 40+(GUID_ENCODING_LENGTH+3)*numRows );
-
-		if( numRows != 1 ) {
-			g_string_printf( sql, "SELECT * FROM %s WHERE guid IN (", TRANSACTION_TABLE );
-		} else {
-			g_string_printf( sql, "SELECT * FROM %s WHERE guid =", TRANSACTION_TABLE );
-		}
-
-        for( r = 0; r < numRows; r++ ) {
-			const GUID* guid;
-
-			guid = gnc_gda_load_tx_guid( be, model, r );
-    		guid_to_string_buff( guid, guid_buf );
-			if( r != 0 ) {
-				g_string_append( sql, "," );
-			}
-			g_string_append( sql, "'" );
-			g_string_append( sql, guid_buf );
-			g_string_append( sql, "'" );
-        }
-
-		if( numRows != 1 ) {
-			g_string_append( sql, ")" );
-		}
-    }
-
-	buf = sql->str;
-	g_string_free( sql, FALSE );
-	return buf;
-#else
-#if 1
-    acct_guid = get_guid_from_query( pQuery );
-    guid_to_string_buff( acct_guid, guid_buf );
-    buf = g_strdup_printf( "SELECT * FROM %s WHERE guid IN (SELECT DISTINCT tx_guid FROM %s WHERE account_guid = '%s')",
-                            TRANSACTION_TABLE, SPLIT_TABLE, guid_buf );
-    return buf;
-#else
-    GdaQuery* query;
-    GdaQuery* subQuery;
-    GdaQueryTarget* target;
-    GdaQueryField* allFields;
-    GdaQueryField* field;
-    GValue value;
-    GdaQueryCondition* cond;
-    GdaQueryField* key;
-    GdaQueryField* key_value;
-
-    acct_guid = get_guid_from_query( pQuery );
-    guid_to_string_buff( acct_guid, guid_buf );
-
-    /* Subquery */
-
-    /* SELECT */
-    subQuery = gda_query_new( be->pDict );
-    gda_query_set_query_type( subQuery, GDA_QUERY_TYPE_SELECT );
-
-    /* FROM splits */
-    target = gda_query_target_new( subQuery, SPLIT_TABLE );
-    gda_query_add_target( subQuery, target, NULL );
-    g_object_unref( G_OBJECT(target) );
-
-    /* tx_guid */
-    field = gda_query_field_field_new( subQuery, "tx_guid" );
-    gda_query_field_set_visible( field, TRUE );
-    gda_entity_add_field( GDA_ENTITY(subQuery), GDA_ENTITY_FIELD(field) );
-    g_object_unref( G_OBJECT(field) );
-
-    /* WHERE */
-    memset( &value, 0, sizeof( GValue ) );
-    g_value_init( &value, G_TYPE_STRING );
-    g_value_set_string( &value, guid_buf );
-    cond = gnc_gda_create_condition_from_field( subQuery, "account_guid", &value );
-    gda_query_set_condition( subQuery, cond );
-    g_object_unref( G_OBJECT(cond) );
-
-    /* Main query */
-
-    /* SELECT * FROM transactions */
-    query = gnc_gda_create_select_query( be, TRANSACTION_TABLE );
-    gda_query_add_sub_query( query, subQuery );
-    g_object_unref( G_OBJECT(subQuery) );
-
-    /* WHERE */
-    cond = gda_query_condition_new( query, GDA_QUERY_CONDITION_LEAF_IN );
-    gda_query_set_condition( query, cond );
-    g_object_unref( G_OBJECT(cond) );
-
-    key = gda_query_field_field_new( query, "account_guid" );
-    gda_query_field_set_visible( key, TRUE );
-    gda_query_condition_leaf_set_operator( cond,
-                                            GDA_QUERY_CONDITION_OP_LEFT,
-                                            GDA_QUERY_FIELD(key) );
-    g_object_unref( G_OBJECT(key) );
-
-    key_value = gda_query_field_value_new( query, G_TYPE_STRING );
-    gda_query_field_set_visible( key_value, TRUE );
-    gda_query_field_value_set_is_parameter( GDA_QUERY_FIELD_VALUE(key_value), TRUE );
-
-    g_object_set( key_value, "value-provider", subQuery, NULL );
-    gda_query_condition_leaf_set_operator( cond, GDA_QUERY_CONDITION_OP_RIGHT,
-                                                GDA_QUERY_FIELD(key_value) );
-
-    return query;
-#endif
-#endif
-}
-
-static void
-run_split_query( GncGdaBackend* be, gpointer pQuery )
-{
-    GdaQuery* query;
-    const gchar* sql;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pQuery != NULL );
-
-#if 1
-    sql = (const gchar*)pQuery;
-
-	query = gnc_gda_create_query_from_sql( be, sql );
-#else
-    query = GDA_QUERY(pQuery);
-#endif
-    query_transactions( be, query );
-}
-
-static void
-free_split_query( GncGdaBackend* be, gpointer pQuery )
-{
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pQuery != NULL );
-
-#if 1
-    g_free( pQuery );
-#else
-    g_object_unref( G_OBJECT(pQuery) );
-#endif
-}
-
-/* ----------------------------------------------------------------- */
-static void
-load_tx_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GUID guid;
-    const GUID* pGuid;
-	Transaction* tx = NULL;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        pGuid = NULL;
-    } else {
-        string_to_guid( g_value_get_string( val ), &guid );
-        pGuid = &guid;
-    }
-	if( pGuid != NULL ) {
-		tx = xaccTransLookup( pGuid, be->primary_book );
-	}
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, tx, NULL );
-    } else {
-		(*setter)( pObject, (const gpointer)tx );
-    }
-}
-
-static col_type_handler_t tx_guid_handler =
-        { load_tx_guid, gnc_gda_create_objectref_guid_col,
-            gnc_gda_get_gvalue_objectref_guid_for_query, gnc_gda_get_gvalue_objectref_guid_cond };
-/* ================================================================= */
-void
-gnc_gda_init_transaction_handler( void )
-{
-    static GncGdaDataType_t be_data_tx =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_TRANS,
-        gnc_gda_save_transaction,    /* commit */
-        load_all_tx,                 /* initial_load */
-        create_transaction_tables    /* create tables */
-    };
-    static GncGdaDataType_t be_data_split =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_SPLIT,
-        commit_split,                /* commit */
-        NULL,                        /* initial_load */
-        NULL,                        /* create tables */
-        compile_split_query,
-        run_split_query,
-        free_split_query
-    };
-
-    qof_object_register_backend( GNC_ID_TRANS, GNC_GDA_BACKEND, &be_data_tx );
-    qof_object_register_backend( GNC_ID_SPLIT, GNC_GDA_BACKEND, &be_data_split );
-
-	gnc_gda_register_col_type_handler( CT_TXREF, &tx_guid_handler );
-}
-
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/backend/gda/gnc-transaction-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gnc-transaction-gda.h	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gnc-transaction-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,51 +0,0 @@
-/********************************************************************
- * gnc-transaction-gda.h: load and save data to SQL via libgda      *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-/** @file gnc-transaction-gda.h
- *  @brief load and save data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_TRANSACTION_GDA_H_
-#define GNC_TRANSACTION_GDA_H_
-
-#include "qof.h"
-#include <gmodule.h>
-
-void gnc_gda_init_transaction_handler( void );
-void gnc_gda_transaction_commit_splits( GncGdaBackend* be, Transaction* pTx );
-void gnc_gda_save_transaction( QofInstance* inst, GncGdaBackend* be );
-void gnc_gda_get_account_balances( GncGdaBackend* be, Account* pAccount, 
-								    gnc_numeric* start_balance,
-								    gnc_numeric* cleared_balance,
-									gnc_numeric* reconciled_balance );
-
-typedef struct {
-	Account* acct;
-	gnc_numeric start_balance;
-	gnc_numeric cleared_balance;
-	gnc_numeric reconciled_balance;
-} acct_balances_t;
-GList* gnc_gda_get_account_balances_for_list( GncGdaBackend* be, GList* list );
-
-#endif /* GNC_TRANSACTION_GDA_H_ */

Modified: gnucash/branches/gda-dev2/src/backend/gda/gncmod-backend-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/gda/gncmod-backend-gda.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/backend/gda/gncmod-backend-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -2,7 +2,7 @@
  * gncmod-backend-gda.c
  * module definition/initialization for the gda backend module
  *
- * Copyright (c) 2001 Linux Developers Group, Inc.
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
  *********************************************************************/
 
 #include <stdio.h>


Property changes on: gnucash/branches/gda-dev2/src/backend/sql
___________________________________________________________________
Name: svn:ignore
   + Makefile
Makefile.in
.deps
.libs
*.lo



Added: gnucash/branches/gda-dev2/src/backend/sql/Makefile.am
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/Makefile.am	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/Makefile.am	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,63 @@
+SUBDIRS = . test
+
+# Now a shared library AND a GModule
+lib_LTLIBRARIES = libgnc-backend-utils-sql.la
+pkglib_LTLIBRARIES = libgncmod-backend-sql.la
+
+
+AM_CFLAGS = \
+  -I.. -I../.. \
+  -DLOCALE_DIR=\""$(datadir)/locale"\" \
+  -I${top_srcdir}/src/backend \
+  -I${top_srcdir}/src/engine \
+  -I${top_srcdir}/src/core-utils\
+  -I${top_srcdir}/lib/libc\
+  ${QOF_CFLAGS} \
+  ${GLIB_CFLAGS} \
+  ${LIBGDA_CFLAGS} \
+  ${GCONF_CFLAGS} \
+  ${WARN_CFLAGS}
+
+libgnc_backend_utils_sql_la_SOURCES = \
+  gnc-backend-util-sql.c \
+  gnc-account-sql.c \
+  gnc-book-sql.c \
+  gnc-budget-sql.c \
+  gnc-commodity-sql.c \
+  gnc-lots-sql.c \
+  gnc-price-sql.c \
+  gnc-recurrence-sql.c \
+  gnc-schedxaction-sql.c \
+  gnc-slots-sql.c \
+  gnc-transaction-sql.c
+
+libgncmod_backend_sql_la_SOURCES = \
+  gnc-backend-sql.c
+
+noinst_HEADERS = \
+  gnc-account-sql.h \
+  gnc-backend-sql.h \
+  gnc-backend-util-sql.h \
+  gnc-book-sql.h \
+  gnc-budget-sql.h \
+  gnc-commodity-sql.h \
+  gnc-lots-sql.h \
+  gnc-price-sql.h \
+  gnc-recurrence-sql.h \
+  gnc-schedxaction-sql.h \
+  gnc-slots-sql.h \
+  gnc-transaction-sql.h
+
+libgnc_backend_utils_sql_la_LIBADD = \
+   ${GLIB_LIBS} ${GCONF_LIBS} \
+   ${top_builddir}/src/engine/libgncmod-engine.la \
+   ${QOF_LIBS}
+
+libgncmod_backend_sql_la_LDFLAGS = -module -avoid-version
+libgncmod_backend_sql_la_LIBADD = \
+   ${GLIB_LIBS} ${GCONF_LIBS} \
+   ${top_builddir}/src/engine/libgncmod-engine.la \
+   libgnc-backend-utils-sql.la \
+   ${QOF_LIBS}
+
+INCLUDES = -DG_LOG_DOMAIN=\"gnc.backend.sql\"

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-account-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-account-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-account-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,360 @@
+/********************************************************************
+ * gnc-account-sql.c: load and save data to SQL                     *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-account-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "qof.h"
+#include "Account.h"
+#include "AccountP.h"
+#include "gnc-commodity.h"
+
+#include "gnc-backend-util-sql.h"
+
+#include "gnc-account-sql.h"
+#include "gnc-commodity-sql.h"
+#include "gnc-slots-sql.h"
+#include "gnc-transaction-sql.h"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TABLE_NAME "accounts"
+#define TABLE_VERSION 1
+
+static gpointer get_parent( gpointer pObject, const QofParam* );
+static void set_parent( gpointer pObject, gpointer pValue );
+static void set_parent_guid( gpointer pObject, gpointer pValue );
+
+#define ACCOUNT_MAX_NAME_LEN /*2048*/20
+#define ACCOUNT_MAX_TYPE_LEN 2048
+#define ACCOUNT_MAX_CODE_LEN 2048
+#define ACCOUNT_MAX_DESCRIPTION_LEN 2048
+
+static const col_cvt_t col_table[] =
+{
+    { "guid",           CT_GUID,         0,                           COL_NNUL|COL_PKEY, "guid" },
+    { "name",           CT_STRING,       ACCOUNT_MAX_NAME_LEN,        COL_NNUL,          "name" },
+    { "account_type",   CT_STRING,       ACCOUNT_MAX_TYPE_LEN,        COL_NNUL,          NULL, ACCOUNT_TYPE_ },
+    { "commodity_guid", CT_COMMODITYREF, 0,                           COL_NNUL,          "commodity" },
+    { "parent_guid",    CT_GUID,         0,                           0,                 NULL, NULL, get_parent, set_parent },
+    { "code",           CT_STRING,       ACCOUNT_MAX_CODE_LEN,        0,                 "code" },
+    { "description",    CT_STRING,       ACCOUNT_MAX_DESCRIPTION_LEN, 0,                 "description" },
+    { NULL }
+};
+static col_cvt_t parent_col_table[] =
+{
+    { "parent_guid", CT_GUID, 0, 0, NULL, NULL, NULL, set_parent_guid },
+    { NULL }
+};
+
+typedef struct {
+	Account* pAccount;
+	GUID guid;
+} account_parent_guid_struct;
+
+/* ================================================================= */
+
+static gpointer
+get_parent( gpointer pObject, const QofParam* param )
+{
+    const Account* pAccount;
+    const Account* pParent;
+    const GUID* parent_guid;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( GNC_IS_ACCOUNT(pObject), NULL );
+
+    pAccount = GNC_ACCOUNT(pObject);
+    pParent = gnc_account_get_parent( pAccount );
+    if( pParent == NULL ) {
+        parent_guid = NULL;
+    } else {
+        parent_guid = qof_instance_get_guid( QOF_INSTANCE(pParent) );
+    }
+
+    return (gpointer)parent_guid;
+}
+
+static void 
+set_parent( gpointer pObject, gpointer pValue )
+{
+    Account* pAccount;
+    QofBook* pBook;
+    GUID* guid = (GUID*)pValue;
+    Account* pParent;
+    
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( GNC_IS_ACCOUNT(pObject) );
+
+    pAccount = GNC_ACCOUNT(pObject);
+    pBook = qof_instance_get_book( QOF_INSTANCE(pAccount) );
+    if( guid != NULL ) {
+        pParent = xaccAccountLookup( guid, pBook );
+        if( pParent != NULL ) {
+            gnc_account_append_child( pParent, pAccount );
+        }
+    }
+}
+
+static void
+set_parent_guid( gpointer pObject, gpointer pValue )
+{
+	account_parent_guid_struct* s = (account_parent_guid_struct*)pObject;
+    GUID* guid = (GUID*)pValue;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( pValue != NULL );
+
+	s->guid = *guid;
+}
+
+static void
+load_single_account( GncSqlBackend* be, GncSqlRow* row, GList** pList,
+				GList** l_accounts_needing_parents )
+{
+    const GUID* guid;
+    GUID acc_guid;
+	Account* pAccount;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( l_accounts_needing_parents != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    acc_guid = *guid;
+
+    pAccount = xaccAccountLookup( &acc_guid, be->primary_book );
+    if( pAccount == NULL ) {
+        pAccount = xaccMallocAccount( be->primary_book );
+    }
+	xaccAccountBeginEdit( pAccount );
+    gnc_sql_load_object( be, row, GNC_ID_ACCOUNT, pAccount, col_table );
+	*pList = g_list_append( *pList, pAccount );
+
+	xaccAccountCommitEdit( pAccount );
+    qof_instance_mark_clean( QOF_INSTANCE(pAccount) );
+
+	/* If we don't have a parent, it might be because the parent account hasn't
+	   been loaded yet.  Remember the account and its parent guid for later. */
+	if( gnc_account_get_parent( pAccount ) == NULL ) {
+		account_parent_guid_struct* s = g_slice_new( account_parent_guid_struct );
+		s->pAccount = pAccount;
+		gnc_sql_load_object( be, row, GNC_ID_ACCOUNT, s, parent_col_table );
+		*l_accounts_needing_parents = g_list_prepend( *l_accounts_needing_parents, s );
+	}
+}
+
+static void
+load_all_accounts( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt = NULL;
+    GncSqlResult* result;
+    QofBook* pBook;
+    gnc_commodity_table* pTable;
+    int numRows;
+    int r;
+    Account* parent;
+	GList* l_accounts_needing_parents = NULL;
+	GList* list = NULL;
+
+	g_return_if_fail( be != NULL );
+
+    pBook = be->primary_book;
+    pTable = gnc_commodity_table_get_table( pBook );
+
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+	if( result != NULL ) {
+	    GncSqlRow* row = gnc_sql_result_get_first_row( result );
+
+    	while( row != NULL ) {
+        	load_single_account( be, row, &list, &l_accounts_needing_parents );
+			row = gnc_sql_result_get_next_row( result );
+    	}
+		gnc_sql_result_dispose( result );
+
+		if( list != NULL ) {
+			gnc_sql_slots_load_for_list( be, list );
+		}
+
+		/* While there are items on the list of accounts needing parents,
+		   try to see if the parent has now been loaded.  Theory says that if
+		   items are removed from the front and added to the back if the
+		   parent is still not available, then eventually, the list will
+		   shrink to size 0. */
+		if( l_accounts_needing_parents != NULL ) {
+			gboolean progress_made = TRUE;
+	
+			Account* pParent;
+			GList* elem;
+				
+			while( progress_made ) {
+				progress_made = FALSE;
+				for( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) ) {
+					account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data;
+					const gchar* name = xaccAccountGetName( s->pAccount );
+   					pParent = xaccAccountLookup( &s->guid, be->primary_book );
+					if( pParent != NULL ) {
+						gnc_account_append_child( pParent, s->pAccount );
+						l_accounts_needing_parents = g_list_delete_link( l_accounts_needing_parents, elem );
+						progress_made = TRUE;
+					}
+				}
+			}
+	
+			/* Any accounts left over must be parented by the root account */
+			for( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) ) {
+				account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data;
+	            Account* root;
+	            root = gnc_book_get_root_account( pBook );
+            	if( root == NULL ) {
+                	root = gnc_account_create_root( pBook );
+            	}
+            	gnc_account_append_child( root, s->pAccount ); 
+			}
+		}
+	}
+}
+
+/* ================================================================= */
+static void
+create_account_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+void
+gnc_sql_save_account( QofInstance* inst, GncSqlBackend* be )
+{
+    Account* pAcc = GNC_ACCOUNT(inst);
+    const GUID* guid;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_ACCOUNT(inst) );
+
+    // If there is no commodity yet, this might be because a new account name has been entered directly
+    // into the register and an account window will be opened.  The account info is not complete yet,
+    // but the name has been set, triggering this commit
+    if( xaccAccountGetCommodity( pAcc ) != NULL ) {
+		gint op;
+
+        // Ensure the commodity is in the db
+        gnc_sql_save_commodity( be, xaccAccountGetCommodity( pAcc ) );
+
+		if( qof_instance_get_destroying( inst ) ) {
+			op = OP_DB_DELETE;
+		} else if( be->is_pristine_db ) {
+			op = OP_DB_ADD;
+		} else {
+			op = OP_DB_ADD_OR_UPDATE;
+		}
+
+        (void)gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_ACCOUNT, pAcc, col_table );
+
+        // Now, commit or delete any slots
+        guid = qof_instance_get_guid( inst );
+        if( !qof_instance_get_destroying(inst) ) {
+            gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+        } else {
+            gnc_sql_slots_delete( be, guid );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+load_account_guid( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GUID guid;
+    const GUID* pGuid;
+	Account* account = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        pGuid = NULL;
+    } else {
+        string_to_guid( g_value_get_string( val ), &guid );
+        pGuid = &guid;
+    }
+	if( pGuid != NULL ) {
+		account = xaccAccountLookup( pGuid, be->primary_book );
+	}
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, account, NULL );
+    } else {
+		(*setter)( pObject, (const gpointer)account );
+    }
+}
+
+static col_type_handler_t account_guid_handler
+	= { load_account_guid,
+		gnc_sql_add_objectref_guid_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+        gnc_sql_add_gvalue_objectref_guid_to_slist };
+/* ================================================================= */
+void
+gnc_sql_init_account_handler( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_ACCOUNT,
+        gnc_sql_save_account,				/* commit */
+        load_all_accounts,				/* initial_load */
+        create_account_tables		/* create_tables */
+    };
+
+    qof_object_register_backend( GNC_ID_ACCOUNT, GNC_SQL_BACKEND, &be_data );
+
+	gnc_sql_register_col_type_handler( CT_ACCOUNTREF, &account_guid_handler );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-account-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-account-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-account-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,38 @@
+/********************************************************************
+ * gnc-account-sql.h: load and save data to SQL                     *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-account-sql.h
+ *  @brief load and save accounts data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_ACCOUNT_SQL_H_
+#define GNC_ACCOUNT_SQL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+void gnc_sql_init_account_handler( void );
+void gnc_sql_save_account( QofInstance* inst, GncSqlBackend* be );
+
+#endif /* GNC_ACCOUNT_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,842 @@
+/********************************************************************
+ * gnc-backend-sql.c: load and save data to SQL                     *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-backend-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <glib/gstdio.h>
+
+#include "qof.h"
+#include "qofquery-p.h"
+#include "qofquerycore-p.h"
+#include "Account.h"
+#include "TransLog.h"
+#include "gnc-engine.h"
+#include "SX-book.h"
+#include "Recurrence.h"
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-gconf-utils.h"
+
+#include "gnc-account-sql.h"
+#include "gnc-book-sql.h"
+#include "gnc-budget-sql.h"
+#include "gnc-commodity-sql.h"
+#include "gnc-lots-sql.h"
+#include "gnc-price-sql.h"
+#include "gnc-pricedb.h"
+#include "gnc-recurrence-sql.h"
+#include "gnc-schedxaction-sql.h"
+#include "gnc-slots-sql.h"
+#include "gnc-transaction-sql.h"
+
+#include "gnc-backend-sql.h"
+
+static const gchar* convert_search_obj( QofIdType objType );
+static void gnc_sql_init_object_handlers( void );
+static void update_save_progress( GncSqlBackend* be );
+
+#define TRANSACTION_NAME "trans"
+
+typedef struct {
+    QofIdType searchObj;
+    gpointer pCompiledQuery;
+} gnc_sql_query_info;
+
+/* callback structure */
+typedef struct {
+    gboolean ok;
+    GncSqlBackend* be;
+    QofInstance* inst;
+    QofQuery* pQuery;
+    gpointer pCompiledQuery;
+    gnc_sql_query_info* pQueryInfo;
+} sql_backend;
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define SQLITE_PROVIDER_NAME "SQLite"
+#define URI_PREFIX "gda://"
+
+/* ================================================================= */
+
+void
+gnc_sql_init( GncSqlBackend* be )
+{
+	gnc_sql_register_standard_col_type_handlers();
+	gnc_sql_init_object_handlers();
+}
+
+/* ================================================================= */
+
+static void
+create_tables_cb( const gchar* type, gpointer data_p, gpointer be_p )
+{
+    GncSqlDataType_t* pData = data_p;
+    GncSqlBackend* be = be_p;
+
+    g_return_if_fail( type != NULL && data_p != NULL && be_p != NULL );
+    g_return_if_fail( pData->version == GNC_SQL_BACKEND_VERSION );
+
+    if( pData->create_tables != NULL ) {
+        (pData->create_tables)( be );
+    }
+}
+
+/* ================================================================= */
+
+static const gchar* fixed_load_order[] =
+{ GNC_ID_BOOK, GNC_ID_COMMODITY, GNC_ID_ACCOUNT, NULL };
+
+static void
+initial_load_cb( const gchar* type, gpointer data_p, gpointer be_p )
+{
+    GncSqlDataType_t* pData = data_p;
+    GncSqlBackend* be = be_p;
+	int i;
+
+    g_return_if_fail( type != NULL && data_p != NULL && be_p != NULL );
+    g_return_if_fail( pData->version == GNC_SQL_BACKEND_VERSION );
+
+	// Don't need to load anything if it has already been loaded with the fixed order
+	for( i = 0; fixed_load_order[i] != NULL; i++ ) {
+    	if( g_ascii_strcasecmp( type, fixed_load_order[i] ) == 0 ) return;
+	}
+
+    if( pData->initial_load != NULL ) {
+        (pData->initial_load)( be );
+    }
+}
+
+void
+gnc_sql_load( GncSqlBackend* be, QofBook *book )
+{
+    GncSqlDataType_t* pData;
+	int i;
+	Account* root;
+	GError* error = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( book != NULL );
+
+    ENTER( "be=%p, book=%p", be, book );
+
+    g_assert( be->primary_book == NULL );
+    be->primary_book = book;
+
+	// Set up table version information
+//	gnc_sql_init_version_info( be );
+
+    // Call all object backends to create any required tables
+//    qof_object_foreach_backend( GNC_SQL_BACKEND, create_tables_cb, be );
+
+    /* Load any initial stuff */
+    be->loading = TRUE;
+    
+    /* Some of this needs to happen in a certain order */
+	for( i = 0; fixed_load_order[i] != NULL; i++ ) {
+    	pData = qof_object_lookup_backend( fixed_load_order[i], GNC_SQL_BACKEND );
+    	if( pData->initial_load != NULL ) {
+        	(pData->initial_load)( be );
+		}
+    }
+
+	root = gnc_book_get_root_account( book );
+	gnc_account_foreach_descendant( root, (AccountCb)xaccAccountBeginEdit, NULL );
+
+    qof_object_foreach_backend( GNC_SQL_BACKEND, initial_load_cb, be );
+
+	gnc_account_foreach_descendant( root, (AccountCb)xaccAccountCommitEdit, NULL );
+
+    be->loading = FALSE;
+
+	// Mark the book as clean
+	qof_book_mark_saved( book );
+
+    LEAVE( "" );
+}
+
+/* ================================================================= */
+
+static gint
+compare_namespaces(gconstpointer a, gconstpointer b)
+{
+    const gchar *sa = (const gchar *) a;
+    const gchar *sb = (const gchar *) b;
+
+    return( safe_strcmp( sa, sb ) );
+}
+
+static gint
+compare_commodity_ids(gconstpointer a, gconstpointer b)
+{
+    const gnc_commodity *ca = (const gnc_commodity *) a;
+    const gnc_commodity *cb = (const gnc_commodity *) b;
+  
+    return( safe_strcmp( gnc_commodity_get_mnemonic( ca ),
+                     	 gnc_commodity_get_mnemonic( cb ) ) );
+}
+
+static void
+write_commodities( GncSqlBackend* be, QofBook* book )
+{
+    gnc_commodity_table* tbl;
+    GList* namespaces;
+    GList* lp;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( book != NULL );
+
+    tbl = gnc_book_get_commodity_table( book );
+    namespaces = gnc_commodity_table_get_namespaces( tbl );
+    if( namespaces != NULL ) {
+        namespaces = g_list_sort( namespaces, compare_namespaces );
+    }
+    for( lp = namespaces; lp != NULL; lp = lp->next ) {
+        GList* comms;
+        GList* lp2;
+        
+        comms = gnc_commodity_table_get_commodities( tbl, lp->data );
+        comms = g_list_sort( comms, compare_commodity_ids );
+
+        for( lp2 = comms; lp2 != NULL; lp2 = lp2->next ) {
+	    	gnc_sql_save_commodity( be, GNC_COMMODITY(lp2->data) );
+        }
+    }
+}
+
+static void
+write_account_tree( GncSqlBackend* be, Account* root )
+{
+    GList* descendants;
+    GList* node;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( root != NULL );
+
+    descendants = gnc_account_get_descendants( root );
+    for( node = descendants; node != NULL; node = g_list_next(node) ) {
+        gnc_sql_save_account( QOF_INSTANCE(GNC_ACCOUNT(node->data)), be );
+		update_save_progress( be );
+    }
+    g_list_free( descendants );
+}
+
+static void
+write_accounts( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    write_account_tree( be, gnc_book_get_root_account( be->primary_book ) );
+}
+
+static int
+write_tx( Transaction* tx, gpointer data )
+{
+    GncSqlBackend* be = (GncSqlBackend*)data;
+
+	g_return_val_if_fail( tx != NULL, 0 );
+	g_return_val_if_fail( data != NULL, 0 );
+
+    gnc_sql_save_transaction( QOF_INSTANCE(tx), be );
+	update_save_progress( be );
+
+    return 0;
+}
+
+static void
+write_transactions( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+	
+    xaccAccountTreeForEachTransaction( gnc_book_get_root_account( be->primary_book ),
+                                       write_tx,
+                                       (gpointer)be );
+}
+
+static void
+write_template_transactions( GncSqlBackend* be )
+{
+    Account* ra;
+
+	g_return_if_fail( be != NULL );
+
+    ra = gnc_book_get_template_root( be->primary_book );
+    if( gnc_account_n_descendants( ra ) > 0 ) {
+        write_account_tree( be, ra );
+        xaccAccountTreeForEachTransaction( ra, write_tx, (gpointer)be );
+    }
+}
+
+static void
+write_schedXactions( GncSqlBackend* be )
+{
+    GList* schedXactions;
+    SchedXaction* tmpSX;
+
+	g_return_if_fail( be != NULL );
+
+    schedXactions = gnc_book_get_schedxactions( be->primary_book )->sx_list;
+
+    for( ; schedXactions != NULL; schedXactions = schedXactions->next ) {
+        tmpSX = schedXactions->data;
+		gnc_sql_save_schedxaction( QOF_INSTANCE( tmpSX ), be );
+    }
+}
+
+static void
+write_cb( const gchar* type, gpointer data_p, gpointer be_p )
+{
+    GncSqlDataType_t* pData = data_p;
+    GncSqlBackend* be = (GncSqlBackend*)be_p;
+
+    g_return_if_fail( type != NULL && data_p != NULL && be_p != NULL );
+    g_return_if_fail( pData->version == GNC_SQL_BACKEND_VERSION );
+
+    if( pData->write != NULL ) {
+        (pData->write)( be );
+    }
+}
+
+static void
+update_save_progress( GncSqlBackend* be )
+{
+	if( be->be.percentage != NULL ) {
+		gint percent_done;
+
+		be->operations_done++;
+		percent_done = be->operations_done * 100 / be->obj_total;
+		if( percent_done > 100 ) {
+			percent_done = 100;
+		}
+		(be->be.percentage)( NULL, percent_done );
+	}
+}
+
+void
+gnc_sql_sync_all( GncSqlBackend* be, QofBook *book )
+{
+    GError* error = NULL;
+    gint row;
+    gint numTables;
+	gboolean status;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( book != NULL );
+
+    ENTER( "book=%p, primary=%p", book, be->primary_book );
+
+#if 0
+    /* Destroy the current contents of the database */
+	mstore = gda_connection_get_meta_store( be->pConnection );
+	tables = gda_connection_get_meta_store_data( be->pConnection, GDA_CONNECTION_META_TABLES, &error, 0 );
+//    tables = gda_connection_get_schema( be->pConnection,
+                                        //GDA_CONNECTION_SCHEMA_TABLES,
+                                        //NULL,
+                                        //&error );
+    if( error != NULL ) {
+        PERR( "SQL error: %s\n", error->message );
+    }
+    numTables = gda_data_model_get_n_rows( tables );
+    for( row = 0; row < numTables; row++ ) {
+        const GValue* row_value;
+        const gchar* table_name;
+		GdaServerOperation* op;
+
+        row_value = gda_data_model_get_value_at( tables, 0, row );
+        table_name = g_value_get_string( row_value );
+        error = NULL;
+		op = gda_prepare_drop_table( be->pConnection, table_name, &error );
+		if( error != NULL ) {
+			PERR( "Unable to create op: %s\n", error->message );
+		}
+		if( op != NULL ) {
+			error = NULL;
+			gda_perform_drop_table( op, &error );
+            if( error != NULL ) {
+                PERR( "SQL error: %s\n", error->message );
+            }
+        }
+    }
+#endif
+
+	gnc_sql_reset_version_info( be );
+
+    /* Create new tables */
+	be->is_pristine_db = TRUE;
+    qof_object_foreach_backend( GNC_SQL_BACKEND, create_tables_cb, be );
+
+    /* Save all contents */
+	be->primary_book = book;
+	be->obj_total = 0;
+    be->obj_total += 1 + gnc_account_n_descendants( gnc_book_get_root_account( book ) );
+	be->obj_total += gnc_book_count_transactions( book );
+	be->operations_done = 0;
+
+	error = NULL;
+	gnc_sql_connection_begin_transaction( be->conn );
+
+	// FIXME: should write the set of commodities that are used 
+    //write_commodities( be, book );
+	gnc_sql_save_book( QOF_INSTANCE(book), be );
+    write_accounts( be );
+    write_transactions( be );
+    write_template_transactions( be );
+    write_schedXactions( be );
+    qof_object_foreach_backend( GNC_SQL_BACKEND, write_cb, be );
+
+	gnc_sql_connection_commit_transaction( be->conn );
+	be->is_pristine_db = FALSE;
+
+	// Mark the book as clean
+	qof_book_mark_saved( book );
+
+    LEAVE( "book=%p", book );
+}
+
+/* ================================================================= */
+/* Routines to deal with the creation of multiple books. */
+
+void
+gnc_sql_begin_edit( QofBackend *qbe, QofInstance *inst )
+{
+	g_return_if_fail( qbe != NULL );
+	g_return_if_fail( inst != NULL );
+}
+
+void
+gnc_sql_rollback_edit( QofBackend *qbe, QofInstance *inst )
+{
+	g_return_if_fail( qbe != NULL );
+	g_return_if_fail( inst != NULL );
+}
+
+static void
+commit_cb( const gchar* type, gpointer data_p, gpointer be_data_p )
+{
+    GncSqlDataType_t* pData = data_p;
+    sql_backend* be_data = be_data_p;
+
+    g_return_if_fail( type != NULL && pData != NULL && be_data != NULL );
+    g_return_if_fail( pData->version == GNC_SQL_BACKEND_VERSION );
+
+    /* If this has already been handled, or is not the correct handler, return */
+    if( strcmp( pData->type_name, be_data->inst->e_type ) != 0 ) return;
+    if( be_data->ok ) return;
+
+    if( pData->commit != NULL ) {
+        (pData->commit)( be_data->inst, be_data->be );
+        be_data->ok = TRUE;
+    }
+}
+
+/* Commit_edit handler - find the correct backend handler for this object
+ * type and call its commit handler
+ */
+void
+gnc_sql_commit_edit( QofBackend *qbe, QofInstance *inst )
+{
+	GncSqlBackend* be = (GncSqlBackend*)qbe;
+    sql_backend be_data;
+	GError* error;
+	gboolean status;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( inst != NULL );
+
+    /* During initial load where objects are being created, don't commit
+    anything */
+    if( be->loading ) {
+	    return;
+	}
+
+	// The engine has a PriceDB object but it isn't in the database
+	if( strcmp( inst->e_type, "PriceDB" ) == 0 ) {
+    	qof_instance_mark_clean(inst);
+    	qof_book_mark_saved( be->primary_book );
+		return;
+	}
+
+    ENTER( " " );
+
+    DEBUG( "%s dirty = %d, do_free=%d\n",
+             (inst->e_type ? inst->e_type : "(null)"),
+             qof_instance_get_dirty_flag(inst), qof_instance_get_destroying(inst) );
+
+    if( !qof_instance_get_dirty_flag(inst) && !qof_instance_get_destroying(inst) && GNC_IS_TRANS(inst) ) {
+        gnc_sql_transaction_commit_splits( be, GNC_TRANS(inst) );
+    }
+
+    if( !qof_instance_get_dirty_flag(inst) && !qof_instance_get_destroying(inst) ) return;
+
+	error = NULL;
+	gnc_sql_connection_begin_transaction( be->conn );
+
+    be_data.ok = FALSE;
+    be_data.be = be;
+    be_data.inst = inst;
+    qof_object_foreach_backend( GNC_SQL_BACKEND, commit_cb, &be_data );
+
+    if( !be_data.ok ) {
+        PERR( "gnc_sql_commit_edit(): Unknown object type '%s'\n", inst->e_type );
+		gnc_sql_connection_rollback_transaction( be->conn );
+
+		// Don't let unknown items still mark the book as being dirty
+    	qof_instance_mark_clean(inst);
+    	qof_book_mark_saved( be->primary_book );
+        return;
+    }
+	gnc_sql_connection_commit_transaction( be->conn );
+
+    qof_instance_mark_clean(inst);
+    qof_book_mark_saved( be->primary_book );
+
+	LEAVE( "" );
+}
+/* ---------------------------------------------------------------------- */
+
+/* Query processing */
+
+static const gchar*
+convert_search_obj( QofIdType objType )
+{
+    return (gchar*)objType;
+}
+
+static void
+handle_and_term( QofQueryTerm* pTerm, gchar* sql )
+{
+    GSList* pParamPath;
+    QofQueryPredData* pPredData;
+    gboolean isInverted;
+    GSList* name;
+    gchar val[GUID_ENCODING_LENGTH+1];
+
+	g_return_if_fail( pTerm != NULL );
+	g_return_if_fail( sql != NULL );
+
+    pParamPath = qof_query_term_get_param_path( pTerm );
+    pPredData = qof_query_term_get_pred_data( pTerm );
+    isInverted = qof_query_term_is_inverted( pTerm );
+
+    strcat( sql, "(" );
+    if( isInverted ) {
+        strcat( sql, "!" );
+    }
+
+    for( name = pParamPath; name != NULL; name = name->next ) {
+        if( name != pParamPath ) strcat( sql, "." );
+        strcat( sql, name->data );
+    }
+
+    if( pPredData->how == QOF_COMPARE_LT ) {
+        strcat( sql, "<" );
+    } else if( pPredData->how == QOF_COMPARE_LTE ) {
+        strcat( sql, "<=" );
+    } else if( pPredData->how == QOF_COMPARE_EQUAL ) {
+        strcat( sql, "=" );
+    } else if( pPredData->how == QOF_COMPARE_GT ) {
+        strcat( sql, ">" );
+    } else if( pPredData->how == QOF_COMPARE_GTE ) {
+        strcat( sql, ">=" );
+    } else if( pPredData->how == QOF_COMPARE_NEQ ) {
+        strcat( sql, "~=" );
+    } else {
+        strcat( sql, "??" );
+    }
+
+    if( strcmp( pPredData->type_name, "string" ) == 0 ) {
+        query_string_t pData = (query_string_t)pPredData;
+        strcat( sql, "'" );
+        strcat( sql, pData->matchstring );
+        strcat( sql, "'" );
+    } else if( strcmp( pPredData->type_name, "date" ) == 0 ) {
+        query_date_t pData = (query_date_t)pPredData;
+
+        (void)gnc_timespec_to_iso8601_buff( pData->date, val );
+        strcat( sql, "'" );
+        strncat( sql, val, 4+1+2+1+2 );
+        strcat( sql, "'" );
+    } else if( strcmp( pPredData->type_name, "numeric" ) == 0 ) {
+        query_numeric_t pData = (query_numeric_t)pPredData;
+    
+        strcat( sql, "numeric" );
+    } else if( strcmp( pPredData->type_name, "guid" ) == 0 ) {
+        query_guid_t pData = (query_guid_t)pPredData;
+        (void)guid_to_string_buff( pData->guids->data, val );
+        strcat( sql, "'" );
+        strcat( sql, val );
+        strcat( sql, "'" );
+    } else if( strcmp( pPredData->type_name, "gint32" ) == 0 ) {
+        query_int32_t pData = (query_int32_t)pPredData;
+
+        sprintf( val, "%d", pData->val );
+        strcat( sql, val );
+    } else if( strcmp( pPredData->type_name, "gint64" ) == 0 ) {
+        query_int64_t pData = (query_int64_t)pPredData;
+    
+        sprintf( val, "%" G_GINT64_FORMAT, pData->val );
+        strcat( sql, val );
+    } else if( strcmp( pPredData->type_name, "double" ) == 0 ) {
+        query_double_t pData = (query_double_t)pPredData;
+
+        sprintf( val, "%f", pData->val );
+        strcat( sql, val );
+    } else if( strcmp( pPredData->type_name, "boolean" ) == 0 ) {
+        query_boolean_t pData = (query_boolean_t)pPredData;
+
+        sprintf( val, "%d", pData->val );
+        strcat( sql, val );
+    } else {
+        g_assert( FALSE );
+    }
+
+    strcat( sql, ")" );
+}
+
+static void
+compile_query_cb( const gchar* type, gpointer data_p, gpointer be_data_p )
+{
+    GncSqlDataType_t* pData = data_p;
+    sql_backend* be_data = be_data_p;
+
+    g_return_if_fail( type != NULL && pData != NULL && be_data != NULL );
+    g_return_if_fail( pData->version == GNC_SQL_BACKEND_VERSION );
+
+	// Is this the right item?
+    if( strcmp( type, be_data->pQueryInfo->searchObj ) != 0 ) return;
+    if( be_data->ok ) return;
+
+    if( pData->compile_query != NULL ) {
+        be_data->pQueryInfo->pCompiledQuery = (pData->compile_query)(
+                                                            be_data->be,
+                                                            be_data->pQuery );
+        be_data->ok = TRUE;
+    }
+}
+
+static gpointer
+gnc_sql_compile_query( QofBackend* pBEnd, QofQuery* pQuery )
+{
+    GncSqlBackend *be = (GncSqlBackend*)pBEnd;
+    GList* pBookList;
+    QofIdType searchObj;
+    gchar sql[1000];
+    sql_backend be_data;
+    gnc_sql_query_info* pQueryInfo;
+
+	g_return_val_if_fail( pBEnd != NULL, NULL );
+	g_return_val_if_fail( pQuery != NULL, NULL );
+
+	ENTER( " " );
+
+    searchObj = qof_query_get_search_for( pQuery );
+
+    pQueryInfo = g_malloc( sizeof( gnc_sql_query_info ) );
+
+    // Try various objects first
+    be_data.ok = FALSE;
+    be_data.be = be;
+    be_data.pQuery = pQuery;
+    pQueryInfo->searchObj = searchObj;
+    be_data.pQueryInfo = pQueryInfo;
+
+    qof_object_foreach_backend( GNC_SQL_BACKEND, compile_query_cb, &be_data );
+    if( be_data.ok ) {
+		LEAVE( "" );
+        return be_data.pQueryInfo;
+    }
+
+    pBookList = qof_query_get_books( pQuery );
+
+    /* Convert search object type to table name */
+    sprintf( sql, "SELECT * from %s", convert_search_obj( searchObj ) );
+    if( !qof_query_has_terms( pQuery ) ) {
+        strcat( sql, ";" );
+    } else {
+        GList* pOrTerms = qof_query_get_terms( pQuery );
+        GList* orTerm;
+
+        strcat( sql, " WHERE " );
+
+        for( orTerm = pOrTerms; orTerm != NULL; orTerm = orTerm->next ) {
+            GList* pAndTerms = (GList*)orTerm->data;
+            GList* andTerm;
+
+            if( orTerm != pOrTerms ) strcat( sql, " OR " );
+            strcat( sql, "(" );
+            for( andTerm = pAndTerms; andTerm != NULL; andTerm = andTerm->next ) {
+                if( andTerm != pAndTerms ) strcat( sql, " AND " );
+                handle_and_term( (QofQueryTerm*)andTerm->data, sql );
+            }
+            strcat( sql, ")" );
+        }
+    }
+
+    DEBUG( "Compiled: %s\n", sql );
+    pQueryInfo->pCompiledQuery =  g_strdup( sql );
+
+	LEAVE( "" );
+
+    return pQueryInfo;
+}
+
+static void
+free_query_cb( const gchar* type, gpointer data_p, gpointer be_data_p )
+{
+    GncSqlDataType_t* pData = data_p;
+    sql_backend* be_data = be_data_p;
+
+    g_return_if_fail( type != NULL && pData != NULL && be_data != NULL );
+    g_return_if_fail( pData->version == GNC_SQL_BACKEND_VERSION );
+    if( be_data->ok ) return;
+    if( strcmp( type, be_data->pQueryInfo->searchObj ) != 0 ) return;
+
+    if( pData->free_query != NULL ) {
+        (pData->free_query)( be_data->be, be_data->pCompiledQuery );
+        be_data->ok = TRUE;
+    }
+}
+
+static void
+gnc_sql_free_query( QofBackend* pBEnd, gpointer pQuery )
+{
+    GncSqlBackend *be = (GncSqlBackend*)pBEnd;
+    gnc_sql_query_info* pQueryInfo = (gnc_sql_query_info*)pQuery;
+    sql_backend be_data;
+
+	g_return_if_fail( pBEnd != NULL );
+	g_return_if_fail( pQuery != NULL );
+
+	ENTER( " " );
+
+    // Try various objects first
+    be_data.ok = FALSE;
+    be_data.be = be;
+    be_data.pCompiledQuery = pQuery;
+    be_data.pQueryInfo = pQueryInfo;
+
+    qof_object_foreach_backend( GNC_SQL_BACKEND, free_query_cb, &be_data );
+    if( be_data.ok ) {
+		LEAVE( "" );
+        return;
+    }
+
+    DEBUG( "%s\n", (gchar*)pQueryInfo->pCompiledQuery );
+    g_free( pQueryInfo->pCompiledQuery );
+    g_free( pQueryInfo );
+
+	LEAVE( "" );
+}
+
+static void
+run_query_cb( const gchar* type, gpointer data_p, gpointer be_data_p )
+{
+    GncSqlDataType_t* pData = data_p;
+    sql_backend* be_data = be_data_p;
+
+    g_return_if_fail( type != NULL && pData != NULL && be_data != NULL );
+    g_return_if_fail( pData->version == GNC_SQL_BACKEND_VERSION );
+    if( be_data->ok ) return;
+
+	// Is this the right item?
+    if( strcmp( type, be_data->pQueryInfo->searchObj ) != 0 ) return;
+
+    if( pData->run_query != NULL ) {
+        (pData->run_query)( be_data->be, be_data->pCompiledQuery );
+        be_data->ok = TRUE;
+    }
+}
+
+static void
+gnc_sql_run_query( QofBackend* pBEnd, gpointer pQuery )
+{
+    GncSqlBackend *be = (GncSqlBackend*)pBEnd;
+    gnc_sql_query_info* pQueryInfo = (gnc_sql_query_info*)pQuery;
+    sql_backend be_data;
+
+	g_return_if_fail( pBEnd != NULL );
+	g_return_if_fail( pQuery != NULL );
+    g_return_if_fail( !be->in_query );
+
+	ENTER( " " );
+
+    be->loading = TRUE;
+    be->in_query = TRUE;
+
+    qof_event_suspend();
+
+    // Try various objects first
+    be_data.ok = FALSE;
+    be_data.be = be;
+    be_data.pCompiledQuery = pQueryInfo->pCompiledQuery;
+    be_data.pQueryInfo = pQueryInfo;
+
+    qof_object_foreach_backend( GNC_SQL_BACKEND, run_query_cb, &be_data );
+    be->loading = FALSE;
+    be->in_query = FALSE;
+    qof_event_resume();
+//    if( be_data.ok ) {
+//		LEAVE( "" );
+//       	return;
+//    }
+
+	// Mark the book as clean
+	qof_instance_mark_clean( QOF_INSTANCE(be->primary_book) );
+
+//    DEBUG( "%s\n", (gchar*)pQueryInfo->pCompiledQuery );
+
+	LEAVE( "" );
+}
+
+/* ================================================================= */
+static void
+gnc_sql_init_object_handlers( void )
+{
+    gnc_sql_init_book_handler();
+    gnc_sql_init_commodity_handler();
+    gnc_sql_init_account_handler();
+    gnc_sql_init_budget_handler();
+    gnc_sql_init_price_handler();
+    gnc_sql_init_transaction_handler();
+    gnc_sql_init_slots_handler();
+	gnc_sql_init_recurrence_handler();
+    gnc_sql_init_schedxaction_handler();
+    gnc_sql_init_lot_handler();
+}
+
+/* ================================================================= */
+
+G_MODULE_EXPORT void
+qof_backend_module_init(void)
+{
+}
+
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,44 @@
+/********************************************************************
+ * gnc-backend-sql.h: load and save data to SQL                     *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-backend-sql.h
+ *  @brief load and save data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_BACKEND_SQL_H_
+#define GNC_BACKEND_SQL_H_
+
+#include <gmodule.h>
+
+void gnc_sql_init( GncSqlBackend* be );
+void gnc_sql_load( GncSqlBackend* be, QofBook *book );
+void gnc_sql_sync_all( GncSqlBackend* be, QofBook *book );
+void gnc_sql_begin_edit( QofBackend* qbe, QofInstance *inst );
+void gnc_sql_rollback_edit( QofBackend* qbe, QofInstance *inst );
+void gnc_sql_commit_edit( QofBackend* qbe, QofInstance *inst );
+
+G_MODULE_EXPORT void
+qof_backend_module_init(void);
+
+#endif /* GNC_BACKEND_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-util-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-util-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-util-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,1824 @@
+/********************************************************************
+ * gnc-backend-util-sql.c: load and save data to SQL                *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-backend-util-sql.c
+ *  @brief load and save data to SQL - utility functions
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file contains utility routines to support saving/restoring
+ * data to/from an SQL db
+ */
+
+#include <stdlib.h>
+#include "config.h"
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#include "qof.h"
+#include "qofquery-p.h"
+#include "qofquerycore-p.h"
+#include "TransLog.h"
+#include "gnc-engine.h"
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-gconf-utils.h"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+static void register_table_version( const GncSqlBackend* be, const gchar* table_name, gint version );
+static gint get_table_version( const GncSqlBackend* be, const gchar* table_name );
+
+/* ================================================================= */
+static gint64
+get_integer_value( const GValue* value )
+{
+	g_return_val_if_fail( value != NULL, 0 );
+
+	if( G_VALUE_HOLDS_INT(value) ) {
+		return g_value_get_int( value );
+	} else if( G_VALUE_HOLDS_UINT(value) ) {
+		return g_value_get_uint( value );
+	} else if( G_VALUE_HOLDS_LONG(value) ) {
+		return g_value_get_long( value );
+	} else if( G_VALUE_HOLDS_ULONG(value) ) {
+		return g_value_get_ulong( value );
+	} else if( G_VALUE_HOLDS_INT64(value) ) {
+		return g_value_get_int64( value );
+	} else if( G_VALUE_HOLDS_UINT64(value) ) {
+		return g_value_get_uint64( value );
+	} else {
+		PWARN( "Unknown type: %s", G_VALUE_TYPE_NAME( value ) );
+	}
+
+	return 0;
+}
+
+static GValue*
+create_gvalue_from_string( gchar* s )
+{
+	GValue* s_gval;
+
+	s_gval = g_new0( GValue, 1 );
+	g_value_init( s_gval, G_TYPE_STRING );
+	g_value_take_string( s_gval, s );
+
+	return s_gval;
+}
+
+/* ----------------------------------------------------------------- */
+static gpointer
+get_autoinc_id( gpointer pObject, const QofParam* param )
+{
+    // Just need a 0 to force a new recurrence id
+    return (gpointer)0;
+}
+
+static void
+set_autoinc_id( gpointer pObject, gpointer pValue )
+{
+    // Nowhere to put the ID
+}
+
+QofAccessFunc
+gnc_sql_get_getter( QofIdTypeConst obj_name, const col_cvt_t* table_row )
+{
+    QofAccessFunc getter;
+
+	g_return_val_if_fail( obj_name != NULL, NULL );
+	g_return_val_if_fail( table_row != NULL, NULL );
+
+	if( (table_row->flags & COL_AUTOINC) != 0 ) {
+		getter = get_autoinc_id;
+    } else if( table_row->param_name != NULL ) {
+        getter = qof_class_get_parameter_getter( obj_name,
+                                                table_row->param_name );
+    } else {
+        getter = table_row->getter;
+    }
+
+    return getter;
+}
+
+/* ----------------------------------------------------------------- */
+void
+gnc_sql_add_colname_to_list( const col_cvt_t* table_row, GList** pList )
+{
+	(*pList) = g_list_append( (*pList), g_strdup( table_row->col_name ) );
+}
+
+/* ----------------------------------------------------------------- */
+void
+gnc_sql_add_subtable_colnames_to_list( const col_cvt_t* table_row, const col_cvt_t* subtable,
+								GList** pList )
+{
+	const col_cvt_t* subtable_row;
+	gchar* buf;
+
+	for( subtable_row = subtable; subtable_row->col_name != NULL; subtable_row++ ) {
+		buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable_row->col_name );
+		(*pList) = g_list_append( (*pList), buf );
+	}
+}
+
+GncSqlColumnInfo*
+gnc_sql_create_column_info( const col_cvt_t* table_row, const gchar* type,
+							gint size )
+{
+	GncSqlColumnInfo* info;
+
+	info = g_new0( GncSqlColumnInfo, 1 );
+	info->name = table_row->col_name;
+	info->type_name = type;
+	info->size = size;
+	info->is_primary_key = (table_row->flags & COL_PKEY) ? TRUE : FALSE;
+	info->null_allowed = (table_row->flags & COL_NNUL) ? FALSE : TRUE;
+
+	return info;
+}
+
+/* ----------------------------------------------------------------- */
+static void
+load_string( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    const gchar* s;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        s = NULL;
+    } else {
+        s = g_value_get_string( val );
+    }
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, s, NULL );
+    } else {
+		(*setter)( pObject, (const gpointer)s );
+    }
+}
+
+static void
+add_string_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+    const gchar* dbms_type;
+	GncSqlColumnInfo* info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	info = gnc_sql_create_column_info( table_row,
+					gnc_sql_connection_get_column_type_name( be->conn,
+											G_TYPE_STRING, table_row->size ),
+				    table_row->size );
+
+	*pList = g_list_append( *pList, info );
+}
+
+static void
+add_gvalue_string_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+                const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    QofAccessFunc getter;
+    gchar* s;
+	GValue* value = g_new0( GValue, 1 );
+	gchar* buf;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+    memset( value, 0, sizeof( GValue ) );
+	if( table_row->gobj_param_name != NULL ) {
+		g_object_get( pObject, table_row->gobj_param_name, &s, NULL );
+	} else {
+    	getter = gnc_sql_get_getter( obj_name, table_row );
+    	s = (gchar*)(*getter)( pObject, NULL );
+	}
+    if( s ) {
+		buf = g_strdup_printf( "'%s'", s );
+        g_value_init( value, G_TYPE_STRING );
+        g_value_take_string( value, buf );
+    } else {
+        g_value_init( value, G_TYPE_STRING );
+		g_value_set_string( value, "NULL" );
+	}
+
+	(*pList) = g_slist_append( (*pList), value );
+}
+
+static col_type_handler_t string_handler
+    = { load_string,
+		add_string_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+        add_gvalue_string_to_slist };
+/* ----------------------------------------------------------------- */
+typedef gint (*IntAccessFunc)( const gpointer );
+typedef void (*IntSetterFunc)( const gpointer, gint );
+
+static void
+load_int( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    gint int_value;
+	IntSetterFunc i_setter;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        int_value = 0;
+    } else {
+        int_value = get_integer_value( val );
+    }
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, int_value, NULL );
+    } else {
+		i_setter = (IntSetterFunc)setter;
+    	(*i_setter)( pObject, int_value );
+    }
+}
+
+static void
+add_int_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+    const gchar* dbms_type;
+	GncSqlColumnInfo* info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	info = gnc_sql_create_column_info( table_row,
+					gnc_sql_connection_get_column_type_name( be->conn,
+											G_TYPE_INT, table_row->size ),
+				    0 );
+
+	*pList = g_list_append( *pList, info );
+}
+
+static void
+add_gvalue_int_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+                const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    gint int_value;
+    IntAccessFunc i_getter;
+	GValue* value;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	value = g_new0( GValue, 1 );
+
+    i_getter = (IntAccessFunc)gnc_sql_get_getter( obj_name, table_row );
+    int_value = (*i_getter)( pObject );
+    g_value_init( value, G_TYPE_INT );
+    g_value_set_int( value, int_value );
+
+	(*pList) = g_slist_append( (*pList), value );
+}
+
+static col_type_handler_t int_handler
+	= { load_int,
+		add_int_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+		add_gvalue_int_to_slist };
+/* ----------------------------------------------------------------- */
+typedef gboolean (*BooleanAccessFunc)( const gpointer );
+typedef void (*BooleanSetterFunc)( const gpointer, gboolean );
+
+static void
+load_boolean( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    gint int_value;
+	BooleanSetterFunc b_setter;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        int_value = 0;
+    } else {
+        int_value = g_value_get_int( val );
+    }
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, int_value, NULL );
+    } else {
+		b_setter = (BooleanSetterFunc)setter;
+    	(*b_setter)( pObject, int_value ? TRUE : FALSE );
+    }
+}
+
+static void
+add_boolean_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+    const gchar* dbms_type;
+	GncSqlColumnInfo* info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	info = gnc_sql_create_column_info( table_row,
+					gnc_sql_connection_get_column_type_name( be->conn,
+											G_TYPE_INT, table_row->size ),
+				    0 );
+
+	*pList = g_list_append( *pList, info );
+}
+
+static void
+add_gvalue_boolean_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+                const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    gint int_value;
+    BooleanAccessFunc b_getter;
+	GValue* value;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+    value = g_new0( GValue, 1 );
+
+    b_getter = (BooleanAccessFunc)gnc_sql_get_getter( obj_name, table_row );
+    int_value = ((*b_getter)( pObject )) ? 1 : 0;
+    g_value_init( value, G_TYPE_INT );
+    g_value_set_int( value, int_value );
+
+	(*pList) = g_slist_append( (*pList), value );
+}
+
+static col_type_handler_t boolean_handler
+	= { load_boolean,
+		add_boolean_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+		add_gvalue_boolean_to_slist };
+/* ----------------------------------------------------------------- */
+typedef gint64 (*Int64AccessFunc)( const gpointer );
+typedef void (*Int64SetterFunc)( const gpointer, gint64 );
+
+static void
+load_int64( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    gint64 i64_value = 0;
+	Int64SetterFunc i64_setter = (Int64SetterFunc)setter;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( setter != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val != NULL ) {
+        i64_value = get_integer_value( val );
+    }
+    (*i64_setter)( pObject, i64_value );
+}
+
+static void
+add_int64_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+    const gchar* dbms_type;
+	GncSqlColumnInfo* info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	info = gnc_sql_create_column_info( table_row,
+					gnc_sql_connection_get_column_type_name( be->conn,
+											G_TYPE_INT64, table_row->size ),
+				    0 );
+
+	*pList = g_list_append( *pList, info );
+}
+
+static void
+add_gvalue_int64_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+				const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    gint64 i64_value;
+    Int64AccessFunc getter;
+	GValue* value;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+    value = g_new0( GValue, 1 );
+    getter = (Int64AccessFunc)gnc_sql_get_getter( obj_name, table_row );
+    i64_value = (*getter)( pObject );
+    g_value_init( value, G_TYPE_INT64 );
+    g_value_set_int64( value, i64_value );
+
+	(*pList) = g_slist_append( (*pList), value );
+}
+
+static col_type_handler_t int64_handler
+	= { load_int64,
+		add_int64_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+		add_gvalue_int64_to_slist };
+/* ----------------------------------------------------------------- */
+
+static void
+load_double( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    gdouble d_value;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        (*setter)( pObject, (gpointer)NULL );
+    } else {
+		if( G_VALUE_HOLDS(val, G_TYPE_INT) ) {
+			d_value = g_value_get_int( val );
+		} else {
+			d_value = g_value_get_double( val );
+		}
+        (*setter)( pObject, (gpointer)&d_value );
+    }
+}
+
+static void
+add_double_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+    const gchar* dbms_type;
+	GncSqlColumnInfo* info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	info = gnc_sql_create_column_info( table_row,
+					gnc_sql_connection_get_column_type_name( be->conn,
+											G_TYPE_DOUBLE, table_row->size ),
+				    0 );
+
+	*pList = g_list_append( *pList, info );
+}
+
+static void
+add_gvalue_double_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+						const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    QofAccessFunc getter;
+    gdouble* pDouble;
+    gdouble d_value;
+	GValue* value;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+	value = g_new0( GValue, 1 );
+    getter = gnc_sql_get_getter( obj_name, table_row );
+    pDouble = (*getter)( pObject, NULL );
+    if( pDouble != NULL ) {
+        d_value = *pDouble;
+        g_value_init( value, G_TYPE_DOUBLE );
+        g_value_set_double( value, d_value );
+    } else {
+        g_value_init( value, G_TYPE_STRING );
+		g_value_set_string( value, "NULL" );
+	}
+
+	(*pList) = g_slist_append( (*pList), value );
+}
+
+static col_type_handler_t double_handler
+	= { load_double,
+		add_double_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+		add_gvalue_double_to_slist };
+/* ----------------------------------------------------------------- */
+
+static void
+load_guid( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GUID guid;
+    const GUID* pGuid;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        pGuid = NULL;
+    } else {
+        string_to_guid( g_value_get_string( val ), &guid );
+        pGuid = &guid;
+    }
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, pGuid, NULL );
+    } else {
+		(*setter)( pObject, (const gpointer)pGuid );
+    }
+}
+
+static void
+add_guid_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+    const gchar* dbms_type;
+	GncSqlColumnInfo* info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	info = gnc_sql_create_column_info( table_row, "CHAR", GUID_ENCODING_LENGTH );
+
+	*pList = g_list_append( *pList, info );
+}
+
+static void
+add_gvalue_guid_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+					const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    QofAccessFunc getter;
+    const GUID* guid;
+    gchar guid_buf[GUID_ENCODING_LENGTH+1];
+	GValue* value;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    value = g_new0( GValue, 1 );
+	if( table_row->gobj_param_name != NULL ) {
+		g_object_get( pObject, table_row->gobj_param_name, &guid, NULL );
+	} else {
+    	getter = gnc_sql_get_getter( obj_name, table_row );
+    	guid = (*getter)( pObject, NULL );
+	}
+    if( guid != NULL ) {
+		gchar* buf;
+        (void)guid_to_string_buff( guid, guid_buf );
+		buf = g_strdup_printf( "'%s'", guid_buf );
+        g_value_init( value, G_TYPE_STRING );
+        g_value_take_string( value, buf );
+    } else {
+        g_value_init( value, G_TYPE_STRING );
+		g_value_set_string( value, "NULL" );
+	}
+
+	(*pList) = g_slist_append( (*pList), value );
+}
+
+static col_type_handler_t guid_handler
+	= { load_guid,
+		add_guid_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+        add_gvalue_guid_to_slist };
+/* ----------------------------------------------------------------- */
+
+void
+gnc_sql_add_gvalue_objectref_guid_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+						const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    QofAccessFunc getter;
+    const GUID* guid = NULL;
+    gchar guid_buf[GUID_ENCODING_LENGTH+1];
+	QofInstance* inst;
+	GValue* value;
+	gchar* buf;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+	value = g_new0( GValue, 1 );
+	if( table_row->gobj_param_name != NULL ) {
+		g_object_get( pObject, table_row->gobj_param_name, &inst, NULL );
+	} else {
+    	getter = gnc_sql_get_getter( obj_name, table_row );
+    	inst = (*getter)( pObject, NULL );
+	}
+	if( inst != NULL ) {
+		guid = qof_instance_get_guid( inst );
+	}
+    if( guid != NULL ) {
+        (void)guid_to_string_buff( guid, guid_buf );
+		buf = g_strdup_printf( "'%s'", guid_buf );
+        g_value_init( value, G_TYPE_STRING );
+        g_value_take_string( value, buf );
+    } else {
+        g_value_init( value, G_TYPE_STRING );
+		g_value_set_string( value, "NULL" );
+	}
+
+	(*pList) = g_slist_append( (*pList), value );
+}
+
+void
+gnc_sql_add_objectref_guid_col_info_to_list( const GncSqlBackend* be,
+								const col_cvt_t* table_row,
+								GList** pList )
+{
+	add_guid_col_info_to_list( be, table_row, pList );
+}
+
+/* ----------------------------------------------------------------- */
+typedef Timespec (*TimespecAccessFunc)( const gpointer );
+typedef void (*TimespecSetterFunc)( const gpointer, Timespec );
+
+#define TIMESPEC_STR_FORMAT "'%04d%02d%02d%02d%02d%02d'"
+#define TIMESPEC_COL_SIZE (4+2+2+2+2+2)
+
+static void
+load_timespec( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GDate* date;
+    Timespec ts = {0, 0};
+	TimespecSetterFunc ts_setter;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+	ts_setter = (TimespecSetterFunc)setter;
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        (*ts_setter)( pObject, ts );
+    } else {
+		if( G_VALUE_HOLDS_STRING( val ) ) {
+			const gchar* s = g_value_get_string( val );
+			gchar* buf;
+			buf = g_strdup_printf( "%c%c%c%c-%c%c-%c%c %c%c:%c%c:%c%c",
+									s[0], s[1], s[2], s[3],
+									s[4], s[5],
+									s[6], s[7],
+									s[9], s[10],
+									s[11], s[12],
+									s[13], s[14] );
+		    ts = gnc_iso8601_to_timespec_gmt( buf );
+			(*ts_setter)( pObject, ts );
+			g_free( buf );
+
+		} else {
+			PWARN( "Unknown timespec type: %s", G_VALUE_TYPE_NAME( val ) );
+        }
+    }
+}
+
+static void
+add_timespec_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+    const gchar* dbms_type;
+	GncSqlColumnInfo* info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	info = gnc_sql_create_column_info( table_row,
+					"CHAR", TIMESPEC_COL_SIZE );
+
+	*pList = g_list_append( *pList, info );
+}
+
+static void
+add_gvalue_timespec_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+                const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    TimespecAccessFunc ts_getter;
+    Timespec ts;
+	gchar* datebuf;
+	time_t time;
+	struct tm tm;
+	gint year;
+	GValue* value;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+    value = g_new0( GValue, 1 );
+    ts_getter = (TimespecAccessFunc)gnc_sql_get_getter( obj_name, table_row );
+    ts = (*ts_getter)( pObject );
+
+	time = timespecToTime_t( ts );
+	(void)gmtime_r( &time, &tm );	
+
+	if( tm.tm_year < 60 ) year = tm.tm_year + 2000;
+	else year = tm.tm_year + 1900;
+
+	datebuf = g_strdup_printf( TIMESPEC_STR_FORMAT,
+					year, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec );
+    g_value_init( value, G_TYPE_STRING );
+	g_value_take_string( value, datebuf );
+
+	(*pList) = g_slist_append( (*pList), value );
+}
+
+static col_type_handler_t timespec_handler
+	= { load_timespec,
+		add_timespec_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+		add_gvalue_timespec_to_slist };
+/* ----------------------------------------------------------------- */
+#define DATE_COL_SIZE 8
+
+static void
+load_date( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GDate* date;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+		date = g_date_new_dmy( 1, 1, 1970 );
+        (*setter)( pObject, date );
+		g_date_free( date );
+    } else {
+		if( G_VALUE_HOLDS_STRING( val ) ) {
+			// Format of date is YYYYMMDD
+			const gchar* s = g_value_get_string( val );
+			gchar buf[5];
+			guint year, month, day;
+
+			strncpy( buf, &s[0], 4 );
+			buf[4] = '\0';
+			year = atoi( buf );
+			strncpy( buf, &s[4], 2 );
+			buf[2] = '\0';
+			month = atoi( buf );
+			day = atoi( &s[6] );
+
+			date = g_date_new_dmy( day, month, year );
+			(*setter)( pObject, date );
+			g_date_free( date );
+
+		} else {
+			PWARN( "Unknown timespec type: %s", G_VALUE_TYPE_NAME( val ) );
+        }
+    }
+}
+
+static void
+add_date_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+    const gchar* dbms_type;
+	GncSqlColumnInfo* info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	info = gnc_sql_create_column_info( table_row,
+					"CHAR", DATE_COL_SIZE );
+
+	*pList = g_list_append( *pList, info );
+}
+
+static void
+add_gvalue_date_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+				const gpointer pObject,
+                const col_cvt_t* table_row, GSList** pList )
+{
+    GDate* date;
+    QofAccessFunc getter;
+	gchar* buf;
+	GValue* value;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    value = g_new0( GValue, 1 );
+    getter = gnc_sql_get_getter( obj_name, table_row );
+    date = (GDate*)(*getter)( pObject, NULL );
+	buf = g_strdup_printf( "'%04d%02d%02d'",
+					g_date_get_year( date ), g_date_get_month( date ), g_date_get_day( date ) );
+    g_value_init( value, G_TYPE_STRING );
+    g_value_take_string( value, buf );
+
+	(*pList) = g_slist_append( (*pList), value );
+}
+
+static col_type_handler_t date_handler
+	= { load_date,
+		add_date_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+		add_gvalue_date_to_slist };
+/* ----------------------------------------------------------------- */
+typedef gnc_numeric (*NumericGetterFunc)( const gpointer );
+typedef void (*NumericSetterFunc)( gpointer, gnc_numeric );
+
+static const col_cvt_t numeric_col_table[] =
+{
+    { "num",    CT_INT64, 0, COL_NNUL, "guid" },
+    { "denom",  CT_INT64, 0, COL_NNUL, "guid" },
+	{ NULL }
+};
+
+static void
+load_numeric( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    gchar* buf;
+    gint64 num, denom;
+    gnc_numeric n;
+    gboolean isNull = FALSE;
+	NumericSetterFunc n_setter = (NumericSetterFunc)setter;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    buf = g_strdup_printf( "%s_num", table_row->col_name );
+    val = gnc_sql_row_get_value_at_col_name( row, buf );
+    g_free( buf );
+    if( val == NULL ) {
+        isNull = TRUE;
+        num = 0;
+    } else {
+        num = get_integer_value( val );
+    }
+    buf = g_strdup_printf( "%s_denom", table_row->col_name );
+    val = gnc_sql_row_get_value_at_col_name( row, buf );
+    g_free( buf );
+    if( val == NULL ) {
+        isNull = TRUE;
+        denom = 1;
+    } else {
+        denom = get_integer_value( val );
+    }
+    n = gnc_numeric_create( num, denom );
+    if( !isNull ) {
+        (*n_setter)( pObject, n );
+    }
+}
+
+static void
+add_numeric_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+    const gchar* dbms_type;
+	GncSqlColumnInfo* info;
+    gchar* buf;
+	const col_cvt_t* subtable_row;
+	const gchar* type;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	for( subtable_row = numeric_col_table; subtable_row->col_name != NULL; subtable_row++ ) {
+    	buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable_row->col_name );
+		info = g_new0( GncSqlColumnInfo, 1 );
+		info->name = buf;
+		info->type_name = gnc_sql_connection_get_column_type_name( be->conn,
+											G_TYPE_INT64, table_row->size );
+		info->is_primary_key = (table_row->flags & COL_PKEY) ? TRUE : FALSE;
+		info->null_allowed = (table_row->flags & COL_NNUL) ? FALSE : TRUE;
+		*pList = g_list_append( *pList, info );
+	}
+}
+
+static void
+add_numeric_colname_to_list( const col_cvt_t* table_row, GList** pList )
+{
+	gnc_sql_add_subtable_colnames_to_list( table_row, numeric_col_table, pList );
+}
+
+static void
+add_gvalue_numeric_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+					const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    NumericGetterFunc getter;
+    gnc_numeric n;
+    GValue* num_value;
+    GValue* denom_value;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    getter = (NumericGetterFunc)gnc_sql_get_getter( obj_name, table_row );
+    n = (*getter)( pObject );
+
+    num_value = g_new0( GValue, 1 );
+    g_value_init( num_value, G_TYPE_INT64 );
+    g_value_set_int64( num_value, gnc_numeric_num( n ) );
+    denom_value = g_new0( GValue, 1 );
+    g_value_init( denom_value, G_TYPE_INT64 );
+    g_value_set_int64( denom_value, gnc_numeric_denom( n ) );
+
+	(*pList) = g_slist_append( (*pList), num_value );
+	(*pList) = g_slist_append( (*pList), denom_value );
+}
+
+static col_type_handler_t numeric_handler
+	= { load_numeric,
+		add_numeric_col_info_to_list,
+		add_numeric_colname_to_list,
+		add_gvalue_numeric_to_slist };
+/* ================================================================= */
+
+static GHashTable* g_columnTypeHash = NULL;
+
+void
+gnc_sql_register_col_type_handler( const gchar* colType, const col_type_handler_t* handler )
+{
+	g_return_if_fail( colType != NULL );
+	g_return_if_fail( handler != NULL );
+
+	if( g_columnTypeHash == NULL ) {
+		g_columnTypeHash = g_hash_table_new( g_str_hash, g_str_equal );
+	}
+
+	g_hash_table_insert( g_columnTypeHash, (gpointer)colType, (gpointer)handler );
+	DEBUG( "Col type %s registered\n", colType );
+}
+
+static col_type_handler_t*
+get_handler( const col_cvt_t* table_row )
+{
+    col_type_handler_t* pHandler;
+
+	g_return_val_if_fail( table_row != NULL, NULL );
+	g_return_val_if_fail( table_row->col_type != NULL, NULL );
+
+	pHandler = g_hash_table_lookup( g_columnTypeHash, table_row->col_type );
+	if( pHandler == NULL ) {
+        g_assert( FALSE );
+    }
+
+    return pHandler;
+}
+
+void
+gnc_sql_register_standard_col_type_handlers( void )
+{
+	gnc_sql_register_col_type_handler( CT_STRING, &string_handler );
+    gnc_sql_register_col_type_handler( CT_BOOLEAN, &boolean_handler );
+    gnc_sql_register_col_type_handler( CT_INT, &int_handler );
+    gnc_sql_register_col_type_handler( CT_INT64, &int64_handler );
+    gnc_sql_register_col_type_handler( CT_DOUBLE, &double_handler );
+    gnc_sql_register_col_type_handler( CT_GUID, &guid_handler );
+    gnc_sql_register_col_type_handler( CT_TIMESPEC, &timespec_handler );
+    gnc_sql_register_col_type_handler( CT_GDATE, &date_handler );
+    gnc_sql_register_col_type_handler( CT_NUMERIC, &numeric_handler );
+}
+
+void 
+_retrieve_guid_( gpointer pObject, gpointer pValue )
+{
+    GUID* pGuid = (GUID*)pObject;
+    GUID* guid = (GUID*)pValue;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( pValue != NULL );
+
+	memcpy( pGuid, guid, sizeof( GUID ) );
+}
+
+
+// Table to retrieve just the guid
+static col_cvt_t guid_table[] =
+{
+    { "guid", CT_GUID, 0, 0, NULL, NULL, NULL, _retrieve_guid_ },
+    { NULL }
+};
+
+const GUID*
+gnc_sql_load_guid( const GncSqlBackend* be, GncSqlRow* row )
+{
+	static GUID guid;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( row != NULL, NULL );
+
+    gnc_sql_load_object( be, row, NULL, &guid, guid_table );
+
+    return &guid;
+}
+
+// Table to retrieve just the guid
+static col_cvt_t tx_guid_table[] =
+{
+    { "tx_guid", CT_GUID, 0, 0, NULL, NULL, NULL, _retrieve_guid_ },
+    { NULL }
+};
+
+const GUID*
+gnc_sql_load_tx_guid( const GncSqlBackend* be, GncSqlRow* row )
+{
+    static GUID guid;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( row != NULL, NULL );
+
+    gnc_sql_load_object( be, row, NULL, &guid, tx_guid_table );
+
+    return &guid;
+}
+
+void
+gnc_sql_load_object( const GncSqlBackend* be, GncSqlRow* row,
+                    QofIdTypeConst obj_name, gpointer pObject,
+                    const col_cvt_t* table_row )
+{
+    int col;
+    QofSetterFunc setter;
+    col_type_handler_t* pHandler;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    for( col = 0; table_row[col].col_name != NULL; col++ ) {
+		if( (table_row[col].flags & COL_AUTOINC) != 0 ) {
+			setter = set_autoinc_id;
+        } else if( table_row[col].param_name != NULL ) {
+            setter = qof_class_get_parameter_setter( obj_name,
+                                                    table_row[col].param_name );
+        } else {
+            setter = table_row[col].setter;
+        }
+        pHandler = get_handler( &table_row[col] );
+        pHandler->load_fn( be, row, setter, pObject, &table_row[col] );
+    }
+}
+
+/* ================================================================= */
+GncSqlStatement*
+gnc_sql_create_select_statement( const GncSqlBackend* be, const gchar* table_name,
+							const col_cvt_t* table_row )
+{
+	gchar* sql;
+
+	sql = g_strdup_printf( "SELECT * FROM %s", table_name );
+	return gnc_sql_create_statement_from_sql( be, sql );
+}
+
+static GncSqlStatement*
+create_single_col_select_statement( const GncSqlBackend* be,
+							const gchar* table_name,
+							const col_cvt_t* table_row )
+{
+	gchar* sql;
+
+	sql = g_strdup_printf( "SELECT %s FROM %s", table_row->col_name, table_name );
+	return gnc_sql_create_statement_from_sql( be, sql );
+}
+
+/* ================================================================= */
+gint
+gnc_sql_execute_statement( GncSqlBackend* be, GncSqlStatement* stmt )
+{
+    GError* error = NULL;
+    gint numRows;
+
+	g_return_val_if_fail( be != NULL, -1 );
+	g_return_val_if_fail( stmt != NULL, -1 );
+
+	numRows = gnc_sql_connection_execute_nonselect_statement( be->conn, stmt );
+    if( error != NULL ) {
+        PERR( "SQL error: %s\n%s\n", gnc_sql_statement_to_sql( stmt ), error->message );
+		qof_backend_set_error( &be->be, ERR_BACKEND_SERVER_ERR );
+    }
+
+    return numRows;
+}
+
+GncSqlResult*
+gnc_sql_execute_select_statement( GncSqlBackend* be, GncSqlStatement* stmt )
+{
+    GError* error = NULL;
+    GncSqlResult* result;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( stmt != NULL, NULL );
+
+    result = gnc_sql_connection_execute_select_statement( be->conn, stmt );
+    if( error != NULL ) {
+        PERR( "SQL error: %s\n%s\n", gnc_sql_statement_to_sql( stmt ), error->message );
+		qof_backend_set_error( &be->be, ERR_BACKEND_SERVER_ERR );
+    }
+
+    return result;
+}
+
+GncSqlResult*
+gnc_sql_execute_sql_statement( GncSqlBackend* be, GncSqlStatement* sqlStmt )
+{
+	return gnc_sql_execute_select_statement( be, sqlStmt );
+}
+
+GncSqlStatement*
+gnc_sql_create_statement_from_sql( const GncSqlBackend* be, const gchar* sql )
+{
+    GError* error = NULL;
+	GncSqlStatement* stmt;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( sql != NULL, NULL );
+
+	stmt = gnc_sql_connection_create_statement_from_sql( be->conn, sql );
+    if( stmt == NULL ) {
+        PERR( "SQL error: %s\n%s\n", sql, error->message );
+    }
+
+	return stmt;
+}
+
+GncSqlResult*
+gnc_sql_execute_select_sql( const GncSqlBackend* be, const gchar* sql )
+{
+	GncSqlStatement* stmt;
+    GError* error = NULL;
+	GncSqlResult* result = NULL;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( sql != NULL, NULL );
+
+	stmt = gnc_sql_create_statement_from_sql( be, sql );
+    if( stmt == NULL ) {
+		return NULL;
+    }
+	result = gnc_sql_connection_execute_select_statement( be->conn, stmt );
+    if( error != NULL ) {
+        PERR( "SQL error: %s\n%s\n", sql, error->message );
+    }
+
+	return result;
+}
+
+gint
+gnc_sql_execute_nonselect_sql( const GncSqlBackend* be, const gchar* sql )
+{
+	GncSqlStatement* stmt;
+    GError* error = NULL;
+	gint result;
+
+	g_return_val_if_fail( be != NULL, 0 );
+	g_return_val_if_fail( sql != NULL, 0 );
+
+	stmt = gnc_sql_create_statement_from_sql( be, sql );
+    if( stmt == NULL ) {
+		return 0;
+    }
+	result = gnc_sql_connection_execute_nonselect_statement( be->conn, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( error != NULL ) {
+        PERR( "SQL error: %s\n%s\n", sql, error->message );
+    }
+	return result;
+}
+
+int
+gnc_sql_execute_select_get_count( const GncSqlBackend* be, const gchar* sql )
+{
+    int count = 0;
+    GncSqlResult* result;
+
+	g_return_val_if_fail( be != NULL, 0 );
+	g_return_val_if_fail( sql != NULL, 0 );
+
+    result = gnc_sql_execute_select_sql( be, sql );
+    if( result != NULL ) {
+        count = gnc_sql_result_get_num_rows( result );
+		gnc_sql_result_dispose( result );
+    }
+
+    return count;
+}
+
+int
+gnc_sql_execute_statement_get_count( GncSqlBackend* be, GncSqlStatement* stmt )
+{
+    GncSqlResult* result;
+	int count = 0;
+
+	g_return_val_if_fail( be != NULL, 0 );
+	g_return_val_if_fail( stmt != NULL, 0 );
+
+    result = gnc_sql_execute_select_statement( be, stmt );
+    if( result != NULL ) {
+        count = gnc_sql_result_get_num_rows( result );
+		gnc_sql_result_dispose( result );
+    }
+
+    return count;
+}
+
+guint
+gnc_sql_append_guid_list_to_sql( GString* sql, GList* list, guint maxCount )
+{
+	gchar guid_buf[GUID_ENCODING_LENGTH+1];
+	gboolean first_guid = TRUE;
+	guint count;
+
+	g_return_val_if_fail( sql != NULL, 0 );
+
+	if( list == NULL ) return 0;
+
+	for( count = 0; list != NULL && count < maxCount; list = list->next, count++ ) {
+		QofInstance* inst = QOF_INSTANCE(list->data);
+    	guid_to_string_buff( qof_instance_get_guid( inst ), guid_buf );
+
+		if( !first_guid ) {
+			g_string_append( sql, "," );
+		}
+		g_string_append( sql, "'" );
+		g_string_append( sql, guid_buf );
+		g_string_append( sql, "'" );
+		first_guid = FALSE;
+    }
+
+	return count;
+}
+/* ================================================================= */
+
+gboolean
+gnc_sql_object_is_it_in_db( GncSqlBackend* be, const gchar* table_name,
+                    QofIdTypeConst obj_name, gpointer pObject,
+                    const col_cvt_t* table )
+{
+    GncSqlStatement* sqlStmt;
+    int count;
+    col_type_handler_t* pHandler;
+	GSList* list = NULL;
+
+	g_return_val_if_fail( be != NULL, FALSE );
+	g_return_val_if_fail( table_name != NULL, FALSE );
+	g_return_val_if_fail( obj_name != NULL, FALSE );
+	g_return_val_if_fail( pObject != NULL, FALSE );
+	g_return_val_if_fail( table != NULL, FALSE );
+
+    /* SELECT * FROM */
+    sqlStmt = create_single_col_select_statement( be, table_name, table );
+
+    /* WHERE */
+    pHandler = get_handler( table );
+	pHandler->add_gvalue_to_slist_fn( be, obj_name, pObject, table, &list );
+	gnc_sql_statement_add_where_cond( sqlStmt, obj_name, pObject, &table[0], (GValue*)(list->data) );
+
+    count = gnc_sql_execute_statement_get_count( be, sqlStmt );
+    if( count == 0 ) {
+        return FALSE;
+    } else {
+        return TRUE;
+    }
+}
+
+gboolean
+gnc_sql_do_db_operation( GncSqlBackend* be,
+                        E_DB_OPERATION op,
+                        const gchar* table_name,
+                        QofIdTypeConst obj_name, gpointer pObject,
+                        const col_cvt_t* table )
+{
+    GncSqlStatement* sqlStmt;
+
+	g_return_val_if_fail( be != NULL, FALSE );
+	g_return_val_if_fail( table_name != NULL, FALSE );
+	g_return_val_if_fail( obj_name != NULL, FALSE );
+	g_return_val_if_fail( pObject != NULL, FALSE );
+	g_return_val_if_fail( table != NULL, FALSE );
+
+    if( op == OP_DB_ADD_OR_UPDATE ) {
+        if( gnc_sql_object_is_it_in_db( be, table_name, obj_name, pObject, table ) ) {
+            sqlStmt = gnc_sql_build_update_statement( be, table_name, obj_name, pObject, table );
+        } else {
+            sqlStmt = gnc_sql_build_insert_statement( be, table_name, obj_name, pObject, table );
+        }
+    } else if( op == OP_DB_DELETE ) {
+        sqlStmt = gnc_sql_build_delete_statement( be, table_name, obj_name, pObject, table );
+    } else if( op == OP_DB_ADD ) {
+        sqlStmt = gnc_sql_build_insert_statement( be, table_name, obj_name, pObject, table );
+    } else {
+        g_assert( FALSE );
+    }
+    if( sqlStmt != NULL ) {
+        gnc_sql_execute_statement( be, sqlStmt );
+
+        return TRUE;
+    } else {
+        return FALSE;
+    }
+}
+
+#define INITIAL_SQL_BUF_LEN 500
+
+static GSList*
+create_gslist_from_values( GncSqlBackend* be,
+                            QofIdTypeConst obj_name, gpointer pObject,
+                            const col_cvt_t* table )
+{
+	GSList* list = NULL;
+	col_type_handler_t* pHandler;
+	const col_cvt_t* table_row;
+
+    for( table_row = table; table_row->col_name != NULL; table_row++ ) {
+		if(( table_row->flags & COL_AUTOINC ) == 0 ) {
+    		pHandler = get_handler( table_row );
+			pHandler->add_gvalue_to_slist_fn( be, obj_name, pObject, table_row, &list );
+		}
+    }
+
+	return list;
+}
+
+gchar*
+gnc_sql_get_sql_value( const GValue* value )
+{
+	if( value != NULL && G_IS_VALUE( value ) ) {
+		if( g_value_type_transformable( G_VALUE_TYPE(value), G_TYPE_STRING ) ) {
+			GValue *string;
+			gchar *str;
+			
+			string = g_value_init( g_new0( GValue, 1 ), G_TYPE_STRING );
+			g_value_transform( value, string );
+			str = g_value_dup_string( string );
+			g_free( string );
+			return str;
+		} else {
+			return "$$$";
+		}
+	} else {
+		return "";
+	}
+}
+
+GncSqlStatement*
+gnc_sql_build_insert_statement( GncSqlBackend* be,
+                            const gchar* table_name,
+                            QofIdTypeConst obj_name, gpointer pObject,
+                            const col_cvt_t* table )
+{
+	GncSqlStatement* stmt;
+	GString* sql;
+	GSList* values;
+	GSList* node;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( table_name != NULL, NULL );
+	g_return_val_if_fail( obj_name != NULL, NULL );
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( table != NULL, NULL );
+
+	sql = g_string_new( g_strdup_printf( "INSERT INTO %s VALUES(", table_name ) );
+	values = create_gslist_from_values( be, obj_name, pObject, table );
+	for( node = values; node != NULL; node = node->next ) {
+		GValue* value = (GValue*)node->data;
+		if( node != values ) {
+			g_string_append( sql, "," );
+		}
+		g_string_append( sql, gnc_sql_get_sql_value( value ) );
+	}
+	g_string_append( sql, ")" );
+
+	stmt = gnc_sql_connection_create_statement_from_sql( be->conn, sql->str );
+	return stmt;
+}
+
+GncSqlStatement*
+gnc_sql_build_update_statement( GncSqlBackend* be,
+                            const gchar* table_name,
+                            QofIdTypeConst obj_name, gpointer pObject,
+                            const col_cvt_t* table )
+{
+	GncSqlStatement* stmt;
+	GString* sql;
+	GSList* values;
+	GList* colnames = NULL;
+	GSList* value;
+	GList* colname;
+	gboolean firstCol;
+	const col_cvt_t* table_row = table;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( table_name != NULL, NULL );
+	g_return_val_if_fail( obj_name != NULL, NULL );
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( table != NULL, NULL );
+
+    // Get all col names and all values
+	for( ; table_row->col_name != NULL; table_row++ ) {
+    	col_type_handler_t* pHandler;
+
+		// Add col names to the list
+		pHandler = get_handler( table_row );
+		pHandler->add_colname_to_list_fn( table_row, &colnames );
+	}
+	values = create_gslist_from_values( be, obj_name, pObject, table );
+
+	// Create the SQL statement
+	sql = g_string_new( g_strdup_printf( "UPDATE %s SET ", table_name ) );
+
+	firstCol = TRUE;
+	for( colname = colnames->next, value = values->next;
+					colname != NULL && value != NULL;
+					colname = colname->next, value = value->next ) {
+		if( !firstCol ) {
+			g_string_append( sql, "," );
+		}
+		g_string_append( sql, (gchar*)colname->data );
+		g_string_append( sql, "=" );
+		g_string_append( sql, gnc_sql_get_sql_value( (GValue*)(value->data) ) );
+		firstCol = FALSE;
+	}
+	g_list_free( colnames );
+	if( value != NULL || colname != NULL ) {
+		PERR( "Mismatch in number of column names and values" );
+	}
+
+	stmt = gnc_sql_connection_create_statement_from_sql( be->conn, sql->str );
+	gnc_sql_statement_add_where_cond( stmt, obj_name, pObject, &table[0], (GValue*)(values->data) );
+	g_slist_free( values );
+
+	return stmt;
+}
+
+GncSqlStatement*
+gnc_sql_build_delete_statement( GncSqlBackend* be,
+                            const gchar* table_name,
+                            QofIdTypeConst obj_name, gpointer pObject,
+                            const col_cvt_t* table )
+{
+	GncSqlStatement* stmt;
+	GString* sql;
+    col_type_handler_t* pHandler;
+	GSList* list = NULL;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( table_name != NULL, NULL );
+	g_return_val_if_fail( obj_name != NULL, NULL );
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( table != NULL, NULL );
+
+	sql = g_string_new( g_strdup_printf( "DELETE FROM %s ", table_name ) );
+
+	stmt = gnc_sql_connection_create_statement_from_sql( be->conn, sql->str );
+
+    /* WHERE */
+    pHandler = get_handler( table );
+	pHandler->add_gvalue_to_slist_fn( be, obj_name, pObject, table, &list );
+	gnc_sql_statement_add_where_cond( stmt, obj_name, pObject, &table[0], (GValue*)(list->data) );
+
+	return stmt;
+}
+
+/* ================================================================= */
+#if 0
+void
+gnc_gda_add_table_column( GdaServerOperation* op, const gchar* arg, const gchar* dbms_type,
+            gint size, gint flags, guint col_num )
+{
+    gchar* buf;
+	GError* error = NULL;
+	gboolean ok;
+
+	g_return_if_fail( op != NULL );
+	g_return_if_fail( arg != NULL );
+	g_return_if_fail( dbms_type != NULL );
+
+	ok = gda_server_operation_set_value_at( op, arg, &error, "/FIELDS_A/@COLUMN_NAME/%d", col_num );
+	if( !ok ) return;
+	ok = gda_server_operation_set_value_at( op, dbms_type, &error, "/FIELDS_A/@COLUMN_TYPE/%d", col_num );
+	if( !ok ) return;
+    if( size != 0 ) {
+        buf = g_strdup_printf( "%d", size );
+		ok = gda_server_operation_set_value_at( op, buf, &error, "/FIELDS_A/@COLUMN_SIZE/%d", col_num );
+        g_free( buf );
+		if( !ok ) return;
+    }
+	ok = gda_server_operation_set_value_at( op,
+										(flags & COL_PKEY) ? "TRUE" : "FALSE",
+										&error, "/FIELDS_A/@COLUMN_PKEY/%d", col_num );
+	if( error != NULL ) {
+		PWARN( "Error setting PKEY for %s: %s\n", arg, error->message );
+	}
+	if( !ok ) return;
+	ok = gda_server_operation_set_value_at( op,
+										(flags & COL_NNUL) ? "TRUE" : "FALSE",
+										&error, "/FIELDS_A/@COLUMN_NNUL/%d", col_num );
+	if( error != NULL ) {
+		PWARN( "Error setting NNUL for %s: %s\n", arg, error->message );
+	}
+	if( !ok ) return;
+	ok = gda_server_operation_set_value_at( op,
+										(flags & COL_AUTOINC) ? "TRUE" : "FALSE",
+										&error, "/FIELDS_A/@COLUMN_AUTOINC/%d", col_num );
+	if( error != NULL ) {
+		PWARN( "Error setting AUTOINC for %s: %s\n", arg, error->message );
+	}
+	if( !ok ) return;
+	ok = gda_server_operation_set_value_at( op,
+										(flags & COL_UNIQUE) ? "TRUE" : "FALSE",
+										&error, "/FIELDS_A/@COLUMN_UNIQUE/%d", col_num );
+	if( error != NULL ) {
+		PWARN( "Error setting UNIQUE for %s: %s\n", arg, error->message );
+	}
+}
+#endif
+
+static gboolean
+create_table( const GncSqlBackend* be, const gchar* table_name,
+				const col_cvt_t* col_table, GError** pError )
+{
+	GList* col_info_list = NULL;
+    
+	g_return_val_if_fail( be != NULL, FALSE );
+	g_return_val_if_fail( table_name != NULL, FALSE );
+	g_return_val_if_fail( col_table != NULL, FALSE );
+	g_return_val_if_fail( pError != NULL, FALSE );
+    
+    for( ; col_table->col_name != NULL; col_table++ ) {
+        col_type_handler_t* pHandler;
+
+        pHandler = get_handler( col_table );
+        pHandler->add_col_info_to_list_fn( be, col_table, &col_info_list );
+    }
+	gnc_sql_connection_create_table( be->conn, table_name, col_info_list );
+	return TRUE;
+}
+
+gboolean
+gnc_sql_create_table( const GncSqlBackend* be, const gchar* table_name,
+					gint table_version, const col_cvt_t* col_table, GError** error )
+{
+	gboolean ok;
+
+	ok = create_table( be, table_name, col_table, error );
+	if( ok ) {
+		register_table_version( be, table_name, table_version );
+	}
+	return ok;
+}
+
+void
+gnc_sql_create_index( const GncSqlBackend* be, const gchar* index_name,
+					const gchar* table_name,
+                    const col_cvt_t* col_table )
+{
+    g_return_if_fail( be != NULL );
+	g_return_if_fail( index_name != NULL );
+	g_return_if_fail( table_name != NULL );
+	g_return_if_fail( col_table != NULL );
+    
+	gnc_sql_connection_create_index( be->conn, index_name, table_name,
+								col_table );
+}
+
+gint
+gnc_sql_get_table_version( const GncSqlBackend* be, const gchar* table_name )
+{
+	g_return_val_if_fail( be != NULL, 0 );
+	g_return_val_if_fail( table_name != NULL, 0 );
+
+	/* If the db is pristine because it's being saved, the table does not
+	 * exist.  This gets around a GDA-3 bug where deleting all tables and
+	 * updating the meta-data leaves the meta-data still thinking 1 table
+	 * exists.
+	 */
+	if( be->is_pristine_db ) {
+		return 0;
+	}
+
+	return get_table_version( be, table_name );
+	}
+
+/* ================================================================= */
+#define VERSION_TABLE_NAME "versions"
+#define MAX_TABLE_NAME_LEN 50
+#define TABLE_COL_NAME "table_name"
+#define VERSION_COL_NAME "table_version"
+
+static col_cvt_t version_table[] =
+{
+    { TABLE_COL_NAME,   CT_STRING, MAX_TABLE_NAME_LEN },
+	{ VERSION_COL_NAME, CT_INT },
+    { NULL }
+};
+
+/**
+ * Sees if the version table exists, and if it does, loads the info into
+ * the version hash table.  Otherwise, it creates an empty version table.
+ *
+ * @param be Backend struct
+ */
+void
+gnc_sql_init_version_info( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+	be->versions = g_hash_table_new( g_str_hash, g_str_equal );
+
+	if( gnc_sql_connection_does_table_exist( be->conn, VERSION_TABLE_NAME ) ) {
+		GncSqlResult* result;
+		gchar* sql;
+
+		sql = g_strdup_printf( "SELECT * FROM %s", VERSION_TABLE_NAME );
+		result = gnc_sql_execute_select_sql( be, sql );
+		g_free( sql );
+		if( result != NULL ) {
+			const GValue* name;
+			const GValue* version;
+			GncSqlRow* row;
+
+			row = gnc_sql_result_get_first_row( result );
+			while( row != NULL ) {
+    			name = gnc_sql_row_get_value_at_col_name( row, TABLE_COL_NAME );
+				version = gnc_sql_row_get_value_at_col_name( row, VERSION_COL_NAME );
+				g_hash_table_insert( be->versions,
+									(gpointer)g_value_get_string( name ),
+									GINT_TO_POINTER(g_value_get_int( version )) );
+				row = gnc_sql_result_get_next_row( result );
+			}
+			gnc_sql_result_dispose( result );
+		}
+	} else {
+		gboolean ok;
+		GError* error = NULL;
+
+		ok = create_table( be, VERSION_TABLE_NAME, version_table, &error );
+		if( error != NULL ) {
+			PERR( "Error creating versions table: %s\n", error->message );
+		}
+	}
+}
+
+/**
+ * Resets the version table information by removing all version table info.
+ * It also recreates the version table in the db.
+ *
+ * @param be Backend struct
+ */
+void
+gnc_sql_reset_version_info( GncSqlBackend* be )
+{
+	gboolean ok;
+	GError* error = NULL;
+
+	g_return_if_fail( be != NULL );
+
+	ok = create_table( be, VERSION_TABLE_NAME, version_table, &error );
+	if( error != NULL ) {
+		PERR( "Error creating versions table: %s\n", error->message );
+	}
+	if( be->versions == NULL ) {
+		be->versions = g_hash_table_new( g_str_hash, g_str_equal );
+	} else {
+		g_hash_table_remove_all( be->versions );
+	}
+}
+
+/**
+ * Finalizes the version table info by destroying the hash table.
+ *
+ * @param be Backend struct
+ */
+void
+gnc_sql_finalize_version_info( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+	g_hash_table_destroy( be->versions );
+}
+
+/**
+ * Registers the version for a table.  Registering involves updating the
+ * db version table and also the hash table.
+ *
+ * @param be Backend struct
+ * @param table_name Table name
+ * @param version Version number
+ */
+static void
+register_table_version( const GncSqlBackend* be, const gchar* table_name, gint version )
+{
+	gchar* sql;
+	gint cur_version;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_name != NULL );
+	g_return_if_fail( version > 0 );
+
+	cur_version = get_table_version( be, table_name );
+	if( cur_version != version ) {
+		if( cur_version == 0 ) {
+			sql = g_strdup_printf( "INSERT INTO %s VALUES('%s',%d)", VERSION_TABLE_NAME,
+								table_name, version );
+		} else {
+			sql = g_strdup_printf( "UPDATE %s SET %s=%d WHERE %s='%s'", VERSION_TABLE_NAME,
+								VERSION_COL_NAME, version,
+								TABLE_COL_NAME, table_name );
+		}
+		(void)gnc_sql_execute_nonselect_sql( be, sql );
+		g_free( sql );
+	}
+
+	g_hash_table_insert( be->versions, (gpointer)table_name, GINT_TO_POINTER(version) );
+}
+
+/**
+ * Returns the registered version number for a table.
+ *
+ * @param be Backend struct
+ * @param table_name Table name
+ * @return Version number
+ */
+static gint
+get_table_version( const GncSqlBackend* be, const gchar* table_name )
+{
+	g_return_val_if_fail( be != NULL, 0 );
+	g_return_val_if_fail( table_name != NULL, 0 );
+
+	return GPOINTER_TO_INT(g_hash_table_lookup( be->versions, table_name ));
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-util-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-util-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-backend-util-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,301 @@
+/********************************************************************
+ * gnc-backend-util-sql.h: load and save data to SQL                *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-backend-sql.h
+ *  @brief load and save data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_BACKEND_SQL_UTIL_H_
+#define GNC_BACKEND_SQL_UTIL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+#include "qofbackend-p.h"
+
+/**
+ */
+typedef struct col_cvt col_cvt_t;
+typedef struct GncSqlStatement GncSqlStatement;
+typedef struct GncSqlResult GncSqlResult;
+typedef struct GncSqlConnection GncSqlConnection;
+typedef struct GncSqlRow GncSqlRow;
+
+struct GncSqlStatement
+{
+	void (*dispose)( GncSqlStatement* );
+	gchar* (*toSql)( GncSqlStatement* );
+	void (*addWhereCond)( GncSqlStatement*, QofIdTypeConst, gpointer, const col_cvt_t*, GValue* );
+};
+#define gnc_sql_statement_dispose(STMT) \
+		(STMT)->dispose(STMT)
+#define gnc_sql_statement_to_sql(STMT) \
+		(STMT)->toSql(STMT)
+#define gnc_sql_statement_add_where_cond(STMT,TYPENAME,OBJ,COLDESC,VALUE) \
+		(STMT)->addWhereCond(STMT, TYPENAME, OBJ, COLDESC, VALUE)
+
+typedef struct {
+	const gchar* name;
+	const gchar* type_name;
+	gint size;
+	gboolean is_primary_key;
+	gboolean null_allowed;
+} GncSqlColumnInfo;
+
+struct GncSqlConnection
+{
+	void (*dispose)( GncSqlConnection* );
+	GncSqlResult* (*executeSelectStatement)( GncSqlConnection*, GncSqlStatement* );
+	gint (*executeNonSelectStatement)( GncSqlConnection*, GncSqlStatement* );
+	GncSqlStatement* (*createStatementFromSql)( GncSqlConnection*, const gchar* );
+	gboolean (*doesTableExist)( GncSqlConnection*, const gchar* );
+	void (*beginTransaction)( GncSqlConnection* );
+	void (*rollbackTransaction)( GncSqlConnection* );
+	void (*commitTransaction)( GncSqlConnection* );
+	const gchar* (*getColumnTypeName)( GncSqlConnection*, GType, gint size );
+	void (*createTable)( GncSqlConnection*, const gchar*, const GList* );
+	void (*createIndex)( GncSqlConnection*, const gchar*, const gchar*, const col_cvt_t* );
+};
+#define gnc_sql_connection_dispose(CONN) (CONN)->dispose(CONN)
+#define gnc_sql_connection_execute_select_statement(CONN,STMT) \
+		(CONN)->executeSelectStatement(CONN,STMT)
+#define gnc_sql_connection_execute_nonselect_statement(CONN,STMT) \
+		(CONN)->executeNonSelectStatement(CONN,STMT)
+#define gnc_sql_connection_create_statement_from_sql(CONN,SQL) \
+		(CONN)->createStatementFromSql(CONN,SQL)
+#define gnc_sql_connection_does_table_exist(CONN,NAME) \
+		(CONN)->doesTableExist(CONN,NAME)
+#define gnc_sql_connection_begin_transaction(CONN) \
+		(CONN)->beginTransaction(CONN)
+#define gnc_sql_connection_rollback_transaction(CONN) \
+		(CONN)->rollbackTransaction(CONN)
+#define gnc_sql_connection_commit_transaction(CONN) \
+		(CONN)->commitTransaction(CONN)
+#define gnc_sql_connection_get_column_type_name(CONN,TYPE,SIZE) \
+		(CONN)->getColumnTypeName(CONN,TYPE,SIZE)
+#define gnc_sql_connection_create_table(CONN,NAME,COLLIST) \
+		(CONN)->createTable(CONN,NAME,COLLIST)
+#define gnc_sql_connection_create_index(CONN,INDEXNAME,TABLENAME,COLTABLE) \
+		(CONN)->createIndex(CONN,INDEXNAME,TABLENAME,COLTABLE)
+
+struct GncSqlRow
+{
+	const GValue* (*getValueAtColName)( GncSqlRow*, const gchar* );
+	void (*dispose)( GncSqlRow* );
+};
+#define gnc_sql_row_get_value_at_col_name(ROW,N) \
+		(ROW)->getValueAtColName(ROW,N)
+#define gnc_sql_row_dispose(ROW) \
+		(ROW)->dispose(ROW)
+
+struct GncSqlResult
+{
+	int (*getNumRows)( GncSqlResult* );
+    GncSqlRow* (*getFirstRow)( GncSqlResult* );
+	GncSqlRow* (*getNextRow)( GncSqlResult* );
+	void (*dispose)( GncSqlResult* );
+};
+#define gnc_sql_result_get_num_rows(RESULT) \
+		(RESULT)->getNumRows(RESULT)
+#define gnc_sql_result_get_first_row(RESULT) \
+		(RESULT)->getFirstRow(RESULT)
+#define gnc_sql_result_get_next_row(RESULT) \
+		(RESULT)->getNextRow(RESULT)
+#define gnc_sql_result_dispose(RESULT) \
+		(RESULT)->dispose(RESULT)
+
+struct GncSqlBackend_struct
+{
+  QofBackend be;
+
+  GncSqlConnection* conn;
+
+  QofBook *primary_book;	/* The primary, main open book */
+  gboolean	loading;		/* We are performing an initial load */
+  gboolean  in_query;
+  gboolean  supports_transactions;
+  gboolean  is_pristine_db;	// Are we saving to a new pristine db?
+
+  gint obj_total;			// Total # of objects (for percentage calculation)
+  gint operations_done;		// Number of operations (save/load) done
+  GHashTable* versions;		// Version number for each table
+};
+typedef struct GncSqlBackend_struct GncSqlBackend;
+
+/**
+ * Struct used to pass in a new data type for GDA storage.  This contains
+ * the set of callbacks to read and write GDA for new data objects..  New
+ * types should register an instance of this object with the engine.
+ *
+ * commit()			- commit an object to the db
+ * initial_load()	- load stuff when new db opened
+ * create_tables()  - create any db tables
+ * compile_query()  - compile a backend object query
+ * run_query()      - run a compiled query
+ * free_query()     - free a compiled query
+ * write()          - write all objects
+ */
+#define GNC_SQL_BACKEND             "gnc:sql:1"
+#define GNC_SQL_BACKEND_VERSION	1
+typedef struct
+{
+  int		version;	/* backend version number */
+  const gchar *	type_name;	/* The XML tag for this type */
+
+  void		(*commit)( QofInstance* inst, GncSqlBackend* be );
+  void		(*initial_load)( GncSqlBackend* pBackend );
+  void		(*create_tables)( GncSqlBackend* pBackend );
+  gpointer	(*compile_query)( GncSqlBackend* pBackend, QofQuery* pQuery );
+  void		(*run_query)( GncSqlBackend* pBackend, gpointer pQuery );
+  void		(*free_query)( GncSqlBackend* pBackend, gpointer pQuery );
+  void		(*write)( GncSqlBackend* pBackend );
+} GncSqlDataType_t;
+
+// Type for conversion of db row to object.
+#define CT_STRING "ct_string"
+#define CT_GUID "ct_guid"
+#define CT_INT "ct_int"
+#define CT_INT64 "ct_int64"
+#define CT_TIMESPEC "ct_timespec"
+#define CT_GDATE "ct_gdate"
+#define CT_NUMERIC "ct_numeric"
+#define CT_DOUBLE "ct_double"
+#define CT_BOOLEAN "ct_boolean"
+#define CT_ACCOUNTREF "ct_accountref"
+#define CT_COMMODITYREF "ct_commodityref"
+#define CT_TXREF "ct_txref"
+#define CT_LOTREF "ct_lotref"
+
+struct col_cvt {
+	const gchar* col_name;
+	const gchar* col_type;
+	gint size;
+#define COL_PKEY	0x01
+#define COL_NNUL	0x02
+#define COL_UNIQUE	0x04
+#define COL_AUTOINC	0x08
+	gint flags;
+	const gchar* gobj_param_name;	// If non-null, use g_object_get/g_object_set
+	const gchar* param_name;	// If non null, use qof getter/setter
+	QofAccessFunc getter;
+	QofSetterFunc setter;
+};
+
+typedef enum {
+	OP_DB_ADD,
+	OP_DB_ADD_OR_UPDATE,
+	OP_DB_DELETE
+} E_DB_OPERATION;
+
+typedef void (*GNC_SQL_LOAD_FN)( const GncSqlBackend* be,
+								GncSqlRow* row,
+                                QofSetterFunc setter, gpointer pObject,
+                                const col_cvt_t* table );
+typedef void (*GNC_SQL_ADD_COL_INFO_TO_LIST_FN)( const GncSqlBackend* be,
+                        						const col_cvt_t* table_row,
+												GList** pList );
+typedef void (*GNC_SQL_ADD_COLNAME_TO_LIST_FN)( const col_cvt_t* table_row, GList** pList );
+typedef void (*GNC_SQL_ADD_GVALUE_TO_SLIST_FN)( const GncSqlBackend* be,
+                QofIdTypeConst obj_name, const gpointer pObject,
+                const col_cvt_t* table_row, GSList** pList );
+
+typedef struct {
+    GNC_SQL_LOAD_FN                 load_fn;
+    GNC_SQL_ADD_COL_INFO_TO_LIST_FN add_col_info_to_list_fn;
+    GNC_SQL_ADD_COLNAME_TO_LIST_FN  add_colname_to_list_fn;
+	GNC_SQL_ADD_GVALUE_TO_SLIST_FN	add_gvalue_to_slist_fn;
+} col_type_handler_t;
+
+GncSqlColumnInfo* gnc_sql_create_column_info( const col_cvt_t* table_row, const gchar* type, gint size );
+QofAccessFunc gnc_sql_get_getter( QofIdTypeConst obj_name, const col_cvt_t* table_row );
+
+void gnc_sql_add_colname_to_list( const col_cvt_t* table_row, GList** pList );
+
+gboolean gnc_sql_do_db_operation( GncSqlBackend* pBackend,
+									E_DB_OPERATION op,
+									const gchar* table_name,
+									QofIdTypeConst obj_name,
+									gpointer pObject,
+									const col_cvt_t* table );
+GncSqlStatement* gnc_sql_build_insert_statement( GncSqlBackend* pBackend,
+									const gchar* table_name,
+									QofIdTypeConst obj_name,
+									gpointer pObject,
+									const col_cvt_t* table );
+GncSqlStatement* gnc_sql_build_update_statement( GncSqlBackend* pBackend,
+									const gchar* table_name,
+									QofIdTypeConst obj_name,
+									gpointer pObject,
+									const col_cvt_t* table );
+GncSqlStatement* gnc_sql_build_delete_statement( GncSqlBackend* pBackend,
+									const gchar* table_name,
+									QofIdTypeConst obj_name,
+									gpointer pObject,
+									const col_cvt_t* table );
+gint gnc_sql_execute_statement( GncSqlBackend* pBackend, GncSqlStatement* statement );
+GncSqlResult* gnc_sql_execute_select_statement( GncSqlBackend* pBackend, GncSqlStatement* statement );
+GncSqlResult* gnc_sql_execute_sql_statement( GncSqlBackend* pBackend, GncSqlStatement* sqlStmt );
+GncSqlResult* gnc_sql_execute_select_sql( const GncSqlBackend* pBackend, const gchar* sql );
+gint gnc_sql_execute_nonselect_sql( const GncSqlBackend* pBackend, const gchar* sql );
+GncSqlStatement* gnc_sql_create_statement_from_sql( const GncSqlBackend* pBackend, const gchar* sql );
+int gnc_sql_execute_select_get_count( const GncSqlBackend* pBackend, const gchar* sql );
+int gnc_sql_execute_statement_get_count( GncSqlBackend* pBackend, GncSqlStatement* statement );
+void gnc_sql_load_object( const GncSqlBackend* be, GncSqlRow* row,
+						QofIdTypeConst obj_name, gpointer pObject,
+						const col_cvt_t* table );
+gboolean gnc_sql_object_is_it_in_db( GncSqlBackend* be,
+									const gchar* table_name,
+									QofIdTypeConst obj_name, const gpointer pObject,
+									const col_cvt_t* table );
+gint gnc_sql_get_table_version( const GncSqlBackend* be, const gchar* table_name );
+gboolean gnc_sql_create_table( const GncSqlBackend* be, const gchar* table_name,
+								gint table_version, const col_cvt_t* col_table,
+						GError** error );
+void gnc_sql_create_index( const GncSqlBackend* be, const gchar* index_name,
+						const gchar* table_name, const col_cvt_t* col_table );
+const GUID* gnc_sql_load_guid( const GncSqlBackend* be, GncSqlRow* row );
+const GUID* gnc_sql_load_tx_guid( const GncSqlBackend* be, GncSqlRow* row );
+GncSqlStatement* gnc_sql_create_select_statement( const GncSqlBackend* be, const gchar* table_name,
+										const col_cvt_t* col_table );
+void gnc_sql_register_col_type_handler( const gchar* colType, const col_type_handler_t* handler );
+void gnc_sql_register_standard_col_type_handlers( void );
+
+void gnc_sql_add_gvalue_objectref_guid_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+                const gpointer pObject, const col_cvt_t* table_row, GSList** pList );
+void gnc_sql_add_objectref_guid_col_info_to_list( const GncSqlBackend* be,
+	            const col_cvt_t* table_row, GList** pList );
+guint gnc_sql_append_guid_list_to_sql( GString* str, GList* list, guint maxCount );
+void gnc_sql_add_subtable_colnames_to_list( const col_cvt_t* table_row, const col_cvt_t* subtable,
+								GList** pList );
+gchar* gnc_sql_get_sql_value( const GValue* value );
+
+void _retrieve_guid_( gpointer pObject, gpointer pValue );
+void gnc_sql_init_version_info( GncSqlBackend* be );
+void gnc_sql_reset_version_info( GncSqlBackend* be );
+void gnc_sql_finalize_version_info( GncSqlBackend* be );
+
+G_MODULE_EXPORT const gchar *
+g_module_check_init( GModule *module );
+
+#endif /* GNC_BACKEND_SQL_UTIL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-book-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-book-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-book-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,243 @@
+/********************************************************************
+ * gnc-book-sql.c: load and save data to SQL                        *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-book-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "qof.h"
+
+#include "gnc-backend-util-sql.h"
+
+#include "gnc-book-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gnc-engine.h"
+#include "gnc-book.h"
+#include "SX-book.h"
+#include "SX-book-p.h"
+
+#define BOOK_TABLE "books"
+#define TABLE_VERSION 1
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+static gpointer get_root_account_guid( gpointer pObject, const QofParam* );
+static void set_root_account_guid( gpointer pObject, gpointer pValue );
+static gpointer get_root_template_guid( gpointer pObject, const QofParam* );
+static void set_root_template_guid( gpointer pObject, gpointer pValue );
+
+static const col_cvt_t col_table[] =
+{
+    { "guid",               CT_GUID, 0, COL_NNUL|COL_PKEY, "guid" },
+    { "root_account_guid",  CT_GUID, 0, COL_NNUL,          NULL, NULL, get_root_account_guid,  set_root_account_guid },
+    { "root_template_guid", CT_GUID, 0, COL_NNUL,          NULL, NULL, get_root_template_guid, set_root_template_guid },
+    { NULL }
+};
+
+/* ================================================================= */
+static gpointer
+get_root_account_guid( gpointer pObject, const QofParam* param )
+{
+    GNCBook* book = QOF_BOOK(pObject);
+    const Account* root;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL );
+
+    root = gnc_book_get_root_account( book );
+    return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) );
+}
+
+static void 
+set_root_account_guid( gpointer pObject, gpointer pValue )
+{
+    GNCBook* book = QOF_BOOK(pObject);
+    const Account* root;
+    GUID* guid = (GUID*)pValue;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( QOF_IS_BOOK(pObject) );
+	g_return_if_fail( pValue != NULL );
+
+    root = gnc_book_get_root_account( book );
+    qof_instance_set_guid( QOF_INSTANCE(root), guid );
+}
+
+static gpointer
+get_root_template_guid( gpointer pObject, const QofParam* param )
+{
+    const GNCBook* book = QOF_BOOK(pObject);
+    const Account* root;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL );
+
+    root = gnc_book_get_template_root( book );
+    return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) );
+}
+
+static void 
+set_root_template_guid( gpointer pObject, gpointer pValue )
+{
+    GNCBook* book = QOF_BOOK(pObject);
+    GUID* guid = (GUID*)pValue;
+    Account* root;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( QOF_IS_BOOK(pObject) );
+	g_return_if_fail( pValue != NULL );
+
+    root = gnc_book_get_template_root( book );
+    if( root == NULL ) {
+        root = xaccMallocAccount( book );
+        xaccAccountBeginEdit( root );
+        xaccAccountSetType( root, ACCT_TYPE_ROOT );
+        xaccAccountCommitEdit( root );
+        gnc_book_set_template_root( book, root );
+    }
+    qof_instance_set_guid( QOF_INSTANCE(root), guid );
+}
+
+/* ================================================================= */
+static void
+load_single_book( GncSqlBackend* be, GncSqlRow* row )
+{
+    const GUID* guid;
+    GUID book_guid;
+	GNCBook* pBook;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    book_guid = *guid;
+
+	pBook = be->primary_book;
+	if( pBook == NULL ) {
+	    pBook = gnc_book_new();
+	}
+
+    gnc_sql_load_object( be, row, GNC_ID_BOOK, pBook, col_table );
+    gnc_sql_slots_load( be, QOF_INSTANCE(pBook) );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pBook) );
+}
+
+static void
+load_all_books( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+
+	g_return_if_fail( be != NULL );
+
+    stmt = gnc_sql_create_select_statement( be, BOOK_TABLE, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+	if( result != NULL ) {
+		GncSqlRow* row = gnc_sql_result_get_first_row( result );
+
+		// If there are no rows, try committing the book
+		if( row == NULL ) {
+   	    	gnc_sql_save_book( QOF_INSTANCE(be->primary_book), be );
+		} else {
+			// Otherwise, load the 1st book.
+        	load_single_book( be, row );
+		}
+
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+create_book_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, BOOK_TABLE );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, BOOK_TABLE, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+void
+gnc_sql_save_book( QofInstance* inst, GncSqlBackend* be )
+{
+    const GUID* guid;
+	gint op;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( QOF_IS_BOOK(inst) );
+
+	if( qof_instance_get_destroying( inst ) ) {
+		op = OP_DB_DELETE;
+	} else if( be->is_pristine_db ) {
+		op = OP_DB_ADD;
+	} else {
+		op = OP_DB_ADD_OR_UPDATE;
+	}
+    (void)gnc_sql_do_db_operation( be, op, BOOK_TABLE, GNC_ID_BOOK, inst, col_table );
+
+    // Delete old slot info
+    guid = qof_instance_get_guid( inst );
+
+    // Now, commit any slots
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+/* ================================================================= */
+void
+gnc_sql_init_book_handler( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_BOOK,
+        gnc_sql_save_book,                 /* commit */
+        load_all_books,                    /* initial_load */
+        create_book_tables 		           /* create_tables */
+    };
+
+    qof_object_register_backend( GNC_ID_BOOK, GNC_SQL_BACKEND, &be_data );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-book-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-book-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-book-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,38 @@
+/********************************************************************
+ * gnc-book-sql.h: load and save data to SQL                        *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-book-sql.h
+ *  @brief load and save data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_BOOK_SQL_H_
+#define GNC_BOOK_SQL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+void gnc_sql_init_book_handler( void );
+void gnc_sql_save_book( QofInstance* inst, GncSqlBackend* be );
+
+#endif /* GNC_BOOK_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-budget-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-budget-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-budget-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,195 @@
+/********************************************************************
+ * gnc-budget-sql.c: load and save data to SQL                      *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-budget-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "qof.h"
+
+#include "gnc-backend-util-sql.h"
+
+#include "Recurrence.h"
+
+#include "gnc-budget-sql.h"
+#include "gnc-slots-sql.h"
+#include "gnc-recurrence-sql.h"
+
+#include "gnc-budget.h"
+
+#define BUDGET_TABLE "budgets"
+#define TABLE_VERSION 1
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define BUDGET_MAX_NAME_LEN 2048
+#define BUDGET_MAX_DESCRIPTION_LEN 2048
+
+static const col_cvt_t col_table[] =
+{
+    { "guid",        CT_GUID,   0,                          COL_NNUL|COL_PKEY, "guid" },
+    { "name",        CT_STRING, BUDGET_MAX_NAME_LEN,        COL_NNUL,          NULL, "name" },
+    { "description", CT_STRING, BUDGET_MAX_DESCRIPTION_LEN, 0,                 NULL, "description" },
+    { "num_periods", CT_INT,    0,                          COL_NNUL,          NULL, "num_periods" },
+    { NULL }
+};
+
+/* ================================================================= */
+static void
+load_single_budget( GncSqlBackend* be, GncSqlRow* row, GList** pList )
+{
+    const GUID* guid;
+    GUID budget_guid;
+	GncBudget* pBudget;
+	Recurrence* r;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    budget_guid = *guid;
+
+    pBudget = gnc_budget_lookup( &budget_guid, be->primary_book );
+    if( pBudget == NULL ) {
+        pBudget = gnc_budget_new( be->primary_book );
+    }
+
+    gnc_sql_load_object( be, row, GNC_ID_BUDGET, pBudget, col_table );
+	r = g_new0( Recurrence, 1 );
+	gnc_sql_recurrence_load( be, gnc_budget_get_guid( pBudget ), r );
+	*pList = g_list_append( *pList, pBudget );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pBudget) );
+}
+
+static void
+load_all_budgets( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    int r;
+	GList* list = NULL;
+
+	g_return_if_fail( be != NULL );
+
+    stmt = gnc_sql_create_select_statement( be, BUDGET_TABLE, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+	if( result != NULL ) {
+		GncSqlRow* row = gnc_sql_result_get_first_row( result );
+
+        while( row != NULL ) {
+            load_single_budget( be, row, &list );
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+
+		if( list != NULL ) {
+			gnc_sql_slots_load_for_list( be, list );
+		}
+    }
+}
+
+/* ================================================================= */
+static void
+create_budget_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, BUDGET_TABLE );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, BUDGET_TABLE, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+save_budget( QofInstance* inst, GncSqlBackend* be )
+{
+    GncBudget* pBudget = GNC_BUDGET(inst);
+    const GUID* guid;
+	gint op;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_BUDGET(inst) );
+
+	if( qof_instance_get_destroying( inst ) ) {
+		op = OP_DB_DELETE;
+	} else if( be->is_pristine_db ) {
+		op = OP_DB_ADD;
+	} else {
+		op = OP_DB_ADD_OR_UPDATE;
+	}
+    (void)gnc_sql_do_db_operation( be, op, BUDGET_TABLE, GNC_ID_BUDGET, pBudget, col_table );
+
+    // Now, commit any slots and recurrence
+    guid = qof_instance_get_guid( inst );
+    if( !qof_instance_get_destroying(inst) ) {
+		gnc_sql_recurrence_save( be, guid, gnc_budget_get_recurrence( pBudget ) );
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_recurrence_delete( be, guid );
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+static void
+write_budgets( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    qof_collection_foreach( qof_book_get_collection( be->primary_book, GNC_ID_BUDGET ),
+                            (QofInstanceForeachCB)save_budget, be );
+}
+
+/* ================================================================= */
+void
+gnc_sql_init_budget_handler( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_BUDGET,
+        save_budget,    		        /* commit */
+        load_all_budgets,               /* initial_load */
+        create_budget_tables,	        /* create_tables */
+		NULL, NULL, NULL,
+		write_budgets					/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_BUDGET, GNC_SQL_BACKEND, &be_data );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-budget-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-budget-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-budget-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,37 @@
+/********************************************************************
+ * gnc-budget-sql.h: load and save data to SQL                      *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-budget-sql.h
+ *  @brief load and save data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_BUDGET_SQL_H_
+#define GNC_BUDGET_SQL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+void gnc_sql_init_budget_handler( void );
+
+#endif /* GNC_BUDGET_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-commodity-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-commodity-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-commodity-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,297 @@
+/********************************************************************
+ * gnc-commodity-sql.c: load and save data to SQL                   *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-commodity-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "qof.h"
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-commodity.h"
+
+#include "gnc-commodity-sql.h"
+#include "gnc-slots-sql.h"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+static gpointer get_quote_source_name( gpointer pObject, const QofParam* );
+static void set_quote_source_name( gpointer pObject, gpointer pValue );
+
+#define COMMODITIES_TABLE "commodities"
+#define TABLE_VERSION 1
+
+#define COMMODITY_MAX_NAMESPACE_LEN 2048
+#define COMMODITY_MAX_MNEMONIC_LEN 2048
+#define COMMODITY_MAX_FULLNAME_LEN 2048
+#define COMMODITY_MAX_CUSIP_LEN 2048
+#define COMMODITY_MAX_QUOTESOURCE_LEN 2048
+#define COMMODITY_MAX_QUOTE_TZ_LEN 2048
+
+static const col_cvt_t col_table[] = {
+    { "guid",         CT_GUID,   0,                             COL_NNUL|COL_PKEY, "guid" },
+    { "namespace",    CT_STRING, COMMODITY_MAX_NAMESPACE_LEN,   COL_NNUL,          NULL, NULL,
+            (QofAccessFunc)gnc_commodity_get_namespace,
+            (QofSetterFunc)gnc_commodity_set_namespace },
+    { "mnemonic",     CT_STRING, COMMODITY_MAX_MNEMONIC_LEN,    COL_NNUL,          NULL, NULL,
+            (QofAccessFunc)gnc_commodity_get_mnemonic,
+            (QofSetterFunc)gnc_commodity_set_mnemonic },
+    { "fullname",     CT_STRING, COMMODITY_MAX_FULLNAME_LEN,    0,                 NULL, NULL,
+            (QofAccessFunc)gnc_commodity_get_fullname,
+            (QofSetterFunc)gnc_commodity_set_fullname },
+    { "cusip",        CT_STRING, COMMODITY_MAX_CUSIP_LEN,       0,                 NULL, NULL,
+            (QofAccessFunc)gnc_commodity_get_cusip,
+            (QofSetterFunc)gnc_commodity_set_cusip },
+    { "fraction",     CT_INT,    0,                             COL_NNUL,          NULL, NULL,
+            (QofAccessFunc)gnc_commodity_get_fraction,
+            (QofSetterFunc)gnc_commodity_set_fraction },
+    { "quote_flag",   CT_INT,    0,                             COL_NNUL,          NULL, NULL,
+            (QofAccessFunc)gnc_commodity_get_quote_flag,
+            (QofSetterFunc)gnc_commodity_set_quote_flag },
+    { "quote_source", CT_STRING, COMMODITY_MAX_QUOTESOURCE_LEN, 0,                 NULL, NULL,
+            get_quote_source_name, set_quote_source_name },
+    { "quote_tz",     CT_STRING, COMMODITY_MAX_QUOTE_TZ_LEN,    0,                 NULL, NULL,
+            (QofAccessFunc)gnc_commodity_get_quote_tz,
+            (QofSetterFunc)gnc_commodity_set_quote_tz },
+    { NULL }
+};
+
+/* ================================================================= */
+
+static gpointer
+get_quote_source_name( gpointer pObject, const QofParam* param )
+{
+    const gnc_commodity* pCommodity = GNC_COMMODITY(pObject);
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( GNC_IS_COMMODITY(pObject), NULL );
+
+    return (gpointer)gnc_quote_source_get_internal_name(
+                            gnc_commodity_get_quote_source(pCommodity));
+}
+
+static void 
+set_quote_source_name( gpointer pObject, gpointer pValue )
+{
+    gnc_commodity* pCommodity = GNC_COMMODITY(pObject);
+    const gchar* quote_source_name = (const gchar*)pValue;
+    gnc_quote_source* quote_source;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( GNC_IS_COMMODITY(pObject) );
+
+	if( pValue == NULL ) return;
+
+    quote_source = gnc_quote_source_lookup_by_internal( quote_source_name );
+    gnc_commodity_set_quote_source( pCommodity, quote_source );
+}
+
+static gnc_commodity*
+load_single_commodity( GncSqlBackend* be, GncSqlRow* row )
+{
+    QofBook* pBook = be->primary_book;
+    int col;
+    const GValue* val;
+    gnc_commodity* pCommodity;
+
+    pCommodity = gnc_commodity_new( pBook, NULL, NULL, NULL, NULL, 100 );
+
+    gnc_sql_load_object( be, row, GNC_ID_COMMODITY, pCommodity, col_table );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pCommodity) );
+
+    return pCommodity;
+}
+
+static void
+load_all_commodities( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    gnc_commodity_table* pTable;
+
+    pTable = gnc_commodity_table_get_table( be->primary_book );
+    stmt = gnc_sql_create_select_statement( be, COMMODITIES_TABLE, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        int r;
+        gnc_commodity* pCommodity;
+		GList* list = NULL;
+		GncSqlRow* row = gnc_sql_result_get_first_row( result );
+
+        while( row != NULL ) {
+            gnc_commodity* c;
+
+            pCommodity = load_single_commodity( be, row );
+
+            if( pCommodity != NULL ) {
+                GUID guid;
+
+                guid = *qof_instance_get_guid( QOF_INSTANCE(pCommodity) );
+                pCommodity = gnc_commodity_table_insert( pTable, pCommodity );
+				list = g_list_append( list, pCommodity );
+                qof_instance_set_guid( QOF_INSTANCE(pCommodity), &guid );
+            }
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+
+		if( list != NULL ) {
+			gnc_sql_slots_load_for_list( be, list );
+		}
+    }
+}
+/* ================================================================= */
+static void
+create_commodities_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, COMMODITIES_TABLE );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, COMMODITIES_TABLE, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+commit_commodity( QofInstance* inst, GncSqlBackend* be )
+{
+    const GUID* guid;
+	gint op;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_COMMODITY(inst) );
+
+	if( qof_instance_get_destroying( inst ) ) {
+		op = OP_DB_DELETE;
+	} else if( be->is_pristine_db ) {
+		op = OP_DB_ADD;
+	} else {
+		op = OP_DB_ADD_OR_UPDATE;
+	}
+    (void)gnc_sql_do_db_operation( be, op, COMMODITIES_TABLE, GNC_ID_COMMODITY, inst, col_table );
+
+    // Delete old slot info
+    guid = qof_instance_get_guid( inst );
+
+    // Now, commit or delete any slots
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+static gboolean
+is_commodity_in_db( GncSqlBackend* be, gnc_commodity* pCommodity )
+{
+	g_return_val_if_fail( be != NULL, FALSE );
+	g_return_val_if_fail( pCommodity != NULL, FALSE );
+
+    return gnc_sql_object_is_it_in_db( be, COMMODITIES_TABLE, GNC_ID_COMMODITY,
+                                pCommodity, col_table );
+}
+
+void
+gnc_sql_save_commodity( GncSqlBackend* be, gnc_commodity* pCommodity )
+{
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( pCommodity != NULL );
+
+    if( !is_commodity_in_db( be, pCommodity ) ) {
+        commit_commodity( QOF_INSTANCE(pCommodity), be );
+    }
+}
+
+/* ----------------------------------------------------------------- */
+
+static void
+load_commodity_guid( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GUID guid;
+    const GUID* pGuid;
+	gnc_commodity* commodity = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+	val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        pGuid = NULL;
+    } else {
+        string_to_guid( g_value_get_string( val ), &guid );
+        pGuid = &guid;
+    }
+	if( pGuid != NULL ) {
+		commodity = gnc_commodity_find_commodity_by_guid( pGuid, be->primary_book );
+	}
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, commodity, NULL );
+    } else {
+		(*setter)( pObject, (const gpointer)commodity );
+    }
+}
+
+static col_type_handler_t commodity_guid_handler
+	= { load_commodity_guid,
+		gnc_sql_add_objectref_guid_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+        gnc_sql_add_gvalue_objectref_guid_to_slist };
+/* ================================================================= */
+void
+gnc_sql_init_commodity_handler( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_COMMODITY,
+        commit_commodity,            /* commit */
+        load_all_commodities,            /* initial_load */
+        create_commodities_tables    /* create_tables */
+    };
+
+    qof_object_register_backend( GNC_ID_COMMODITY, GNC_SQL_BACKEND, &be_data );
+
+	gnc_sql_register_col_type_handler( CT_COMMODITYREF, &commodity_guid_handler );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-commodity-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-commodity-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-commodity-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,38 @@
+/********************************************************************
+ * gnc-commodity-sql.h: load and save data to SQL                   *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-commodity-sql.h
+ *  @brief load and save data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_COMMODITY_SQL_H_
+#define GNC_COMMODITY_SQL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+void gnc_sql_init_commodity_handler( void );
+void gnc_sql_save_commodity( GncSqlBackend* be, gnc_commodity* pCommodity );
+
+#endif /* GNC_COMMODITY_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-lots-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-lots-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-lots-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,245 @@
+/********************************************************************
+ * gnc-lots-sql.c: load and save data to SQL                        *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-lots-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "qof.h"
+#include "gnc-lot.h"
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gnc-lots-sql.h"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TABLE_NAME "lots"
+#define TABLE_VERSION 1
+
+static gpointer get_lot_account( gpointer pObject, const QofParam* param );
+static void set_lot_account( gpointer pObject, gpointer pValue );
+static void set_lot_is_closed( gpointer pObject, gboolean value );
+
+static const col_cvt_t col_table[] =
+{
+    { "guid",         CT_GUID,    0, COL_NNUL|COL_PKEY, "guid" },
+    { "account_guid", CT_GUID,    0, COL_NNUL,          NULL, NULL, get_lot_account,   set_lot_account },
+    { "is_closed",    CT_BOOLEAN, 0, COL_NNUL,          NULL, NULL,
+		(QofAccessFunc)gnc_lot_is_closed, (QofSetterFunc)set_lot_is_closed },
+    { NULL }
+};
+
+/* ================================================================= */
+static gpointer
+get_lot_account( gpointer pObject, const QofParam* param )
+{
+    const GNCLot* lot = GNC_LOT(pObject);
+    const Account* pAccount;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( GNC_IS_LOT(pObject), NULL );
+
+    pAccount = gnc_lot_get_account( lot );
+    return (gpointer)qof_instance_get_guid( QOF_INSTANCE(pAccount) );
+}
+
+static void 
+set_lot_account( gpointer pObject, gpointer pValue )
+{
+    GNCLot* lot = GNC_LOT(pObject);
+    QofBook* pBook;
+    GUID* guid = (GUID*)pValue;
+    Account* pAccount;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( GNC_IS_LOT(pObject) );
+	g_return_if_fail( pValue != NULL );
+
+    pBook = qof_instance_get_book( QOF_INSTANCE(lot) );
+    pAccount = xaccAccountLookup( guid, pBook );
+    xaccAccountInsertLot( pAccount, lot );
+}
+
+static void
+set_lot_is_closed( gpointer pObject, gboolean closed )
+{
+    GNCLot* lot = GNC_LOT(pObject);
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( GNC_IS_LOT(pObject) );
+
+    lot->is_closed = closed;
+}
+
+static void
+load_single_lot( GncSqlBackend* be, GncSqlRow* row, GList** pList )
+{
+	GNCLot* lot;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    lot = gnc_lot_new( be->primary_book );
+
+    gnc_sql_load_object( be, row, GNC_ID_LOT, lot, col_table );
+	*pList = g_list_append( *pList, lot );
+
+    qof_instance_mark_clean( QOF_INSTANCE(lot) );
+}
+
+static void
+load_all_lots( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+
+	g_return_if_fail( be != NULL );
+
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        int r;
+		GList* list = NULL;
+        GncSqlRow* row = gnc_sql_result_get_first_row( result );
+
+        while( row != NULL ) {
+            load_single_lot( be, row, &list );
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+
+		if( list != NULL ) {
+			gnc_sql_slots_load_for_list( be, list );
+		}
+    }
+}
+
+/* ================================================================= */
+static void
+create_lots_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+
+static void
+commit_lot( QofInstance* inst, GncSqlBackend* be )
+{
+	gint op;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_LOT(inst) );
+
+	if( qof_instance_get_destroying( inst ) ) {
+		op = OP_DB_DELETE;
+	} else if( be->is_pristine_db ) {
+		op = OP_DB_ADD;
+	} else {
+		op = OP_DB_ADD_OR_UPDATE;
+	}
+    (void)gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_LOT, inst, col_table );
+
+    // Now, commit any slots
+    gnc_sql_slots_save( be, qof_instance_get_guid( inst ),
+                        qof_instance_get_slots( inst ) );
+}
+
+/* ----------------------------------------------------------------- */
+static void
+load_lot_guid( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GUID guid;
+    const GUID* pGuid;
+	GNCLot* lot = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        pGuid = NULL;
+    } else {
+        string_to_guid( g_value_get_string( val ), &guid );
+        pGuid = &guid;
+    }
+	if( pGuid != NULL ) {
+		lot = gnc_lot_lookup( pGuid, be->primary_book );
+	}
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, lot, NULL );
+    } else {
+		(*setter)( pObject, (const gpointer)lot );
+    }
+}
+
+static col_type_handler_t lot_guid_handler
+	= { load_lot_guid,
+		gnc_sql_add_objectref_guid_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+        gnc_sql_add_gvalue_objectref_guid_to_slist };
+/* ================================================================= */
+void
+gnc_sql_init_lot_handler( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_LOT,
+        commit_lot,            /* commit */
+        load_all_lots,            /* initial_load */
+        create_lots_tables    /* create tables */
+    };
+
+    qof_object_register_backend( GNC_ID_LOT, GNC_SQL_BACKEND, &be_data );
+
+	gnc_sql_register_col_type_handler( CT_LOTREF, &lot_guid_handler );
+}
+
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-lots-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-lots-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-lots-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,37 @@
+/********************************************************************
+ * gnc-lots-sql.h: load and save data to SQL                        *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-lots-sql.h
+ *  @brief load and save data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_LOT_SQL_H_
+#define GNC_LOT_SQL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+void gnc_sql_init_lot_handler( void );
+
+#endif /* GNC_LOT_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-price-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-price-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-price-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,207 @@
+/********************************************************************
+ * gnc-price-sql.c: load and save data to SQL                       *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-price-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "qof.h"
+#include "gnc-pricedb.h"
+
+#include "gnc-backend-util-sql.h"
+
+#include "gnc-commodity-sql.h"
+#include "gnc-price-sql.h"
+#include "gnc-slots-sql.h"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TABLE_NAME "prices"
+#define TABLE_VERSION 1
+
+#define PRICE_MAX_SOURCE_LEN 2048
+#define PRICE_MAX_TYPE_LEN 2048
+
+static const col_cvt_t col_table[] =
+{
+    { "guid",           CT_GUID,           0,                    COL_NNUL|COL_PKEY, "guid" },
+    { "commodity_guid", CT_COMMODITYREF,   0,                    COL_NNUL,          NULL, PRICE_COMMODITY },
+    { "currency_guid",  CT_COMMODITYREF,   0,                    COL_NNUL,          NULL, PRICE_CURRENCY },
+    { "date",           CT_TIMESPEC,       0,                    COL_NNUL,          NULL, PRICE_DATE },
+    { "source",         CT_STRING,         PRICE_MAX_SOURCE_LEN, 0,                 NULL, PRICE_SOURCE },
+    { "type",           CT_STRING,         PRICE_MAX_TYPE_LEN,   0,                 NULL, PRICE_TYPE },
+    { "value",          CT_NUMERIC,        0,                    COL_NNUL,          NULL, PRICE_VALUE },
+    { NULL }
+};
+
+/* ================================================================= */
+
+static GNCPrice*
+load_single_price( GncSqlBackend* be, GncSqlRow* row, GList** pList )
+{
+	GNCPrice* pPrice;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( row != NULL, NULL );
+
+    pPrice = gnc_price_create( be->primary_book );
+
+    gnc_sql_load_object( be, row, GNC_ID_PRICE, pPrice, col_table );
+	*pList = g_list_append( *pList, pPrice );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pPrice) );
+
+    return pPrice;
+}
+
+static void
+load_all_prices( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    QofBook* pBook;
+    GNCPriceDB* pPriceDB;
+
+	g_return_if_fail( be != NULL );
+
+    pBook = be->primary_book;
+    pPriceDB = gnc_book_get_pricedb( pBook );
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        int r;
+        GNCPrice* pPrice;
+		GList* list = NULL;
+		GncSqlRow* row = gnc_sql_result_get_first_row( result );
+
+        while( row != NULL ) {
+            pPrice = load_single_price( be, row, &list );
+
+            if( pPrice != NULL ) {
+                gnc_pricedb_add_price( pPriceDB, pPrice );
+            }
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+
+		if( list != NULL ) {
+			gnc_sql_slots_load_for_list( be, list );
+		}
+    }
+}
+
+/* ================================================================= */
+static void
+create_prices_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+
+static void
+save_price( QofInstance* inst, GncSqlBackend* be )
+{
+    GNCPrice* pPrice = GNC_PRICE(inst);
+	gint op;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_PRICE(inst) );
+
+    /* Ensure commodity and currency are in the db */
+	gnc_sql_save_commodity( be, gnc_price_get_commodity( pPrice ) );
+    gnc_sql_save_commodity( be, gnc_price_get_currency( pPrice ) );
+
+	if( qof_instance_get_destroying( inst ) ) {
+		op = OP_DB_DELETE;
+	} else if( be->is_pristine_db ) {
+		op = OP_DB_ADD;
+	} else {
+		op = OP_DB_ADD_OR_UPDATE;
+	}
+    (void)gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_PRICE, pPrice, col_table );
+}
+
+static gboolean
+write_price( GNCPrice* p, gpointer data )
+{
+    GncSqlBackend* be = (GncSqlBackend*)data;
+
+	g_return_val_if_fail( p != NULL, FALSE );
+	g_return_val_if_fail( data != NULL, FALSE );
+
+    save_price( QOF_INSTANCE(p), be );
+
+    return TRUE;
+}
+
+static void
+write_prices( GncSqlBackend* be )
+{
+    GNCPriceDB* priceDB;
+
+	g_return_if_fail( be != NULL );
+
+    priceDB = gnc_book_get_pricedb( be->primary_book );
+
+    gnc_pricedb_foreach_price( priceDB, write_price, be, TRUE );
+}
+
+/* ================================================================= */
+void
+gnc_sql_init_price_handler( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_PRICE,
+        save_price,         		/* commit */
+        load_all_prices,            /* initial_load */
+        create_prices_tables,    	/* create tables */
+		NULL, NULL, NULL,
+		write_prices				/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_PRICE, GNC_SQL_BACKEND, &be_data );
+}
+
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-price-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-price-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-price-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,37 @@
+/********************************************************************
+ * gnc-price-sql.h: load and save data to SQL                       *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-price-sql.h
+ *  @brief load and save data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_PRICE_SQL_H_
+#define GNC_PRICE_SQL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+void gnc_sql_init_price_handler( void );
+
+#endif /* GNC_PRICE_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-recurrence-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-recurrence-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-recurrence-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,355 @@
+/********************************************************************
+ * gnc-recurrence-sql.c: load and save data to SQL                  *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-recurrence-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "qof.h"
+#include "gnc-engine.h"
+#include "Recurrence.h"
+
+#include "gnc-backend-util-sql.h"
+
+#include "gnc-recurrence-sql.h"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TABLE_NAME "recurrences"
+#define TABLE_VERSION 1
+
+#define BUDGET_MAX_RECURRENCE_PERIOD_TYPE_LEN 2048
+
+typedef struct {
+    GncSqlBackend* be;
+    const GUID* guid;
+	Recurrence* pRecurrence;
+} recurrence_info_t;
+
+static gpointer get_obj_guid( gpointer pObject, const QofParam* param );
+static void set_obj_guid( gpointer pObject, gpointer pValue );
+static gint get_recurrence_mult( gpointer pObject );
+static void set_recurrence_mult( gpointer pObject, gint value );
+static gpointer get_recurrence_period_type( gpointer pObject, const QofParam* );
+static void set_recurrence_period_type( gpointer pObject, gpointer pValue );
+static gpointer get_recurrence_period_start( gpointer pObject, const QofParam* );
+static void set_recurrence_period_start( gpointer pObject, gpointer pValue );
+
+static const col_cvt_t col_table[] =
+{
+    { "obj_guid",                CT_GUID,   0,                                     COL_NNUL, NULL, NULL,
+            get_obj_guid, set_obj_guid },
+    { "recurrence_mult",         CT_INT,    0,                                     COL_NNUL, NULL, NULL,
+            (QofAccessFunc)get_recurrence_mult, (QofSetterFunc)set_recurrence_mult },
+    { "recurrence_period_type",  CT_STRING, BUDGET_MAX_RECURRENCE_PERIOD_TYPE_LEN, COL_NNUL, NULL, NULL,
+			get_recurrence_period_type, set_recurrence_period_type },
+    { "recurrence_period_start", CT_GDATE,  0,                                     COL_NNUL, NULL, NULL,
+            get_recurrence_period_start, set_recurrence_period_start },
+    { NULL }
+};
+
+/* Special column table because we need to be able to access the table by
+a column other than the primary key */
+static const col_cvt_t guid_col_table[] =
+{
+    { "obj_guid", CT_GUID, 0, 0, NULL, NULL, get_obj_guid, set_obj_guid },
+    { NULL }
+};
+
+/* ================================================================= */
+
+static gpointer
+get_obj_guid( gpointer pObject, const QofParam* param )
+{
+    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+
+    return (gpointer)pInfo->guid;
+}
+
+static void
+set_obj_guid( gpointer pObject, gpointer pValue )
+{
+    // Nowhere to put the GUID
+}
+
+static gint
+get_recurrence_mult( gpointer pObject )
+{
+    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
+	
+	g_return_val_if_fail( pObject != NULL, 0 );
+	g_return_val_if_fail( pInfo->pRecurrence != NULL, 0 );
+
+	return pInfo->pRecurrence->mult;
+}
+
+static void
+set_recurrence_mult( gpointer pObject, gint value )
+{
+    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( pInfo->pRecurrence != NULL );
+
+    pInfo->pRecurrence->mult = value;
+}
+
+static gpointer
+get_recurrence_period_type( gpointer pObject, const QofParam* param )
+{
+    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( pInfo->pRecurrence != NULL, NULL );
+
+    return (gpointer)recurrencePeriodTypeToString(
+                            recurrenceGetPeriodType( pInfo->pRecurrence ) );
+}
+
+static void
+set_recurrence_period_type( gpointer pObject, gpointer pValue )
+{
+    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( pInfo->pRecurrence != NULL );
+	g_return_if_fail( pValue != NULL );
+
+    pInfo->pRecurrence->ptype = recurrencePeriodTypeFromString( (gchar*)pValue );
+}
+
+static gpointer
+get_recurrence_period_start( gpointer pObject, const QofParam* param )
+{
+    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
+    static GDate date;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( pInfo->pRecurrence != NULL, NULL );
+
+    date = recurrenceGetDate( pInfo->pRecurrence );
+    return (gpointer)&date;
+}
+
+static void
+set_recurrence_period_start( gpointer pObject, gpointer pValue )
+{
+    recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
+    GDate* date = (GDate*)pValue;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( pInfo->pRecurrence != NULL );
+	g_return_if_fail( pValue != NULL );
+
+    pInfo->pRecurrence->start = *date;
+}
+
+/* ================================================================= */
+
+void
+gnc_sql_recurrence_save( GncSqlBackend* be, const GUID* guid, const Recurrence* r )
+{
+    recurrence_info_t recurrence_info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( guid != NULL );
+	g_return_if_fail( r != NULL );
+
+	gnc_sql_recurrence_delete( be, guid );
+
+    recurrence_info.be = be;
+    recurrence_info.guid = guid;
+	recurrence_info.pRecurrence = (Recurrence*)r;
+    (void)gnc_sql_do_db_operation( be, OP_DB_ADD, TABLE_NAME,
+                                TABLE_NAME, &recurrence_info, col_table );
+}
+
+void
+gnc_sql_recurrence_save_list( GncSqlBackend* be, const GUID* guid, GList* schedule )
+{
+    recurrence_info_t recurrence_info;
+	GList* l;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( guid != NULL );
+
+	gnc_sql_recurrence_delete( be, guid );
+
+    recurrence_info.be = be;
+    recurrence_info.guid = guid;
+	for( l = schedule; l != NULL; l = g_list_next( l ) ) {
+		recurrence_info.pRecurrence = (Recurrence*)l->data;
+    	(void)gnc_sql_do_db_operation( be, OP_DB_ADD, TABLE_NAME,
+                                TABLE_NAME, &recurrence_info, col_table );
+	}
+}
+
+void
+gnc_sql_recurrence_delete( GncSqlBackend* be, const GUID* guid )
+{
+    recurrence_info_t recurrence_info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( guid != NULL );
+
+    recurrence_info.be = be;
+    recurrence_info.guid = guid;
+    (void)gnc_sql_do_db_operation( be, OP_DB_DELETE, TABLE_NAME,
+                                TABLE_NAME, &recurrence_info, guid_col_table );
+}
+
+static void
+load_recurrence( GncSqlBackend* be, GncSqlRow* row, Recurrence* r )
+{
+    recurrence_info_t recurrence_info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( r != NULL );
+
+    recurrence_info.be = be;
+	recurrence_info.pRecurrence = r;
+
+    gnc_sql_load_object( be, row, TABLE_NAME, &recurrence_info, col_table );
+	g_free( row );
+}
+
+static GncSqlResult*
+gnc_sql_set_recurrences_from_db( GncSqlBackend* be, const GUID* guid )
+{
+    gchar* buf;
+    gchar guid_buf[GUID_ENCODING_LENGTH+1];
+    gchar* field_name;
+    GncSqlStatement* stmt;
+    GValue value;
+	GError* error = NULL;
+	GncSqlResult* result;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( guid != NULL, NULL );
+
+    guid_to_string_buff( guid, guid_buf );
+    memset( &value, 0, sizeof( value ) );
+    g_value_init( &value, G_TYPE_STRING );
+    g_value_set_string( &value, guid_buf );
+
+	buf = g_strdup_printf( "SELECT * FROM %s WHERE obj_guid='%s'", TABLE_NAME, guid_buf );
+	stmt = gnc_sql_connection_create_statement_from_sql( be->conn, buf );
+	g_free( buf );
+
+    result = gnc_sql_execute_select_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+	return result;
+}
+
+void
+gnc_sql_recurrence_load( GncSqlBackend* be, const GUID* guid, Recurrence* pRecurrence )
+{
+	GncSqlResult* result;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( guid != NULL );
+	g_return_if_fail( pRecurrence != NULL );
+
+	result = gnc_sql_set_recurrences_from_db( be, guid );
+    if( result != NULL ) {
+        int numRows = gnc_sql_result_get_num_rows( result );
+
+		if( numRows > 0 ) {
+			if( numRows > 1 ) {
+				g_warning( "More than 1 recurrence found: first one used" );
+			}
+			load_recurrence( be, gnc_sql_result_get_first_row( result ),
+							pRecurrence );
+		} else {
+			g_warning( "No recurrences found" );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+void
+gnc_sql_recurrence_load_list( GncSqlBackend* be, const GUID* guid, GList** pSchedule )
+{
+	GncSqlResult* result;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( guid != NULL );
+	g_return_if_fail( pSchedule != NULL );
+
+	result = gnc_sql_set_recurrences_from_db( be, guid );
+    if( result != NULL ) {
+        GncSqlRow* row = gnc_sql_result_get_first_row( result );
+
+		while( row != NULL ) {
+			Recurrence* pRecurrence = g_new0( Recurrence, 1 );
+			load_recurrence( be, row, pRecurrence );
+			*pSchedule = g_list_append( *pSchedule, pRecurrence );
+			row = gnc_sql_result_get_next_row( result );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+create_recurrence_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+void
+gnc_sql_init_recurrence_handler( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_ACCOUNT,
+        NULL,                    /* commit - cannot occur */
+        NULL,                    /* initial_load - cannot occur */
+        create_recurrence_tables        /* create_tables */
+    };
+
+    qof_object_register_backend( TABLE_NAME, GNC_SQL_BACKEND, &be_data );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-recurrence-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-recurrence-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-recurrence-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,43 @@
+/********************************************************************
+ * gnc-recurrence-sql.h: load and save data to SQL                  *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-recurrence-sql.h
+ *  @brief load and save accounts data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_RECURRENCE_SQL_H_
+#define GNC_RECURRENCE_SQL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+void gnc_sql_recurrence_save( GncSqlBackend* be, const GUID* guid, const Recurrence* pRecurrence );
+void gnc_sql_recurrence_save_list( GncSqlBackend* be, const GUID* guid, GList* schedule );
+void gnc_sql_recurrence_delete( GncSqlBackend* be, const GUID* guid );
+void gnc_sql_recurrence_load( GncSqlBackend* be, const GUID* guid, Recurrence* pRecurrence );
+void gnc_sql_recurrence_load_list( GncSqlBackend* be, const GUID* guid, GList** pSchedule );
+
+void gnc_sql_init_recurrence_handler( void );
+
+#endif /* GNC_RECURRENCE_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-schedxaction-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-schedxaction-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-schedxaction-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,313 @@
+/********************************************************************
+ * gnc-schedxaction-sql.c: load and save data to SQL                *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-schedxaction-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "qof.h"
+
+#include "gnc-backend-util-sql.h"
+
+#include "gnc-schedxaction-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "SchedXaction.h"
+#include "SX-book.h"
+#include "Recurrence.h"
+
+#include "gnc-recurrence-sql.h"
+
+#define SCHEDXACTION_TABLE "schedxactions"
+#define TABLE_VERSION 1
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define SX_MAX_NAME_LEN 2048
+
+static gboolean get_autocreate( gpointer pObject );
+static void set_autocreate( gpointer pObject, gboolean value );
+static gboolean get_autonotify( gpointer pObject );
+static void set_autonotify( gpointer pObject, gboolean value );
+static gint get_instance_count( gpointer pObject );
+static gpointer get_template_act_guid( gpointer pObject, const QofParam* param );
+static void set_template_act_guid( gpointer pObject, gpointer pValue );
+
+static const col_cvt_t col_table[] =
+{
+    { "guid",              CT_GUID,    0,               COL_NNUL|COL_PKEY, "guid" },
+    { "name",              CT_STRING,  SX_MAX_NAME_LEN, 0,                 NULL, GNC_SX_NAME },
+	{ "enabled",           CT_BOOLEAN, 0,               COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)xaccSchedXactionGetEnabled, (QofSetterFunc)xaccSchedXactionSetEnabled },
+    { "start_date",        CT_GDATE,   0,               COL_NNUL,          NULL, GNC_SX_START_DATE },
+    { "last_occur",        CT_GDATE,   0,               0,                 NULL, GNC_SX_LAST_DATE },
+    { "num_occur",         CT_INT,     0,               COL_NNUL,          NULL, GNC_SX_NUM_OCCUR },
+    { "rem_occur",         CT_INT,     0,               COL_NNUL,          NULL, GNC_SX_REM_OCCUR },
+    { "auto_create",       CT_BOOLEAN, 0,               COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)get_autocreate,        (QofSetterFunc)set_autocreate },
+    { "auto_notify",       CT_BOOLEAN, 0,               COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)get_autonotify,        (QofSetterFunc)set_autonotify },
+    { "adv_creation",      CT_INT,     0,               COL_NNUL,          NULL, NULL,
+            (QofAccessFunc)xaccSchedXactionGetAdvanceCreation,
+            (QofSetterFunc)xaccSchedXactionSetAdvanceCreation },
+    { "adv_notify",        CT_INT,     0,               COL_NNUL,          NULL, NULL,
+            (QofAccessFunc)xaccSchedXactionGetAdvanceReminder,
+            (QofSetterFunc)xaccSchedXactionSetAdvanceReminder },
+	{ "instance_count",    CT_INT,     0,               COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)get_instance_count, (QofSetterFunc)gnc_sx_set_instance_count },
+    { "template_act_guid", CT_GUID,    0,               COL_NNUL,          NULL, NULL,
+			get_template_act_guid, set_template_act_guid },
+    { NULL }
+};
+
+/* ================================================================= */
+
+static gboolean
+get_autocreate( gpointer pObject )
+{
+    const SchedXaction* pSx = GNC_SX(pObject);
+    gboolean autoCreate;
+    gboolean autoNotify;
+
+	g_return_val_if_fail( pObject != NULL, FALSE );
+	g_return_val_if_fail( GNC_IS_SX(pObject), FALSE );
+
+    xaccSchedXactionGetAutoCreate( pSx, &autoCreate, &autoNotify );
+    return autoCreate;
+}
+
+static void 
+set_autocreate( gpointer pObject, gboolean value )
+{
+    SchedXaction* pSx = GNC_SX(pObject);
+    gboolean autoNotify;
+	gboolean dummy;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( GNC_IS_SX(pObject) );
+
+    xaccSchedXactionGetAutoCreate( pSx, &dummy, &autoNotify );
+    xaccSchedXactionSetAutoCreate( pSx, value, autoNotify );
+}
+
+static gboolean
+get_autonotify( gpointer pObject )
+{
+    const SchedXaction* pSx = GNC_SX(pObject);
+    gboolean autoCreate;
+    gboolean autoNotify;
+
+	g_return_val_if_fail( pObject != NULL, FALSE );
+	g_return_val_if_fail( GNC_IS_SX(pObject), FALSE );
+
+    xaccSchedXactionGetAutoCreate( pSx, &autoCreate, &autoNotify );
+    return autoNotify;
+}
+
+static void 
+set_autonotify( gpointer pObject, gboolean value )
+{
+    SchedXaction* pSx = GNC_SX(pObject);
+    gboolean autoCreate;
+    gboolean dummy;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( GNC_IS_SX(pObject) );
+
+    xaccSchedXactionGetAutoCreate( pSx, &autoCreate, &dummy );
+    xaccSchedXactionSetAutoCreate( pSx, autoCreate, value );
+}
+
+static gint
+get_instance_count( gpointer pObject )
+{
+    const SchedXaction* pSx = GNC_SX(pObject);
+
+	g_return_val_if_fail( pObject != NULL, FALSE );
+	g_return_val_if_fail( GNC_IS_SX(pObject), FALSE );
+
+    return gnc_sx_get_instance_count( pSx, NULL );
+}
+
+static gpointer
+get_template_act_guid( gpointer pObject, const QofParam* param )
+{
+    const SchedXaction* pSx = GNC_SX(pObject);
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( GNC_IS_SX(pObject), NULL );
+
+    return (gpointer)xaccAccountGetGUID( pSx->template_acct );
+}
+
+static void 
+set_template_act_guid( gpointer pObject, gpointer pValue )
+{
+    SchedXaction* pSx = GNC_SX(pObject);
+    QofBook* pBook;
+    GUID* guid = (GUID*)pValue;
+	Account* pAcct;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( GNC_IS_SX(pObject) );
+	g_return_if_fail( pValue != NULL );
+
+    pBook = qof_instance_get_book( QOF_INSTANCE(pSx) );
+	pAcct = xaccAccountLookup( guid, pBook );
+	sx_set_template_account( pSx, pAcct );
+}
+
+/* ================================================================= */
+static SchedXaction*
+load_single_sx( GncSqlBackend* be, GncSqlRow* row, GList** pList )
+{
+    const GUID* guid;
+    GUID sx_guid;
+	SchedXaction* pSx;
+	GList* schedule = NULL;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( row != NULL, NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    sx_guid = *guid;
+
+    pSx = xaccSchedXactionMalloc( be->primary_book );
+
+    gnc_sql_load_object( be, row, GNC_SX_ID, pSx, col_table );
+	gnc_sql_recurrence_load_list( be, guid, &schedule );
+	gnc_sx_set_schedule( pSx, schedule );
+	*pList = g_list_append( *pList, pSx );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pSx) );
+
+    return pSx;
+}
+
+static void
+load_all_sxes( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt = NULL;
+    GncSqlResult* result;
+
+	g_return_if_fail( be != NULL );
+
+    stmt = gnc_sql_create_select_statement( be, SCHEDXACTION_TABLE, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+		GncSqlRow* row;
+        int r;
+     	SchedXactions *sxes;
+		GList* list = NULL;
+     	sxes = gnc_book_get_schedxactions( be->primary_book );
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            SchedXaction* sx;
+			
+			sx = load_single_sx( be, row, &list );
+			if( sx != NULL ) {
+		    	gnc_sxes_add_sx(sxes, sx);
+			}
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+
+		if( list != NULL ) {
+			gnc_sql_slots_load_for_list( be, list );
+		}
+    }
+}
+
+/* ================================================================= */
+static void
+create_sx_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, SCHEDXACTION_TABLE );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, SCHEDXACTION_TABLE, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+void
+gnc_sql_save_schedxaction( QofInstance* inst, GncSqlBackend* be )
+{
+    SchedXaction* pSx = GNC_SX(inst);
+    const GUID* guid;
+	gint op;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_SX(inst) );
+	g_return_if_fail( be != NULL );
+
+	if( qof_instance_get_destroying( inst ) ) {
+		op = OP_DB_DELETE;
+	} else if( be->is_pristine_db ) {
+		op = OP_DB_ADD;
+	} else {
+		op = OP_DB_ADD_OR_UPDATE;
+	}
+    (void)gnc_sql_do_db_operation( be, op, SCHEDXACTION_TABLE, /*GNC_ID_SCHEDXACTION*/GNC_SX_ID, pSx, col_table );
+    guid = qof_instance_get_guid( inst );
+	gnc_sql_recurrence_save_list( be, guid, gnc_sx_get_schedule( pSx ) );
+
+    // Now, commit any slots
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+/* ================================================================= */
+void
+gnc_sql_init_schedxaction_handler( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_SCHEDXACTION,
+        gnc_sql_save_schedxaction,                /* commit */
+        load_all_sxes,                /* initial_load */
+        create_sx_tables        /* create_tables */
+    };
+
+    qof_object_register_backend( GNC_ID_SCHEDXACTION, GNC_SQL_BACKEND, &be_data );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-schedxaction-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-schedxaction-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-schedxaction-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,38 @@
+/********************************************************************
+ * gnc-schedxaction-sql.h: load and save data to SQL                *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-backend-sql.h
+ *  @brief load and save data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_SCHEDXACTION_SQL_H_
+#define GNC_SCHEDXACTION_SQL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+void gnc_sql_init_schedxaction_handler( void );
+void gnc_sql_save_schedxaction( QofInstance* inst, GncSqlBackend* be );
+
+#endif /* GNC_SCHEDXACTION_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-slots-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-slots-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-slots-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,573 @@
+/********************************************************************
+ * gnc-slots-sql.c: load and save data to SQL                       *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-slots-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "qof.h"
+#include "gnc-engine.h"
+
+#include "gnc-backend-util-sql.h"
+
+#include "gnc-slots-sql.h"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TABLE_NAME "slots"
+#define TABLE_VERSION 1
+
+typedef struct {
+    GncSqlBackend* be;
+    const GUID* guid;
+    KvpFrame* pKvpFrame;
+    KvpValueType value_type;
+    KvpValue* pKvpValue;
+    GString* path;
+} slot_info_t;
+
+static gpointer get_obj_guid( gpointer pObject, const QofParam* param );
+static void set_obj_guid( gpointer pObject, gpointer pValue );
+static gpointer get_path( gpointer pObject, const QofParam* param );
+static void set_path( gpointer pObject, gpointer pValue );
+static gpointer get_slot_type( gpointer pObject, const QofParam* param );
+static void set_slot_type( gpointer pObject, gpointer pValue );
+static gint64 get_int64_val( gpointer pObject, const QofParam* param );
+static void set_int64_val( gpointer pObject, gint64 pValue );
+static gpointer get_string_val( gpointer pObject, const QofParam* param );
+static void set_string_val( gpointer pObject, gpointer pValue );
+static gpointer get_double_val( gpointer pObject, const QofParam* param );
+static void set_double_val( gpointer pObject, gpointer pValue );
+static Timespec get_timespec_val( gpointer pObject, const QofParam* param );
+static void set_timespec_val( gpointer pObject, Timespec ts );
+static gpointer get_guid_val( gpointer pObject, const QofParam* param );
+static void set_guid_val( gpointer pObject, gpointer pValue );
+static gnc_numeric get_numeric_val( gpointer pObject, const QofParam* param );
+static void set_numeric_val( gpointer pObject, gnc_numeric value );
+
+#define SLOT_MAX_PATHNAME_LEN 4096
+#define SLOT_MAX_STRINGVAL_LEN 4096
+
+static const col_cvt_t col_table[] =
+{
+    { "obj_guid",     CT_GUID,     0,                     COL_NNUL, NULL, NULL,
+			get_obj_guid,     set_obj_guid },
+    { "name",         CT_STRING,   SLOT_MAX_PATHNAME_LEN, COL_NNUL, NULL, NULL,
+			get_path,         set_path },
+    { "slot_type",    CT_INT,      0,                     COL_NNUL, NULL, NULL,
+			get_slot_type,    set_slot_type, },
+    { "int64_val",    CT_INT64,    0,                     0,        NULL, NULL,
+			(QofAccessFunc)get_int64_val,    (QofSetterFunc)set_int64_val },
+    { "string_val",   CT_STRING,   SLOT_MAX_PATHNAME_LEN, 0,        NULL, NULL,
+			get_string_val,   set_string_val },
+    { "double_val",   CT_DOUBLE,   0,                     0,        NULL, NULL,
+			get_double_val,   set_double_val },
+    { "timespec_val", CT_TIMESPEC, 0,                     0,        NULL, NULL,
+			(QofAccessFunc)get_timespec_val, (QofSetterFunc)set_timespec_val },
+    { "guid_val",     CT_GUID,     0,                     0,        NULL, NULL,
+			get_guid_val,     set_guid_val },
+    { "numeric_val",  CT_NUMERIC,  0,                     0,        NULL, NULL,
+			(QofAccessFunc)get_numeric_val, (QofSetterFunc)set_numeric_val },
+    { NULL }
+};
+
+/* Special column table because we need to be able to access the table by
+a column other than the primary key */
+static const col_cvt_t obj_guid_col_table[] =
+{
+    { "obj_guid", CT_GUID, 0, 0, NULL, NULL, get_obj_guid, _retrieve_guid_ },
+    { NULL }
+};
+
+/* ================================================================= */
+
+static gpointer
+get_obj_guid( gpointer pObject, const QofParam* param )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+
+    return (gpointer)pInfo->guid;
+}
+
+static void
+set_obj_guid( gpointer pObject, gpointer pValue )
+{
+    // Nowhere to put the GUID
+}
+
+static gpointer
+get_path( gpointer pObject, const QofParam* param )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+
+    return (gpointer)pInfo->path->str;
+}
+
+static void
+set_path( gpointer pObject, gpointer pValue )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( pValue != NULL );
+
+    pInfo->path = g_string_new( (gchar*)pValue );
+}
+
+static gpointer
+get_slot_type( gpointer pObject, const QofParam* param )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+
+    return (gpointer)kvp_value_get_type( pInfo->pKvpValue );
+}
+
+static void
+set_slot_type( gpointer pObject, gpointer pValue )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( pValue != NULL );
+
+    pInfo->value_type = (KvpValueType)pValue;
+}
+
+static gint64
+get_int64_val( gpointer pObject, const QofParam* param )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_val_if_fail( pObject != NULL, 0 );
+
+    if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_GINT64 ) {
+        return kvp_value_get_gint64( pInfo->pKvpValue );
+    } else {
+        return 0;
+    }
+}
+
+static void
+set_int64_val( gpointer pObject, gint64 value )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+
+    if( pInfo->value_type == KVP_TYPE_GINT64 ) {
+        kvp_frame_set_gint64( pInfo->pKvpFrame, pInfo->path->str, value );
+    }
+}
+
+static gpointer
+get_string_val( gpointer pObject, const QofParam* param )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+
+    if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_STRING ) {
+        return (gpointer)kvp_value_get_string( pInfo->pKvpValue );
+    } else {
+        return NULL;
+    }
+}
+
+static void
+set_string_val( gpointer pObject, gpointer pValue )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+
+    if( pInfo->value_type == KVP_TYPE_STRING && pValue != NULL ) {
+        kvp_frame_set_string( pInfo->pKvpFrame, pInfo->path->str, (const gchar*)pValue );
+    }
+}
+
+static gpointer
+get_double_val( gpointer pObject, const QofParam* param )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+    static double d_val;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+
+    if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_DOUBLE ) {
+        d_val = kvp_value_get_double( pInfo->pKvpValue );
+        return (gpointer)&d_val;
+    } else {
+        return NULL;
+    }
+}
+
+static void
+set_double_val( gpointer pObject, gpointer pValue )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+
+    if( pInfo->value_type == KVP_TYPE_DOUBLE && pValue != NULL ) {
+        kvp_frame_set_double( pInfo->pKvpFrame, pInfo->path->str, *(double*)pValue );
+    }
+}
+
+static Timespec
+get_timespec_val( gpointer pObject, const QofParam* param )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_val_if_fail( pObject != NULL, gnc_dmy2timespec( 1, 1, 1970 ) );
+
+//if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_TIMESPEC ) {
+    return kvp_value_get_timespec( pInfo->pKvpValue );
+}
+
+static void
+set_timespec_val( gpointer pObject, Timespec ts )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+
+    if( pInfo->value_type == KVP_TYPE_TIMESPEC ) {
+        kvp_frame_set_timespec( pInfo->pKvpFrame, pInfo->path->str, ts );
+    }
+}
+
+static gpointer
+get_guid_val( gpointer pObject, const QofParam* param )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+
+    if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_GUID ) {
+        return (gpointer)kvp_value_get_guid( pInfo->pKvpValue );
+    } else {
+        return NULL;
+    }
+}
+
+static void
+set_guid_val( gpointer pObject, gpointer pValue )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+
+    if( pInfo->value_type == KVP_TYPE_GUID && pValue != NULL ) {
+        kvp_frame_set_guid( pInfo->pKvpFrame, pInfo->path->str, (GUID*)pValue );
+    }
+}
+
+static gnc_numeric
+get_numeric_val( gpointer pObject, const QofParam* param )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_val_if_fail( pObject != NULL, gnc_numeric_zero() );
+
+    if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_NUMERIC ) {
+        return kvp_value_get_numeric( pInfo->pKvpValue );
+    } else {
+        return gnc_numeric_zero();
+    }
+}
+
+static void
+set_numeric_val( gpointer pObject, gnc_numeric value )
+{
+    slot_info_t* pInfo = (slot_info_t*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+
+    if( pInfo->value_type == KVP_TYPE_NUMERIC ) {
+        kvp_frame_set_numeric( pInfo->pKvpFrame, pInfo->path->str, value );
+    }
+}
+
+static void
+save_slot( const gchar* key, KvpValue* value, gpointer data )
+{
+    slot_info_t* pSlot_info = (slot_info_t*)data;
+    gint curlen;
+
+	g_return_if_fail( key != NULL );
+	g_return_if_fail( value != NULL );
+	g_return_if_fail( data != NULL );
+
+    curlen = pSlot_info->path->len;
+    pSlot_info->pKvpValue = value;
+    if( curlen != 0 ) {
+        g_string_append( pSlot_info->path, "/" );
+    }
+    g_string_append( pSlot_info->path, key );
+
+    if( kvp_value_get_type( value ) == KVP_TYPE_FRAME ) {
+        KvpFrame* pKvpFrame = kvp_value_get_frame( value );
+        kvp_frame_for_each_slot( pKvpFrame, save_slot, pSlot_info );
+    } else {
+        (void)gnc_sql_do_db_operation( pSlot_info->be, OP_DB_ADD, TABLE_NAME,
+                                        TABLE_NAME, pSlot_info, col_table );
+    }
+
+    g_string_truncate( pSlot_info->path, curlen );
+}
+
+void
+gnc_sql_slots_save( GncSqlBackend* be, const GUID* guid, KvpFrame* pFrame )
+{
+    slot_info_t slot_info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( guid != NULL );
+	g_return_if_fail( pFrame != NULL );
+
+    // If this is not saving into a new db, clear out the old saved slots first
+	if( !be->is_pristine_db ) {
+    	gnc_sql_slots_delete( be, guid );
+	}
+
+    slot_info.be = be;
+    slot_info.guid = guid;
+    slot_info.path = g_string_new( "" );
+    kvp_frame_for_each_slot( pFrame, save_slot, &slot_info );
+    g_string_free( slot_info.path, TRUE );
+}
+
+void
+gnc_sql_slots_delete( GncSqlBackend* be, const GUID* guid )
+{
+    slot_info_t slot_info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( guid != NULL );
+
+    slot_info.be = be;
+    slot_info.guid = guid;
+    (void)gnc_sql_do_db_operation( be, OP_DB_DELETE, TABLE_NAME,
+                                TABLE_NAME, &slot_info, obj_guid_col_table );
+}
+
+static void
+load_slot( GncSqlBackend* be, GncSqlRow* row, KvpFrame* pFrame )
+{
+    slot_info_t slot_info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pFrame != NULL );
+
+    slot_info.be = be;
+    slot_info.pKvpFrame = pFrame;
+    slot_info.path = NULL;
+
+    gnc_sql_load_object( be, row, TABLE_NAME, &slot_info, col_table );
+
+    if( slot_info.path != NULL ) {
+        g_string_free( slot_info.path, TRUE );
+    }
+}
+
+void
+gnc_sql_slots_load( GncSqlBackend* be, QofInstance* inst )
+{
+    gchar* buf;
+    GncSqlResult* result;
+    gchar guid_buf[GUID_ENCODING_LENGTH+1];
+    gchar* field_name;
+    GncSqlStatement* stmt;
+    GValue value;
+	const GUID* guid;
+	KvpFrame* pFrame;
+	GError* error = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( inst != NULL );
+
+	guid = qof_instance_get_guid( inst );
+	pFrame = qof_instance_get_slots( inst );
+    guid_to_string_buff( guid, guid_buf );
+    memset( &value, 0, sizeof( value ) );
+    g_value_init( &value, G_TYPE_STRING );
+    g_value_set_string( &value, guid_buf );
+
+	buf = g_strdup_printf( "SELECT * FROM %s WHERE obj_guid='%s'", TABLE_NAME, guid_buf );
+	stmt = gnc_sql_create_statement_from_sql( be, buf );
+	g_free( buf );
+
+	result = gnc_sql_execute_select_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        GncSqlRow* row = gnc_sql_result_get_first_row( result );
+
+        while( row != NULL ) {
+            load_slot( be, row, pFrame );
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+    }
+}
+
+static const GUID*
+load_obj_guid( const GncSqlBackend* be, GncSqlRow* row )
+{
+    static GUID guid;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( row != NULL, NULL );
+
+    gnc_sql_load_object( be, row, NULL, &guid, obj_guid_col_table );
+
+    return &guid;
+}
+
+static void
+load_slot_for_list_item( GncSqlBackend* be, GncSqlRow* row, QofCollection* coll )
+{
+    slot_info_t slot_info;
+	const GUID* guid;
+	QofInstance* inst;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( coll != NULL );
+
+	guid = load_obj_guid( be, row );
+	inst = qof_collection_lookup_entity( coll, guid );
+
+    slot_info.be = be;
+    slot_info.pKvpFrame = qof_instance_get_slots( inst );
+    slot_info.path = NULL;
+
+    gnc_sql_load_object( be, row, TABLE_NAME, &slot_info, col_table );
+
+    if( slot_info.path != NULL ) {
+        g_string_free( slot_info.path, TRUE );
+    }
+}
+
+void
+gnc_sql_slots_load_for_list( GncSqlBackend* be, GList* list )
+{
+	QofCollection* coll;
+	GncSqlStatement* stmt;
+	GString* sql;
+    gchar guid_buf[GUID_ENCODING_LENGTH+1];
+	gboolean first_guid = TRUE;
+	GncSqlResult* result;
+	gboolean single_item;
+
+	g_return_if_fail( be != NULL );
+
+	// Ignore empty list
+	if( list == NULL ) return;
+
+	coll = qof_instance_get_collection( QOF_INSTANCE(list->data) );
+
+	// Create the query for all slots for all items on the list
+	sql = g_string_sized_new( 40+(GUID_ENCODING_LENGTH+3)*g_list_length( list ) );
+	g_string_append_printf( sql, "SELECT * FROM %s WHERE %s ", TABLE_NAME, obj_guid_col_table[0].col_name );
+	if( g_list_length( list ) != 1 ) {
+		g_string_append( sql, "IN (" );
+		single_item = FALSE;
+	} else {
+		g_string_append( sql, "= " );
+		single_item = TRUE;
+	}
+	(void)gnc_sql_append_guid_list_to_sql( sql, list, G_MAXUINT );
+	if( !single_item ) {
+		g_string_append( sql, ")" );
+	}
+
+	// Execute the query and load the slots
+	stmt = gnc_sql_create_statement_from_sql( be, sql->str );
+	result = gnc_sql_execute_select_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        GncSqlRow* row = gnc_sql_result_get_first_row( result );
+
+        while( row != NULL ) {
+            load_slot_for_list_item( be, row, coll );
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+    }
+	g_string_free( sql, TRUE );
+}
+
+/* ================================================================= */
+static void
+create_slots_tables( GncSqlBackend* be )
+{
+	GError* error = NULL;
+	gboolean ok;
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+	if( version == 0 ) {
+    	gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+		if( error != NULL ) {
+			g_critical( "Unable to create SLOTS table: %s\n", error->message );
+		}
+#if 0
+		// FIXME: Create index
+		ok = gnc_sql_create_index( be, "slots_guid_index", TABLE_NAME, obj_guid_col_table, &error );
+		if( !ok ) {
+			g_critical( "Unable to create index: %s\n", error->message );
+		}
+#endif
+	}
+}
+
+/* ================================================================= */
+void
+gnc_sql_init_slots_handler( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_ACCOUNT,
+        NULL,                    /* commit - cannot occur */
+        NULL,                    /* initial_load - cannot occur */
+        create_slots_tables        /* create_tables */
+    };
+
+    qof_object_register_backend( TABLE_NAME, GNC_SQL_BACKEND, &be_data );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-slots-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-slots-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-slots-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,42 @@
+/********************************************************************
+ * gnc-slots-sql.h: load and save data to SQL                       *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-slots-sql.h
+ *  @brief load and save accounts data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_SLOTS_SQL_H_
+#define GNC_SLOTS_SQL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+void gnc_sql_slots_save( GncSqlBackend* be, const GUID* guid, KvpFrame* pFrame );
+void gnc_sql_slots_delete( GncSqlBackend* be, const GUID* guid );
+void gnc_sql_slots_load( GncSqlBackend* be, QofInstance* inst );
+void gnc_sql_slots_load_for_list( GncSqlBackend* be, GList* list );
+
+void gnc_sql_init_slots_handler( void );
+
+#endif /* GNC_SLOTS_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-transaction-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-transaction-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-transaction-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,730 @@
+/********************************************************************
+ * gnc-transaction-sql.c: load and save data to SQL                 *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-transaction-sql.c
+ *  @brief load and save data to SQL 
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL db
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "qof.h"
+#include "qofquery-p.h"
+#include "qofquerycore-p.h"
+
+#include "Account.h"
+#include "Transaction.h"
+#include "engine-helpers.h"
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-transaction-sql.h"
+#include "gnc-commodity.h"
+#include "gnc-commodity-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gnc-engine.h"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TRANSACTION_TABLE "transactions"
+#define TX_TABLE_VERSION 1
+#define SPLIT_TABLE "splits"
+#define SPLIT_TABLE_VERSION 1
+
+typedef struct {
+    GncSqlBackend* be;
+    const GUID* guid;
+} split_info_t;
+
+#define TX_MAX_NUM_LEN 2048
+#define TX_MAX_DESCRIPTION_LEN 2048
+
+static const col_cvt_t tx_col_table[] =
+{
+    { "guid",          CT_GUID,           0,                      COL_NNUL|COL_PKEY, "guid" },
+    { "currency_guid", CT_COMMODITYREF,   0,                      COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)xaccTransGetCurrency, (QofSetterFunc)xaccTransSetCurrency },
+    { "num",           CT_STRING,         TX_MAX_NUM_LEN,         COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)xaccTransGetNum, (QofSetterFunc)xaccTransSetNum },
+    { "post_date",     CT_TIMESPEC,       0,                      COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)xaccTransRetDatePostedTS, (QofSetterFunc)gnc_transaction_set_date_posted },
+    { "enter_date",    CT_TIMESPEC,       0,                      COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)xaccTransRetDateEnteredTS, (QofSetterFunc)gnc_transaction_set_date_entered },
+    { "description",   CT_STRING,         TX_MAX_DESCRIPTION_LEN, 0,                 NULL, NULL,
+            (QofAccessFunc)xaccTransGetDescription, (QofSetterFunc)xaccTransSetDescription },
+    { NULL }
+};
+
+static gpointer get_split_reconcile_state( gpointer pObject, const QofParam* param );
+static void set_split_reconcile_state( gpointer pObject, gpointer pValue );
+static void set_split_reconcile_date( gpointer pObject, Timespec ts );
+
+#define SPLIT_MAX_MEMO_LEN 2048
+#define SPLIT_MAX_ACTION_LEN 2048
+
+static const col_cvt_t split_col_table[] =
+{
+    { "guid",            CT_GUID,         0,                    COL_NNUL|COL_PKEY, "guid" },
+    { "tx_guid",         CT_TXREF,        0,                    COL_NNUL,          NULL, SPLIT_TRANS },
+    { "account_guid",    CT_ACCOUNTREF,   0,                    COL_NNUL,          NULL, SPLIT_ACCOUNT },
+    { "memo",            CT_STRING,       SPLIT_MAX_MEMO_LEN,   COL_NNUL,          NULL, SPLIT_MEMO },
+    { "action",          CT_STRING,       SPLIT_MAX_ACTION_LEN, COL_NNUL,          NULL, SPLIT_ACTION },
+    { "reconcile_state", CT_STRING,       1,                    COL_NNUL,          NULL, NULL,
+			get_split_reconcile_state, set_split_reconcile_state },
+    { "reconcile_date",  CT_TIMESPEC,     0,                    COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)xaccSplitRetDateReconciledTS, (QofSetterFunc)set_split_reconcile_date },
+    { "value",           CT_NUMERIC,      0,                    COL_NNUL,          NULL, SPLIT_VALUE },
+    { "quantity",        CT_NUMERIC,      0,                    COL_NNUL,          NULL, SPLIT_AMOUNT },
+    { NULL }
+};
+
+static const col_cvt_t guid_col_table[] =
+{
+    { "tx_guid", CT_GUID, 0, 0, "guid" },
+    { NULL }
+};
+
+static void retrieve_numeric_value( gpointer pObject, gnc_numeric value );
+
+/* ================================================================= */
+
+static gpointer
+get_split_reconcile_state( gpointer pObject, const QofParam* param )
+{
+    const Split* pSplit = GNC_SPLIT(pObject);
+    static gchar c[2];
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( GNC_IS_SPLIT(pObject), NULL );
+
+    c[0] = xaccSplitGetReconcile( pSplit );
+    c[1] = '\0';
+    return (gpointer)c;
+}
+
+static void 
+set_split_reconcile_state( gpointer pObject, gpointer pValue )
+{
+    Split* pSplit = GNC_SPLIT(pObject);
+    const gchar* s = (const gchar*)pValue;
+
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( GNC_IS_SPLIT(pObject) );
+	g_return_if_fail( pValue != NULL );
+
+    xaccSplitSetReconcile( pSplit, s[0] );
+}
+
+static void 
+set_split_reconcile_date( gpointer pObject, Timespec ts )
+{
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( GNC_IS_SPLIT(pObject) );
+
+    xaccSplitSetDateReconciledTS( GNC_SPLIT(pObject), &ts );
+}
+
+static void 
+retrieve_numeric_value( gpointer pObject, gnc_numeric value )
+{
+    gnc_numeric* pResult = (gnc_numeric*)pObject;
+
+	g_return_if_fail( pObject != NULL );
+
+    *pResult = value;
+}
+
+
+// Table to retrieve just the quantity
+static col_cvt_t quantity_table[] =
+{
+    { "quantity", CT_NUMERIC, 0, COL_NNUL, NULL, NULL, NULL, (QofSetterFunc)retrieve_numeric_value },
+    { NULL }
+};
+
+static gnc_numeric
+get_gnc_numeric_from_row( GncSqlBackend* be, GncSqlRow* row )
+{
+	gnc_numeric val = gnc_numeric_zero();
+
+	g_return_val_if_fail( be != NULL, val );
+	g_return_val_if_fail( row != NULL, val );
+
+    gnc_sql_load_object( be, row, NULL, &val, quantity_table );
+
+    return val;
+}
+
+static void
+load_single_split( GncSqlBackend* be, GncSqlRow* row, GList** pList )
+{
+    const GUID* guid;
+    GUID split_guid;
+	Split* pSplit;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    split_guid = *guid;
+
+    pSplit = xaccSplitLookup( &split_guid, be->primary_book );
+    if( pSplit == NULL ) {
+        pSplit = xaccMallocSplit( be->primary_book );
+    }
+
+    /* If the split is dirty, don't overwrite it */
+    if( !qof_instance_is_dirty( QOF_INSTANCE(pSplit) ) ) {
+    	gnc_sql_load_object( be, row, GNC_ID_SPLIT, pSplit, split_col_table );
+		*pList = g_list_append( *pList, pSplit );
+	}
+
+    g_assert( pSplit == xaccSplitLookup( &split_guid, be->primary_book ) );
+}
+
+static void
+load_all_splits_for_tx( GncSqlBackend* be, const GUID* tx_guid )
+{
+    GncSqlResult* result;
+    gchar guid_buf[GUID_ENCODING_LENGTH+1];
+    GncSqlStatement* stmt;
+    GValue value;
+	gchar* buf;
+	GError* error = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( tx_guid != NULL );
+
+    guid_to_string_buff( tx_guid, guid_buf );
+    memset( &value, 0, sizeof( GValue ) );
+    g_value_init( &value, G_TYPE_STRING );
+    g_value_set_string( &value, guid_buf );
+
+	buf = g_strdup_printf( "SELECT * FROM %s WHERE tx_guid='%s'", SPLIT_TABLE, guid_buf );
+	stmt = gnc_sql_create_statement_from_sql( be, buf );
+	g_free( buf );
+
+    result = gnc_sql_execute_select_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        int r;
+		GList* list = NULL;
+		GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_split( be, row, &list );
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+
+		if( list != NULL ) {
+			gnc_sql_slots_load_for_list( be, list );
+		}
+    }
+}
+
+static void
+load_splits_for_tx_list( GncSqlBackend* be, GList* list )
+{
+	GString* sql;
+	QofCollection* col;
+	GncSqlResult* result;
+	gboolean first_guid = TRUE;
+
+	g_return_if_fail( be != NULL );
+
+	if( list == NULL ) return;
+
+	sql = g_string_sized_new( 40+(GUID_ENCODING_LENGTH+3)*g_list_length( list ) );
+	g_string_append_printf( sql, "SELECT * FROM %s WHERE %s IN (", SPLIT_TABLE, guid_col_table[0].col_name );
+	(void)gnc_sql_append_guid_list_to_sql( sql, list, G_MAXUINT );
+	g_string_append( sql, ")" );
+
+	// Execute the query and load the splits
+	result = gnc_sql_execute_select_sql( be, sql->str );
+    if( result != NULL ) {
+		GList* list = NULL;
+		GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_split( be, row, &list );
+			row = gnc_sql_result_get_next_row( result );
+        }
+
+		if( list != NULL ) {
+			gnc_sql_slots_load_for_list( be, list );
+		}
+
+		gnc_sql_result_dispose( result );
+    }
+	g_string_free( sql, TRUE );
+}
+
+static void
+load_single_tx( GncSqlBackend* be, GncSqlRow* row, GList** pList )
+{
+    const GUID* guid;
+    GUID tx_guid;
+	Transaction* pTx;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    tx_guid = *guid;
+
+    pTx = xaccTransLookup( &tx_guid, be->primary_book );
+    if( pTx == NULL ) {
+        pTx = xaccMallocTransaction( be->primary_book );
+    }
+    xaccTransBeginEdit( pTx );
+    gnc_sql_load_object( be, row, GNC_ID_TRANS, pTx, tx_col_table );
+	*pList = g_list_append( *pList, pTx );
+
+    g_assert( pTx == xaccTransLookup( &tx_guid, be->primary_book ) );
+}
+
+static void
+query_transactions( GncSqlBackend* be, GncSqlStatement* stmt )
+{
+    GncSqlResult* result;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( stmt != NULL );
+
+    result = gnc_sql_execute_select_statement( be, stmt );
+    if( result != NULL ) {
+		GList* tx_list = NULL;
+		GList* node;
+		GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_tx( be, row, &tx_list );
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+
+		if( tx_list != NULL ) {
+			gnc_sql_slots_load_for_list( be, tx_list );
+			load_splits_for_tx_list( be, tx_list );
+		}
+
+		// Commit all of the transactions
+		for( node = tx_list; node != NULL; node = node->next ) {
+			Transaction* pTx = GNC_TRANSACTION(node->data);
+    		qof_instance_mark_clean( QOF_INSTANCE(pTx) );
+    		xaccTransCommitEdit( pTx );
+		}
+    }
+}
+
+static void
+load_tx_by_guid( GncSqlBackend* be, GUID* tx_guid )
+{
+    GncSqlStatement* stmt;
+	gchar* sql;
+    gchar guid_buf[GUID_ENCODING_LENGTH+1];
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( tx_guid != NULL );
+
+    guid_to_string_buff( tx_guid, guid_buf );
+	sql = g_strdup_printf( "SELECT * FROM %s WHERE guid = %s", TRANSACTION_TABLE, guid_buf );
+	stmt = gnc_sql_create_statement_from_sql( be, sql );
+	query_transactions( be, stmt );
+}
+
+/* ================================================================= */
+static void
+load_all_tx( GncSqlBackend* be )
+{
+	gchar* sql;
+	GncSqlStatement* stmt;
+
+	g_return_if_fail( be != NULL );
+
+	sql = g_strdup_printf( "SELECT * FROM %s", TRANSACTION_TABLE );
+	stmt = gnc_sql_create_statement_from_sql( be, sql );
+	g_free( sql );
+	query_transactions( be, stmt );
+}
+
+/* ================================================================= */
+static void
+create_transaction_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TRANSACTION_TABLE );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TRANSACTION_TABLE, TX_TABLE_VERSION, tx_col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+
+	version = gnc_sql_get_table_version( be, SPLIT_TABLE );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, SPLIT_TABLE, SPLIT_TABLE_VERSION, split_col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+/* ================================================================= */
+static void
+delete_split_slots_cb( gpointer data, gpointer user_data )
+{
+    split_info_t* split_info = (split_info_t*)user_data;
+    Split* pSplit = GNC_SPLIT(data);
+
+	g_return_if_fail( data != NULL );
+	g_return_if_fail( GNC_IS_SPLIT(data) );
+	g_return_if_fail( user_data != NULL );
+
+    gnc_sql_slots_delete( split_info->be,
+                    qof_instance_get_guid( QOF_INSTANCE(pSplit) ) );
+}
+
+static void
+delete_splits( GncSqlBackend* be, Transaction* pTx )
+{
+    split_info_t split_info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( pTx != NULL );
+
+    (void)gnc_sql_do_db_operation( be, OP_DB_DELETE, SPLIT_TABLE,
+                                SPLIT_TABLE, pTx, guid_col_table );
+    split_info.be = be;
+
+    g_list_foreach( xaccTransGetSplitList( pTx ), delete_split_slots_cb, &split_info );
+}
+
+static void
+commit_split( QofInstance* inst, GncSqlBackend* be )
+{
+	gint op;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( be != NULL );
+
+	if( qof_instance_get_destroying( inst ) ) {
+		op = OP_DB_DELETE;
+	} else if( be->is_pristine_db ) {
+		op = OP_DB_ADD;
+	} else {
+		op = OP_DB_ADD_OR_UPDATE;
+	}
+    (void)gnc_sql_do_db_operation( be, op, SPLIT_TABLE, GNC_ID_SPLIT, inst, split_col_table );
+    gnc_sql_slots_save( be,
+                        qof_instance_get_guid( inst ),
+                        qof_instance_get_slots( inst ) );
+}
+
+static void
+save_split_cb( gpointer data, gpointer user_data )
+{
+    split_info_t* split_info = (split_info_t*)user_data;
+    Split* pSplit = GNC_SPLIT(data);
+
+	g_return_if_fail( data != NULL );
+	g_return_if_fail( GNC_IS_SPLIT(data) );
+	g_return_if_fail( user_data != NULL );
+
+    commit_split( QOF_INSTANCE(pSplit), split_info->be );
+}
+
+static void
+save_splits( GncSqlBackend* be, const GUID* tx_guid, SplitList* pSplitList )
+{
+    split_info_t split_info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( tx_guid != NULL );
+	g_return_if_fail( pSplitList != NULL );
+
+    split_info.be = be;
+    split_info.guid = tx_guid;
+    g_list_foreach( pSplitList, save_split_cb, &split_info );
+}
+
+void
+gnc_sql_save_transaction( QofInstance* inst, GncSqlBackend* be )
+{
+    Transaction* pTx = GNC_TRANS(inst);
+    const GUID* guid;
+	gint op;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_TRANS(inst) );
+	g_return_if_fail( be != NULL );
+
+    // Ensure the commodity is in the db
+    gnc_sql_save_commodity( be, xaccTransGetCurrency( pTx ) );
+
+	if( qof_instance_get_destroying( inst ) ) {
+		op = OP_DB_DELETE;
+	} else if( be->is_pristine_db ) {
+		op = OP_DB_ADD;
+	} else {
+		op = OP_DB_ADD_OR_UPDATE;
+	}
+
+    (void)gnc_sql_do_db_operation( be, op, TRANSACTION_TABLE, GNC_ID_TRANS, pTx, tx_col_table );
+
+    guid = qof_instance_get_guid( inst );
+
+    // Delete any old slots and splits for this transaction
+	if( !be->is_pristine_db ) {
+    	delete_splits( be, pTx );
+	}
+
+    if( !qof_instance_get_destroying(inst) ) {
+        SplitList* splits;
+
+        // Now, commit any slots and splits
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+        splits = xaccTransGetSplitList( pTx );
+        save_splits( be, guid, splits );
+
+        /* Mark the splits as clean */
+        splits = xaccTransGetSplitList( pTx );
+        for( ; splits != NULL; splits = splits->next ) {
+            QofInstance* inst = QOF_INSTANCE(splits->data);
+
+            qof_instance_mark_clean(inst);
+        }
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+void
+gnc_sql_transaction_commit_splits( GncSqlBackend* be, Transaction* pTx )
+{
+    SplitList* splits;
+    Split* s;
+    QofBackend* qbe = (QofBackend*)be;
+    
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( pTx != NULL );
+
+    splits = xaccTransGetSplitList( pTx );
+    for( ; splits != NULL; splits = splits->next ) {
+        s = GNC_SPLIT(splits->data);
+
+        qbe->commit( qbe, QOF_INSTANCE(s) );
+    }
+}
+
+/* ================================================================= */
+static const GUID*
+get_guid_from_query( QofQuery* pQuery )
+{
+    GList* pOrTerms;
+    GList* pAndTerms;
+    GList* andTerm;
+    QofQueryTerm* pTerm;
+    QofQueryPredData* pPredData;
+    GSList* pParamPath;
+
+	g_return_val_if_fail( pQuery != NULL, NULL );
+
+    pOrTerms = qof_query_get_terms( pQuery );
+    pAndTerms = (GList*)pOrTerms->data;
+    andTerm = pAndTerms->next;
+    pTerm = (QofQueryTerm*)andTerm->data;
+
+    pPredData = qof_query_term_get_pred_data( pTerm );
+    pParamPath = qof_query_term_get_param_path( pTerm );
+
+    if( strcmp( pPredData->type_name, "guid" ) == 0 ) {
+        query_guid_t pData = (query_guid_t)pPredData;
+        return pData->guids->data;
+    } else {
+        return NULL;
+    }
+}
+
+static gpointer
+compile_split_query( GncSqlBackend* be, QofQuery* pQuery )
+{
+	GString* sql;
+    const GUID* acct_guid;
+    gchar guid_buf[GUID_ENCODING_LENGTH+1];
+	GncSqlResult* result;
+	gchar* buf;
+
+	g_return_val_if_fail( be != NULL, NULL );
+	g_return_val_if_fail( pQuery != NULL, NULL );
+
+    acct_guid = get_guid_from_query( pQuery );
+    guid_to_string_buff( acct_guid, guid_buf );
+	sql = g_string_new( "" );
+	g_string_printf( sql, "SELECT DISTINCT tx_guid FROM %s WHERE account_guid='%s'", SPLIT_TABLE, guid_buf );
+	result = gnc_sql_execute_select_sql( be, sql->str );
+    if( result != NULL ) {
+        int numRows;
+        int r;
+		GncSqlRow* row;
+
+		numRows = gnc_sql_result_get_num_rows( result );
+		sql = g_string_sized_new( 40+(GUID_ENCODING_LENGTH+3)*numRows );
+
+		if( numRows != 1 ) {
+			g_string_printf( sql, "SELECT * FROM %s WHERE guid IN (", TRANSACTION_TABLE );
+		} else {
+			g_string_printf( sql, "SELECT * FROM %s WHERE guid =", TRANSACTION_TABLE );
+		}
+
+		row = gnc_sql_result_get_first_row( result );
+        for( r = 0; row != NULL; r++ ) {
+			const GUID* guid;
+
+			guid = gnc_sql_load_tx_guid( be, row );
+    		guid_to_string_buff( guid, guid_buf );
+			if( r != 0 ) {
+				g_string_append( sql, "," );
+			}
+			g_string_append( sql, "'" );
+			g_string_append( sql, guid_buf );
+			g_string_append( sql, "'" );
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+
+		if( numRows != 1 ) {
+			g_string_append( sql, ")" );
+		}
+    }
+
+	buf = sql->str;
+	g_string_free( sql, FALSE );
+	return buf;
+}
+
+static void
+run_split_query( GncSqlBackend* be, gpointer pQuery )
+{
+    GncSqlStatement* stmt;
+    const gchar* sql;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( pQuery != NULL );
+
+    sql = (const gchar*)pQuery;
+
+	stmt = gnc_sql_create_statement_from_sql( be, sql );
+    query_transactions( be, stmt );
+}
+
+static void
+free_split_query( GncSqlBackend* be, gpointer pQuery )
+{
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( pQuery != NULL );
+
+    g_free( pQuery );
+}
+
+/* ----------------------------------------------------------------- */
+static void
+load_tx_guid( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GUID guid;
+    const GUID* pGuid;
+	Transaction* tx = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        pGuid = NULL;
+    } else {
+        string_to_guid( g_value_get_string( val ), &guid );
+        pGuid = &guid;
+    }
+	if( pGuid != NULL ) {
+		tx = xaccTransLookup( pGuid, be->primary_book );
+	}
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, tx, NULL );
+    } else {
+		(*setter)( pObject, (const gpointer)tx );
+    }
+}
+
+static col_type_handler_t tx_guid_handler
+	= { load_tx_guid,
+		gnc_sql_add_objectref_guid_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+        gnc_sql_add_gvalue_objectref_guid_to_slist };
+/* ================================================================= */
+void
+gnc_sql_init_transaction_handler( void )
+{
+    static GncSqlDataType_t be_data_tx =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_TRANS,
+        gnc_sql_save_transaction,    /* commit */
+        load_all_tx,                 /* initial_load */
+        create_transaction_tables    /* create tables */
+    };
+    static GncSqlDataType_t be_data_split =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_SPLIT,
+        commit_split,                /* commit */
+        NULL,                        /* initial_load */
+        NULL,                        /* create tables */
+        compile_split_query,
+        run_split_query,
+        free_split_query
+    };
+
+    qof_object_register_backend( GNC_ID_TRANS, GNC_SQL_BACKEND, &be_data_tx );
+    qof_object_register_backend( GNC_ID_SPLIT, GNC_SQL_BACKEND, &be_data_split );
+
+	gnc_sql_register_col_type_handler( CT_TXREF, &tx_guid_handler );
+}
+
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/backend/sql/gnc-transaction-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gnc-transaction-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gnc-transaction-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,51 @@
+/********************************************************************
+ * gnc-transaction-sql.h: load and save data to SQL                 *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+\********************************************************************/
+/** @file gnc-transaction-sql.h
+ *  @brief load and save data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_TRANSACTION_SQL_H_
+#define GNC_TRANSACTION_SQL_H_
+
+#include "qof.h"
+#include <gmodule.h>
+
+void gnc_sql_init_transaction_handler( void );
+void gnc_sql_transaction_commit_splits( GncSqlBackend* be, Transaction* pTx );
+void gnc_sql_save_transaction( QofInstance* inst, GncSqlBackend* be );
+void gnc_sql_get_account_balances( GncSqlBackend* be, Account* pAccount, 
+								    gnc_numeric* start_balance,
+								    gnc_numeric* cleared_balance,
+									gnc_numeric* reconciled_balance );
+
+typedef struct {
+	Account* acct;
+	gnc_numeric start_balance;
+	gnc_numeric cleared_balance;
+	gnc_numeric reconciled_balance;
+} acct_balances_t;
+GList* gnc_sql_get_account_balances_for_list( GncSqlBackend* be, GList* list );
+
+#endif /* GNC_TRANSACTION_SQL_H_ */

Added: gnucash/branches/gda-dev2/src/backend/sql/gncmod-backend-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/gncmod-backend-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/gncmod-backend-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,63 @@
+/*********************************************************************
+ * gncmod-backend-gda.c
+ * module definition/initialization for the gda backend module
+ *
+ * Copyright (c) 2001 Linux Developers Group, Inc.
+ *********************************************************************/
+
+#include <stdio.h>
+#include <gmodule.h>
+/* #include <glib-gobject.h> */
+
+#include "gnc-module.h"
+#include "gnc-module-api.h"
+
+/* version of the gnc module system interface we require */
+int gnc_module_system_interface = 0;
+
+/* module versioning uses libtool semantics. */
+int gnc_module_current  = 0;
+int gnc_module_revision = 0;
+int gnc_module_age      = 0;
+
+static GNCModule engine;
+
+gchar *
+gnc_module_path(void)
+{
+  return g_strdup("gnucash/backend/gda");
+}
+
+gchar *
+gnc_module_description(void)
+{
+  return g_strdup("The GDA/SQL backend for GnuCash");
+}
+
+int
+gnc_module_init(int refcount)
+{
+  engine = gnc_module_load("gnucash/engine", 0);
+  if(!engine) return FALSE;
+
+  /* Need to initialize g-type engine for gconf */
+  if (refcount == 0) {
+    g_type_init();
+  }
+
+  return TRUE;
+}
+
+int
+gnc_module_end(int refcount)
+{
+  int unload = TRUE;
+
+  if (engine)
+    unload = gnc_module_unload(engine);
+
+  if (refcount == 0)
+    engine = NULL;
+
+  return unload;
+}


Property changes on: gnucash/branches/gda-dev2/src/backend/sql/test
___________________________________________________________________
Name: svn:ignore
   + Makefile
Makefile.in
.deps



Added: gnucash/branches/gda-dev2/src/backend/sql/test/Makefile.am
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/test/Makefile.am	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/test/Makefile.am	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,48 @@
+SUBDIRS = .
+
+TESTS = \
+  test-load-backend
+
+GNC_TEST_DEPS := \
+  --gnc-module-dir ${top_builddir}/src/engine \
+  --guile-load-dir ${top_builddir}/src/engine \
+  --library-dir    ${top_builddir}/lib/libqof/qof \
+  --library-dir    ${top_builddir}/src/core-utils \
+  --library-dir    ${top_builddir}/src/gnc-module \
+  --library-dir    ${top_builddir}/src/engine \
+  --library-dir    ${top_builddir}/src/backend/gda
+
+TESTS_ENVIRONMENT := \
+  GNC_ACCOUNT_PATH=${top_srcdir}/accounts/C \
+  SRCDIR=${srcdir} \
+  $(shell ${top_srcdir}/src/gnc-test-env --no-exports ${GNC_TEST_DEPS})
+
+check_PROGRAMS = \
+  test-load-backend
+
+#noinst_HEADERS = test-file-stuff.h
+
+LDADD = ${top_builddir}/src/test-core/libgncmod-test.la \
+        ${top_builddir}/src/gnc-module/libgnc-module.la \
+        ${top_builddir}/src/engine/libgncmod-engine.la \
+        ${top_builddir}/src/engine/test-core/libgncmod-test-engine.la \
+        ${top_builddir}/src/core-utils/libgnc-core-utils.la \
+        ${QOF_LIBS} \
+        ${top_builddir}/lib/libc/libc-missing.la
+
+AM_CFLAGS = \
+  -I${top_srcdir}/lib/libc \
+  -I${top_srcdir}/src \
+  -I${top_srcdir}/src/core-utils \
+  -I${top_srcdir}/src/gnc-module \
+  -I${top_srcdir}/src/test-core \
+  -I${top_srcdir}/src/engine \
+  -I${top_srcdir}/src/engine/test-core \
+  -I${top_srcdir}/src/backend/gda \
+  -I${top_srcdir}/src/backend/qsf \
+  ${GLIB_CFLAGS} \
+  ${QOF_CFLAGS} \
+  ${GUILE_INCS} \
+  ${GCONF_CFLAGS}
+
+INCLUDES = -DG_LOG_DOMAIN=\"gnc.backend.gda\"

Added: gnucash/branches/gda-dev2/src/backend/sql/test/test-load-backend.c
===================================================================
--- gnucash/branches/gda-dev2/src/backend/sql/test/test-load-backend.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/backend/sql/test/test-load-backend.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,45 @@
+/***************************************************************************
+ *            test-load-backend.c
+ *
+ *  Replaces the guile version to test the GModule file backend loading.
+ *
+ *  Sun Oct  9 18:58:47 2005
+ *  Copyright  2005  Neil Williams
+ *  linux at codehelp.co.uk
+ ****************************************************************************/
+
+/*
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ *  02110-1301, USA.
+ */
+ 
+#include "config.h"
+#include "qof.h"
+#include "cashobjects.h"
+#include "test-stuff.h"
+
+#define GNC_LIB_NAME "gncmod-backend-gda"
+
+int main (int argc, char ** argv)
+{
+    qof_init();
+    cashobjects_register();
+    do_test(
+        qof_load_backend_library ("../.libs/", GNC_LIB_NAME),
+        " loading gnc-backend-gda GModule failed");
+    print_test_results();
+    qof_close();
+    exit(get_rv());
+}

Modified: gnucash/branches/gda-dev2/src/business/business-core/Makefile.am
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/Makefile.am	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/business/business-core/Makefile.am	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,4 +1,4 @@
-SUBDIRS = . gda test file
+SUBDIRS = . sql test file
 
 pkglib_LTLIBRARIES = libgncmod-business-core.la
 

Copied: gnucash/branches/gda-dev2/src/business/business-core/sql (from rev 17171, gnucash/branches/gda-dev2/src/business/business-core/gda)

Modified: gnucash/branches/gda-dev2/src/business/business-core/sql/Makefile.am
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/Makefile.am	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/Makefile.am	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,11 +1,11 @@
 SUBDIRS = .
 
-pkglib_LTLIBRARIES = libgncmod-business-backend-gda.la
+pkglib_LTLIBRARIES = libgncmod-business-backend-sql.la
 
 AM_CFLAGS = \
   -I${top_srcdir}/src \
   -I${top_srcdir}/src/backend \
-  -I${top_srcdir}/src/backend/gda \
+  -I${top_srcdir}/src/backend/sql \
   -I${top_srcdir}/src/engine \
   -I${top_srcdir}/src/gnc-module \
   -I${top_srcdir}/src/business/business-core \
@@ -13,42 +13,42 @@
   ${GLIB_CFLAGS} \
   ${LIBGDA_CFLAGS}
 
-libgncmod_business_backend_gda_la_SOURCES = \
-  gncmod-business-backend-gda.c \
-  gnc-address-gda.c \
-  gnc-bill-term-gda.c \
-  gnc-customer-gda.c \
-  gnc-employee-gda.c \
-  gnc-entry-gda.c \
-  gnc-invoice-gda.c \
-  gnc-job-gda.c \
-  gnc-order-gda.c \
-  gnc-owner-gda.c \
-  gnc-tax-table-gda.c \
-  gnc-vendor-gda.c
+libgncmod_business_backend_sql_la_SOURCES = \
+  gncmod-business-backend-sql.c \
+  gnc-address-sql.c \
+  gnc-bill-term-sql.c \
+  gnc-customer-sql.c \
+  gnc-employee-sql.c \
+  gnc-entry-sql.c \
+  gnc-invoice-sql.c \
+  gnc-job-sql.c \
+  gnc-order-sql.c \
+  gnc-owner-sql.c \
+  gnc-tax-table-sql.c \
+  gnc-vendor-sql.c
 
 noinst_HEADERS = \
-  gnc-address-gda.h \
-  gnc-bill-term-gda.h \
-  gnc-customer-gda.h \
-  gnc-employee-gda.h \
-  gnc-entry-gda.h \
-  gnc-invoice-gda.h \
-  gnc-job-gda.h \
-  gnc-order-gda.h \
-  gnc-owner-gda.h \
-  gnc-tax-table-gda.h \
-  gnc-vendor-gda.h
+  gnc-address-sql.h \
+  gnc-bill-term-sql.h \
+  gnc-customer-sql.h \
+  gnc-employee-sql.h \
+  gnc-entry-sql.h \
+  gnc-invoice-sql.h \
+  gnc-job-sql.h \
+  gnc-order-sql.h \
+  gnc-owner-sql.h \
+  gnc-tax-table-sql.h \
+  gnc-vendor-sql.h
 
-libgncmod_business_backend_gda_la_LDFLAGS = -module -avoid-version
+libgncmod_business_backend_sql_la_LDFLAGS = -module -avoid-version
 
-libgncmod_business_backend_gda_la_LIBADD = \
+libgncmod_business_backend_sql_la_LIBADD = \
   ${top_builddir}/src/business/business-core/libgncmod-business-core.la \
-  ${top_builddir}/src/backend/gda/libgnc-backend-gda-utils.la \
+  ${top_builddir}/src/backend/sql/libgnc-backend-utils-sql.la \
   ${top_builddir}/src/engine/libgncmod-engine.la \
   ${top_builddir}/src/gnc-module/libgnc-module.la \
   ${QOF_LIBS} \
   ${GLIB_LIBS} \
   ${LIBGDA_LIBS}
 
-INCLUDES = -DG_LOG_DOMAIN=\"gnc.backend.gda\"
+INCLUDES = -DG_LOG_DOMAIN=\"gnc.backend.sql\"

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-address-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,255 +0,0 @@
-/********************************************************************\
- * gnc-address-gda.c -- address gda backend implementation          *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-/** @file gnc-address-gda.c
- *  @brief load and save address data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <libgda/libgda.h>
-
-#include "gnc-engine.h"
-#include "gnc-address-gda.h"
-
-#include "gnc-backend-util-gda.h"
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define ADDRESS_MAX_NAME_LEN 1024
-#define ADDRESS_MAX_ADDRESS_LINE_LEN 1024
-#define ADDRESS_MAX_PHONE_LEN 128
-#define ADDRESS_MAX_FAX_LEN 128
-#define ADDRESS_MAX_EMAIL_LEN 256
-
-static col_cvt_t col_table[] =
-{
-	{ "name",  CT_STRING, ADDRESS_MAX_NAME_LEN,         COL_NNUL, NULL, ADDRESS_NAME },
-	{ "addr1", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, NULL, ADDRESS_ONE },
-	{ "addr2", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, NULL, ADDRESS_TWO },
-	{ "addr3", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, NULL, ADDRESS_THREE },
-	{ "addr4", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, NULL, ADDRESS_FOUR },
-	{ "phone", CT_STRING, ADDRESS_MAX_PHONE_LEN,        COL_NNUL, NULL, ADDRESS_PHONE },
-	{ "fax",   CT_STRING, ADDRESS_MAX_FAX_LEN,          COL_NNUL, NULL, ADDRESS_FAX },
-	{ "email", CT_STRING, ADDRESS_MAX_EMAIL_LEN,        COL_NNUL, NULL, ADDRESS_EMAIL },
-	{ NULL }
-};
-
-typedef void (*AddressSetterFunc)( gpointer, GncAddress* );
-typedef GncAddress* (*AddressGetterFunc)( const gpointer );
-
-static void
-load_address( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    gchar* buf;
-    GncAddress* addr;
-	AddressSetterFunc a_setter = (AddressSetterFunc)setter;
-	const col_cvt_t* subtable;
-	const gchar* s;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    addr = gncAddressCreate( be->primary_book, NULL );
-	for( subtable = col_table; subtable->col_name != NULL; subtable++ ) {
-    	buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable->col_name );
-    	val = gda_data_model_get_value_at_col_name( pModel, buf, row );
-    	g_free( buf );
-    	if( gda_value_is_null( val ) ) {
-        	s = NULL;
-    	} else {
-        	s = g_value_get_string( val );
-    	}
-    	if( subtable->gobj_param_name != NULL ) {
-			g_object_set( addr, subtable->gobj_param_name, s, NULL );
-    	} else {
-	        if( subtable->param_name != NULL ) {
-            	setter = qof_class_get_parameter_setter( GNC_ID_ADDRESS, subtable->param_name );
-        	} else {
-            	setter = subtable->setter;
-        	}
-			(*setter)( addr, (const gpointer)s );
-    	}
-    }
-    (*a_setter)( pObject, addr );
-}
-
-static void
-get_gvalue_address( const GncGdaBackend* be, QofIdTypeConst obj_name, const gpointer pObject,
-                	const col_cvt_t* table_row, GValue* value )
-{
-    AddressGetterFunc getter;
-    GncAddress* addr;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-
-	getter = (AddressGetterFunc)gnc_gda_get_getter( obj_name, table_row );
-    addr = (*getter)( pObject );
-    g_value_init( value, gnc_address_get_type() );
-    g_value_set_object( value, addr );
-}
-
-static void
-get_gvalue_address_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                			const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-    GValue subfield_value;
-    GncAddress* addr;
-    gchar* s;
-    QofAccessFunc getter;
-	const col_cvt_t* subtable_row;
-	gchar* buf;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    memset( &value, 0, sizeof( GValue ) );
-    get_gvalue_address( be, obj_name, pObject, table_row, &value );
-
-    if( G_VALUE_TYPE(&value) != 0 ) {
-        addr = g_value_get_object( &value );
-		for( subtable_row = col_table; subtable_row->col_name != NULL; subtable_row++ ) {
-    		buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable_row->col_name );
-			
-    		memset( &subfield_value, 0, sizeof( GValue ) );
-			if( subtable_row->gobj_param_name != NULL ) {
-				g_object_get( addr, subtable_row->gobj_param_name, &s, NULL );
-			} else {
-    			getter = gnc_gda_get_getter( GNC_ID_ADDRESS, subtable_row );
-    			s = (gchar*)(*getter)( addr, NULL );
-			}
-    		if( s ) {
-        		g_value_init( &subfield_value, G_TYPE_STRING );
-        		g_value_set_string( &subfield_value, s );
-			}
-    		gnc_gda_add_field_to_query( query, buf, &subfield_value );
-			g_free( buf );
-		}
-    }
-}
-
-static GdaQueryCondition*
-get_gvalue_address_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-	GValue subfield_value;
-    GncAddress* addr;
-    gchar* buf;
-    GdaQueryCondition* sub_cond;
-    GdaQueryCondition* cond;
-	const col_cvt_t* subtable_row;
-	gchar* s;
-	QofAccessFunc getter;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    memset( &value, 0, sizeof( GValue ) );
-    get_gvalue_address( be, obj_name, pObject, table_row, &value );
-
-    cond = gda_query_condition_new( query, GDA_QUERY_CONDITION_NODE_AND );
-    if( G_VALUE_TYPE(&value) != 0 ) {
-        addr = g_value_get_object( &value );
-		for( subtable_row = col_table; subtable_row->col_name != NULL; subtable_row++ ) {
-    		buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable_row->col_name );
-			
-    		memset( &subfield_value, 0, sizeof( GValue ) );
-			if( subtable_row->gobj_param_name != NULL ) {
-				g_object_get( addr, subtable_row->gobj_param_name, &s, NULL );
-			} else {
-    			getter = gnc_gda_get_getter( GNC_ID_ADDRESS, subtable_row );
-    			s = (gchar*)(*getter)( addr, NULL );
-			}
-    		if( s ) {
-        		g_value_init( &subfield_value, G_TYPE_STRING );
-        		g_value_set_string( &subfield_value, s );
-			}
-    		sub_cond = gnc_gda_create_condition_from_field( query, buf, &subfield_value );
-    		gda_query_condition_node_add_child( cond, sub_cond, NULL );
-			g_free( buf );
-		}
-    }
-
-    return cond;
-}
-
-static void
-create_address_col( GdaServerProvider* server, GdaConnection* cnn,
-        	    GdaServerOperation* op, const col_cvt_t* table_row )
-{
-    const gchar* dbms_type;
-    gchar* buf;
-	const col_cvt_t* subtable_row;
-
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    dbms_type = gda_server_provider_get_default_dbms_type( server, cnn,
-                                                            G_TYPE_STRING );
-	for( subtable_row = col_table; subtable_row->col_name != NULL; subtable_row++ ) {
-    	buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable_row->col_name );
-    	gnc_gda_add_table_column( op, buf, dbms_type, subtable_row->size, subtable_row->flags );
-    	g_free( buf );
-    }
-}
-
-static col_type_handler_t address_handler =
-        { load_address, create_address_col,
-            get_gvalue_address_for_query, get_gvalue_address_cond };
-
-/* ================================================================= */
-void
-gnc_address_gda_initialize( void )
-{
-	gnc_gda_register_col_type_handler( CT_ADDRESS, &address_handler );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-address-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,38 +0,0 @@
-/* gnc-address-gda.h -- Address GDA header
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-address-gda.h
- *  @brief load and save address data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_ADDRESS_GDA_H
-#define GNC_ADDRESS_GDA_H
-
-#include "gncAddress.h"
-
-#define CT_ADDRESS "address"
-
-void gnc_address_gda_initialize( void );
-
-#endif /* GNC_ADDRESS_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,217 @@
+/********************************************************************\
+ * gnc-address-sql.c -- address sql backend implementation          *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-address-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-engine.h"
+#include "gnc-address-sql.h"
+
+#include "gnc-backend-util-sql.h"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define ADDRESS_MAX_NAME_LEN 1024
+#define ADDRESS_MAX_ADDRESS_LINE_LEN 1024
+#define ADDRESS_MAX_PHONE_LEN 128
+#define ADDRESS_MAX_FAX_LEN 128
+#define ADDRESS_MAX_EMAIL_LEN 256
+
+static col_cvt_t col_table[] =
+{
+	{ "name",  CT_STRING, ADDRESS_MAX_NAME_LEN,         COL_NNUL, NULL, ADDRESS_NAME },
+	{ "addr1", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, NULL, ADDRESS_ONE },
+	{ "addr2", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, NULL, ADDRESS_TWO },
+	{ "addr3", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, NULL, ADDRESS_THREE },
+	{ "addr4", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, NULL, ADDRESS_FOUR },
+	{ "phone", CT_STRING, ADDRESS_MAX_PHONE_LEN,        COL_NNUL, NULL, ADDRESS_PHONE },
+	{ "fax",   CT_STRING, ADDRESS_MAX_FAX_LEN,          COL_NNUL, NULL, ADDRESS_FAX },
+	{ "email", CT_STRING, ADDRESS_MAX_EMAIL_LEN,        COL_NNUL, NULL, ADDRESS_EMAIL },
+	{ NULL }
+};
+
+typedef void (*AddressSetterFunc)( gpointer, GncAddress* );
+typedef GncAddress* (*AddressGetterFunc)( const gpointer );
+
+static void
+load_address( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    gchar* buf;
+    GncAddress* addr;
+	AddressSetterFunc a_setter = (AddressSetterFunc)setter;
+	const col_cvt_t* subtable;
+	const gchar* s;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    addr = gncAddressCreate( be->primary_book, NULL );
+	for( subtable = col_table; subtable->col_name != NULL; subtable++ ) {
+    	buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable->col_name );
+    	val = gnc_sql_row_get_value_at_col_name( row, buf );
+    	g_free( buf );
+    	if( val == NULL ) {
+        	s = NULL;
+    	} else {
+        	s = g_value_get_string( val );
+    	}
+    	if( subtable->gobj_param_name != NULL ) {
+			g_object_set( addr, subtable->gobj_param_name, s, NULL );
+    	} else {
+	        if( subtable->param_name != NULL ) {
+            	setter = qof_class_get_parameter_setter( GNC_ID_ADDRESS, subtable->param_name );
+        	} else {
+            	setter = subtable->setter;
+        	}
+			(*setter)( addr, (const gpointer)s );
+    	}
+    }
+    (*a_setter)( pObject, addr );
+}
+
+static void
+add_address_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+	GncSqlColumnInfo* info;
+    gchar* buf;
+	const col_cvt_t* subtable_row;
+	const gchar* type;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+	for( subtable_row = col_table; subtable_row->col_name != NULL; subtable_row++ ) {
+    	buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable_row->col_name );
+		info = g_new0( GncSqlColumnInfo, 1 );
+		info->name = buf;
+		info->type_name = gnc_sql_connection_get_column_type_name( be->conn,
+											G_TYPE_STRING, table_row->size );
+		info->is_primary_key = (table_row->flags & COL_PKEY) ? TRUE : FALSE;
+		info->null_allowed = (table_row->flags & COL_NNUL) ? FALSE : TRUE;
+		*pList = g_list_append( *pList, info );
+	}
+}
+
+static void
+add_address_colname_to_list( const col_cvt_t* table_row, GList** pList )
+{
+	gnc_sql_add_subtable_colnames_to_list( table_row, col_table, pList );
+}
+
+static void
+get_gvalue_address( const GncSqlBackend* be, QofIdTypeConst obj_name, const gpointer pObject,
+                	const col_cvt_t* table_row, GValue* value )
+{
+    AddressGetterFunc getter;
+    GncAddress* addr;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( value != NULL );
+
+    memset( value, 0, sizeof( GValue ) );
+
+	getter = (AddressGetterFunc)gnc_sql_get_getter( obj_name, table_row );
+    addr = (*getter)( pObject );
+    g_value_init( value, gnc_address_get_type() );
+    g_value_set_object( value, addr );
+}
+
+static void
+add_gvalue_address_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+          			const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    GValue value;
+    GValue* subfield_value;
+    GncAddress* addr;
+    gchar* s;
+    QofAccessFunc getter;
+	const col_cvt_t* subtable_row;
+	gchar* buf;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    memset( &value, 0, sizeof( GValue ) );
+    get_gvalue_address( be, obj_name, pObject, table_row, &value );
+
+    if( G_VALUE_TYPE(&value) != 0 ) {
+        addr = g_value_get_object( &value );
+		for( subtable_row = col_table; subtable_row->col_name != NULL; subtable_row++ ) {
+    		buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable_row->col_name );
+			
+			subfield_value = g_new0( GValue, 1 );
+			if( subtable_row->gobj_param_name != NULL ) {
+				g_object_get( addr, subtable_row->gobj_param_name, &s, NULL );
+			} else {
+    			getter = gnc_sql_get_getter( GNC_ID_ADDRESS, subtable_row );
+    			s = (gchar*)(*getter)( addr, NULL );
+			}
+    		if( s ) {
+        		g_value_init( subfield_value, G_TYPE_STRING );
+        		g_value_take_string( subfield_value,
+									g_strdup_printf( "'%s'", s ) );
+			} else {
+				g_value_init( subfield_value, G_TYPE_STRING );
+				g_value_set_string( subfield_value, "NULL" );
+			}
+			(*pList) = g_slist_append( (*pList), subfield_value );
+			g_free( buf );
+		}
+    }
+}
+
+static col_type_handler_t address_handler
+	= { load_address,
+		add_address_col_info_to_list,
+		add_address_colname_to_list,
+		add_gvalue_address_to_slist };
+
+/* ================================================================= */
+void
+gnc_address_sql_initialize( void )
+{
+	gnc_sql_register_col_type_handler( CT_ADDRESS, &address_handler );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-address-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,38 @@
+/* gnc-address-sql.h -- Address SQL header
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-address-sql.h
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_ADDRESS_SQL_H
+#define GNC_ADDRESS_SQL_H
+
+#include "gncAddress.h"
+
+#define CT_ADDRESS "address"
+
+void gnc_address_sql_initialize( void );
+
+#endif /* GNC_ADDRESS_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-bill-term-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,235 +0,0 @@
-/********************************************************************\
- * gnc-bill-term-gda.c -- billing term gda backend                  *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <libgda/libgda.h>
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gncBillTermP.h"
-#include "gncInvoice.h"
-#include "gnc-bill-term-gda.h"
-#include "qof.h"
-
-#define _GNC_MOD_NAME	GNC_ID_BILLTERM
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define MAX_NAME_LEN 2048
-#define MAX_DESCRIPTION_LEN 2048
-#define MAX_TYPE_LEN 2048
-
-static void set_invisible( gpointer data, gboolean value );
-
-#define TABLE_NAME "billterms"
-
-static col_cvt_t col_table[] =
-{
-	{ "guid",         CT_GUID,        0,                   COL_NNUL|COL_PKEY, "guid" },
-	{ "name",         CT_STRING,      MAX_NAME_LEN,        COL_NNUL,          NULL, GNC_BILLTERM_NAME },
-	{ "description",  CT_STRING,      MAX_DESCRIPTION_LEN, COL_NNUL,          NULL, GNC_BILLTERM_DESC },
-	{ "refcount",     CT_INT,         0,                   COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)gncBillTermGetRefcount,  (QofSetterFunc)gncBillTermSetRefcount },
-	{ "invisible",    CT_BOOLEAN,     0,                   COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)gncBillTermGetInvisible, (QofSetterFunc)set_invisible },
-	{ "parent",       CT_BILLTERMREF, 0,                   0,                 NULL, NULL,
-			(QofAccessFunc)gncBillTermGetParent,    (QofSetterFunc)gncBillTermSetParent },
-	{ "child",        CT_BILLTERMREF, 0,                   0,                 NULL, NULL,
-			(QofAccessFunc)gncBillTermReturnChild,  (QofSetterFunc)gncBillTermSetChild },
-	{ "type",         CT_STRING,      MAX_TYPE_LEN,        COL_NNUL,          NULL, GNC_BILLTERM_TYPE },
-	{ "duedays",      CT_INT,         0,                   0,                 0,    GNC_BILLTERM_DUEDAYS },
-	{ "discountdays", CT_INT,         0,                   0,                 0,    GNC_BILLTERM_DISCDAYS },
-	{ "discount",     CT_NUMERIC,     0,                   0,                 0,    GNC_BILLTERM_DISCOUNT },
-	{ "cutoff",       CT_INT,         0,                   0,                 0,    GNC_BILLTERM_CUTOFF },
-	{ NULL }
-};
-
-static void
-set_invisible( gpointer data, gboolean value )
-{
-	GncBillTerm* term = GNC_BILLTERM(data);
-
-	g_return_if_fail( term != NULL );
-
-	if( value ) {
-		gncBillTermMakeInvisible( term );
-	}
-}
-
-static void
-load_single_billterm( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    const GUID* guid;
-    GUID v_guid;
-	GncBillTerm* pBillTerm;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    v_guid = *guid;
-
-    pBillTerm = gncBillTermLookup( be->primary_book, &v_guid );
-    if( pBillTerm == NULL ) {
-        pBillTerm = gncBillTermCreate( be->primary_book );
-    }
-    gnc_gda_load_object( be, pModel, row, GNC_ID_BILLTERM, pBillTerm, col_table );
-    gnc_gda_slots_load( be, QOF_INSTANCE( pBillTerm ) );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pBillTerm) );
-}
-
-static void
-load_all_billterms( GncGdaBackend* be )
-{
-    static GdaQuery* query = NULL;
-    GdaObject* ret;
-    QofBook* pBook;
-
-	g_return_if_fail( be != NULL );
-
-    pBook = be->primary_book;
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_billterm( be, pModel, r );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-write_billterms( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    qof_object_foreach( GNC_ID_BILLTERM, be->primary_book, (QofInstanceForeachCB)gnc_gda_save_billterm, (gpointer)be );
-}
-
-/* ================================================================= */
-static void
-create_billterm_tables( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    gnc_gda_create_table_if_needed( be, TABLE_NAME, col_table );
-}
-
-/* ================================================================= */
-void
-gnc_gda_save_billterm( QofInstance* inst, GncGdaBackend* be )
-{
-    const GUID* guid;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( !GNC_IS_BILLTERM(inst) );
-	g_return_if_fail( be != NULL );
-
-    (void)gnc_gda_do_db_operation( be,
-                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
-                        TABLE_NAME,
-                        GNC_ID_BILLTERM, inst,
-                        col_table );
-
-    // Now, commit or delete any slots
-    guid = qof_instance_get_guid( inst );
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-/* ================================================================= */
-static void
-load_billterm_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GUID guid;
-    const GUID* pGuid;
-	GncBillTerm* term = NULL;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        pGuid = NULL;
-    } else {
-        string_to_guid( g_value_get_string( val ), &guid );
-        pGuid = &guid;
-    }
-	if( pGuid != NULL ) {
-		term = gncBillTermLookup( be->primary_book, pGuid );
-	}
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, term, NULL );
-    } else {
-		(*setter)( pObject, (const gpointer)term );
-    }
-}
-
-static col_type_handler_t billterm_guid_handler =
-        { load_billterm_guid, gnc_gda_create_objectref_guid_col,
-            gnc_gda_get_gvalue_objectref_guid_for_query, gnc_gda_get_gvalue_objectref_guid_cond };
-/* ================================================================= */
-void
-gnc_billterm_gda_initialize( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_BILLTERM,
-        gnc_gda_save_billterm,				/* commit */
-        load_all_billterms,					/* initial_load */
-        create_billterm_tables,				/* create_tables */
-		NULL, NULL, NULL,
-		write_billterms						/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_BILLTERM, GNC_GDA_BACKEND, &be_data );
-
-	gnc_gda_register_col_type_handler( CT_BILLTERMREF, &billterm_guid_handler );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-bill-term-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,40 +0,0 @@
-/*
- * gnc-bill-term-gda.h -- billing term gda backend
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-bill-term-gda.h
- *  @brief load and save accounts data to SQL via libgda
- *  @author Copyright (c) 2006 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_BILLTERM_GDA_H
-#define GNC_BILLTERM_GDA_H
-
-#include "gncBillTerm.h"
-
-#define CT_BILLTERMREF "billterm"
-
-void gnc_billterm_gda_initialize( void );
-void gnc_gda_save_billterm( QofInstance* inst, GncGdaBackend* be );
-
-#endif /* GNC_BILLTERM_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,247 @@
+/********************************************************************\
+ * gnc-bill-term-sql.c -- billing term sql backend                  *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-bill-term-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gncBillTermP.h"
+#include "gncInvoice.h"
+#include "gnc-bill-term-sql.h"
+#include "qof.h"
+
+#define _GNC_MOD_NAME	GNC_ID_BILLTERM
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define MAX_NAME_LEN 2048
+#define MAX_DESCRIPTION_LEN 2048
+#define MAX_TYPE_LEN 2048
+
+static void set_invisible( gpointer data, gboolean value );
+
+#define TABLE_NAME "billterms"
+#define TABLE_VERSION 1
+
+static col_cvt_t col_table[] =
+{
+	{ "guid",         CT_GUID,        0,                   COL_NNUL|COL_PKEY, "guid" },
+	{ "name",         CT_STRING,      MAX_NAME_LEN,        COL_NNUL,          NULL, GNC_BILLTERM_NAME },
+	{ "description",  CT_STRING,      MAX_DESCRIPTION_LEN, COL_NNUL,          NULL, GNC_BILLTERM_DESC },
+	{ "refcount",     CT_INT,         0,                   COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)gncBillTermGetRefcount,  (QofSetterFunc)gncBillTermSetRefcount },
+	{ "invisible",    CT_BOOLEAN,     0,                   COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)gncBillTermGetInvisible, (QofSetterFunc)set_invisible },
+	{ "parent",       CT_BILLTERMREF, 0,                   0,                 NULL, NULL,
+			(QofAccessFunc)gncBillTermGetParent,    (QofSetterFunc)gncBillTermSetParent },
+	{ "child",        CT_BILLTERMREF, 0,                   0,                 NULL, NULL,
+			(QofAccessFunc)gncBillTermReturnChild,  (QofSetterFunc)gncBillTermSetChild },
+	{ "type",         CT_STRING,      MAX_TYPE_LEN,        COL_NNUL,          NULL, GNC_BILLTERM_TYPE },
+	{ "duedays",      CT_INT,         0,                   0,                 0,    GNC_BILLTERM_DUEDAYS },
+	{ "discountdays", CT_INT,         0,                   0,                 0,    GNC_BILLTERM_DISCDAYS },
+	{ "discount",     CT_NUMERIC,     0,                   0,                 0,    GNC_BILLTERM_DISCOUNT },
+	{ "cutoff",       CT_INT,         0,                   0,                 0,    GNC_BILLTERM_CUTOFF },
+	{ NULL }
+};
+
+static void
+set_invisible( gpointer data, gboolean value )
+{
+	GncBillTerm* term = GNC_BILLTERM(data);
+
+	g_return_if_fail( term != NULL );
+
+	if( value ) {
+		gncBillTermMakeInvisible( term );
+	}
+}
+
+static void
+load_single_billterm( GncSqlBackend* be, GncSqlRow* row )
+{
+    const GUID* guid;
+	GncBillTerm* pBillTerm;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    pBillTerm = gncBillTermLookup( be->primary_book, guid );
+    if( pBillTerm == NULL ) {
+        pBillTerm = gncBillTermCreate( be->primary_book );
+    }
+    gnc_sql_load_object( be, row, GNC_ID_BILLTERM, pBillTerm, col_table );
+    gnc_sql_slots_load( be, QOF_INSTANCE( pBillTerm ) );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pBillTerm) );
+}
+
+static void
+load_all_billterms( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    QofBook* pBook;
+
+	g_return_if_fail( be != NULL );
+
+    pBook = be->primary_book;
+
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+		GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_billterm( be, row );
+			row = gnc_sql_result_get_next_row( result );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+write_billterms( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    qof_object_foreach( GNC_ID_BILLTERM, be->primary_book, (QofInstanceForeachCB)gnc_sql_save_billterm, (gpointer)be );
+}
+
+/* ================================================================= */
+static void
+create_billterm_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+void
+gnc_sql_save_billterm( QofInstance* inst, GncSqlBackend* be )
+{
+    const GUID* guid;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( !GNC_IS_BILLTERM(inst) );
+	g_return_if_fail( be != NULL );
+
+    (void)gnc_sql_do_db_operation( be,
+                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
+                        TABLE_NAME,
+                        GNC_ID_BILLTERM, inst,
+                        col_table );
+
+    // Now, commit or delete any slots
+    guid = qof_instance_get_guid( inst );
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+/* ================================================================= */
+static void
+load_billterm_guid( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GUID guid;
+    const GUID* pGuid;
+	GncBillTerm* term = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        pGuid = NULL;
+    } else {
+        string_to_guid( g_value_get_string( val ), &guid );
+        pGuid = &guid;
+    }
+	if( pGuid != NULL ) {
+		term = gncBillTermLookup( be->primary_book, pGuid );
+	}
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, term, NULL );
+    } else {
+		(*setter)( pObject, (const gpointer)term );
+    }
+}
+
+static col_type_handler_t billterm_guid_handler
+	= { load_billterm_guid,
+		gnc_sql_add_objectref_guid_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+		gnc_sql_add_gvalue_objectref_guid_to_slist };
+/* ================================================================= */
+void
+gnc_billterm_sql_initialize( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_BILLTERM,
+        gnc_sql_save_billterm,				/* commit */
+        load_all_billterms,					/* initial_load */
+        create_billterm_tables,				/* create_tables */
+		NULL, NULL, NULL,
+		write_billterms						/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_BILLTERM, GNC_SQL_BACKEND, &be_data );
+
+	gnc_sql_register_col_type_handler( CT_BILLTERMREF, &billterm_guid_handler );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-bill-term-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,40 @@
+/*
+ * gnc-bill-term-sql.h -- billing term sql backend
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-bill-term-sql.h
+ *  @brief load and save accounts data to SQL
+ *  @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_BILLTERM_SQL_H
+#define GNC_BILLTERM_SQL_H
+
+#include "gncBillTerm.h"
+
+#define CT_BILLTERMREF "billterm"
+
+void gnc_billterm_sql_initialize( void );
+void gnc_sql_save_billterm( QofInstance* inst, GncSqlBackend* be );
+
+#endif /* GNC_BILLTERM_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-customer-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,216 +0,0 @@
-/********************************************************************\
- * gnc-customer-gda.c -- customer gda backend                       *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <libgda/libgda.h>
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gncBillTermP.h"
-#include "gncCustomerP.h"
-#include "gncTaxTableP.h"
-#include "gnc-customer-gda.h"
-#include "gnc-address-gda.h"
-#include "gnc-bill-term-gda.h"
-#include "gnc-tax-table-gda.h"
-
-#define _GNC_MOD_NAME	GNC_ID_CUSTOMER
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TABLE_NAME "customers"
-
-#define MAX_NAME_LEN 2048
-#define MAX_ID_LEN 2048
-#define MAX_NOTES_LEN 2048
-
-static col_cvt_t col_table[] =
-{
-	{ "guid",         CT_GUID,          0,             COL_NNUL|COL_PKEY, "guid" },
-	{ "name",         CT_STRING,        MAX_NAME_LEN,  COL_NNUL,          NULL, CUSTOMER_NAME },
-	{ "id",           CT_STRING,        MAX_ID_LEN,    COL_NNUL,          NULL, CUSTOMER_ID },
-	{ "notes",        CT_STRING,        MAX_NOTES_LEN, COL_NNUL,          NULL, CUSTOMER_NOTES },
-	{ "active",       CT_BOOLEAN,       0,             COL_NNUL,          NULL, QOF_PARAM_ACTIVE },
-	{ "discount",     CT_NUMERIC,       0,             COL_NNUL,          NULL, CUSTOMER_DISCOUNT },
-	{ "credit",       CT_NUMERIC,       0,             COL_NNUL,          NULL, CUSTOMER_CREDIT },
-	{ "currency",     CT_COMMODITYREF,  0,             COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)gncCustomerGetCurrency, (QofSetterFunc)gncCustomerSetCurrency },
-	{ "tax_override", CT_BOOLEAN,       0,             COL_NNUL,          NULL, CUSTOMER_TT_OVER },
-	{ "addr",         CT_ADDRESS,       0,             0,                 NULL, CUSTOMER_ADDR },
-	{ "shipaddr",     CT_ADDRESS,       0,             0,                 NULL, CUSTOMER_SHIPADDR },
-	{ "terms",        CT_BILLTERMREF,   0,             0,                 NULL, CUSTOMER_TERMS },
-	{ "tax_included", CT_INT,           0,             0,                 NULL, NULL,
-			(QofAccessFunc)gncCustomerGetTaxIncluded, (QofSetterFunc)gncCustomerSetTaxIncluded },
-	{ "taxtable",     CT_TAXTABLEREF,   0,             0,                 NULL, NULL,
-			(QofAccessFunc)gncCustomerGetTaxTable, (QofSetterFunc)gncCustomerSetTaxTable },
-	{ NULL }
-};
-
-static void
-load_single_customer( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    const GUID* guid;
-    GUID customer_guid;
-	GncCustomer* pCustomer;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    customer_guid = *guid;
-
-    pCustomer = gncCustomerLookup( be->primary_book, &customer_guid );
-    if( pCustomer == NULL ) {
-        pCustomer = gncCustomerCreate( be->primary_book );
-    }
-    gnc_gda_load_object( be, pModel, row, GNC_ID_CUSTOMER, pCustomer, col_table );
-    gnc_gda_slots_load( be, QOF_INSTANCE(pCustomer) );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pCustomer) );
-}
-
-static void
-load_all_customers( GncGdaBackend* be )
-{
-    static GdaQuery* query = NULL;
-    GdaObject* ret;
-    QofBook* pBook;
-
-	g_return_if_fail( be != NULL );
-
-    pBook = be->primary_book;
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_customer( be, pModel, r );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_customer_tables( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    gnc_gda_create_table_if_needed( be, TABLE_NAME, col_table );
-}
-
-/* ================================================================= */
-static void
-save_customer( QofInstance* inst, GncGdaBackend* be )
-{
-    const GUID* guid;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_CUSTOMER(inst) );
-	g_return_if_fail( be != NULL );
-
-    (void)gnc_gda_do_db_operation( be,
-                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
-                        TABLE_NAME,
-                        GNC_ID_CUSTOMER, inst,
-                        col_table );
-
-    // Now, commit or delete any slots
-    guid = qof_instance_get_guid( inst );
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-/* ================================================================= */
-static gboolean
-customer_should_be_saved( GncCustomer *customer )
-{
-    const char *id;
-
-	g_return_val_if_fail( customer != NULL, FALSE );
-
-    /* Make sure this is a valid customer before we save it -- should have an ID */
-    id = gncCustomerGetID( customer );
-    if( id == NULL || *id == '\0' ) {
-        return FALSE;
-	}
-
-    return TRUE;
-}
-
-static void
-write_single_customer( QofInstance *term_p, gpointer be_p )
-{
-    GncGdaBackend* be = (GncGdaBackend*)be_p;
-
-	g_return_if_fail( term_p != NULL );
-	g_return_if_fail( GNC_IS_CUSTOMER(term_p) );
-	g_return_if_fail( be_p != NULL );
-
-	if( customer_should_be_saved( GNC_CUSTOMER(term_p) ) ) {
-    	save_customer( term_p, be );
-	}
-}
-
-static void
-write_customers( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    qof_object_foreach( GNC_ID_CUSTOMER, be->primary_book, write_single_customer, (gpointer)be );
-}
-
-/* ================================================================= */
-void
-gnc_customer_gda_initialize( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_CUSTOMER,
-        save_customer,						/* commit */
-        load_all_customers,					/* initial_load */
-        create_customer_tables,				/* create_tables */
-		NULL, NULL, NULL,
-		write_customers						/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_CUSTOMER, GNC_GDA_BACKEND, &be_data );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-customer-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,35 +0,0 @@
-/*
- * gnc-customer-gda.h -- customer gda backend
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-customer-gda.h
- *  @brief load and save customer data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_CUSTOMER_GDA_H
-#define GNC_CUSTOMER_GDA_H
-
-void gnc_customer_gda_initialize( void );
-
-#endif /* GNC_CUSTOMER_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,227 @@
+/********************************************************************\
+ * gnc-customer-sql.c -- customer sql backend                       *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-customer-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gncBillTermP.h"
+#include "gncCustomerP.h"
+#include "gncTaxTableP.h"
+#include "gnc-customer-sql.h"
+#include "gnc-address-sql.h"
+#include "gnc-bill-term-sql.h"
+#include "gnc-tax-table-sql.h"
+
+#define _GNC_MOD_NAME	GNC_ID_CUSTOMER
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TABLE_NAME "customers"
+#define TABLE_VERSION 1
+
+#define MAX_NAME_LEN 2048
+#define MAX_ID_LEN 2048
+#define MAX_NOTES_LEN 2048
+
+static col_cvt_t col_table[] =
+{
+	{ "guid",         CT_GUID,          0,             COL_NNUL|COL_PKEY, "guid" },
+	{ "name",         CT_STRING,        MAX_NAME_LEN,  COL_NNUL,          NULL, CUSTOMER_NAME },
+	{ "id",           CT_STRING,        MAX_ID_LEN,    COL_NNUL,          NULL, CUSTOMER_ID },
+	{ "notes",        CT_STRING,        MAX_NOTES_LEN, COL_NNUL,          NULL, CUSTOMER_NOTES },
+	{ "active",       CT_BOOLEAN,       0,             COL_NNUL,          NULL, QOF_PARAM_ACTIVE },
+	{ "discount",     CT_NUMERIC,       0,             COL_NNUL,          NULL, CUSTOMER_DISCOUNT },
+	{ "credit",       CT_NUMERIC,       0,             COL_NNUL,          NULL, CUSTOMER_CREDIT },
+	{ "currency",     CT_COMMODITYREF,  0,             COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)gncCustomerGetCurrency, (QofSetterFunc)gncCustomerSetCurrency },
+	{ "tax_override", CT_BOOLEAN,       0,             COL_NNUL,          NULL, CUSTOMER_TT_OVER },
+	{ "addr",         CT_ADDRESS,       0,             0,                 NULL, CUSTOMER_ADDR },
+	{ "shipaddr",     CT_ADDRESS,       0,             0,                 NULL, CUSTOMER_SHIPADDR },
+	{ "terms",        CT_BILLTERMREF,   0,             0,                 NULL, CUSTOMER_TERMS },
+	{ "tax_included", CT_INT,           0,             0,                 NULL, NULL,
+			(QofAccessFunc)gncCustomerGetTaxIncluded, (QofSetterFunc)gncCustomerSetTaxIncluded },
+	{ "taxtable",     CT_TAXTABLEREF,   0,             0,                 NULL, NULL,
+			(QofAccessFunc)gncCustomerGetTaxTable, (QofSetterFunc)gncCustomerSetTaxTable },
+	{ NULL }
+};
+
+static void
+load_single_customer( GncSqlBackend* be, GncSqlRow* row )
+{
+    const GUID* guid;
+	GncCustomer* pCustomer;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    pCustomer = gncCustomerLookup( be->primary_book, guid );
+    if( pCustomer == NULL ) {
+        pCustomer = gncCustomerCreate( be->primary_book );
+    }
+    gnc_sql_load_object( be, row, GNC_ID_CUSTOMER, pCustomer, col_table );
+    gnc_sql_slots_load( be, QOF_INSTANCE(pCustomer) );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pCustomer) );
+}
+
+static void
+load_all_customers( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    QofBook* pBook;
+
+	g_return_if_fail( be != NULL );
+
+    pBook = be->primary_book;
+
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+		GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_customer( be, row );
+			row = gnc_sql_result_get_next_row( result );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+create_customer_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+save_customer( QofInstance* inst, GncSqlBackend* be )
+{
+    const GUID* guid;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_CUSTOMER(inst) );
+	g_return_if_fail( be != NULL );
+
+    (void)gnc_sql_do_db_operation( be,
+                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
+                        TABLE_NAME,
+                        GNC_ID_CUSTOMER, inst,
+                        col_table );
+
+    // Now, commit or delete any slots
+    guid = qof_instance_get_guid( inst );
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+/* ================================================================= */
+static gboolean
+customer_should_be_saved( GncCustomer *customer )
+{
+    const char *id;
+
+	g_return_val_if_fail( customer != NULL, FALSE );
+
+    /* Make sure this is a valid customer before we save it -- should have an ID */
+    id = gncCustomerGetID( customer );
+    if( id == NULL || *id == '\0' ) {
+        return FALSE;
+	}
+
+    return TRUE;
+}
+
+static void
+write_single_customer( QofInstance *term_p, gpointer be_p )
+{
+    GncSqlBackend* be = (GncSqlBackend*)be_p;
+
+	g_return_if_fail( term_p != NULL );
+	g_return_if_fail( GNC_IS_CUSTOMER(term_p) );
+	g_return_if_fail( be_p != NULL );
+
+	if( customer_should_be_saved( GNC_CUSTOMER(term_p) ) ) {
+    	save_customer( term_p, be );
+	}
+}
+
+static void
+write_customers( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    qof_object_foreach( GNC_ID_CUSTOMER, be->primary_book, write_single_customer, (gpointer)be );
+}
+
+/* ================================================================= */
+void
+gnc_customer_sql_initialize( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_CUSTOMER,
+        save_customer,						/* commit */
+        load_all_customers,					/* initial_load */
+        create_customer_tables,				/* create_tables */
+		NULL, NULL, NULL,
+		write_customers						/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_CUSTOMER, GNC_SQL_BACKEND, &be_data );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-customer-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,35 @@
+/*
+ * gnc-customer-sql.h -- customer sql backend
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-customer-sql.h
+ *  @brief load and save customer data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_CUSTOMER_SQL_H
+#define GNC_CUSTOMER_SQL_H
+
+void gnc_customer_sql_initialize( void );
+
+#endif /* GNC_CUSTOMER_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-employee-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,220 +0,0 @@
-/********************************************************************\
- * gnc-employee-xml-v2.c -- employee xml i/o implementation         *
- *                                                                  *
- * Copyright (C) 2002 Derek Atkins <warlord at MIT.EDU>                *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <libgda/libgda.h>
-
-#include "gnc-commodity.h"
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-slots-gda.h"
-#include "gnc-commodity-gda.h"
-
-#include "gncEmployeeP.h"
-#include "gnc-employee-gda.h"
-#include "gnc-address-gda.h"
-
-#include "gnc-backend-util-gda.h"
-
-#define _GNC_MOD_NAME	GNC_ID_EMPLOYEE
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define MAX_USERNAME_LEN 2048
-#define MAX_ID_LEN 2048
-#define MAX_LANGUAGE_LEN 2048
-#define MAX_ACL_LEN 2048
-
-#define TABLE_NAME "employees"
-
-static col_cvt_t col_table[] =
-{
-	{ "guid",       CT_GUID,          0,                COL_NNUL|COL_PKEY, "guid" },
-	{ "username",   CT_STRING,        MAX_USERNAME_LEN, COL_NNUL,          NULL, EMPLOYEE_USERNAME },
-	{ "id",         CT_STRING,        MAX_ID_LEN,       COL_NNUL,          NULL, EMPLOYEE_ID },
-	{ "language",   CT_STRING,        MAX_LANGUAGE_LEN, COL_NNUL,          NULL, EMPLOYEE_LANGUAGE },
-	{ "acl",        CT_STRING,        MAX_ACL_LEN,      COL_NNUL,          NULL, EMPLOYEE_ACL },
-	{ "active",     CT_BOOLEAN,       0,                COL_NNUL,          NULL, QOF_PARAM_ACTIVE },
-	{ "currency",   CT_COMMODITYREF,  0,                COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)gncEmployeeGetCurrency, (QofSetterFunc)gncEmployeeSetCurrency },
-	{ "ccard_guid", CT_ACCOUNTREF,    0,                COL_NNUL,          NULL, EMPLOYEE_CC },
-	{ "workday",    CT_NUMERIC,       0,                COL_NNUL,          NULL, EMPLOYEE_WORKDAY },
-	{ "rate",       CT_NUMERIC,       0,                COL_NNUL,          NULL, EMPLOYEE_RATE },
-	{ "addr",       CT_ADDRESS,       0,                0,                 NULL, EMPLOYEE_ADDR },
-    { NULL }
-};
-
-static void
-load_single_employee( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    const GUID* guid;
-    GUID emp_guid;
-	GncEmployee* pEmployee;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    emp_guid = *guid;
-
-    pEmployee = gncEmployeeLookup( be->primary_book, &emp_guid );
-    if( pEmployee == NULL ) {
-        pEmployee = gncEmployeeCreate( be->primary_book );
-    }
-    gnc_gda_load_object( be, pModel, row, GNC_ID_EMPLOYEE, pEmployee, col_table );
-    gnc_gda_slots_load( be, QOF_INSTANCE(pEmployee) );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pEmployee) );
-}
-
-static void
-load_all_employees( GncGdaBackend* be )
-{
-    static GdaQuery* query = NULL;
-    GdaObject* ret;
-    QofBook* pBook;
-    gnc_commodity_table* pTable;
-
-	g_return_if_fail( be != NULL );
-
-    pBook = be->primary_book;
-    pTable = gnc_commodity_table_get_table( pBook );
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_employee( be, pModel, r );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_employee_tables( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    gnc_gda_create_table_if_needed( be, TABLE_NAME, col_table );
-}
-
-/* ================================================================= */
-static void
-save_employee( QofInstance* inst, GncGdaBackend* be )
-{
-    GncEmployee* emp = GNC_EMPLOYEE(inst);
-    const GUID* guid;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_EMPLOYEE(inst) );
-	g_return_if_fail( be != NULL );
-
-    // Ensure the commodity is in the db
-    gnc_gda_save_commodity( be, gncEmployeeGetCurrency( emp ) );
-
-    (void)gnc_gda_do_db_operation( be,
-                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
-                        TABLE_NAME,
-                        GNC_ID_EMPLOYEE, emp,
-                        col_table );
-
-    // Now, commit or delete any slots
-    guid = qof_instance_get_guid( inst );
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-/* ================================================================= */
-static gboolean
-employee_should_be_saved( GncEmployee *employee )
-{
-    const char *id;
-
-	g_return_val_if_fail( employee != NULL, FALSE );
-
-    /* make sure this is a valid employee before we save it -- should have an ID */
-    id = gncEmployeeGetID( employee );
-    if( id == NULL || *id == '\0' ) {
-        return FALSE;
-	}
-
-    return TRUE;
-}
-static void
-write_single_employee( QofInstance *term_p, gpointer be_p )
-{
-    GncGdaBackend* be = (GncGdaBackend*)be_p;
-
-	g_return_if_fail( term_p != NULL );
-	g_return_if_fail( GNC_IS_EMPLOYEE(term_p) );
-	g_return_if_fail( be_p != NULL );
-
-	if( employee_should_be_saved( GNC_EMPLOYEE(term_p) ) ) {
-    	save_employee( term_p, be );
-	}
-}
-
-static void
-write_employees( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    qof_object_foreach( GNC_ID_EMPLOYEE, be->primary_book, write_single_employee, (gpointer)be );
-}
-
-/* ================================================================= */
-void
-gnc_employee_gda_initialize( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_EMPLOYEE,
-        save_employee,						/* commit */
-        load_all_employees,					/* initial_load */
-        create_employee_tables,				/* create_tables */
-		NULL, NULL, NULL,
-		write_employees						/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_EMPLOYEE, GNC_GDA_BACKEND, &be_data );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-employee-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,35 +0,0 @@
-/*
- * gnc-employee-gda.h -- employee gda backend
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-employee-gda.h
- *  @brief load and save employee data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_EMPLOYEE_GDA_H
-#define GNC_EMPLOYEE_GDA_H
-
-void gnc_employee_gda_initialize( void );
-
-#endif /* GNC_EMPLOYEE_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,228 @@
+/********************************************************************\
+ * gnc-employee-sql.c -- employee sql implementation                *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-employee-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-commodity.h"
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-slots-sql.h"
+#include "gnc-commodity-sql.h"
+
+#include "gncEmployeeP.h"
+#include "gnc-employee-sql.h"
+#include "gnc-address-sql.h"
+
+#include "gnc-backend-util-sql.h"
+
+#define _GNC_MOD_NAME	GNC_ID_EMPLOYEE
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define MAX_USERNAME_LEN 2048
+#define MAX_ID_LEN 2048
+#define MAX_LANGUAGE_LEN 2048
+#define MAX_ACL_LEN 2048
+
+#define TABLE_NAME "employees"
+#define TABLE_VERSION 1
+
+static col_cvt_t col_table[] =
+{
+	{ "guid",       CT_GUID,          0,                COL_NNUL|COL_PKEY, "guid" },
+	{ "username",   CT_STRING,        MAX_USERNAME_LEN, COL_NNUL,          NULL, EMPLOYEE_USERNAME },
+	{ "id",         CT_STRING,        MAX_ID_LEN,       COL_NNUL,          NULL, EMPLOYEE_ID },
+	{ "language",   CT_STRING,        MAX_LANGUAGE_LEN, COL_NNUL,          NULL, EMPLOYEE_LANGUAGE },
+	{ "acl",        CT_STRING,        MAX_ACL_LEN,      COL_NNUL,          NULL, EMPLOYEE_ACL },
+	{ "active",     CT_BOOLEAN,       0,                COL_NNUL,          NULL, QOF_PARAM_ACTIVE },
+	{ "currency",   CT_COMMODITYREF,  0,                COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)gncEmployeeGetCurrency, (QofSetterFunc)gncEmployeeSetCurrency },
+	{ "ccard_guid", CT_ACCOUNTREF,    0,                COL_NNUL,          NULL, EMPLOYEE_CC },
+	{ "workday",    CT_NUMERIC,       0,                COL_NNUL,          NULL, EMPLOYEE_WORKDAY },
+	{ "rate",       CT_NUMERIC,       0,                COL_NNUL,          NULL, EMPLOYEE_RATE },
+	{ "addr",       CT_ADDRESS,       0,                0,                 NULL, EMPLOYEE_ADDR },
+    { NULL }
+};
+
+static void
+load_single_employee( GncSqlBackend* be, GncSqlRow* row )
+{
+    const GUID* guid;
+	GncEmployee* pEmployee;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    pEmployee = gncEmployeeLookup( be->primary_book, guid );
+    if( pEmployee == NULL ) {
+        pEmployee = gncEmployeeCreate( be->primary_book );
+    }
+    gnc_sql_load_object( be, row, GNC_ID_EMPLOYEE, pEmployee, col_table );
+    gnc_sql_slots_load( be, QOF_INSTANCE(pEmployee) );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pEmployee) );
+}
+
+static void
+load_all_employees( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    QofBook* pBook;
+    gnc_commodity_table* pTable;
+
+	g_return_if_fail( be != NULL );
+
+    pBook = be->primary_book;
+    pTable = gnc_commodity_table_get_table( pBook );
+
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_employee( be, row );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+create_employee_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+save_employee( QofInstance* inst, GncSqlBackend* be )
+{
+    GncEmployee* emp = GNC_EMPLOYEE(inst);
+    const GUID* guid;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_EMPLOYEE(inst) );
+	g_return_if_fail( be != NULL );
+
+    // Ensure the commodity is in the db
+    gnc_sql_save_commodity( be, gncEmployeeGetCurrency( emp ) );
+
+    (void)gnc_sql_do_db_operation( be,
+                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
+                        TABLE_NAME,
+                        GNC_ID_EMPLOYEE, emp,
+                        col_table );
+
+    // Now, commit or delete any slots
+    guid = qof_instance_get_guid( inst );
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+/* ================================================================= */
+static gboolean
+employee_should_be_saved( GncEmployee *employee )
+{
+    const char *id;
+
+	g_return_val_if_fail( employee != NULL, FALSE );
+
+    /* make sure this is a valid employee before we save it -- should have an ID */
+    id = gncEmployeeGetID( employee );
+    if( id == NULL || *id == '\0' ) {
+        return FALSE;
+	}
+
+    return TRUE;
+}
+static void
+write_single_employee( QofInstance *term_p, gpointer be_p )
+{
+    GncSqlBackend* be = (GncSqlBackend*)be_p;
+
+	g_return_if_fail( term_p != NULL );
+	g_return_if_fail( GNC_IS_EMPLOYEE(term_p) );
+	g_return_if_fail( be_p != NULL );
+
+	if( employee_should_be_saved( GNC_EMPLOYEE(term_p) ) ) {
+    	save_employee( term_p, be );
+	}
+}
+
+static void
+write_employees( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    qof_object_foreach( GNC_ID_EMPLOYEE, be->primary_book, write_single_employee, (gpointer)be );
+}
+
+/* ================================================================= */
+void
+gnc_employee_sql_initialize( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_EMPLOYEE,
+        save_employee,						/* commit */
+        load_all_employees,					/* initial_load */
+        create_employee_tables,				/* create_tables */
+		NULL, NULL, NULL,
+		write_employees						/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_EMPLOYEE, GNC_SQL_BACKEND, &be_data );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-employee-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,35 @@
+/*
+ * gnc-employee-sql.h -- employee sql backend
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-employee-sql.h
+ *  @brief load and save employee data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_EMPLOYEE_SQL_H
+#define GNC_EMPLOYEE_SQL_H
+
+void gnc_employee_sql_initialize( void );
+
+#endif /* GNC_EMPLOYEE_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-entry-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,223 +0,0 @@
-/********************************************************************\
- * gnc-entry-gda.c -- entry libgda backend                          *
- *                                                                  *
- * Copyright (C) 2007-2008 Phil Longstaff (plongstaff at rogers.com)   *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-#include <libgda/libgda.h>
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gncEntryP.h"
-#include "gncOrderP.h"
-#include "gncInvoiceP.h"
-#include "gncTaxTableP.h"
-#include "gnc-entry-gda.h"
-#include "gnc-invoice-gda.h"
-#include "gnc-order-gda.h"
-#include "gnc-owner-gda.h"
-#include "gnc-tax-table-gda.h"
-
-#define _GNC_MOD_NAME	GNC_ID_ENTRY
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TABLE_NAME "entries"
-#define MAX_DESCRIPTION_LEN 2048
-#define MAX_ACTION_LEN 2048
-#define MAX_NOTES_LEN 2048
-#define MAX_DISCTYPE_LEN 2048
-#define MAX_DISCHOW_LEN 2048
-
-static col_cvt_t col_table[] =
-{
-	{ "guid",          CT_GUID,        0,                   COL_NNUL|COL_PKEY, "guid" },
-	{ "date",          CT_TIMESPEC,    0,                   COL_NNUL,          NULL, ENTRY_DATE },
-	{ "entered",       CT_TIMESPEC,    0,                   COL_NNUL,          NULL, ENTRY_DATE_ENTERED },
-	{ "description",   CT_STRING,      MAX_DESCRIPTION_LEN, 0,                 NULL, ENTRY_DESC },
-	{ "action",        CT_STRING,      MAX_ACTION_LEN,      0,                 NULL, ENTRY_ACTION },
-	{ "notes",         CT_STRING,      MAX_NOTES_LEN,       0,                 NULL, ENTRY_NOTES },
-	{ "quantity",      CT_NUMERIC,     0,                   0,                 NULL, ENTRY_QTY },
-	{ "i_acct",        CT_ACCOUNTREF,  0,                   0,                 NULL, ENTRY_IACCT },
-	{ "i_price",       CT_NUMERIC,     0,                   0,                 NULL, ENTRY_IPRICE },
-	{ "i_discount",    CT_NUMERIC,     0,                   0,                 NULL, NULL,
-			(QofAccessFunc)gncEntryGetInvDiscount, (QofSetterFunc)gncEntrySetInvDiscount },
-	{ "invoice",       CT_INVOICEREF,  0,                   0,                 NULL, NULL,
-			(QofAccessFunc)gncEntryGetInvoice, (QofSetterFunc)gncEntrySetInvoice },
-	{ "i_disc_type",   CT_STRING,      MAX_DISCTYPE_LEN,    0,        			NULL, ENTRY_INV_DISC_TYPE },
-	{ "i_disc_how",    CT_STRING,      MAX_DISCHOW_LEN,     0,        			NULL, ENTRY_INV_DISC_HOW },
-	{ "i_taxable",     CT_BOOLEAN,     0,                   0,        			NULL, ENTRY_INV_TAXABLE },
-	{ "i_taxincluded", CT_BOOLEAN,     0,                   0,        			NULL, ENTRY_INV_TAX_INC },
-	{ "i_taxtable",    CT_TAXTABLEREF, 0,                   0,        			NULL, NULL,
-			(QofAccessFunc)gncEntryGetInvTaxTable, (QofSetterFunc)gncEntrySetInvTaxTable },
-	{ "b_acct",        CT_ACCOUNTREF,  0,                   0,        			NULL, ENTRY_BACCT },
-	{ "b_price",       CT_NUMERIC,     0,                   0,        			NULL, ENTRY_BPRICE },
-	{ "bill",          CT_INVOICEREF,  0,                   0,        			NULL, NULL,
-			(QofAccessFunc)gncEntryGetBill, (QofSetterFunc)gncEntrySetBill },
-	{ "b_taxable",     CT_BOOLEAN,     0,                   0,        			NULL, ENTRY_BILL_TAXABLE },
-	{ "b_taxincluded", CT_BOOLEAN,     0,                   0,        			NULL, ENTRY_BILL_TAX_INC },
-	{ "b_taxtable",    CT_TAXTABLEREF, 0,                   0,        			NULL, NULL,
-			(QofAccessFunc)gncEntryGetBillTaxTable, (QofSetterFunc)gncEntrySetBillTaxTable },
-	{ "b_paytype",     CT_INT,         0,                   0,        			NULL, NULL,
-			(QofAccessFunc)gncEntryGetBillPayment, (QofSetterFunc)gncEntrySetBillPayment },
-	{ "billable",      CT_BOOLEAN,     0,                   0,        			NULL, ENTRY_BILLABLE },
-	{ "billto",        CT_OWNERREF,    0,                   0,        			NULL, ENTRY_BILLTO },
-	{ "order_guid",    CT_ORDERREF,    0,                   0,        			NULL, NULL,
-			(QofAccessFunc)gncEntryGetOrder, (QofSetterFunc)gncEntrySetOrder },
-	{ NULL }
-};
-
-static void
-load_single_entry( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    const GUID* guid;
-    GUID v_guid;
-	GncEntry* pEntry;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    v_guid = *guid;
-
-    pEntry = gncEntryLookup( be->primary_book, &v_guid );
-    if( pEntry == NULL ) {
-        pEntry = gncEntryCreate( be->primary_book );
-    }
-    gnc_gda_load_object( be, pModel, row, GNC_ID_ENTRY, pEntry, col_table );
-    gnc_gda_slots_load( be, QOF_INSTANCE(pEntry) );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pEntry) );
-}
-
-static void
-load_all_entries( GncGdaBackend* be )
-{
-    static GdaQuery* query = NULL;
-    GdaObject* ret;
-    QofBook* pBook;
-
-	g_return_if_fail( be != NULL );
-
-    pBook = be->primary_book;
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_entry( be, pModel, r );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_entry_tables( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    gnc_gda_create_table_if_needed( be, TABLE_NAME, col_table );
-}
-
-/* ================================================================= */
-static void
-save_entry( QofInstance* inst, GncGdaBackend* be )
-{
-    const GUID* guid;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_ENTRY(inst) );
-	g_return_if_fail( be != NULL );
-
-    (void)gnc_gda_do_db_operation( be,
-                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
-                        TABLE_NAME,
-                        GNC_ID_ENTRY, inst,
-                        col_table );
-
-    // Now, commit or delete any slots
-    guid = qof_instance_get_guid( inst );
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-/* ================================================================= */
-static void
-write_single_entry( QofInstance *term_p, gpointer be_p )
-{
-    GncGdaBackend* be = (GncGdaBackend*)be_p;
-	GncEntry* entry = GNC_ENTRY(term_p);
-
-	g_return_if_fail( term_p != NULL );
-	g_return_if_fail( GNC_IS_ENTRY(term_p) );
-	g_return_if_fail( be_p != NULL );
-
-  	/* Only save if attached */
-  	if( gncEntryGetOrder( entry ) != NULL || gncEntryGetInvoice( entry ) != NULL ||
-			gncEntryGetBill( entry ) != NULL ) {
-    	save_entry( term_p, be );
-	}
-}
-
-static void
-write_entries( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    qof_object_foreach( GNC_ID_ENTRY, be->primary_book, write_single_entry, (gpointer)be );
-}
-
-/* ================================================================= */
-void
-gnc_entry_gda_initialize( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_ENTRY,
-        save_entry,							/* commit */
-        load_all_entries,					/* initial_load */
-        create_entry_tables,				/* create_tables */
-		NULL, NULL, NULL,
-		write_entries						/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_ENTRY, GNC_GDA_BACKEND, &be_data );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-entry-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,35 +0,0 @@
-/*
- * gnc-entry-gda.h -- entry gda backend
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-entry-gda.h
- *  @brief load and save entry data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_ENTRY_GDA_H
-#define GNC_ENTRY_GDA_H
-
-void gnc_entry_gda_initialize( void );
-
-#endif /* GNC_ENTRY_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,233 @@
+/********************************************************************\
+ * gnc-entry-sql.c -- entry sql backend                             *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-entry-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gncEntryP.h"
+#include "gncOrderP.h"
+#include "gncInvoiceP.h"
+#include "gncTaxTableP.h"
+#include "gnc-entry-sql.h"
+#include "gnc-invoice-sql.h"
+#include "gnc-order-sql.h"
+#include "gnc-owner-sql.h"
+#include "gnc-tax-table-sql.h"
+
+#define _GNC_MOD_NAME	GNC_ID_ENTRY
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TABLE_NAME "entries"
+#define TABLE_VERSION 1
+#define MAX_DESCRIPTION_LEN 2048
+#define MAX_ACTION_LEN 2048
+#define MAX_NOTES_LEN 2048
+#define MAX_DISCTYPE_LEN 2048
+#define MAX_DISCHOW_LEN 2048
+
+static col_cvt_t col_table[] =
+{
+	{ "guid",          CT_GUID,        0,                   COL_NNUL|COL_PKEY, "guid" },
+	{ "date",          CT_TIMESPEC,    0,                   COL_NNUL,          NULL, ENTRY_DATE },
+	{ "entered",       CT_TIMESPEC,    0,                   COL_NNUL,          NULL, ENTRY_DATE_ENTERED },
+	{ "description",   CT_STRING,      MAX_DESCRIPTION_LEN, 0,                 NULL, ENTRY_DESC },
+	{ "action",        CT_STRING,      MAX_ACTION_LEN,      0,                 NULL, ENTRY_ACTION },
+	{ "notes",         CT_STRING,      MAX_NOTES_LEN,       0,                 NULL, ENTRY_NOTES },
+	{ "quantity",      CT_NUMERIC,     0,                   0,                 NULL, ENTRY_QTY },
+	{ "i_acct",        CT_ACCOUNTREF,  0,                   0,                 NULL, ENTRY_IACCT },
+	{ "i_price",       CT_NUMERIC,     0,                   0,                 NULL, ENTRY_IPRICE },
+	{ "i_discount",    CT_NUMERIC,     0,                   0,                 NULL, NULL,
+			(QofAccessFunc)gncEntryGetInvDiscount, (QofSetterFunc)gncEntrySetInvDiscount },
+	{ "invoice",       CT_INVOICEREF,  0,                   0,                 NULL, NULL,
+			(QofAccessFunc)gncEntryGetInvoice, (QofSetterFunc)gncEntrySetInvoice },
+	{ "i_disc_type",   CT_STRING,      MAX_DISCTYPE_LEN,    0,        			NULL, ENTRY_INV_DISC_TYPE },
+	{ "i_disc_how",    CT_STRING,      MAX_DISCHOW_LEN,     0,        			NULL, ENTRY_INV_DISC_HOW },
+	{ "i_taxable",     CT_BOOLEAN,     0,                   0,        			NULL, ENTRY_INV_TAXABLE },
+	{ "i_taxincluded", CT_BOOLEAN,     0,                   0,        			NULL, ENTRY_INV_TAX_INC },
+	{ "i_taxtable",    CT_TAXTABLEREF, 0,                   0,        			NULL, NULL,
+			(QofAccessFunc)gncEntryGetInvTaxTable, (QofSetterFunc)gncEntrySetInvTaxTable },
+	{ "b_acct",        CT_ACCOUNTREF,  0,                   0,        			NULL, ENTRY_BACCT },
+	{ "b_price",       CT_NUMERIC,     0,                   0,        			NULL, ENTRY_BPRICE },
+	{ "bill",          CT_INVOICEREF,  0,                   0,        			NULL, NULL,
+			(QofAccessFunc)gncEntryGetBill, (QofSetterFunc)gncEntrySetBill },
+	{ "b_taxable",     CT_BOOLEAN,     0,                   0,        			NULL, ENTRY_BILL_TAXABLE },
+	{ "b_taxincluded", CT_BOOLEAN,     0,                   0,        			NULL, ENTRY_BILL_TAX_INC },
+	{ "b_taxtable",    CT_TAXTABLEREF, 0,                   0,        			NULL, NULL,
+			(QofAccessFunc)gncEntryGetBillTaxTable, (QofSetterFunc)gncEntrySetBillTaxTable },
+	{ "b_paytype",     CT_INT,         0,                   0,        			NULL, NULL,
+			(QofAccessFunc)gncEntryGetBillPayment, (QofSetterFunc)gncEntrySetBillPayment },
+	{ "billable",      CT_BOOLEAN,     0,                   0,        			NULL, ENTRY_BILLABLE },
+	{ "billto",        CT_OWNERREF,    0,                   0,        			NULL, ENTRY_BILLTO },
+	{ "order_guid",    CT_ORDERREF,    0,                   0,        			NULL, NULL,
+			(QofAccessFunc)gncEntryGetOrder, (QofSetterFunc)gncEntrySetOrder },
+	{ NULL }
+};
+
+static void
+load_single_entry( GncSqlBackend* be, GncSqlRow* row )
+{
+    const GUID* guid;
+	GncEntry* pEntry;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    pEntry = gncEntryLookup( be->primary_book, guid );
+    if( pEntry == NULL ) {
+        pEntry = gncEntryCreate( be->primary_book );
+    }
+    gnc_sql_load_object( be, row, GNC_ID_ENTRY, pEntry, col_table );
+    gnc_sql_slots_load( be, QOF_INSTANCE(pEntry) );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pEntry) );
+}
+
+static void
+load_all_entries( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    QofBook* pBook;
+
+	g_return_if_fail( be != NULL );
+
+    pBook = be->primary_book;
+
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_entry( be, row );
+			row = gnc_sql_result_get_next_row( result );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+create_entry_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+save_entry( QofInstance* inst, GncSqlBackend* be )
+{
+    const GUID* guid;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_ENTRY(inst) );
+	g_return_if_fail( be != NULL );
+
+    (void)gnc_sql_do_db_operation( be,
+                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
+                        TABLE_NAME,
+                        GNC_ID_ENTRY, inst,
+                        col_table );
+
+    // Now, commit or delete any slots
+    guid = qof_instance_get_guid( inst );
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+/* ================================================================= */
+static void
+write_single_entry( QofInstance *term_p, gpointer be_p )
+{
+    GncSqlBackend* be = (GncSqlBackend*)be_p;
+	GncEntry* entry = GNC_ENTRY(term_p);
+
+	g_return_if_fail( term_p != NULL );
+	g_return_if_fail( GNC_IS_ENTRY(term_p) );
+	g_return_if_fail( be_p != NULL );
+
+  	/* Only save if attached */
+  	if( gncEntryGetOrder( entry ) != NULL || gncEntryGetInvoice( entry ) != NULL ||
+			gncEntryGetBill( entry ) != NULL ) {
+    	save_entry( term_p, be );
+	}
+}
+
+static void
+write_entries( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    qof_object_foreach( GNC_ID_ENTRY, be->primary_book, write_single_entry, (gpointer)be );
+}
+
+/* ================================================================= */
+void
+gnc_entry_sql_initialize( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_ENTRY,
+        save_entry,							/* commit */
+        load_all_entries,					/* initial_load */
+        create_entry_tables,				/* create_tables */
+		NULL, NULL, NULL,
+		write_entries						/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_ENTRY, GNC_SQL_BACKEND, &be_data );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-entry-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,35 @@
+/*
+ * gnc-entry-sql.h -- entry sql backend
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-entry-sql.h
+ *  @brief load and save entry data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_ENTRY_SQL_H
+#define GNC_ENTRY_SQL_H
+
+void gnc_entry_sql_initialize( void );
+
+#endif /* GNC_ENTRY_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-invoice-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,263 +0,0 @@
-/********************************************************************\
- * gnc-invoice-gda.c - invoice gda backend                          *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <libgda/libgda.h>
-
-#include "gnc-commodity.h"
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-commodity-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gncBillTermP.h"
-#include "gncInvoiceP.h"
-#include "gnc-invoice-gda.h"
-#include "gnc-owner-gda.h"
-#include "gnc-bill-term-gda.h"
-
-#define _GNC_MOD_NAME	GNC_ID_INVOICE
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TABLE_NAME "invoices"
-
-#define MAX_ID_LEN 2048
-#define MAX_NOTES_LEN 2048
-#define MAX_BILLING_ID_LEN 2048
-
-static col_cvt_t col_table[] =
-{
-	{ "guid",         CT_GUID,         0,                  COL_NNUL|COL_PKEY, "guid" },
-	{ "id",           CT_STRING,       MAX_ID_LEN,         COL_NNUL,          NULL, INVOICE_ID },
-	{ "date_opened",  CT_TIMESPEC,     0,                  COL_NNUL,          NULL, INVOICE_OPENED },
-	{ "date_posted",  CT_TIMESPEC,     0,                  0,                 NULL, INVOICE_POSTED },
-	{ "notes",        CT_STRING,       MAX_NOTES_LEN,      COL_NNUL,          NULL, INVOICE_NOTES },
-	{ "active",       CT_BOOLEAN,      0,                  COL_NNUL,          NULL, QOF_PARAM_ACTIVE },
-	{ "currency",     CT_COMMODITYREF, 0,                  COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)gncInvoiceGetCurrency, (QofSetterFunc)gncInvoiceSetCurrency },
-	{ "owner",        CT_OWNERREF,     0,                  0,                 NULL, NULL,
-			(QofAccessFunc)gncInvoiceGetOwner, (QofSetterFunc)gncInvoiceSetOwner },
-	{ "terms",        CT_BILLTERMREF,  0,                  0,                 NULL, INVOICE_TERMS },
-	{ "billing_id",   CT_STRING,       MAX_BILLING_ID_LEN, 0,                 NULL, INVOICE_BILLINGID },
-	{ "post_txn",     CT_TXREF,        0,                  0,                 NULL, INVOICE_POST_TXN },
-	{ "post_lot",     CT_LOTREF,       0,                  0,                 NULL, NULL,
-			(QofAccessFunc)gncInvoiceGetPostedLot, (QofSetterFunc)gncInvoiceSetPostedLot },
-	{ "post_acc",     CT_ACCOUNTREF,   0,                  0,                 NULL, INVOICE_ACC },
-	{ "billto",       CT_OWNERREF,     0,                  0,                 NULL, NULL,
-			(QofAccessFunc)gncInvoiceGetBillTo, (QofSetterFunc)gncInvoiceSetBillTo },
-	{ "charge_amt",   CT_NUMERIC,      0,                  0,                 NULL, NULL,
-			(QofAccessFunc)gncInvoiceGetToChargeAmount, (QofSetterFunc)gncInvoiceSetToChargeAmount },
-	{ NULL }
-};
-
-static void
-load_single_invoice( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    const GUID* guid;
-    GUID v_guid;
-	GncInvoice* pInvoice;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    v_guid = *guid;
-
-    pInvoice = gncInvoiceLookup( be->primary_book, &v_guid );
-    if( pInvoice == NULL ) {
-        pInvoice = gncInvoiceCreate( be->primary_book );
-    }
-    gnc_gda_load_object( be, pModel, row, GNC_ID_INVOICE, pInvoice, col_table );
-    gnc_gda_slots_load( be, QOF_INSTANCE(pInvoice) );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pInvoice) );
-}
-
-static void
-load_all_invoices( GncGdaBackend* be )
-{
-    static GdaQuery* query = NULL;
-    GdaObject* ret;
-    QofBook* pBook;
-
-	g_return_if_fail( be != NULL );
-
-    pBook = be->primary_book;
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_invoice( be, pModel, r );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_invoice_tables( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    gnc_gda_create_table_if_needed( be, TABLE_NAME, col_table );
-}
-
-/* ================================================================= */
-static void
-save_invoice( QofInstance* inst, GncGdaBackend* be )
-{
-    const GUID* guid;
-	GncInvoice* invoice = GNC_INVOICE(inst);
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_INVOICE(inst) );
-	g_return_if_fail( be != NULL );
-
-    // Ensure the commodity is in the db
-    gnc_gda_save_commodity( be, gncInvoiceGetCurrency( invoice ) );
-
-    (void)gnc_gda_do_db_operation( be,
-                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
-                        TABLE_NAME,
-                        GNC_ID_INVOICE, inst,
-                        col_table );
-
-    // Now, commit or delete any slots
-    guid = qof_instance_get_guid( inst );
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-/* ================================================================= */
-static gboolean
-invoice_should_be_saved( GncInvoice *invoice )
-{
-    const char *id;
-
-	g_return_val_if_fail( invoice != NULL, FALSE );
-
-    /* make sure this is a valid invoice before we save it -- should have an ID */
-    id = gncInvoiceGetID( invoice );
-    if( id == NULL || *id == '\0' ) {
-        return FALSE;
-	}
-
-    return TRUE;
-}
-
-static void
-write_single_invoice( QofInstance *term_p, gpointer be_p )
-{
-    GncGdaBackend* be = (GncGdaBackend*)be_p;
-
-	g_return_if_fail( term_p != NULL );
-	g_return_if_fail( GNC_IS_INVOICE(term_p) );
-	g_return_if_fail( be_p != NULL );
-
-	if( invoice_should_be_saved( GNC_INVOICE(term_p) ) ) {
-    	save_invoice( term_p, be );
-	}
-}
-
-static void
-write_invoices( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    qof_object_foreach( GNC_ID_INVOICE, be->primary_book, write_single_invoice, (gpointer)be );
-}
-
-/* ================================================================= */
-static void
-load_invoice_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GUID guid;
-    const GUID* pGuid;
-	GncInvoice* invoice = NULL;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        pGuid = NULL;
-    } else {
-        string_to_guid( g_value_get_string( val ), &guid );
-        pGuid = &guid;
-    }
-	if( pGuid != NULL ) {
-		invoice = gncInvoiceLookup( be->primary_book, pGuid );
-	}
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, invoice, NULL );
-    } else {
-		(*setter)( pObject, (const gpointer)invoice );
-    }
-}
-
-static col_type_handler_t invoice_guid_handler =
-        { load_invoice_guid, gnc_gda_create_objectref_guid_col,
-            gnc_gda_get_gvalue_objectref_guid_for_query, gnc_gda_get_gvalue_objectref_guid_cond };
-/* ================================================================= */
-void
-gnc_invoice_gda_initialize( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_INVOICE,
-        save_invoice,						/* commit */
-        load_all_invoices,					/* initial_load */
-        create_invoice_tables,				/* create_tables */
-		NULL, NULL, NULL,
-		write_invoices						/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_INVOICE, GNC_GDA_BACKEND, &be_data );
-
-	gnc_gda_register_col_type_handler( CT_INVOICEREF, &invoice_guid_handler );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-invoice-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,37 +0,0 @@
-/*
- * gnc-invoice-gda.h -- invoice gda backend
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-invoice-gda.h
- *  @brief load and save invoice data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_INVOICE_GDA_H
-#define GNC_INVOICE_GDA_H
-
-#define CT_INVOICEREF "invoice"
-
-void gnc_invoice_gda_initialize( void );
-
-#endif /* GNC_INVOICE_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,275 @@
+/********************************************************************\
+ * gnc-invoice-sql.c - invoice sql backend                          *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-invoice-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-commodity.h"
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-commodity-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gncBillTermP.h"
+#include "gncInvoiceP.h"
+#include "gnc-invoice-sql.h"
+#include "gnc-owner-sql.h"
+#include "gnc-bill-term-sql.h"
+
+#define _GNC_MOD_NAME	GNC_ID_INVOICE
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TABLE_NAME "invoices"
+#define TABLE_VERSION 1
+
+#define MAX_ID_LEN 2048
+#define MAX_NOTES_LEN 2048
+#define MAX_BILLING_ID_LEN 2048
+
+static col_cvt_t col_table[] =
+{
+	{ "guid",         CT_GUID,         0,                  COL_NNUL|COL_PKEY, "guid" },
+	{ "id",           CT_STRING,       MAX_ID_LEN,         COL_NNUL,          NULL, INVOICE_ID },
+	{ "date_opened",  CT_TIMESPEC,     0,                  COL_NNUL,          NULL, INVOICE_OPENED },
+	{ "date_posted",  CT_TIMESPEC,     0,                  0,                 NULL, INVOICE_POSTED },
+	{ "notes",        CT_STRING,       MAX_NOTES_LEN,      COL_NNUL,          NULL, INVOICE_NOTES },
+	{ "active",       CT_BOOLEAN,      0,                  COL_NNUL,          NULL, QOF_PARAM_ACTIVE },
+	{ "currency",     CT_COMMODITYREF, 0,                  COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)gncInvoiceGetCurrency, (QofSetterFunc)gncInvoiceSetCurrency },
+	{ "owner",        CT_OWNERREF,     0,                  0,                 NULL, NULL,
+			(QofAccessFunc)gncInvoiceGetOwner, (QofSetterFunc)gncInvoiceSetOwner },
+	{ "terms",        CT_BILLTERMREF,  0,                  0,                 NULL, INVOICE_TERMS },
+	{ "billing_id",   CT_STRING,       MAX_BILLING_ID_LEN, 0,                 NULL, INVOICE_BILLINGID },
+	{ "post_txn",     CT_TXREF,        0,                  0,                 NULL, INVOICE_POST_TXN },
+	{ "post_lot",     CT_LOTREF,       0,                  0,                 NULL, NULL,
+			(QofAccessFunc)gncInvoiceGetPostedLot, (QofSetterFunc)gncInvoiceSetPostedLot },
+	{ "post_acc",     CT_ACCOUNTREF,   0,                  0,                 NULL, INVOICE_ACC },
+	{ "billto",       CT_OWNERREF,     0,                  0,                 NULL, NULL,
+			(QofAccessFunc)gncInvoiceGetBillTo, (QofSetterFunc)gncInvoiceSetBillTo },
+	{ "charge_amt",   CT_NUMERIC,      0,                  0,                 NULL, NULL,
+			(QofAccessFunc)gncInvoiceGetToChargeAmount, (QofSetterFunc)gncInvoiceSetToChargeAmount },
+	{ NULL }
+};
+
+static void
+load_single_invoice( GncSqlBackend* be, GncSqlRow* row )
+{
+    const GUID* guid;
+	GncInvoice* pInvoice;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    pInvoice = gncInvoiceLookup( be->primary_book, guid );
+    if( pInvoice == NULL ) {
+        pInvoice = gncInvoiceCreate( be->primary_book );
+    }
+    gnc_sql_load_object( be, row, GNC_ID_INVOICE, pInvoice, col_table );
+    gnc_sql_slots_load( be, QOF_INSTANCE(pInvoice) );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pInvoice) );
+}
+
+static void
+load_all_invoices( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    QofBook* pBook;
+
+	g_return_if_fail( be != NULL );
+
+    pBook = be->primary_book;
+
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_invoice( be, row );
+			row = gnc_sql_result_get_next_row( result );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+create_invoice_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+save_invoice( QofInstance* inst, GncSqlBackend* be )
+{
+    const GUID* guid;
+	GncInvoice* invoice = GNC_INVOICE(inst);
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_INVOICE(inst) );
+	g_return_if_fail( be != NULL );
+
+    // Ensure the commodity is in the db
+    gnc_sql_save_commodity( be, gncInvoiceGetCurrency( invoice ) );
+
+    (void)gnc_sql_do_db_operation( be,
+                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
+                        TABLE_NAME,
+                        GNC_ID_INVOICE, inst,
+                        col_table );
+
+    // Now, commit or delete any slots
+    guid = qof_instance_get_guid( inst );
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+/* ================================================================= */
+static gboolean
+invoice_should_be_saved( GncInvoice *invoice )
+{
+    const char *id;
+
+	g_return_val_if_fail( invoice != NULL, FALSE );
+
+    /* make sure this is a valid invoice before we save it -- should have an ID */
+    id = gncInvoiceGetID( invoice );
+    if( id == NULL || *id == '\0' ) {
+        return FALSE;
+	}
+
+    return TRUE;
+}
+
+static void
+write_single_invoice( QofInstance *term_p, gpointer be_p )
+{
+    GncSqlBackend* be = (GncSqlBackend*)be_p;
+
+	g_return_if_fail( term_p != NULL );
+	g_return_if_fail( GNC_IS_INVOICE(term_p) );
+	g_return_if_fail( be_p != NULL );
+
+	if( invoice_should_be_saved( GNC_INVOICE(term_p) ) ) {
+    	save_invoice( term_p, be );
+	}
+}
+
+static void
+write_invoices( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    qof_object_foreach( GNC_ID_INVOICE, be->primary_book, write_single_invoice, (gpointer)be );
+}
+
+/* ================================================================= */
+static void
+load_invoice_guid( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GUID guid;
+    const GUID* pGuid;
+	GncInvoice* invoice = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val != NULL ) {
+        pGuid = NULL;
+    } else {
+        string_to_guid( g_value_get_string( val ), &guid );
+        pGuid = &guid;
+    }
+	if( pGuid != NULL ) {
+		invoice = gncInvoiceLookup( be->primary_book, pGuid );
+	}
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, invoice, NULL );
+    } else {
+		(*setter)( pObject, (const gpointer)invoice );
+    }
+}
+
+static col_type_handler_t invoice_guid_handler
+	= { load_invoice_guid,
+		gnc_sql_add_objectref_guid_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+		gnc_sql_add_gvalue_objectref_guid_to_slist };
+/* ================================================================= */
+void
+gnc_invoice_sql_initialize( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_INVOICE,
+        save_invoice,						/* commit */
+        load_all_invoices,					/* initial_load */
+        create_invoice_tables,				/* create_tables */
+		NULL, NULL, NULL,
+		write_invoices						/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_INVOICE, GNC_SQL_BACKEND, &be_data );
+
+	gnc_sql_register_col_type_handler( CT_INVOICEREF, &invoice_guid_handler );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-invoice-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,37 @@
+/*
+ * gnc-invoice-sql.h -- invoice sql backend
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-invoice-sql.h
+ *  @brief load and save invoice data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_INVOICE_SQL_H
+#define GNC_INVOICE_SQL_H
+
+#define CT_INVOICEREF "invoice"
+
+void gnc_invoice_sql_initialize( void );
+
+#endif /* GNC_INVOICE_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-job-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,203 +0,0 @@
-/********************************************************************\
- * gnc-job-gda.c -- job gda backend                                 *
- *                                                                  *
- * Copyright (C) 2007-2008 Phil Longstaff (plongstaff at rogers.com)   *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <libgda/libgda.h>
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gncJobP.h"
-#include "gnc-job-gda.h"
-#include "gnc-owner-gda.h"
-
-#define _GNC_MOD_NAME	GNC_ID_JOB
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TABLE_NAME "jobs"
-
-#define MAX_ID_LEN 2048
-#define MAX_NAME_LEN 2048
-#define MAX_REFERENCE_LEN 2048
-
-static col_cvt_t col_table[] =
-{
-	{ "guid",      CT_GUID,     0,                 COL_NNUL|COL_PKEY, "guid" },
-	{ "id",        CT_STRING,   MAX_ID_LEN,        COL_NNUL,          NULL, JOB_ID },
-	{ "name",      CT_STRING,   MAX_NAME_LEN,      COL_NNUL,          NULL, JOB_NAME },
-	{ "reference", CT_STRING,   MAX_REFERENCE_LEN, COL_NNUL,          NULL, JOB_REFERENCE },
-	{ "active",    CT_BOOLEAN,  0,                 COL_NNUL,          NULL, JOB_ACTIVE },
-	{ "owner",     CT_OWNERREF, 0,                 COL_NNUL,          NULL, JOB_OWNER },
-	{ NULL }
-};
-
-static void
-load_single_job( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    const GUID* guid;
-    GUID job_guid;
-	GncJob* pJob;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    job_guid = *guid;
-
-    pJob = gncJobLookup( be->primary_book, &job_guid );
-    if( pJob == NULL ) {
-        pJob = gncJobCreate( be->primary_book );
-    }
-    gnc_gda_load_object( be, pModel, row, GNC_ID_JOB, pJob, col_table );
-    gnc_gda_slots_load( be, QOF_INSTANCE(pJob) );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pJob) );
-}
-
-static void
-load_all_jobs( GncGdaBackend* be )
-{
-    static GdaQuery* query = NULL;
-    GdaObject* ret;
-    QofBook* pBook;
-
-	g_return_if_fail( be != NULL );
-
-    pBook = be->primary_book;
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_job( be, pModel, r );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_job_tables( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    gnc_gda_create_table_if_needed( be, TABLE_NAME, col_table );
-}
-
-/* ================================================================= */
-static void
-save_job( QofInstance* inst, GncGdaBackend* be )
-{
-    const GUID* guid;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_JOB(inst) );
-	g_return_if_fail( be != NULL );
-
-    (void)gnc_gda_do_db_operation( be,
-                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
-                        TABLE_NAME,
-                        GNC_ID_JOB, inst,
-                        col_table );
-
-    // Now, commit or delete any slots
-    guid = qof_instance_get_guid( inst );
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-/* ================================================================= */
-static gboolean
-job_should_be_saved( GncJob *job )
-{
-    const char *id;
-
-	g_return_val_if_fail( job != NULL, FALSE );
-
-    /* make sure this is a valid job before we save it -- should have an ID */
-    id = gncJobGetID( job );
-    if( id == NULL || *id == '\0' ) {
-        return FALSE;
-	}
-
-    return TRUE;
-}
-
-static void
-write_single_job( QofInstance *term_p, gpointer be_p )
-{
-    GncGdaBackend* be = (GncGdaBackend*)be_p;
-
-	g_return_if_fail( term_p != NULL );
-	g_return_if_fail( GNC_IS_JOB(term_p) );
-	g_return_if_fail( be_p != NULL );
-
-	if( job_should_be_saved( GNC_JOB(term_p) ) ) {
-    	save_job( term_p, be );
-	}
-}
-
-static void
-write_jobs( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    qof_object_foreach( GNC_ID_JOB, be->primary_book, write_single_job, (gpointer)be );
-}
-
-/* ================================================================= */
-void
-gnc_job_gda_initialize( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_JOB,
-        save_job,						/* commit */
-        load_all_jobs,					/* initial_load */
-        create_job_tables,				/* create_tables */
-		NULL, NULL, NULL,
-		write_jobs						/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_JOB, GNC_GDA_BACKEND, &be_data );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-job-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,35 +0,0 @@
-/*
- * gnc-job-gda.h -- job gda backend
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-job-gda.h
- *  @brief load and save job data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_JOB_GDA_H
-#define GNC_JOB_GDA_H
-
-void gnc_job_gda_initialize( void );
-
-#endif /* GNC_JOB_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,212 @@
+/********************************************************************\
+ * gnc-job-sql.c -- job sql backend                                 *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-job-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gncJobP.h"
+#include "gnc-job-sql.h"
+#include "gnc-owner-sql.h"
+
+#define _GNC_MOD_NAME	GNC_ID_JOB
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TABLE_NAME "jobs"
+#define TABLE_VERSION 1
+
+#define MAX_ID_LEN 2048
+#define MAX_NAME_LEN 2048
+#define MAX_REFERENCE_LEN 2048
+
+static col_cvt_t col_table[] =
+{
+	{ "guid",      CT_GUID,     0,                 COL_NNUL|COL_PKEY, "guid" },
+	{ "id",        CT_STRING,   MAX_ID_LEN,        COL_NNUL,          NULL, JOB_ID },
+	{ "name",      CT_STRING,   MAX_NAME_LEN,      COL_NNUL,          NULL, JOB_NAME },
+	{ "reference", CT_STRING,   MAX_REFERENCE_LEN, COL_NNUL,          NULL, JOB_REFERENCE },
+	{ "active",    CT_BOOLEAN,  0,                 COL_NNUL,          NULL, JOB_ACTIVE },
+	{ "owner",     CT_OWNERREF, 0,                 COL_NNUL,          NULL, JOB_OWNER },
+	{ NULL }
+};
+
+static void
+load_single_job( GncSqlBackend* be, GncSqlRow* row )
+{
+    const GUID* guid;
+	GncJob* pJob;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    pJob = gncJobLookup( be->primary_book, guid );
+    if( pJob == NULL ) {
+        pJob = gncJobCreate( be->primary_book );
+    }
+    gnc_sql_load_object( be, row, GNC_ID_JOB, pJob, col_table );
+    gnc_sql_slots_load( be, QOF_INSTANCE(pJob) );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pJob) );
+}
+
+static void
+load_all_jobs( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    QofBook* pBook;
+
+	g_return_if_fail( be != NULL );
+
+    pBook = be->primary_book;
+
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_job( be, row );
+			row = gnc_sql_result_get_next_row( result );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+create_job_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+save_job( QofInstance* inst, GncSqlBackend* be )
+{
+    const GUID* guid;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_JOB(inst) );
+	g_return_if_fail( be != NULL );
+
+    (void)gnc_sql_do_db_operation( be,
+                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
+                        TABLE_NAME,
+                        GNC_ID_JOB, inst,
+                        col_table );
+
+    // Now, commit or delete any slots
+    guid = qof_instance_get_guid( inst );
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+/* ================================================================= */
+static gboolean
+job_should_be_saved( GncJob *job )
+{
+    const char *id;
+
+	g_return_val_if_fail( job != NULL, FALSE );
+
+    /* make sure this is a valid job before we save it -- should have an ID */
+    id = gncJobGetID( job );
+    if( id == NULL || *id == '\0' ) {
+        return FALSE;
+	}
+
+    return TRUE;
+}
+
+static void
+write_single_job( QofInstance *term_p, gpointer be_p )
+{
+    GncSqlBackend* be = (GncSqlBackend*)be_p;
+
+	g_return_if_fail( term_p != NULL );
+	g_return_if_fail( GNC_IS_JOB(term_p) );
+	g_return_if_fail( be_p != NULL );
+
+	if( job_should_be_saved( GNC_JOB(term_p) ) ) {
+    	save_job( term_p, be );
+	}
+}
+
+static void
+write_jobs( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    qof_object_foreach( GNC_ID_JOB, be->primary_book, write_single_job, (gpointer)be );
+}
+
+/* ================================================================= */
+void
+gnc_job_sql_initialize( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_JOB,
+        save_job,						/* commit */
+        load_all_jobs,					/* initial_load */
+        create_job_tables,				/* create_tables */
+		NULL, NULL, NULL,
+		write_jobs						/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_JOB, GNC_SQL_BACKEND, &be_data );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-job-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,35 @@
+/*
+ * gnc-job-sql.h -- job sql backend
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-job-sql.h
+ *  @brief load and save job data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_JOB_SQL_H
+#define GNC_JOB_SQL_H
+
+void gnc_job_sql_initialize( void );
+
+#endif /* GNC_JOB_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-order-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,243 +0,0 @@
-/********************************************************************\
- * gnc-order-gda.c -- order gda backend                             *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <libgda/libgda.h>
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gncOrderP.h"
-
-#include "gnc-order-gda.h"
-#include "gnc-owner-gda.h"
-
-#define _GNC_MOD_NAME	GNC_ID_ORDER
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define TABLE_NAME "orders"
-
-#define MAX_ID_LEN 2048
-#define MAX_NOTES_LEN 2048
-#define MAX_REFERENCE_LEN 2048
-
-static col_cvt_t col_table[] =
-{
-	{ "guid",        CT_GUID,     0,                 COL_NNUL|COL_PKEY, "guid" },
-	{ "id",          CT_STRING,   MAX_ID_LEN,        COL_NNUL,          NULL, ORDER_ID },
-	{ "notes",       CT_STRING,   MAX_NOTES_LEN,     COL_NNUL,          NULL, ORDER_NOTES },
-	{ "reference",   CT_STRING,   MAX_REFERENCE_LEN, COL_NNUL,          NULL, ORDER_REFERENCE },
-	{ "active",      CT_BOOLEAN,  0,                 COL_NNUL,          NULL, QOF_PARAM_ACTIVE },
-	{ "date_opened", CT_TIMESPEC, 0,                 COL_NNUL,          NULL, ORDER_OPENED },
-	{ "date_closed", CT_TIMESPEC, 0,                 COL_NNUL,          NULL, ORDER_CLOSED },
-	{ "owner",       CT_OWNERREF, 0,                 COL_NNUL,          NULL, ORDER_OWNER },
-	{ NULL },
-};
-
-static void
-load_single_order( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    const GUID* guid;
-    GUID v_guid;
-	GncOrder* pOrder;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    v_guid = *guid;
-
-    pOrder = gncOrderLookup( be->primary_book, &v_guid );
-    if( pOrder == NULL ) {
-        pOrder = gncOrderCreate( be->primary_book );
-    }
-    gnc_gda_load_object( be, pModel, row, GNC_ID_ORDER, pOrder, col_table );
-    gnc_gda_slots_load( be, QOF_INSTANCE(pOrder) );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pOrder) );
-}
-
-static void
-load_all_orders( GncGdaBackend* be )
-{
-    static GdaQuery* query = NULL;
-    GdaObject* ret;
-    QofBook* pBook;
-
-	g_return_if_fail( be != NULL );
-
-    pBook = be->primary_book;
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_order( be, pModel, r );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_order_tables( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    gnc_gda_create_table_if_needed( be, TABLE_NAME, col_table );
-}
-
-/* ================================================================= */
-static void
-save_order( QofInstance* inst, GncGdaBackend* be )
-{
-    const GUID* guid;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_ORDER(inst) );
-	g_return_if_fail( be != NULL );
-
-    (void)gnc_gda_do_db_operation( be,
-                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
-                        TABLE_NAME,
-                        GNC_ID_ORDER, inst,
-                        col_table );
-
-    // Now, commit or delete any slots
-    guid = qof_instance_get_guid( inst );
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-/* ================================================================= */
-static gboolean
-order_should_be_saved( GncOrder *order )
-{
-    const char *id;
-
-	g_return_val_if_fail( order != NULL, FALSE );
-
-    /* make sure this is a valid order before we save it -- should have an ID */
-    id = gncOrderGetID( order );
-    if( id == NULL || *id == '\0' ) {
-        return FALSE;
-	}
-
-    return TRUE;
-}
-
-static void
-write_single_order( QofInstance *term_p, gpointer be_p )
-{
-    GncGdaBackend* be = (GncGdaBackend*)be_p;
-
-	g_return_if_fail( term_p != NULL );
-	g_return_if_fail( GNC_IS_ORDER(term_p) );
-	g_return_if_fail( be_p != NULL );
-
-	if( order_should_be_saved( GNC_ORDER(term_p) ) ) {
-    	save_order( term_p, be );
-	}
-}
-
-static void
-write_orders( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    qof_object_foreach( GNC_ID_ORDER, be->primary_book, write_single_order, (gpointer)be );
-}
-
-/* ================================================================= */
-static void
-load_order_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GUID guid;
-    const GUID* pGuid;
-	GncOrder* order = NULL;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        pGuid = NULL;
-    } else {
-        string_to_guid( g_value_get_string( val ), &guid );
-        pGuid = &guid;
-    }
-	if( pGuid != NULL ) {
-		order = gncOrderLookup( be->primary_book, pGuid );
-	}
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, order, NULL );
-    } else {
-		(*setter)( pObject, (const gpointer)order );
-    }
-}
-
-static col_type_handler_t order_guid_handler =
-        { load_order_guid, gnc_gda_create_objectref_guid_col,
-            gnc_gda_get_gvalue_objectref_guid_for_query, gnc_gda_get_gvalue_objectref_guid_cond };
-/* ================================================================= */
-void
-gnc_order_gda_initialize( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_ORDER,
-        save_order,						/* commit */
-        load_all_orders,				/* initial_load */
-        create_order_tables,			/* create_tables */
-		NULL, NULL, NULL,
-		write_orders					/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_ORDER, GNC_GDA_BACKEND, &be_data );
-
-	gnc_gda_register_col_type_handler( CT_ORDERREF, &order_guid_handler );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-order-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,37 +0,0 @@
-/*
- * gnc-order-gda.h -- order gda backend
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-order-gda.h
- *  @brief load and save order data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_ORDER_GDA_H
-#define GNC_ORDER_GDA_H
-
-#define CT_ORDERREF "order"
-
-void gnc_order_gda_initialize( void );
-
-#endif /* GNC_ORDER_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,255 @@
+/********************************************************************\
+ * gnc-order-sql.c -- order sql backend                             *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-order-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gncOrderP.h"
+
+#include "gnc-order-sql.h"
+#include "gnc-owner-sql.h"
+
+#define _GNC_MOD_NAME	GNC_ID_ORDER
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define TABLE_NAME "orders"
+#define TABLE_VERSION 1
+
+#define MAX_ID_LEN 2048
+#define MAX_NOTES_LEN 2048
+#define MAX_REFERENCE_LEN 2048
+
+static col_cvt_t col_table[] =
+{
+	{ "guid",        CT_GUID,     0,                 COL_NNUL|COL_PKEY, "guid" },
+	{ "id",          CT_STRING,   MAX_ID_LEN,        COL_NNUL,          NULL, ORDER_ID },
+	{ "notes",       CT_STRING,   MAX_NOTES_LEN,     COL_NNUL,          NULL, ORDER_NOTES },
+	{ "reference",   CT_STRING,   MAX_REFERENCE_LEN, COL_NNUL,          NULL, ORDER_REFERENCE },
+	{ "active",      CT_BOOLEAN,  0,                 COL_NNUL,          NULL, QOF_PARAM_ACTIVE },
+	{ "date_opened", CT_TIMESPEC, 0,                 COL_NNUL,          NULL, ORDER_OPENED },
+	{ "date_closed", CT_TIMESPEC, 0,                 COL_NNUL,          NULL, ORDER_CLOSED },
+	{ "owner",       CT_OWNERREF, 0,                 COL_NNUL,          NULL, ORDER_OWNER },
+	{ NULL },
+};
+
+static void
+load_single_order( GncSqlBackend* be, GncSqlRow* row )
+{
+    const GUID* guid;
+	GncOrder* pOrder;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    pOrder = gncOrderLookup( be->primary_book, guid );
+    if( pOrder == NULL ) {
+        pOrder = gncOrderCreate( be->primary_book );
+    }
+    gnc_sql_load_object( be, row, GNC_ID_ORDER, pOrder, col_table );
+    gnc_sql_slots_load( be, QOF_INSTANCE(pOrder) );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pOrder) );
+}
+
+static void
+load_all_orders( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    QofBook* pBook;
+
+	g_return_if_fail( be != NULL );
+
+    pBook = be->primary_book;
+
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_order( be, row );
+			row = gnc_sql_result_get_next_row( result );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+create_order_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+save_order( QofInstance* inst, GncSqlBackend* be )
+{
+    const GUID* guid;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_ORDER(inst) );
+	g_return_if_fail( be != NULL );
+
+    (void)gnc_sql_do_db_operation( be,
+                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
+                        TABLE_NAME,
+                        GNC_ID_ORDER, inst,
+                        col_table );
+
+    // Now, commit or delete any slots
+    guid = qof_instance_get_guid( inst );
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+/* ================================================================= */
+static gboolean
+order_should_be_saved( GncOrder *order )
+{
+    const char *id;
+
+	g_return_val_if_fail( order != NULL, FALSE );
+
+    /* make sure this is a valid order before we save it -- should have an ID */
+    id = gncOrderGetID( order );
+    if( id == NULL || *id == '\0' ) {
+        return FALSE;
+	}
+
+    return TRUE;
+}
+
+static void
+write_single_order( QofInstance *term_p, gpointer be_p )
+{
+    GncSqlBackend* be = (GncSqlBackend*)be_p;
+
+	g_return_if_fail( term_p != NULL );
+	g_return_if_fail( GNC_IS_ORDER(term_p) );
+	g_return_if_fail( be_p != NULL );
+
+	if( order_should_be_saved( GNC_ORDER(term_p) ) ) {
+    	save_order( term_p, be );
+	}
+}
+
+static void
+write_orders( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    qof_object_foreach( GNC_ID_ORDER, be->primary_book, write_single_order, (gpointer)be );
+}
+
+/* ================================================================= */
+static void
+load_order_guid( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GUID guid;
+    const GUID* pGuid;
+	GncOrder* order = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val != NULL ) {
+        pGuid = NULL;
+    } else {
+        string_to_guid( g_value_get_string( val ), &guid );
+        pGuid = &guid;
+    }
+	if( pGuid != NULL ) {
+		order = gncOrderLookup( be->primary_book, pGuid );
+	}
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, order, NULL );
+    } else {
+		(*setter)( pObject, (const gpointer)order );
+    }
+}
+
+static col_type_handler_t order_guid_handler
+	= { load_order_guid,
+		gnc_sql_add_objectref_guid_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+		gnc_sql_add_gvalue_objectref_guid_to_slist };
+/* ================================================================= */
+void
+gnc_order_sql_initialize( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_ORDER,
+        save_order,						/* commit */
+        load_all_orders,				/* initial_load */
+        create_order_tables,			/* create_tables */
+		NULL, NULL, NULL,
+		write_orders					/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_ORDER, GNC_SQL_BACKEND, &be_data );
+
+	gnc_sql_register_col_type_handler( CT_ORDERREF, &order_guid_handler );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-order-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,37 @@
+/*
+ * gnc-order-sql.h -- order sql backend
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-order-sql.h
+ *  @brief load and save order data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_ORDER_SQL_H
+#define GNC_ORDER_SQL_H
+
+#define CT_ORDERREF "order"
+
+void gnc_order_sql_initialize( void );
+
+#endif /* GNC_ORDER_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-owner-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,329 +0,0 @@
-/********************************************************************\
- * gnc-owner-xml-v2.c -- owner xml i/o implementation           *
- *                                                                  *
- * Copyright (C) 2002 Derek Atkins <warlord at MIT.EDU>                *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <libgda/libgda.h>
-#include "gnc-backend-util-gda.h"
-
-#include "gnc-owner-gda.h"
-#include "gncCustomerP.h"
-#include "gncJobP.h"
-#include "gncEmployeeP.h"
-#include "gncVendorP.h"
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-typedef void (*OwnerSetterFunc)( gpointer, GncOwner* );
-typedef GncOwner* (*OwnerGetterFunc)( const gpointer );
-
-static void
-load_owner( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    gchar* buf;
-	GncOwnerType type;
-    GUID guid;
-	QofBook* book;
-	GncOwner owner;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-	book = be->primary_book;
-    buf = g_strdup_printf( "%s_type", table_row->col_name );
-    val = gda_data_model_get_value_at_col_name( pModel, buf, row );
-	type = (GncOwnerType)g_value_get_int( val );
-    g_free( buf );
-    buf = g_strdup_printf( "%s_guid", table_row->col_name );
-    val = gda_data_model_get_value_at_col_name( pModel, buf, row );
-    g_free( buf );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( !gda_value_is_null( val ) ) {
-        string_to_guid( g_value_get_string( val ), &guid );
-    }
-
-    switch( type ) {
-  	case GNC_OWNER_CUSTOMER:
-  		{
-    		GncCustomer *cust = gncCustomerLookup( book, &guid );
-    		if( cust == NULL ) {
-      			cust = gncCustomerCreate( book );
-      			gncCustomerSetGUID( cust, &guid );
-    		}
-    		gncOwnerInitCustomer( &owner, cust );
-    		break; 
-  		}
-
-  	case GNC_OWNER_JOB:
-  		{
-    		GncJob *job = gncJobLookup( book, &guid );
-    		if( job == NULL ) {
-      			job = gncJobCreate( book );
-      			gncJobSetGUID( job, &guid );
-    		}
-    		gncOwnerInitJob( &owner, job );
-    		break; 
-  		}
-
-  	case GNC_OWNER_VENDOR:
-  		{
-    		GncVendor *vendor = gncVendorLookup( book, &guid );
-    		if( vendor == NULL ) {
-      			vendor = gncVendorCreate( book );
-    		  	gncVendorSetGUID( vendor, &guid );
-    		}
-    		gncOwnerInitVendor( &owner, vendor );
-    		break; 
-  		}
-
-  	case GNC_OWNER_EMPLOYEE:
-  		{
-    		GncEmployee *employee = gncEmployeeLookup( book, &guid );
-    		if( employee == NULL ) {
-      			employee = gncEmployeeCreate( book );
-      			gncEmployeeSetGUID( employee, &guid );
-    		}
-    		gncOwnerInitEmployee( &owner, employee );
-    		break; 
-  		}
-
-  	default:
-    	PWARN("Invalid owner type: %d\n", type );
-	}
-
-	if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, &owner, NULL );
-	} else {
-    	(*setter)( pObject, &owner );
-	}
-}
-
-static void
-get_gvalue_owner( const GncGdaBackend* be, QofIdTypeConst obj_name, const gpointer pObject,
-                	const col_cvt_t* table_row, GValue* value )
-{
-    OwnerGetterFunc getter;
-    GncOwner* owner;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( value != NULL );
-
-    memset( value, 0, sizeof( GValue ) );
-
-	getter = (OwnerGetterFunc)gnc_gda_get_getter( obj_name, table_row );
-    owner = (*getter)( pObject );
-    g_value_init( value, G_TYPE_POINTER );
-    g_value_set_object( value, owner );
-}
-
-static void
-get_gvalue_owner_for_query( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                			const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-    GValue subfield_value;
-    GncOwner* owner;
-	gchar* buf;
-    const GUID* guid;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-	GncOwnerType type;
-	QofInstance* inst = NULL;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( obj_name != NULL );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-	g_return_if_fail( query != NULL );
-
-    memset( &value, 0, sizeof( GValue ) );
-    get_gvalue_owner( be, obj_name, pObject, table_row, &value );
-
-    if( G_VALUE_TYPE(&value) != 0 ) {
-        owner = g_value_get_object( &value );
-    	buf = g_strdup_printf( "%s_type", table_row->col_name );
-    	memset( &subfield_value, 0, sizeof( GValue ) );
-        g_value_init( &subfield_value, G_TYPE_INT );
-        type = gncOwnerGetType( owner );
-        g_value_set_int( &subfield_value, type );
-    	gnc_gda_add_field_to_query( query, buf, &subfield_value );
-		g_free( buf );
-
-    	buf = g_strdup_printf( "%s_guid", table_row->col_name );
-    	memset( &subfield_value, 0, sizeof( GValue ) );
-    	switch( type ) {
-  		case GNC_OWNER_CUSTOMER:
-    		inst = QOF_INSTANCE(gncOwnerGetCustomer( owner ));
-    		break; 
-
-  		case GNC_OWNER_JOB:
-    		inst = QOF_INSTANCE(gncOwnerGetJob( owner ));
-    		break; 
-
-  		case GNC_OWNER_VENDOR:
-    		inst = QOF_INSTANCE(gncOwnerGetVendor( owner ));
-    		break; 
-
-  		case GNC_OWNER_EMPLOYEE:
-    		inst = QOF_INSTANCE(gncOwnerGetEmployee( owner ));
-    		break; 
-
-  		default:
-    		PWARN("Invalid owner type: %d\n", type );
-		}
-		if( inst != NULL ) {
-			guid = qof_instance_get_guid( inst );
-    		if( guid != NULL ) {
-        		(void)guid_to_string_buff( guid, guid_buf );
-        		g_value_init( &subfield_value, G_TYPE_STRING );
-        		g_value_set_string( &subfield_value, guid_buf );
-    			gnc_gda_add_field_to_query( query, buf, &subfield_value );
-    		}
-		}
-		g_free( buf );
-    }
-}
-
-static GdaQueryCondition*
-get_gvalue_owner_cond( const GncGdaBackend* be, QofIdTypeConst obj_name,
-                const gpointer pObject, const col_cvt_t* table_row, GdaQuery* query )
-{
-    GValue value;
-    GValue subfield_value;
-    GncOwner* owner;
-	gchar* buf;
-    const GUID* guid;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-	GncOwnerType type;
-	QofInstance* inst = NULL;
-    GdaQueryCondition* sub_cond;
-    GdaQueryCondition* cond;
-
-	g_return_val_if_fail( be != NULL, NULL );
-	g_return_val_if_fail( obj_name != NULL, NULL );
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( table_row != NULL, NULL );
-	g_return_val_if_fail( query != NULL, NULL );
-
-    memset( &value, 0, sizeof( GValue ) );
-    get_gvalue_owner( be, obj_name, pObject, table_row, &value );
-    cond = gda_query_condition_new( query, GDA_QUERY_CONDITION_NODE_AND );
-
-    if( G_VALUE_TYPE(&value) != 0 ) {
-        owner = g_value_get_object( &value );
-    	buf = g_strdup_printf( "%s_type", table_row->col_name );
-    	memset( &subfield_value, 0, sizeof( GValue ) );
-        g_value_init( &subfield_value, G_TYPE_INT );
-        type = gncOwnerGetType( owner );
-        g_value_set_int( &subfield_value, type );
-    	sub_cond = gnc_gda_create_condition_from_field( query, buf, &subfield_value );
-    	gda_query_condition_node_add_child( cond, sub_cond, NULL );
-		g_free( buf );
-
-    	buf = g_strdup_printf( "%s_guid", table_row->col_name );
-    	memset( &subfield_value, 0, sizeof( GValue ) );
-    	switch( type ) {
-  		case GNC_OWNER_CUSTOMER:
-    		inst = QOF_INSTANCE(gncOwnerGetCustomer( owner ));
-    		break; 
-
-  		case GNC_OWNER_JOB:
-    		inst = QOF_INSTANCE(gncOwnerGetJob( owner ));
-    		break; 
-
-  		case GNC_OWNER_VENDOR:
-    		inst = QOF_INSTANCE(gncOwnerGetVendor( owner ));
-    		break; 
-
-  		case GNC_OWNER_EMPLOYEE:
-    		inst = QOF_INSTANCE(gncOwnerGetEmployee( owner ));
-    		break; 
-
-  		default:
-    		PWARN("Invalid owner type: %d\n", type );
-		}
-		if( inst != NULL ) {
-			guid = qof_instance_get_guid( inst );
-    		if( guid != NULL ) {
-        		(void)guid_to_string_buff( guid, guid_buf );
-        		g_value_init( &subfield_value, G_TYPE_STRING );
-        		g_value_set_string( &subfield_value, guid_buf );
-    			sub_cond = gnc_gda_create_condition_from_field( query, buf, &subfield_value );
-		    	gda_query_condition_node_add_child( cond, sub_cond, NULL );
-    		}
-		}
-		g_free( buf );
-    }
-
-    return cond;
-}
-
-static void
-create_owner_col( GdaServerProvider* server, GdaConnection* cnn,
-    	        GdaServerOperation* op, const col_cvt_t* table_row )
-{
-    const gchar* dbms_type;
-    gchar* buf;
-	const col_cvt_t* subtable_row;
-
-	g_return_if_fail( server != NULL );
-	g_return_if_fail( cnn != NULL );
-	g_return_if_fail( op != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    dbms_type = gda_server_provider_get_default_dbms_type( server, cnn, G_TYPE_INT );
-    buf = g_strdup_printf( "%s_type", table_row->col_name );
-   	gnc_gda_add_table_column( op, buf, dbms_type, table_row->size, table_row->flags );
-   	g_free( buf );
-    dbms_type = gda_server_provider_get_default_dbms_type( server, cnn,
-                                                            G_TYPE_STRING );
-    buf = g_strdup_printf( "%s_guid", table_row->col_name );
-   	gnc_gda_add_table_column( op, buf, dbms_type, GUID_ENCODING_LENGTH, table_row->flags );
-   	g_free( buf );
-}
-
-static col_type_handler_t owner_handler =
-        { load_owner, create_owner_col,
-            get_gvalue_owner_for_query, get_gvalue_owner_cond };
-
-/* ================================================================= */
-void
-gnc_owner_gda_initialize( void )
-{
-	gnc_gda_register_col_type_handler( CT_OWNERREF, &owner_handler );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-owner-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,39 +0,0 @@
-/* gnc-owner-gda.h -- Owner GDA header
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-owner-gda.h
- *  @brief load and save owner data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_OWNER_GDA_H
-#define GNC_OWNER_GDA_H
-
-#include "gncOwner.h"
-#include "qof.h"
-
-#define CT_OWNERREF "owner"
-
-void gnc_owner_gda_initialize( void );
-
-#endif /* GNC_OWNER_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,272 @@
+/********************************************************************\
+ * gnc-owner-sql.c -- owner sql implementation                      *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-owner-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-backend-util-sql.h"
+
+#include "gnc-owner-sql.h"
+#include "gncCustomerP.h"
+#include "gncJobP.h"
+#include "gncEmployeeP.h"
+#include "gncVendorP.h"
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+typedef void (*OwnerSetterFunc)( gpointer, GncOwner* );
+typedef GncOwner* (*OwnerGetterFunc)( const gpointer );
+
+static void
+load_owner( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    gchar* buf;
+	GncOwnerType type;
+    GUID guid;
+	QofBook* book;
+	GncOwner owner;
+	GUID* pGuid;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+	book = be->primary_book;
+    buf = g_strdup_printf( "%s_type", table_row->col_name );
+    val = gnc_sql_row_get_value_at_col_name( row, buf );
+	type = (GncOwnerType)g_value_get_int( val );
+    g_free( buf );
+    buf = g_strdup_printf( "%s_guid", table_row->col_name );
+    val = gnc_sql_row_get_value_at_col_name( row, buf );
+    g_free( buf );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val != NULL ) {
+        string_to_guid( g_value_get_string( val ), &guid );
+    }
+	pGuid = &guid;
+
+    switch( type ) {
+  	case GNC_OWNER_CUSTOMER:
+  		{
+    		GncCustomer *cust = gncCustomerLookup( book, pGuid );
+    		if( cust == NULL ) {
+      			cust = gncCustomerCreate( book );
+      			gncCustomerSetGUID( cust, &guid );
+    		}
+    		gncOwnerInitCustomer( &owner, cust );
+    		break; 
+  		}
+
+  	case GNC_OWNER_JOB:
+  		{
+    		GncJob *job = gncJobLookup( book, pGuid );
+    		if( job == NULL ) {
+      			job = gncJobCreate( book );
+      			gncJobSetGUID( job, &guid );
+    		}
+    		gncOwnerInitJob( &owner, job );
+    		break; 
+  		}
+
+  	case GNC_OWNER_VENDOR:
+  		{
+    		GncVendor *vendor = gncVendorLookup( book, pGuid );
+    		if( vendor == NULL ) {
+      			vendor = gncVendorCreate( book );
+    		  	gncVendorSetGUID( vendor, &guid );
+    		}
+    		gncOwnerInitVendor( &owner, vendor );
+    		break; 
+  		}
+
+  	case GNC_OWNER_EMPLOYEE:
+  		{
+    		GncEmployee *employee = gncEmployeeLookup( book, pGuid );
+    		if( employee == NULL ) {
+      			employee = gncEmployeeCreate( book );
+      			gncEmployeeSetGUID( employee, &guid );
+    		}
+    		gncOwnerInitEmployee( &owner, employee );
+    		break; 
+  		}
+
+  	default:
+    	PWARN("Invalid owner type: %d\n", type );
+	}
+
+	if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, &owner, NULL );
+	} else {
+    	(*setter)( pObject, &owner );
+	}
+}
+
+static void
+add_owner_col_info_to_list( const GncSqlBackend* be, const col_cvt_t* table_row,
+								GList** pList )
+{
+	GncSqlColumnInfo* info;
+    gchar* buf;
+	const col_cvt_t* subtable_row;
+	const gchar* type;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( table_row != NULL );
+	g_return_if_fail( pList != NULL );
+
+    buf = g_strdup_printf( "%s_type", table_row->col_name );
+	info = g_new0( GncSqlColumnInfo, 1 );
+	info->name = buf;
+	info->type_name = gnc_sql_connection_get_column_type_name( be->conn,
+											G_TYPE_INT, table_row->size );
+	info->is_primary_key = (table_row->flags & COL_PKEY) ? TRUE : FALSE;
+	info->null_allowed = (table_row->flags & COL_NNUL) ? FALSE : TRUE;
+	*pList = g_list_append( *pList, info );
+
+   	buf = g_strdup_printf( "%s_guid", table_row->col_name );
+	info = g_new0( GncSqlColumnInfo, 1 );
+	info->name = buf;
+	info->type_name = gnc_sql_connection_get_column_type_name( be->conn,
+										G_TYPE_STRING, GUID_ENCODING_LENGTH );
+	info->is_primary_key = (table_row->flags & COL_PKEY) ? TRUE : FALSE;
+	info->null_allowed = (table_row->flags & COL_NNUL) ? FALSE : TRUE;
+	*pList = g_list_append( *pList, info );
+}
+
+static void
+add_colname_to_list( const col_cvt_t* table_row, GList** pList )
+{
+    gchar* buf;
+
+    buf = g_strdup_printf( "%s_type", table_row->col_name );
+	(*pList) = g_list_append( (*pList), buf );
+    buf = g_strdup_printf( "%s_guid", table_row->col_name );
+	(*pList) = g_list_append( (*pList), buf );
+}
+
+static void
+add_gvalue_owner_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
+                	const gpointer pObject, const col_cvt_t* table_row, GSList** pList )
+{
+    GValue* subfield_value;
+    GncOwner* owner;
+	gchar* buf;
+    const GUID* guid;
+    gchar guid_buf[GUID_ENCODING_LENGTH+1];
+	GncOwnerType type;
+	QofInstance* inst = NULL;
+    OwnerGetterFunc getter;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( obj_name != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+	getter = (OwnerGetterFunc)gnc_sql_get_getter( obj_name, table_row );
+    owner = (*getter)( pObject );
+
+	if( owner != NULL ) {
+    	buf = g_strdup_printf( "%s_type", table_row->col_name );
+		subfield_value = g_new0( GValue, 1 );
+        g_value_init( subfield_value, G_TYPE_INT );
+        type = gncOwnerGetType( owner );
+        g_value_set_int( subfield_value, type );
+		(*pList) = g_slist_append( (*pList), subfield_value );
+		g_free( buf );
+
+    	buf = g_strdup_printf( "%s_guid", table_row->col_name );
+		subfield_value = g_new0( GValue, 1 );
+    	switch( type ) {
+  		case GNC_OWNER_CUSTOMER:
+    		inst = QOF_INSTANCE(gncOwnerGetCustomer( owner ));
+    		break; 
+
+  		case GNC_OWNER_JOB:
+    		inst = QOF_INSTANCE(gncOwnerGetJob( owner ));
+    		break; 
+
+  		case GNC_OWNER_VENDOR:
+    		inst = QOF_INSTANCE(gncOwnerGetVendor( owner ));
+    		break; 
+
+  		case GNC_OWNER_EMPLOYEE:
+    		inst = QOF_INSTANCE(gncOwnerGetEmployee( owner ));
+    		break; 
+
+  		default:
+    		PWARN("Invalid owner type: %d\n", type );
+		}
+        g_value_init( subfield_value, G_TYPE_STRING );
+		if( inst != NULL ) {
+			guid = qof_instance_get_guid( inst );
+    		if( guid != NULL ) {
+        		(void)guid_to_string_buff( guid, guid_buf );
+        		g_value_take_string( subfield_value, g_strdup_printf( "'%s'", guid_buf ) );
+    		} else {
+				g_value_set_string( subfield_value, "NULL" );
+			}
+    	} else {
+			g_value_set_string( subfield_value, "NULL" );
+		}
+		(*pList) = g_slist_append( (*pList), subfield_value );
+		g_free( buf );
+    } else {
+		subfield_value = g_new0( GValue, 1 );
+		g_value_init( subfield_value, G_TYPE_STRING );
+		g_value_set_string( subfield_value, "NULL" );
+		(*pList) = g_slist_append( (*pList), subfield_value );
+		subfield_value = g_new0( GValue, 1 );
+		g_value_init( subfield_value, G_TYPE_STRING );
+		g_value_set_string( subfield_value, "NULL" );
+		(*pList) = g_slist_append( (*pList), subfield_value );
+	}
+}
+
+static col_type_handler_t owner_handler
+	= { load_owner,
+		add_owner_col_info_to_list,
+		add_colname_to_list,
+        add_gvalue_owner_to_slist };
+
+/* ================================================================= */
+void
+gnc_owner_sql_initialize( void )
+{
+	gnc_sql_register_col_type_handler( CT_OWNERREF, &owner_handler );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-owner-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,39 @@
+/* gnc-owner-sql.h -- Owner SQL header
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-owner-sql.h
+ *  @brief load and save owner data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_OWNER_SQL_H
+#define GNC_OWNER_SQL_H
+
+#include "gncOwner.h"
+#include "qof.h"
+
+#define CT_OWNERREF "owner"
+
+void gnc_owner_sql_initialize( void );
+
+#endif /* GNC_OWNER_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-tax-table-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,390 +0,0 @@
-/********************************************************************\
- * gnc-tax-table-xml-v2.c -- tax table xml i/o implementation       *
- *                                                                  *
- * Copyright (C) 2002 Derek Atkins <warlord at MIT.EDU>                *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <libgda/libgda.h>
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gncEntry.h"
-#include "gncTaxTableP.h"
-
-#include "gnc-tax-table-gda.h"
-
-#define _GNC_MOD_NAME	GNC_ID_TAXTABLE
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-static void set_invisible( gpointer data, gboolean value );
-
-typedef struct {
-    GncGdaBackend* be;
-    const GUID* guid;
-} guid_info_t;
-
-static gpointer get_obj_guid( gpointer pObject, const QofParam* param );
-static void set_obj_guid( gpointer pObject, gpointer pValue );
-static gpointer get_child( gpointer pObject, const QofParam* param );
-static void set_parent( gpointer pObject, gpointer pValue );
-
-#define MAX_NAME_LEN 50
-
-#define TT_TABLE_NAME "taxtables"
-
-static col_cvt_t tt_col_table[] =
-{
-	{ "guid",      CT_GUID,        0,            COL_NNUL|COL_PKEY, "guid" },
-	{ "name",      CT_STRING,      MAX_NAME_LEN, COL_NNUL,          NULL, GNC_TT_NAME },
-	{ "refcount",  CT_INT64,       0,            COL_NNUL,          NULL, GNC_TT_REFCOUNT },
-	{ "invisible", CT_BOOLEAN,     0,            COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)gncTaxTableGetInvisible, (QofSetterFunc)set_invisible },
-	{ "child",     CT_TAXTABLEREF, 0,			 0,                 NULL, NULL,
-			get_child, (QofSetterFunc)gncTaxTableSetChild },
-	{ "parent",    CT_TAXTABLEREF, 0,			 0,                 NULL, NULL,
-			(QofAccessFunc)gncTaxTableGetParent, set_parent },
-	{ NULL }
-};
-
-#define TTENTRIES_TABLE_NAME "taxtable_entries"
-
-static col_cvt_t ttentries_col_table[] =
-{
-	{ "taxtable", CT_TAXTABLEREF, 0, COL_NNUL, NULL, NULL,
-			(QofAccessFunc)gncTaxTableEntryGetTable, set_obj_guid },
-	{ "account",  CT_ACCOUNTREF,  0, COL_NNUL, NULL, NULL,
-			(QofAccessFunc)gncTaxTableEntryGetAccount, (QofSetterFunc)gncTaxTableEntrySetAccount },
-	{ "amount",   CT_NUMERIC,     0, COL_NNUL, NULL, NULL,
-			(QofAccessFunc)gncTaxTableEntryGetAmount, (QofSetterFunc)gncTaxTableEntrySetAmount },
-	{ "type",     CT_INT,         0, COL_NNUL, NULL, NULL,
-			(QofAccessFunc)gncTaxTableEntryGetType, (QofSetterFunc)gncTaxTableEntrySetType },
-	{ NULL }
-};
-
-/* Special column table because we need to be able to access the table by
-a column other than the primary key */
-static col_cvt_t guid_col_table[] =
-{
-    { "taxtable", CT_GUID, 0, 0, NULL, NULL, get_obj_guid, set_obj_guid },
-    { NULL }
-};
-
-static gpointer
-get_obj_guid( gpointer pObject, const QofParam* param )
-{
-    guid_info_t* pInfo = (guid_info_t*)pObject;
-
-	g_return_val_if_fail( pInfo != NULL, NULL );
-
-    return (gpointer)pInfo->guid;
-}
-
-static void
-set_obj_guid( gpointer pObject, gpointer pValue )
-{
-    // Nowhere to put the GUID
-}
-
-static void
-set_invisible( gpointer data, gboolean value )
-{
-	GncTaxTable* tt = GNC_TAXTABLE(data);
-
-	g_return_if_fail( data != NULL );
-	g_return_if_fail( GNC_IS_TAXTABLE(data) );
-
-	if( value ) {
-		gncTaxTableMakeInvisible( tt );
-	}
-}
-
-static gpointer
-get_child( gpointer pObject, const QofParam* param )
-{
-	GncTaxTable* tt = GNC_TAXTABLE(pObject);
-
-	g_return_val_if_fail( pObject != NULL, NULL );
-	g_return_val_if_fail( GNC_IS_TAXTABLE(pObject), NULL );
-
-	return gncTaxTableGetChild( tt );
-}
-
-static void
-set_parent( gpointer data, gpointer value )
-{
-	GncTaxTable* tt = GNC_TAXTABLE(data);
-	GncTaxTable* parent;
-
-	g_return_if_fail( data != NULL );
-	g_return_if_fail( GNC_IS_TAXTABLE(data) );
-
-	if( value != NULL ) {
-		parent = GNC_TAXTABLE(value);
-		gncTaxTableSetParent( tt, parent );
-	}
-}
-
-static void
-load_single_ttentry( GncGdaBackend* be, GdaDataModel* pModel, int row, GncTaxTable* tt )
-{
-	GncTaxTableEntry* e = gncTaxTableEntryCreate();
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( tt != NULL );
-
-    gnc_gda_load_object( be, pModel, row, GNC_ID_TAXTABLE, e, ttentries_col_table );
-	gncTaxTableAddEntry( tt, e );
-}
-
-static void
-load_taxtable_entries( GncGdaBackend* be, GncTaxTable* tt )
-{
-    GdaObject* ret;
-    gchar guid_buf[GUID_ENCODING_LENGTH+1];
-    GdaQuery* query;
-    GdaQueryCondition* cond;
-    GValue value;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( tt != NULL );
-
-    guid_to_string_buff( qof_instance_get_guid( QOF_INSTANCE(tt) ), guid_buf );
-    memset( &value, 0, sizeof( GValue ) );
-    g_value_init( &value, G_TYPE_STRING );
-    g_value_set_string( &value, guid_buf );
-    query = gnc_gda_create_select_query( be, TTENTRIES_TABLE_NAME );
-    cond = gnc_gda_create_condition_from_field( query, "taxtable", &value );
-    gda_query_set_condition( query, cond );
-    g_object_unref( G_OBJECT(cond) );
-
-    ret = gnc_gda_execute_query( be, query );
-    g_object_unref( G_OBJECT(query) );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_ttentry( be, pModel, r, tt );
-        }
-    }
-}
-
-static void
-load_single_taxtable( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    const GUID* guid;
-    GUID v_guid;
-	GncTaxTable* tt;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    v_guid = *guid;
-
-    tt = gncTaxTableLookup( be->primary_book, &v_guid );
-    if( tt == NULL ) {
-        tt = gncTaxTableCreate( be->primary_book );
-    }
-    gnc_gda_load_object( be, pModel, row, GNC_ID_TAXTABLE, tt, tt_col_table );
-    gnc_gda_slots_load( be, QOF_INSTANCE(tt) );
-	load_taxtable_entries( be, tt );
-
-    qof_instance_mark_clean( QOF_INSTANCE(tt) );
-}
-
-static void
-load_all_taxtables( GncGdaBackend* be )
-{
-    static GdaQuery* query = NULL;
-    GdaObject* ret;
-
-	g_return_if_fail( be != NULL );
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TT_TABLE_NAME );
-    }
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            load_single_taxtable( be, pModel, r );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_taxtable_tables( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    gnc_gda_create_table_if_needed( be, TT_TABLE_NAME, tt_col_table );
-    gnc_gda_create_table_if_needed( be, TTENTRIES_TABLE_NAME, ttentries_col_table );
-}
-
-/* ================================================================= */
-static void
-delete_all_tt_entries( GncGdaBackend* be, const GUID* guid )
-{
-    guid_info_t guid_info;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( guid != NULL );
-
-    guid_info.be = be;
-    guid_info.guid = guid;
-    (void)gnc_gda_do_db_operation( be, OP_DB_DELETE, TTENTRIES_TABLE_NAME,
-                                TTENTRIES_TABLE_NAME, &guid_info, guid_col_table );
-}
-
-static void
-save_tt_entries( GncGdaBackend* be, const GUID* guid, GList* entries )
-{
-	GList* entry;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( guid != NULL );
-
-    /* First, delete the old slots for this object */
-    delete_all_tt_entries( be, guid );
-
-	for( entry = entries; entry != NULL; entry = entry->next ) {
-		GncTaxTableEntry* e = (GncTaxTableEntry*)entry->data;
-    	(void)gnc_gda_do_db_operation( be,
-                        OP_DB_ADD_OR_UPDATE,
-                        TTENTRIES_TABLE_NAME,
-                        GNC_ID_TAXTABLE, e,
-                        ttentries_col_table );
-    }
-}
-
-static void
-save_taxtable( QofInstance* inst, GncGdaBackend* be )
-{
-    GncTaxTable* tt = GNC_TAXTABLE(inst);
-    const GUID* guid;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_TAXTABLE(inst) );
-	g_return_if_fail( be != NULL );
-
-    (void)gnc_gda_do_db_operation( be,
-                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
-                        TT_TABLE_NAME,
-                        GNC_ID_TAXTABLE, tt,
-                        tt_col_table );
-
-    // Now, commit or delete any slots and tax table entries
-    guid = qof_instance_get_guid( inst );
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-		save_tt_entries( be, guid, gncTaxTableGetEntries( tt ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-		delete_all_tt_entries( be, guid );
-    }
-}
-
-/* ================================================================= */
-static void
-write_taxtables( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    qof_object_foreach( GNC_ID_TAXTABLE, be->primary_book, (QofInstanceForeachCB)save_taxtable, (gpointer)be );
-}
-
-/* ================================================================= */
-static void
-load_taxtable_guid( const GncGdaBackend* be, GdaDataModel* pModel, gint row,
-            QofSetterFunc setter, gpointer pObject,
-            const col_cvt_t* table_row )
-{
-    const GValue* val;
-    GUID guid;
-    const GUID* pGuid;
-	GncTaxTable* taxtable = NULL;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-	g_return_if_fail( pObject != NULL );
-	g_return_if_fail( table_row != NULL );
-
-    val = gda_data_model_get_value_at_col_name( pModel, table_row->col_name, row );
-    if( gda_value_is_null( val ) ) {
-        pGuid = NULL;
-    } else {
-        string_to_guid( g_value_get_string( val ), &guid );
-        pGuid = &guid;
-    }
-	if( pGuid != NULL ) {
-		taxtable = gncTaxTableLookup( be->primary_book, pGuid );
-	}
-    if( table_row->gobj_param_name != NULL ) {
-		g_object_set( pObject, table_row->gobj_param_name, taxtable, NULL );
-    } else {
-		(*setter)( pObject, (const gpointer)taxtable );
-    }
-}
-
-static col_type_handler_t taxtable_guid_handler =
-        { load_taxtable_guid, gnc_gda_create_objectref_guid_col,
-            gnc_gda_get_gvalue_objectref_guid_for_query, gnc_gda_get_gvalue_objectref_guid_cond };
-/* ================================================================= */
-void
-gnc_taxtable_gda_initialize( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_TAXTABLE,
-        save_taxtable,						/* commit */
-        load_all_taxtables,					/* initial_load */
-        create_taxtable_tables,				/* create_tables */
-		NULL, NULL, NULL,
-		write_taxtables						/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_TAXTABLE, GNC_GDA_BACKEND, &be_data );
-
-	gnc_gda_register_col_type_handler( CT_TAXTABLEREF, &taxtable_guid_handler );
-}
-/* ========================== END OF FILE ===================== */
-

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-tax-table-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,37 +0,0 @@
-/*
- * gnc-tax-table-gda.h -- tax table gda backend
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-tax-table-gda.h
- *  @brief load and save tax table data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_TAXTABLE_GDA_H
-#define GNC_TAXTABLE_GDA_H
-
-#define CT_TAXTABLEREF "tax-table"
-
-void gnc_taxtable_gda_initialize( void );
-
-#endif /* GNC_TAXTABLE_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,408 @@
+/********************************************************************\
+ * gnc-tax-table-sql.c -- tax table sql implementation              *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-tax-table-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gncEntry.h"
+#include "gncTaxTableP.h"
+
+#include "gnc-tax-table-sql.h"
+
+#define _GNC_MOD_NAME	GNC_ID_TAXTABLE
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+static void set_invisible( gpointer data, gboolean value );
+
+typedef struct {
+    GncSqlBackend* be;
+    const GUID* guid;
+} guid_info_t;
+
+static gpointer get_obj_guid( gpointer pObject, const QofParam* param );
+static void set_obj_guid( gpointer pObject, gpointer pValue );
+static gpointer get_child( gpointer pObject, const QofParam* param );
+static void set_parent( gpointer pObject, gpointer pValue );
+
+#define MAX_NAME_LEN 50
+
+#define TT_TABLE_NAME "taxtables"
+#define TT_TABLE_VERSION 1
+
+static col_cvt_t tt_col_table[] =
+{
+	{ "guid",      CT_GUID,        0,            COL_NNUL|COL_PKEY, "guid" },
+	{ "name",      CT_STRING,      MAX_NAME_LEN, COL_NNUL,          NULL, GNC_TT_NAME },
+	{ "refcount",  CT_INT64,       0,            COL_NNUL,          NULL, GNC_TT_REFCOUNT },
+	{ "invisible", CT_BOOLEAN,     0,            COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)gncTaxTableGetInvisible, (QofSetterFunc)set_invisible },
+	{ "child",     CT_TAXTABLEREF, 0,			 0,                 NULL, NULL,
+			get_child, (QofSetterFunc)gncTaxTableSetChild },
+	{ "parent",    CT_TAXTABLEREF, 0,			 0,                 NULL, NULL,
+			(QofAccessFunc)gncTaxTableGetParent, set_parent },
+	{ NULL }
+};
+
+#define TTENTRIES_TABLE_NAME "taxtable_entries"
+#define TTENTRIES_TABLE_VERSION 1
+
+static col_cvt_t ttentries_col_table[] =
+{
+	{ "taxtable", CT_TAXTABLEREF, 0, COL_NNUL, NULL, NULL,
+			(QofAccessFunc)gncTaxTableEntryGetTable, set_obj_guid },
+	{ "account",  CT_ACCOUNTREF,  0, COL_NNUL, NULL, NULL,
+			(QofAccessFunc)gncTaxTableEntryGetAccount, (QofSetterFunc)gncTaxTableEntrySetAccount },
+	{ "amount",   CT_NUMERIC,     0, COL_NNUL, NULL, NULL,
+			(QofAccessFunc)gncTaxTableEntryGetAmount, (QofSetterFunc)gncTaxTableEntrySetAmount },
+	{ "type",     CT_INT,         0, COL_NNUL, NULL, NULL,
+			(QofAccessFunc)gncTaxTableEntryGetType, (QofSetterFunc)gncTaxTableEntrySetType },
+	{ NULL }
+};
+
+/* Special column table because we need to be able to access the table by
+a column other than the primary key */
+static col_cvt_t guid_col_table[] =
+{
+    { "taxtable", CT_GUID, 0, 0, NULL, NULL, get_obj_guid, set_obj_guid },
+    { NULL }
+};
+
+static gpointer
+get_obj_guid( gpointer pObject, const QofParam* param )
+{
+    guid_info_t* pInfo = (guid_info_t*)pObject;
+
+	g_return_val_if_fail( pInfo != NULL, NULL );
+
+    return (gpointer)pInfo->guid;
+}
+
+static void
+set_obj_guid( gpointer pObject, gpointer pValue )
+{
+    // Nowhere to put the GUID
+}
+
+static void
+set_invisible( gpointer data, gboolean value )
+{
+	GncTaxTable* tt = GNC_TAXTABLE(data);
+
+	g_return_if_fail( data != NULL );
+	g_return_if_fail( GNC_IS_TAXTABLE(data) );
+
+	if( value ) {
+		gncTaxTableMakeInvisible( tt );
+	}
+}
+
+static gpointer
+get_child( gpointer pObject, const QofParam* param )
+{
+	GncTaxTable* tt = GNC_TAXTABLE(pObject);
+
+	g_return_val_if_fail( pObject != NULL, NULL );
+	g_return_val_if_fail( GNC_IS_TAXTABLE(pObject), NULL );
+
+	return gncTaxTableGetChild( tt );
+}
+
+static void
+set_parent( gpointer data, gpointer value )
+{
+	GncTaxTable* tt = GNC_TAXTABLE(data);
+	GncTaxTable* parent;
+
+	g_return_if_fail( data != NULL );
+	g_return_if_fail( GNC_IS_TAXTABLE(data) );
+
+	if( value != NULL ) {
+		parent = GNC_TAXTABLE(value);
+		gncTaxTableSetParent( tt, parent );
+	}
+}
+
+static void
+load_single_ttentry( GncSqlBackend* be, GncSqlRow* row, GncTaxTable* tt )
+{
+	GncTaxTableEntry* e = gncTaxTableEntryCreate();
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row!= NULL );
+	g_return_if_fail( tt != NULL );
+
+    gnc_sql_load_object( be, row, GNC_ID_TAXTABLE, e, ttentries_col_table );
+	gncTaxTableAddEntry( tt, e );
+}
+
+static void
+load_taxtable_entries( GncSqlBackend* be, GncTaxTable* tt )
+{
+    GncSqlResult* result;
+    gchar guid_buf[GUID_ENCODING_LENGTH+1];
+    GValue value;
+	gchar* buf;
+	GncSqlStatement* stmt;
+	GError* error = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( tt != NULL );
+
+    guid_to_string_buff( qof_instance_get_guid( QOF_INSTANCE(tt) ), guid_buf );
+    memset( &value, 0, sizeof( GValue ) );
+    g_value_init( &value, G_TYPE_STRING );
+    g_value_set_string( &value, guid_buf );
+	buf = g_strdup_printf( "SELECT * FROM %s WHERE taxtable='%s'", TTENTRIES_TABLE_NAME, guid_buf );
+	stmt = gnc_sql_connection_create_statement_from_sql( be->conn, buf );
+    result = gnc_sql_execute_select_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_ttentry( be, row, tt );
+			row = gnc_sql_result_get_next_row( result );
+        }
+		gnc_sql_result_dispose( result );
+    }
+}
+
+static void
+load_single_taxtable( GncSqlBackend* be, GncSqlRow* row )
+{
+    const GUID* guid;
+	GncTaxTable* tt;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    tt = gncTaxTableLookup( be->primary_book, guid );
+    if( tt == NULL ) {
+        tt = gncTaxTableCreate( be->primary_book );
+    }
+    gnc_sql_load_object( be, row, GNC_ID_TAXTABLE, tt, tt_col_table );
+    gnc_sql_slots_load( be, QOF_INSTANCE(tt) );
+	load_taxtable_entries( be, tt );
+
+    qof_instance_mark_clean( QOF_INSTANCE(tt) );
+}
+
+static void
+load_all_taxtables( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+
+	g_return_if_fail( be != NULL );
+
+    /* First time, create the query */
+    stmt = gnc_sql_create_select_statement( be, TT_TABLE_NAME, tt_col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+    if( result != NULL ) {
+        GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_taxtable( be, row );
+			row = gnc_sql_result_get_next_row( result );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+create_taxtable_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TT_TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TT_TABLE_NAME, TT_TABLE_VERSION, tt_col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+
+	version = gnc_sql_get_table_version( be, TTENTRIES_TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TTENTRIES_TABLE_NAME, TTENTRIES_TABLE_VERSION, ttentries_col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+delete_all_tt_entries( GncSqlBackend* be, const GUID* guid )
+{
+    guid_info_t guid_info;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( guid != NULL );
+
+    guid_info.be = be;
+    guid_info.guid = guid;
+    (void)gnc_sql_do_db_operation( be, OP_DB_DELETE, TTENTRIES_TABLE_NAME,
+                                TTENTRIES_TABLE_NAME, &guid_info, guid_col_table );
+}
+
+static void
+save_tt_entries( GncSqlBackend* be, const GUID* guid, GList* entries )
+{
+	GList* entry;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( guid != NULL );
+
+    /* First, delete the old slots for this object */
+    delete_all_tt_entries( be, guid );
+
+	for( entry = entries; entry != NULL; entry = entry->next ) {
+		GncTaxTableEntry* e = (GncTaxTableEntry*)entry->data;
+    	(void)gnc_sql_do_db_operation( be,
+                        OP_DB_ADD_OR_UPDATE,
+                        TTENTRIES_TABLE_NAME,
+                        GNC_ID_TAXTABLE, e,
+                        ttentries_col_table );
+    }
+}
+
+static void
+save_taxtable( QofInstance* inst, GncSqlBackend* be )
+{
+    GncTaxTable* tt = GNC_TAXTABLE(inst);
+    const GUID* guid;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_TAXTABLE(inst) );
+	g_return_if_fail( be != NULL );
+
+    (void)gnc_sql_do_db_operation( be,
+                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
+                        TT_TABLE_NAME,
+                        GNC_ID_TAXTABLE, tt,
+                        tt_col_table );
+
+    // Now, commit or delete any slots and tax table entries
+    guid = qof_instance_get_guid( inst );
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+		save_tt_entries( be, guid, gncTaxTableGetEntries( tt ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+		delete_all_tt_entries( be, guid );
+    }
+}
+
+/* ================================================================= */
+static void
+write_taxtables( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    qof_object_foreach( GNC_ID_TAXTABLE, be->primary_book, (QofInstanceForeachCB)save_taxtable, (gpointer)be );
+}
+
+/* ================================================================= */
+static void
+load_taxtable_guid( const GncSqlBackend* be, GncSqlRow* row,
+            QofSetterFunc setter, gpointer pObject,
+            const col_cvt_t* table_row )
+{
+    const GValue* val;
+    GUID guid;
+    const GUID* pGuid;
+	GncTaxTable* taxtable = NULL;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+	g_return_if_fail( pObject != NULL );
+	g_return_if_fail( table_row != NULL );
+
+    val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
+    if( val == NULL ) {
+        pGuid = NULL;
+    } else {
+        string_to_guid( g_value_get_string( val ), &guid );
+        pGuid = &guid;
+    }
+	if( pGuid != NULL ) {
+		taxtable = gncTaxTableLookup( be->primary_book, pGuid );
+	}
+    if( table_row->gobj_param_name != NULL ) {
+		g_object_set( pObject, table_row->gobj_param_name, taxtable, NULL );
+    } else {
+		(*setter)( pObject, (const gpointer)taxtable );
+    }
+}
+
+static col_type_handler_t taxtable_guid_handler
+	= { load_taxtable_guid,
+		gnc_sql_add_objectref_guid_col_info_to_list,
+		gnc_sql_add_colname_to_list,
+		gnc_sql_add_gvalue_objectref_guid_to_slist };
+/* ================================================================= */
+void
+gnc_taxtable_sql_initialize( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_TAXTABLE,
+        save_taxtable,						/* commit */
+        load_all_taxtables,					/* initial_load */
+        create_taxtable_tables,				/* create_tables */
+		NULL, NULL, NULL,
+		write_taxtables						/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_TAXTABLE, GNC_SQL_BACKEND, &be_data );
+
+	gnc_sql_register_col_type_handler( CT_TAXTABLEREF, &taxtable_guid_handler );
+}
+/* ========================== END OF FILE ===================== */
+

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-tax-table-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,37 @@
+/*
+ * gnc-tax-table-sql.h -- tax table sql backend
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-tax-table-sql.h
+ *  @brief load and save tax table data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_TAXTABLE_SQL_H
+#define GNC_TAXTABLE_SQL_H
+
+#define CT_TAXTABLEREF "tax-table"
+
+void gnc_taxtable_sql_initialize( void );
+
+#endif /* GNC_TAXTABLE_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-vendor-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,220 +0,0 @@
-/********************************************************************\
- * gnc-vendor-gda.c -- vendor gda backend                           *
- *                                                                  *
- * Copyright (C) 2002 Derek Atkins <warlord at MIT.EDU>                *
- *                                                                  *
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
- *                                                                  *
-\********************************************************************/
-
-#include "config.h"
-
-#include <glib.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <libgda/libgda.h>
-
-#include "gnc-commodity.h"
-
-#include "gnc-backend-util-gda.h"
-#include "gnc-commodity-gda.h"
-#include "gnc-slots-gda.h"
-
-#include "gnc-commodity.h"
-#include "gncBillTermP.h"
-#include "gncVendorP.h"
-#include "gncTaxTableP.h"
-#include "gnc-vendor-gda.h"
-#include "gnc-address-gda.h"
-#include "gnc-bill-term-gda.h"
-#include "gnc-tax-table-gda.h"
-
-#define _GNC_MOD_NAME	GNC_ID_VENDOR
-
-static QofLogModule log_module = G_LOG_DOMAIN;
-
-#define MAX_NAME_LEN 2048
-#define MAX_ID_LEN 2048
-#define MAX_NOTES_LEN 2048
-#define MAX_TAX_INC_LEN 2048
-
-#define TABLE_NAME "vendors"
-
-static col_cvt_t col_table[] =
-{
-	{ "guid",         CT_GUID,          0,               COL_NNUL|COL_PKEY, "guid" },
-	{ "name",         CT_STRING,        MAX_NAME_LEN,    COL_NNUL,          NULL, VENDOR_NAME },
-	{ "id",           CT_STRING,        MAX_ID_LEN,      COL_NNUL,          NULL, VENDOR_ID },
-	{ "notes",        CT_STRING,        MAX_NOTES_LEN,   COL_NNUL,          NULL, VENDOR_NOTES },
-	{ "currency",     CT_COMMODITYREF,  0,               COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)gncVendorGetCurrency, (QofSetterFunc)gncVendorSetCurrency },
-	{ "active",       CT_BOOLEAN,       0,               COL_NNUL,          NULL, NULL,
-			(QofAccessFunc)gncVendorGetActive, (QofSetterFunc)gncVendorSetActive },
-	{ "tax_override", CT_BOOLEAN,       0,               COL_NNUL,          NULL, VENDOR_TAX_OVERRIDE },
-	{ "addr",         CT_ADDRESS,       0,               0,                 NULL, VENDOR_ADDR },
-	{ "terms",        CT_BILLTERMREF,   0,               0,                 NULL, VENDOR_TERMS },
-	{ "tax_inc",      CT_STRING,        MAX_TAX_INC_LEN, 0,                 NULL, VENDOR_TAX_INC },
-	{ "tax_table",    CT_TAXTABLEREF,   0,               0,                 NULL, VENDOR_TAX_TABLE },
-	{ NULL }
-};
-
-static void
-load_single_vendor( GncGdaBackend* be, GdaDataModel* pModel, int row )
-{
-    const GUID* guid;
-    GUID v_guid;
-	GncVendor* pVendor;
-
-	g_return_if_fail( be != NULL );
-	g_return_if_fail( pModel != NULL );
-	g_return_if_fail( row >= 0 );
-
-    guid = gnc_gda_load_guid( be, pModel, row );
-    v_guid = *guid;
-
-    pVendor = gncVendorLookup( be->primary_book, &v_guid );
-    if( pVendor == NULL ) {
-        pVendor = gncVendorCreate( be->primary_book );
-    }
-    gnc_gda_load_object( be, pModel, row, GNC_ID_VENDOR, pVendor, col_table );
-    gnc_gda_slots_load( be, QOF_INSTANCE(pVendor) );
-
-    qof_instance_mark_clean( QOF_INSTANCE(pVendor) );
-}
-
-static void
-load_all_vendors( GncGdaBackend* be )
-{
-    static GdaQuery* query = NULL;
-    GdaObject* ret;
-
-	g_return_if_fail( be != NULL );
-
-    /* First time, create the query */
-    if( query == NULL ) {
-        query = gnc_gda_create_select_query( be, TABLE_NAME );
-    }
-
-    ret = gnc_gda_execute_query( be, query );
-    if( GDA_IS_DATA_MODEL( ret ) ) {
-        GdaDataModel* pModel = GDA_DATA_MODEL(ret);
-        int numRows = gda_data_model_get_n_rows( pModel );
-        int r;
-
-        for( r = 0; r < numRows; r++ ) {
-            (void)load_single_vendor( be, pModel, r );
-		}
-    }
-}
-
-/* ================================================================= */
-static void
-create_vendor_tables( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    gnc_gda_create_table_if_needed( be, TABLE_NAME, col_table );
-}
-
-/* ================================================================= */
-static void
-save_vendor( QofInstance* inst, GncGdaBackend* be )
-{
-    GncVendor* v = GNC_VENDOR(inst);
-    const GUID* guid;
-
-	g_return_if_fail( inst != NULL );
-	g_return_if_fail( GNC_IS_VENDOR(inst) );
-	g_return_if_fail( be != NULL );
-
-    // Ensure the commodity is in the db
-    gnc_gda_save_commodity( be, gncVendorGetCurrency( v ) );
-
-    (void)gnc_gda_do_db_operation( be,
-                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
-                        TABLE_NAME,
-                        GNC_ID_VENDOR, v,
-                        col_table );
-
-    // Now, commit or delete any slots
-    guid = qof_instance_get_guid( inst );
-    if( !qof_instance_get_destroying(inst) ) {
-        gnc_gda_slots_save( be, guid, qof_instance_get_slots( inst ) );
-    } else {
-        gnc_gda_slots_delete( be, guid );
-    }
-}
-
-/* ================================================================= */
-static gboolean
-vendor_should_be_saved( GncVendor *vendor )
-{
-    const char *id;
-
-	g_return_val_if_fail( vendor != NULL, FALSE );
-
-    /* make sure this is a valid vendor before we save it -- should have an ID */
-    id = gncVendorGetID( vendor );
-    if( id == NULL || *id == '\0' ) {
-        return FALSE;
-	}
-
-    return TRUE;
-}
-
-static void
-write_single_vendor( QofInstance *term_p, gpointer be_p )
-{
-    GncGdaBackend* be = (GncGdaBackend*)be_p;
-
-	g_return_if_fail( term_p != NULL );
-	g_return_if_fail( GNC_IS_VENDOR(term_p) );
-	g_return_if_fail( be_p != NULL );
-
-	if( vendor_should_be_saved( GNC_VENDOR(term_p) ) ) {
-    	save_vendor( term_p, be );
-	}
-}
-
-static void
-write_vendors( GncGdaBackend* be )
-{
-	g_return_if_fail( be != NULL );
-
-    qof_object_foreach( GNC_ID_VENDOR, be->primary_book, write_single_vendor, (gpointer)be );
-}
-
-/* ================================================================= */
-void
-gnc_vendor_gda_initialize( void )
-{
-    static GncGdaDataType_t be_data =
-    {
-        GNC_GDA_BACKEND_VERSION,
-        GNC_ID_VENDOR,
-        save_vendor,						/* commit */
-        load_all_vendors,					/* initial_load */
-        create_vendor_tables,				/* create_tables */
-		NULL, NULL, NULL,
-		write_vendors						/* write */
-    };
-
-    qof_object_register_backend( GNC_ID_VENDOR, GNC_GDA_BACKEND, &be_data );
-}
-/* ========================== END OF FILE ===================== */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-gda.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gnc-vendor-gda.h	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-gda.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,35 +0,0 @@
-/*
- * gnc-vendor-gda.h -- vendor gda backend
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- */
-
-/** @file gnc-vendor-gda.h
- *  @brief load and save vendor data to SQL via libgda
- *  @author Copyright (c) 2007 Phil Longstaff <plongstaff at rogers.com>
- *
- * This file implements the top-level QofBackend API for saving/
- * restoring data to/from an SQL database via libgda
- */
-
-#ifndef GNC_VENDOR_GDA_H
-#define GNC_VENDOR_GDA_H
-
-void gnc_vendor_gda_initialize( void );
-
-#endif /* GNC_VENDOR_GDA_H */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,232 @@
+/********************************************************************\
+ * gnc-vendor-sql.c -- vendor sql backend                           *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @file gnc-vendor-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gnc-commodity.h"
+
+#include "gnc-backend-util-sql.h"
+#include "gnc-commodity-sql.h"
+#include "gnc-slots-sql.h"
+
+#include "gnc-commodity.h"
+#include "gncBillTermP.h"
+#include "gncVendorP.h"
+#include "gncTaxTableP.h"
+#include "gnc-vendor-sql.h"
+#include "gnc-address-sql.h"
+#include "gnc-bill-term-sql.h"
+#include "gnc-tax-table-sql.h"
+
+#define _GNC_MOD_NAME	GNC_ID_VENDOR
+
+static QofLogModule log_module = G_LOG_DOMAIN;
+
+#define MAX_NAME_LEN 2048
+#define MAX_ID_LEN 2048
+#define MAX_NOTES_LEN 2048
+#define MAX_TAX_INC_LEN 2048
+
+#define TABLE_NAME "vendors"
+#define TABLE_VERSION 1
+
+static col_cvt_t col_table[] =
+{
+	{ "guid",         CT_GUID,          0,               COL_NNUL|COL_PKEY, "guid" },
+	{ "name",         CT_STRING,        MAX_NAME_LEN,    COL_NNUL,          NULL, VENDOR_NAME },
+	{ "id",           CT_STRING,        MAX_ID_LEN,      COL_NNUL,          NULL, VENDOR_ID },
+	{ "notes",        CT_STRING,        MAX_NOTES_LEN,   COL_NNUL,          NULL, VENDOR_NOTES },
+	{ "currency",     CT_COMMODITYREF,  0,               COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)gncVendorGetCurrency, (QofSetterFunc)gncVendorSetCurrency },
+	{ "active",       CT_BOOLEAN,       0,               COL_NNUL,          NULL, NULL,
+			(QofAccessFunc)gncVendorGetActive, (QofSetterFunc)gncVendorSetActive },
+	{ "tax_override", CT_BOOLEAN,       0,               COL_NNUL,          NULL, VENDOR_TAX_OVERRIDE },
+	{ "addr",         CT_ADDRESS,       0,               0,                 NULL, VENDOR_ADDR },
+	{ "terms",        CT_BILLTERMREF,   0,               0,                 NULL, VENDOR_TERMS },
+	{ "tax_inc",      CT_STRING,        MAX_TAX_INC_LEN, 0,                 NULL, VENDOR_TAX_INC },
+	{ "tax_table",    CT_TAXTABLEREF,   0,               0,                 NULL, VENDOR_TAX_TABLE },
+	{ NULL }
+};
+
+static void
+load_single_vendor( GncSqlBackend* be, GncSqlRow* row )
+{
+    const GUID* guid;
+	GncVendor* pVendor;
+
+	g_return_if_fail( be != NULL );
+	g_return_if_fail( row != NULL );
+
+    guid = gnc_sql_load_guid( be, row );
+    pVendor = gncVendorLookup( be->primary_book, guid );
+    if( pVendor == NULL ) {
+        pVendor = gncVendorCreate( be->primary_book );
+    }
+    gnc_sql_load_object( be, row, GNC_ID_VENDOR, pVendor, col_table );
+    gnc_sql_slots_load( be, QOF_INSTANCE(pVendor) );
+
+    qof_instance_mark_clean( QOF_INSTANCE(pVendor) );
+}
+
+static void
+load_all_vendors( GncSqlBackend* be )
+{
+    GncSqlStatement* stmt;
+    GncSqlResult* result;
+    QofBook* pBook;
+
+	g_return_if_fail( be != NULL );
+
+    pBook = be->primary_book;
+
+    stmt = gnc_sql_create_select_statement( be, TABLE_NAME, col_table );
+    result = gnc_sql_execute_sql_statement( be, stmt );
+	gnc_sql_statement_dispose( stmt );
+    if( result != NULL ) {
+        GncSqlRow* row;
+
+		row = gnc_sql_result_get_first_row( result );
+        while( row != NULL ) {
+            load_single_vendor( be, row );
+			row = gnc_sql_result_get_next_row( result );
+		}
+		gnc_sql_result_dispose( result );
+    }
+}
+
+/* ================================================================= */
+static void
+create_vendor_tables( GncSqlBackend* be )
+{
+	gint version;
+
+	g_return_if_fail( be != NULL );
+
+	version = gnc_sql_get_table_version( be, TABLE_NAME );
+    if( version == 0 ) {
+    	GError* error = NULL;
+
+        gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table, &error );
+        if( error != NULL ) {
+            PERR( "Error creating table: %s\n", error->message );
+        }
+    }
+}
+
+/* ================================================================= */
+static void
+save_vendor( QofInstance* inst, GncSqlBackend* be )
+{
+    GncVendor* v = GNC_VENDOR(inst);
+    const GUID* guid;
+
+	g_return_if_fail( inst != NULL );
+	g_return_if_fail( GNC_IS_VENDOR(inst) );
+	g_return_if_fail( be != NULL );
+
+    // Ensure the commodity is in the db
+    gnc_sql_save_commodity( be, gncVendorGetCurrency( v ) );
+
+    (void)gnc_sql_do_db_operation( be,
+                        (qof_instance_get_destroying(inst) ? OP_DB_DELETE : OP_DB_ADD_OR_UPDATE ),
+                        TABLE_NAME,
+                        GNC_ID_VENDOR, v,
+                        col_table );
+
+    // Now, commit or delete any slots
+    guid = qof_instance_get_guid( inst );
+    if( !qof_instance_get_destroying(inst) ) {
+        gnc_sql_slots_save( be, guid, qof_instance_get_slots( inst ) );
+    } else {
+        gnc_sql_slots_delete( be, guid );
+    }
+}
+
+/* ================================================================= */
+static gboolean
+vendor_should_be_saved( GncVendor *vendor )
+{
+    const char *id;
+
+	g_return_val_if_fail( vendor != NULL, FALSE );
+
+    /* make sure this is a valid vendor before we save it -- should have an ID */
+    id = gncVendorGetID( vendor );
+    if( id == NULL || *id == '\0' ) {
+        return FALSE;
+	}
+
+    return TRUE;
+}
+
+static void
+write_single_vendor( QofInstance *term_p, gpointer be_p )
+{
+    GncSqlBackend* be = (GncSqlBackend*)be_p;
+
+	g_return_if_fail( term_p != NULL );
+	g_return_if_fail( GNC_IS_VENDOR(term_p) );
+	g_return_if_fail( be_p != NULL );
+
+	if( vendor_should_be_saved( GNC_VENDOR(term_p) ) ) {
+    	save_vendor( term_p, be );
+	}
+}
+
+static void
+write_vendors( GncSqlBackend* be )
+{
+	g_return_if_fail( be != NULL );
+
+    qof_object_foreach( GNC_ID_VENDOR, be->primary_book, write_single_vendor, (gpointer)be );
+}
+
+/* ================================================================= */
+void
+gnc_vendor_sql_initialize( void )
+{
+    static GncSqlDataType_t be_data =
+    {
+        GNC_SQL_BACKEND_VERSION,
+        GNC_ID_VENDOR,
+        save_vendor,						/* commit */
+        load_all_vendors,					/* initial_load */
+        create_vendor_tables,				/* create_tables */
+		NULL, NULL, NULL,
+		write_vendors						/* write */
+    };
+
+    qof_object_register_backend( GNC_ID_VENDOR, GNC_SQL_BACKEND, &be_data );
+}
+/* ========================== END OF FILE ===================== */

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-sql.h
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-sql.h	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gnc-vendor-sql.h	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,35 @@
+/*
+ * gnc-vendor-sql.h -- vendor sql backend
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/** @file gnc-vendor-sql.h
+ *  @brief load and save vendor data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#ifndef GNC_VENDOR_SQL_H
+#define GNC_VENDOR_SQL_H
+
+void gnc_vendor_sql_initialize( void );
+
+#endif /* GNC_VENDOR_SQL_H */

Deleted: gnucash/branches/gda-dev2/src/business/business-core/sql/gncmod-business-backend-gda.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/gda/gncmod-business-backend-gda.c	2008-05-24 19:38:23 UTC (rev 17171)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gncmod-business-backend-gda.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -1,114 +0,0 @@
-/*********************************************************************
- * gncmod-business-backend-gda.c
- * module definition/initialization for the gda backend module
- *
- * Copyright (c) 2002 Derek Atkins <warlord at MIT.EDU>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, contact:
- *
- * Free Software Foundation           Voice:  +1-617-542-5942
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
- * Boston, MA  02110-1301,  USA       gnu at gnu.org
- *
- *********************************************************************/
-
-#include "config.h"
-#include <gmodule.h>
-
-#include <libgda/libgda.h>
-
-#include "gnc-module.h"
-#include "gnc-module-api.h"
-#include "gnc-engine.h"
-
-#include "gnc-backend-util-gda.h"
-
-#include "gnc-address-gda.h"
-#include "gnc-bill-term-gda.h"
-#include "gnc-customer-gda.h"
-#include "gnc-employee-gda.h"
-#include "gnc-entry-gda.h"
-#include "gnc-invoice-gda.h"
-#include "gnc-job-gda.h"
-#include "gnc-order-gda.h"
-#include "gnc-owner-gda.h"
-#include "gnc-tax-table-gda.h"
-#include "gnc-vendor-gda.h"
-
-GNC_MODULE_API_DECL(libgncmod_business_backend_gda)
-
-/* version of the gnc module system interface we require */
-int libgncmod_business_backend_gda_gnc_module_system_interface = 0;
-
-/* module versioning uses libtool semantics. */
-int libgncmod_business_backend_gda_gnc_module_current  = 0;
-int libgncmod_business_backend_gda_gnc_module_revision = 0;
-int libgncmod_business_backend_gda_gnc_module_age      = 0;
-
-static GNCModule bus_core;
-
-
-gchar *
-libgncmod_business_backend_gda_gnc_module_path(void)
-{
-    return g_strdup( "gnucash/business-core-gda" );
-}
-
-gchar *
-libgncmod_business_backend_gda_gnc_module_description(void)
-{
-    return g_strdup( "The GDA backend for GnuCash business objects" );
-}
-
-int
-libgncmod_business_backend_gda_gnc_module_init(int refcount)
-{
-    if(!gnc_engine_is_initialized()) { return FALSE; }
-
-    bus_core = gnc_module_load( "gnucash/business-core", 0 );
-    if( !bus_core ) return FALSE;
-
-    if( refcount == 0 ) {
-        /* Initialize our pointers into the backend subsystem */
-        gnc_address_gda_initialize();
-        gnc_billterm_gda_initialize();
-        gnc_customer_gda_initialize();
-        gnc_employee_gda_initialize();
-        gnc_entry_gda_initialize();
-        gnc_invoice_gda_initialize();
-        gnc_job_gda_initialize();
-        gnc_order_gda_initialize();
-	    gnc_owner_gda_initialize();
-	    gnc_taxtable_gda_initialize();
-        gnc_vendor_gda_initialize();
-    }
-
-    return TRUE;
-}
-
-int
-libgncmod_business_backend_gda_gnc_module_end(int refcount)
-{
-    int unload = TRUE;
-
-    if( bus_core ) {
-        unload = gnc_module_unload( bus_core );
-	}
-
-    if( refcount == 0 ) {
-        bus_core = NULL;
-    }
-
-    return unload;
-}

Added: gnucash/branches/gda-dev2/src/business/business-core/sql/gncmod-business-backend-sql.c
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-core/sql/gncmod-business-backend-sql.c	                        (rev 0)
+++ gnucash/branches/gda-dev2/src/business/business-core/sql/gncmod-business-backend-sql.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -0,0 +1,118 @@
+/*********************************************************************
+ * gncmod-business-backend-sql.c
+ * module definition/initialization for the sql backend module
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ *
+ *********************************************************************/
+
+/** @file gncmod-business-backend-sql.c
+ *  @brief load and save address data to SQL
+ *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff at rogers.com>
+ *
+ * This file implements the top-level QofBackend API for saving/
+ * restoring data to/from an SQL database
+ */
+
+#include "config.h"
+#include <gmodule.h>
+
+#include "gnc-module.h"
+#include "gnc-module-api.h"
+#include "gnc-engine.h"
+
+#include "gnc-backend-util-sql.h"
+
+#include "gnc-address-sql.h"
+#include "gnc-bill-term-sql.h"
+#include "gnc-customer-sql.h"
+#include "gnc-employee-sql.h"
+#include "gnc-entry-sql.h"
+#include "gnc-invoice-sql.h"
+#include "gnc-job-sql.h"
+#include "gnc-order-sql.h"
+#include "gnc-owner-sql.h"
+#include "gnc-tax-table-sql.h"
+#include "gnc-vendor-sql.h"
+
+GNC_MODULE_API_DECL(libgncmod_business_backend_sql)
+
+/* version of the gnc module system interface we require */
+int libgncmod_business_backend_sql_gnc_module_system_interface = 0;
+
+/* module versioning uses libtool semantics. */
+int libgncmod_business_backend_sql_gnc_module_current  = 0;
+int libgncmod_business_backend_sql_gnc_module_revision = 0;
+int libgncmod_business_backend_sql_gnc_module_age      = 0;
+
+static GNCModule bus_core;
+
+
+gchar *
+libgncmod_business_backend_sql_gnc_module_path(void)
+{
+    return g_strdup( "gnucash/business-core-sql" );
+}
+
+gchar *
+libgncmod_business_backend_sql_gnc_module_description(void)
+{
+    return g_strdup( "The SQL backend for GnuCash business objects" );
+}
+
+int
+libgncmod_business_backend_sql_gnc_module_init(int refcount)
+{
+    if(!gnc_engine_is_initialized()) { return FALSE; }
+
+    bus_core = gnc_module_load( "gnucash/business-core", 0 );
+    if( !bus_core ) return FALSE;
+
+    if( refcount == 0 ) {
+        /* Initialize our pointers into the backend subsystem */
+        gnc_address_sql_initialize();
+        gnc_billterm_sql_initialize();
+        gnc_customer_sql_initialize();
+        gnc_employee_sql_initialize();
+        gnc_entry_sql_initialize();
+        gnc_invoice_sql_initialize();
+        gnc_job_sql_initialize();
+        gnc_order_sql_initialize();
+	    gnc_owner_sql_initialize();
+	    gnc_taxtable_sql_initialize();
+        gnc_vendor_sql_initialize();
+    }
+
+    return TRUE;
+}
+
+int
+libgncmod_business_backend_sql_gnc_module_end(int refcount)
+{
+    int unload = TRUE;
+
+    if( bus_core ) {
+        unload = gnc_module_unload( bus_core );
+	}
+
+    if( refcount == 0 ) {
+        bus_core = NULL;
+    }
+
+    return unload;
+}

Modified: gnucash/branches/gda-dev2/src/business/business-gnome/business-gnome.scm
===================================================================
--- gnucash/branches/gda-dev2/src/business/business-gnome/business-gnome.scm	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/business/business-gnome/business-gnome.scm	2008-06-07 15:38:38 UTC (rev 17192)
@@ -6,7 +6,7 @@
 (gnc:module-load "gnucash/business-utils" 0)
 (gnc:module-load "gnucash/gnome-search" 0)
 (gnc:module-load "gnucash/business-core-xml" 0)
-(gnc:module-load "gnucash/business-core-gda" 0)
+(gnc:module-load "gnucash/business-core-sql" 0)
 (gnc:module-load "gnucash/dialog-tax-table" 0)
 
 (gnc:module-load "gnucash/report/report-gnome" 0)

Modified: gnucash/branches/gda-dev2/src/engine/gnc-engine.c
===================================================================
--- gnucash/branches/gda-dev2/src/engine/gnc-engine.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/engine/gnc-engine.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -76,10 +76,9 @@
     const gchar* lib;
     gboolean required;
   } libs[] = {
-    { "gncmod-backend-gda", TRUE },
-    { GNC_LIB_NAME, TRUE },
-    /* shouldn't the PG gnc-module do this instead of US doing it? */
-//    { "gncmod-backend-postgres", FALSE },
+    { "gncmod-backend-dbi", TRUE },
+//    { "gncmod-backend-gda", TRUE },
+    { "gncmod-backend-xml", TRUE },
     { NULL, FALSE } }, *lib;
   gnc_engine_init_hook_t hook;
   GList * cur;

Modified: gnucash/branches/gda-dev2/src/gnome-utils/dialog-database-connection.c
===================================================================
--- gnucash/branches/gda-dev2/src/gnome-utils/dialog-database-connection.c	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/gnome-utils/dialog-database-connection.c	2008-06-07 15:38:38 UTC (rev 17192)
@@ -113,8 +113,9 @@
     GladeXML* xml;
     GtkWidget* box;
 	GList* ds_node;
-	GList* ds_list;
-	gint numDsns = 0;
+	GdaDataModel* dsns;
+	gint numDsns;
+	gint i;
 
     dcw = g_new0(struct DatabaseConnectionWindow, 1);
     g_return_if_fail(dcw);
@@ -127,13 +128,12 @@
     dcw->rb_predefined = glade_xml_get_widget( xml, "rb_predefined" );
 	box = glade_xml_get_widget( xml, "predefined_connection_box" );
 	dcw->cb_predefined = gtk_combo_box_new_text();
-	ds_list = gda_config_get_data_source_list();
-	for( ds_node = ds_list; ds_node != NULL; ds_node = ds_node->next ) {
-		GdaDataSourceInfo* ds_info = (GdaDataSourceInfo*)ds_node->data;
+	numDsns = gda_config_get_nb_dsn();
+	dsns = gda_config_list_dsn();
+	for( i = 0; i < numDsns; i++ ) {
+		GdaDataSourceInfo* ds_info = gda_config_get_dsn_at_index( i );
 		gtk_combo_box_append_text( GTK_COMBO_BOX(dcw->cb_predefined), g_strdup(ds_info->name) );
-		numDsns++;
 	}
-	gda_config_free_data_source_list( ds_list );
 	if( numDsns != 0 ) {
 		gtk_combo_box_set_active( GTK_COMBO_BOX(dcw->cb_predefined), 0 );
 	} else {

Added: gnucash/branches/gda-dev2/src/pixmaps/22x22/gnucash-icon.png
===================================================================
(Binary files differ)


Property changes on: gnucash/branches/gda-dev2/src/pixmaps/22x22/gnucash-icon.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: gnucash/branches/gda-dev2/src/pixmaps/24x24/gnucash-icon.png
===================================================================
(Binary files differ)


Property changes on: gnucash/branches/gda-dev2/src/pixmaps/24x24/gnucash-icon.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Modified: gnucash/branches/gda-dev2/src/valgrind-gnucash.supp
===================================================================
--- gnucash/branches/gda-dev2/src/valgrind-gnucash.supp	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/valgrind-gnucash.supp	2008-06-07 15:38:38 UTC (rev 17192)
@@ -37,6 +37,12 @@
 #     if Free: name of free-ing fn)
 
 {
+	libld-2.7(Value4)
+	Memcheck:Value4
+	obj:/lib/ld-2.7.so
+	obj:/lib/ld-2.7.so
+}
+{
    libglib-1.2.so.0(Value4)
    Memcheck:Value4
    fun:g_hash_table_lookup
@@ -69,15 +75,15 @@
    fun:link_connection_writev
    fun:giop_send_buffer_write
 }
+#{
+#   pthread_mutex_lock PThread
+#   core:PThread
+#   fun:pthread_mutex_lock
+#   fun:_IO_flockfile
+#   fun:pango_read_line
+#   fun:pango_find_map
+#}
 {
-   pthread_mutex_lock PThread
-   core:PThread
-   fun:pthread_mutex_lock
-   fun:_IO_flockfile
-   fun:pango_read_line
-   fun:pango_find_map
-}
-{
    writev(vector) X11 Param
    Memcheck:Param
    writev(vector[...])

Modified: gnucash/branches/gda-dev2/src/valgrind-libfontconfig.supp
===================================================================
--- gnucash/branches/gda-dev2/src/valgrind-libfontconfig.supp	2008-06-06 16:46:03 UTC (rev 17191)
+++ gnucash/branches/gda-dev2/src/valgrind-libfontconfig.supp	2008-06-07 15:38:38 UTC (rev 17192)
@@ -36,20 +36,22 @@
 #     if Free: name of free-ing fn)
 
 {
-	FcFontRenderPrepare Leak
+	libfontconfig realloc Leak
 	Memcheck:Leak
 	fun:realloc
-	obj:/usr/lib/libfontconfig.so.1.1.0
-	obj:/usr/lib/libfontconfig.so.1.1.0
-	obj:/usr/lib/libfontconfig.so.1.1.0
-	fun:FcFontRenderPrepare
+	obj:/usr/lib/libfontconfig.so.1.3.0
 }
 {
-	libfontconfig malloc Leak
+	libfontconfig malloc Leak #1
 	Memcheck:Leak
 	fun:malloc
-	obj:/usr/lib/libfontconfig.so.1.1.0
+	obj:/usr/lib/libfontconfig.so.1.3.0
 }
+{
+	libfontconfig malloc Leak #2
+	fun:malloc
+	fun:FcStrCopy
+}
 
 
 # ##----------------------------------------------------------------------##



More information about the gnucash-changes mailing list