[libgda/libgda-vala] Set libgdadata version to 5.0. Added initial examples. DataModelIterable implements Gee.Collection a



commit 6f6566b54a5ebf4a0c8bf129a939a3f7b2900f69
Author: Daniel Espinosa <despinosa src gnome org>
Date:   Tue Dec 13 17:55:23 2011 -0600

    Set libgdadata version to 5.0. Added initial examples. DataModelIterable implements Gee.Collection and Gda.DataModel

 configure.ac                              |    2 +-
 libgda/data/DataModelIterator.vala        |  301 ++++++++++++++++++++++++++++-
 libgda/data/DataObject.vala               |   10 +-
 libgda/data/Makefile.am                   |   39 ++--
 libgda/data/SelectQuery.vala              |    2 +-
 libgda/data/libgdadata-0.2.pc.in          |   13 --
 libgda/data/libgdadata-5.0.pc.in          |   13 ++
 samples/Makefile                          |    7 +-
 samples/vala/Makefile.am                  |   77 ++++++++
 samples/vala/SampleDataModelIterator.vala |  206 ++++++++++++++++++++
 samples/vala/SampleDataObject.vala        |  177 +++++++++++++++++
 tests/vala/CheckDataModelIterator.vala    |   14 ++-
 tests/vala/Makefile.am                    |    6 +-
 13 files changed, 825 insertions(+), 42 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 6f56202..32a2754 100644
--- a/configure.ac
+++ b/configure.ac
@@ -907,7 +907,7 @@ libgda/sqlite/virtual/libgda-virtual.h
 libgda/sqlite/virtual/Makefile
 libgda/thread-wrapper/Makefile
 libgda/data/Makefile
-libgda/data/libgdadata-0.2.pc
+libgda/data/libgdadata-5.0.pc
 providers/Makefile
 providers/reuseable/Makefile
 providers/reuseable/postgres/Makefile
diff --git a/libgda/data/DataModelIterator.vala b/libgda/data/DataModelIterator.vala
index 301fea3..5b70257 100644
--- a/libgda/data/DataModelIterator.vala
+++ b/libgda/data/DataModelIterator.vala
@@ -22,7 +22,8 @@
  
  namespace GdaData {
  	
- 	public class DataModelIterable : GLib.Object, Gee.Traversable <Value?>, Gee.Iterable <Value?>
+ 	public class DataModelIterable : GLib.Object, Gee.Traversable <Value?>, 
+ 										Gee.Iterable <Value?>, Gee.Collection <Value?>, Gda.DataModel
  	{
  		private Gda.DataModel model;
  		
@@ -70,6 +71,304 @@
 			var iter = new DataModelIterator (this.model);
  			return iter.stream<A> (f);
 		}
+		
+		// Interface Collection
+		public bool add (Value? item) {
+			int i = this.model.append_row ();
+			if (i >= 0)
+				return true;
+			
+			return false;
+		}
+		
+		/**
+		 * { inheritDoc}
+		 *
+		 * { inheritDoc}<< BR >>
+		 * << BR >>
+		 * collection must include just values for one row, with the same type expected in the table.<<BR>>
+		 * <<BR>>
+		 * If the collection is a database proxy, you need to apply to make changes permanently.
+		 */
+		public bool add_all (Gee.Collection<Value?> collection)
+			requires (collection.size == this.model.get_n_columns ()) 
+		{
+			var vals = collection.to_array ();
+			for (int i = 0; i < this.model.get_n_columns (); i++) {
+				var v = this.model.get_value_at (i, 0);
+				if (vals[i].type () != v.type ())
+					return false;
+			}
+			
+			var r = this.model.append_row ();
+			for (int j = 0; j < this.model.get_n_columns (); j++) {
+				var v2 = vals[j];
+				try {
+					this.model.set_value_at (j, r, v2);
+				}
+				catch {
+					return false;
+				}
+			}
+			return true;
+		}
+		
+		/**
+		 * { inheritDoc}
+		 *
+		 * { inheritDoc}<< BR >>
+		 * <<BR>>
+		 * If the collection is a proxy, you need to apply to make changes permanently.
+		 */
+		public void clear () {
+			for (int i = 0; i < this.model.get_n_rows (); i++ ) {
+				this.model.remove_row (i);
+			}
+		}
+		
+		public bool contains (Value? item)
+		{
+			for (int r = 0; r < this.model.get_n_rows (); r++) {
+				for (int c = 0; c < this.model.get_n_columns (); c++) {
+					Value v = this.model.get_value_at (c, r);
+					if (Gda.value_compare (v, item) == 0)
+						return true;
+				}
+			}
+			return false;
+		}
+		
+		public bool contains_all (Gee.Collection<Value?> collection)
+		{
+			bool ret = true;
+			foreach (Value v in collection) {
+				if (this.contains (v))
+					continue;
+				else
+					ret = false;
+			}
+			
+			return ret;
+		}
+		
+		/**
+		 * { inheritDoc}
+		 *
+		 * { inheritDoc}<< BR >>
+		 * << BR >>
+		 * Search for the first item in the collection that match item and removes it.<<BR>>
+		 * <<BR>>
+		 * ''Caution:'' Removing a single value removes all the row.
+		 * <<BR>>
+		 * If the collection is a database proxy, you need to apply to make changes permanently.
+		 */
+		public bool remove (Value? item) {
+			for (int r = 0; r < this.model.get_n_rows (); r++) {
+				for (int c = 0; c < this.model.get_n_columns (); c++) {
+					try {
+						Value v = this.model.get_value_at (c, r);
+						if (Gda.value_compare (v, item) == 0) {
+							this.model.remove_row (r);
+							return true;
+						}
+					}
+					catch {
+						continue;
+					}
+				}
+			}
+			
+			return false;
+		}
+		
+		/**
+		 * { inheritDoc}
+		 *
+		 * { inheritDoc}<< BR >>
+		 * << BR >>
+		 * Search for the first item in the collection that match item and removes it.<<BR>>
+		 * <<BR>>
+		 * ''Caution:'' Removing a single value removes all the row and its values.
+		 * <<BR>>
+		 * If the collection is a database proxy, you need to apply to make changes permanently.
+		 */
+		public bool remove_all (Gee.Collection<Value?> collection)
+		{
+			foreach (Value v in collection) {
+				for (int r = 0; r < this.model.get_n_rows (); r++) {
+					for (int c = 0; c < this.model.get_n_columns (); c++) {
+						try {
+							Value v2 = this.model.get_value_at (c, r);
+							if (Gda.value_compare (v2, v) == 0) {
+								try {
+									this.model.remove_row (r);
+								}
+								catch {
+									return false;
+								}
+							}
+						}
+						catch {
+							continue;
+						}
+					}
+				}
+			}
+			
+			return true;
+		}
+		
+		/**
+		 * { inheritDoc}
+		 *
+		 * { inheritDoc}<< BR >>
+		 * << BR >>
+		 * Search for the first item in the collection that match item and removes it.<<BR>>
+		 * <<BR>>
+		 * ''Caution:'' Removing a single value removes all the row and its values.
+		 * <<BR>>
+		 * If the collection is a database proxy, you need to apply to make changes permanently.
+		 */
+		public bool retain_all (Gee.Collection<Value?> collection)
+		{
+			foreach (Value v in collection) {
+				for (int r = 0; r < this.model.get_n_rows (); r++) {
+					for (int c = 0; c < this.model.get_n_columns (); c++) {
+						try {
+							Value v2 = this.model.get_value_at (c, r);
+							if (Gda.value_compare (v2, v) != 0) {
+								try {
+									this.model.remove_row (r);
+								}
+								catch {
+									return false;
+								}
+							}
+						}
+						catch {
+							continue;
+						}	
+					}
+				}
+			}
+			
+			return true;
+		}
+		
+		/**
+		 * { inheritDoc}
+		 *
+		 * { inheritDoc}<< BR >>
+		 * << BR >>
+		 * ''Implementation:'' The array is a copy of all values in the DataModel.
+		 */
+		public Value[] to_array ()
+		{
+			Value[] array = new Value[1];
+			array.resize (this.model.get_n_columns () * this.model.get_n_rows ());
+			for (int i = 0; i < this.model.get_n_columns () * this.model.get_n_rows (); i++) {
+				int r = i / this.model.get_n_columns ();
+				int c = i - r * this.model.get_n_columns ();
+				array[i] = this.model.get_value_at (c, r);
+			}
+			
+			return array;
+		}
+		
+		public bool is_empty { 
+			get {
+				if (this.model.get_n_rows () <= 0)
+					return true;
+				
+				return false;
+			} 
+		}
+		public bool read_only { 
+			get {
+				// FIXME: Check if is a Proxy DataModel can be modified to return true
+				return true;
+			}
+		}
+		public Gee.Collection<Value?> read_only_view { 
+			owned get {
+				return (Gee.Collection<Value?>) ((GLib.Object) this).ref ();
+			}
+		}
+		
+		public int size { 
+			get {
+				return this.model.get_n_columns () * this.model.get_n_rows ();
+			} 
+		}
+		
+		// Interface Gda.DataModel
+		public int append_row () throws GLib.Error {
+			return this.model.append_row ();
+		}
+		
+		public Gda.DataModelIter create_iter () {
+			return this.model.create_iter ();
+		}
+		
+		public unowned Gda.Column describe_column (int col) {
+			return this.model.describe_column (col);
+		}
+		
+		public void freeze (bool do_notify_changes) {
+			this.model.freeze (do_notify_changes);
+		}
+		
+		public Gda.DataModelAccessFlags get_access_flags () {
+			return this.model.get_access_flags ();
+		}
+		
+		public Gda.ValueAttribute get_attributes_at (int col, int row) {
+			return this.model.get_attributes_at (col, row);
+		}
+		
+		public int get_n_columns () {
+			return this.model.get_n_columns ();
+		}
+		
+		public int get_n_rows () {
+			return this.model.get_n_rows ();
+		}
+		
+		public unowned GLib.Value? get_value_at (int col, int row) throws GLib.Error {
+			return this.model.get_value_at (col, row);
+		}
+		
+		public bool i_get_notify () {
+			return this.model.i_get_notify ();
+		}
+
+		public bool i_iter_at_row (Gda.DataModelIter iter, int row) {
+			return this.model.i_iter_at_row (iter, row);
+		}
+		
+		public bool i_iter_next (Gda.DataModelIter iter) {
+			return this.model.i_iter_next (iter);
+		}
+		
+		public bool i_iter_prev (Gda.DataModelIter iter) {
+			return this.model.i_iter_prev (iter);
+		}
+		
+		public bool i_iter_set_value (Gda.DataModelIter iter, int col, GLib.Value value) throws GLib.Error {
+			return this.model.i_iter_set_value (iter, col, value);
+		}
+		
+		public bool remove_row (int row) throws GLib.Error {
+			return this.model.remove_row (row);
+		}
+		
+		public void send_hint (Gda.DataModelHint hint, GLib.Value? hint_value) {
+			this.model.send_hint (hint, hint_value);
+		}
+		
+		public bool set_value_at (int col, int row, GLib.Value value) throws GLib.Error {
+			return this.model.set_value_at (col, row, value);
+		}
  	}
  	
  	/**
diff --git a/libgda/data/DataObject.vala b/libgda/data/DataObject.vala
index 81133b5..133cf72 100644
--- a/libgda/data/DataObject.vala
+++ b/libgda/data/DataObject.vala
@@ -26,10 +26,16 @@ namespace GdaData {
         
         private string? _field_id;
         private Value? _id_value;
+        private DataModel? _model;
         
-        protected DataModel? _model;
         protected string? _table;
         
+        public DataModelIterable record {
+        	owned get {
+        		return new DataModelIterable (this._model);
+        	}
+        }
+        
         public Connection connection { get; set; }
         
         public string get_field_id ()
@@ -55,7 +61,7 @@ namespace GdaData {
         	var s = q.get_statement ();
         	var m = this.connection.statement_execute_select (s, null);
         	((DataSelect) m).compute_modification_statements ();
-        	this._model = (DataProxy) DataProxy.new (m);
+        	this._model= (DataProxy) DataProxy.new (m);
         }
         
         public unowned Value? get_value (string field)
diff --git a/libgda/data/Makefile.am b/libgda/data/Makefile.am
index 9eedb53..afe6264 100644
--- a/libgda/data/Makefile.am
+++ b/libgda/data/Makefile.am
@@ -21,35 +21,36 @@ AM_CFLAGS =\
     $(VALA_CFLAGS) \
     $(NULL)
 
-lib_LTLIBRARIES = libgdadata-0.2.la
+lib_LTLIBRARIES = libgdadata-5.0.la
 
 VALAFLAGS = \
     --vapidir=$(top_srcdir)/libgda \
+    --includedir=$(top_srcdir)/libgda \
     --pkg libxml-2.0 \
     --pkg gee-0.8 \
     --pkg libgda-5.0 \
     $(NULL)
 
-libgdadata_0_2_la_VALASOURCES = \
+libgdadata_5_0_la_VALASOURCES = \
 	DataObject.vala \
 	SelectQuery.vala \
 	DataModelIterator.vala \
 	$(NULL)
 
-libgdadata_0_2_la_SOURCES = \
+libgdadata_5_0_la_SOURCES = \
 	gdadata.vala.stamp \
-	$(libgdadata_0_2_la_VALASOURCES:.vala=.c) \
+	$(libgdadata_5_0_la_VALASOURCES:.vala=.c) \
 	$(NULL)	
 
-gdadata-0.2.vapi gdadata.vala.stamp GdaData-0.2.gir: $(libgdadata_0_2_la_VALASOURCES)
-	$(VALA_COMPILER) $(VALAFLAGS) -C -H libgdadata.h --gir=GdaData-0.2.gir --library gdadata-0.2 $^
+gdadata-5.0.vapi gdadata.vala.stamp GdaData-5.0.gir: $(libgdadata_5_0_la_VALASOURCES)
+	$(VALA_COMPILER) $(VALAFLAGS) -C -H libgdadata.h --gir=GdaData-5.0.gir --library gdadata-5.0 $^
 	@touch $@
 
-CLEANFILES+= gdadata-0.2.vapi GdaData-0.2.gir
+CLEANFILES+= gdadata-5.0.vapi GdaData-5.0.gir
 
-libgdadata_0_2_la_LDFLAGS = 
+libgdadata_5_0_la_LDFLAGS = 
 
-libgdadata_0_2_la_LIBADD = \
+libgdadata_5_0_la_LIBADD = \
     $(top_builddir)/libgda/libgda-5.0.la \
 	$(LIBGDA_LIBS) \
 	$(GEE_LIBS) \
@@ -60,11 +61,11 @@ include_HEADERS = \
 	libgdadata.h \
 	$(NULL)
 
-gdadataincludedir = $(includedir)/libgdadata-0.2/
+gdadataincludedir = $(includedir)/libgda-$(GDA_ABI_MAJOR_VERSION).$(GDA_ABI_MINOR_VERSION)/libgda
 gdadatainclude_HEADERS = libgdadata.h
 
 pkgconfigdir = $(libdir)/pkgconfig
-pkgconfig_DATA = libgdadata-0.2.pc
+pkgconfig_DATA = libgdadata-5.0.pc
 
 # GObject Introspection
 
@@ -78,30 +79,30 @@ endif
 
 # GIR files are generated automatically by Valac then is not necessary to scan source code to generate it
 INTROSPECTION_GIRS =
-INTROSPECTION_GIRS += GdaData-0.2.gir
+INTROSPECTION_GIRS += GdaData-5.0.gir
 INTROSPECTION_COMPILER_ARGS = 
 
 GdaData-0.2.typelib: $(INTROSPECTION_GIRS)
 	$(INTROSPECTION_COMPILER) $(INTROSPECTION_COMPILER_ARGS) --includedir=. $< -o $@
 
 gir_DATA = $(INTROSPECTION_GIRS)
-typelibs_DATA = GdaData-0.2.typelib
+typelibs_DATA = GdaData-5.0.typelib
 
 vapidir = $(VALA_VAPIDIR)
-vapi_DATA = gdadata-0.2.vapi
+vapi_DATA = gdadata-5.0.vapi
 
 CLEANFILES += \
     $(INTROSPECTION_GIRS) \
     $(typelibs_DATA) \
-    gdadata-0.2.vapi \
-    libgdadata-0.2.pc \
+    gdadata-5.0.vapi \
+    libgdadata-5.0.pc \
     libgdadata.h \
-    $(libgdadata_0_2_la_VALASOURCES:.vala=.c) \
+    $(libgdadata_5_0_la_VALASOURCES:.vala=.c) \
     $(NULL)
 
 EXTRA_DIST = \
-	libgdadata-0.2.pc \
-	$(libgdadata_0_2_la_VALASOURCES:.vala=.c) \
+	libgdadata-5.0.pc \
+	$(libgdadata_5_0_la_VALASOURCES:.vala=.c) \
 	$(vapi_DATA) \
 	$(INTROSPECTION_GIRS) \
 	$(typelibs_DATA) \
diff --git a/libgda/data/SelectQuery.vala b/libgda/data/SelectQuery.vala
index ef5d38d..8f79fc0 100644
--- a/libgda/data/SelectQuery.vala
+++ b/libgda/data/SelectQuery.vala
@@ -82,7 +82,7 @@ namespace GdaData {
         
         public DataModel execute () 
             throws Error 
-            requires (this.connection.is_opened())
+            requires (this.connection.is_opened ())
         {
             /* Build Select Query */
             var b = this.build ();
diff --git a/libgda/data/libgdadata-5.0.pc.in b/libgda/data/libgdadata-5.0.pc.in
new file mode 100644
index 0000000..151c43c
--- /dev/null
+++ b/libgda/data/libgdadata-5.0.pc.in
@@ -0,0 +1,13 @@
+prefix= prefix@
+exec_prefix= exec_prefix@
+libdir= libdir@
+datarootdir= datarootdir@
+datadir= datadir@
+includedir= includedir@
+
+Name: libgdadata- GDA_ABI_MAJOR_VERSION@  GDA_ABI_MINOR_VERSION@
+Description: Gda Data Library. Data oriented GObject implementations using Vala and Gee
+Version: @VERSION@
+Requires: gobject-2.0 gthread-2.0 libxml-2.0 libgda-5.0 libgee-0.8
+Libs: -L${libdir} -lgdadata- GDA_ABI_MAJOR_VERSION@  GDA_ABI_MINOR_VERSION@
+Cflags: -I${includedir}/libgdadata- GDA_ABI_MAJOR_VERSION@  GDA_ABI_MINOR_VERSION@ -I${includedir}/libgda- GDA_ABI_MAJOR_VERSION@  GDA_ABI_MINOR_VERSION@/libgda @GDA_DEBUG_FLAGS@
diff --git a/samples/Makefile b/samples/Makefile
index 475fdc0..69e7919 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -1,5 +1,10 @@
 SHELL= /bin/sh 
-SUBDIRS = BDB DDL DirDataModel F-Spot Report SimpleExample SqlParserConsole TableCopy Virtual XSLT MetaStore Tree SqlBuilder AsyncExec Gir LdapBrowser CustomUIPlugin
+
+if ENABLE_VALA_EXTENSIONS
+    VALA_EXTENSIONS= vala
+endif
+
+SUBDIRS = BDB DDL DirDataModel F-Spot Report SimpleExample SqlParserConsole TableCopy Virtual XSLT MetaStore Tree SqlBuilder AsyncExec Gir LdapBrowser CustomUIPlugin $(VALA_EXTENSIONS)
 all:
 	for dir in ${SUBDIRS} ; do ( cd $$dir ; ${MAKE} ) ; done
 clean:
diff --git a/samples/vala/Makefile.am b/samples/vala/Makefile.am
new file mode 100644
index 0000000..cab6de9
--- /dev/null
+++ b/samples/vala/Makefile.am
@@ -0,0 +1,77 @@
+NULL = 
+
+AM_CPPFLAGS = \
+	-I$(top_srcdir) \
+	-I$(top_srcdir)/libgda \
+	-I$(top_builddir) \
+	$(COREDEPS_CFLAGS) \
+	$(COREDEPS_WFLAGS) \
+	$(JSON_GLIB_CFLAGS) \
+	$(GEE_CFLAGS) \
+	-DROOT_DIR=\""$(top_srcdir)"\"
+
+BUILT_SOURCES = \
+    sample_dataobject.vala.stamp \
+    sample_datamodeliterator.vala.stamp \
+    $(NULL)
+
+CLEANFILES = \
+    sample_dataobject.vala.stamp \
+    sample_datamodeliterator.vala.stamp \
+    $(NULL)
+
+VALAFLAGS = \
+    --pkg gee-0.8 \
+    --pkg gobject-2.0 \
+    --pkg libxml-2.0 \
+    --pkg libgda-5.0 \
+    --pkg gdadata-0.2 \
+    --includedir=$(top_builddir) \
+    --vapidir=$(top_srcdir)/libgda \
+    --vapidir=$(top_builddir)/libgda/data \
+    $(NULL)
+
+sample_dataobject_VALASOURCES = \
+	SampleDataObject.vala \
+	$(NULL)
+
+sample_datamodeliterator_VALASOURCES = \
+	SampleDataModelIterator.vala \
+	$(NULL)
+
+sample_dataobject.vala.stamp: $(sample_dataobject_VALASOURCES)
+	$(VALA_COMPILER) $(VALAFLAGS) -C -H sample_dataobject.h $^
+	@touch $@
+
+sample_datamodeliterator.vala.stamp: $(sample_datamodeliterator_VALASOURCES)
+	$(VALA_COMPILER) $(VALAFLAGS) -C -H sample_datamodeliterator.h $^
+	@touch $@
+
+sample_dataobject_SOURCES = $(sample_dataobject_VALASOURCES:.vala=.c) sample_dataobject.h
+sample_dataobject_CFLAGS= \
+    -I$(top_builddir)/libgda/data \
+    $(NULL)
+sample_dataobject_LDADD = \
+    $(top_builddir)/libgda/libgda-5.0.la \
+	$(top_builddir)/libgda/data/libgdadata-0.2.la \
+	$(COREDEPS_LIBS)
+
+sample_datamodeliterator_SOURCES = $(sample_datamodeliterator_VALASOURCES:.vala=.c) sample_datamodeliterator.h
+sample_datamodeliterator_CFLAGS= \
+    -I$(top_builddir)/libgda/data \
+    $(NULL)
+sample_datamodeliterator_LDADD = \
+    $(top_builddir)/libgda/libgda-5.0.la \
+	$(top_builddir)/libgda/data/libgdadata-0.2.la \
+	$(COREDEPS_LIBS)
+
+CLEANFILES += \
+    $(sample_dataobject_VALASOURCES:.vala=.c) \
+    sample_dataobject.h \
+    $(sample_datamodeliterator_VALASOURCES:.vala=.c) \
+    sample_datamodeliterator.h \
+    dataobject.db \
+    datamodeliterator.db \
+    $(NULL)
+
+EXTRA_DIST = 
diff --git a/samples/vala/SampleDataModelIterator.vala b/samples/vala/SampleDataModelIterator.vala
new file mode 100644
index 0000000..5b34913
--- /dev/null
+++ b/samples/vala/SampleDataModelIterator.vala
@@ -0,0 +1,206 @@
+/* -*- Mode: Vala; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
+/*
+ * libgdadata Unit Tests
+ * Copyright (C) Daniel Espinosa Ortiz 2011 <esodan gmail com>
+ * 
+ * libgda is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * libgda 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 Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Gda;
+using GdaData;
+
+namespace Check {
+	
+	class Tests : GLib.Object {
+		private Gda.Connection connection;
+		
+		Tests()
+		{
+			try {
+				GLib.FileUtils.unlink("datamodeliterator.db");
+				stdout.printf("Creating Database...\n");
+				this.connection = Connection.open_from_string("SQLite", "DB_DIR=.;DB_NAME=datamodeliterator", null, Gda.ConnectionOptions.NONE);
+				stdout.printf("Creating table 'user'...\n");
+				this.connection.execute_non_select_command("CREATE TABLE user (id int PRIMARY KEY, name string, city string)");
+				this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (1, \"Daniel\", \"Mexico\")");
+				this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (2, \"Jhon\", \"USA\")");
+				
+				stdout.printf("Creating table 'company'...\n");
+				this.connection.execute_non_select_command("CREATE TABLE company (id int PRIMARY KEY, name string, responsability string)");
+				this.connection.execute_non_select_command("INSERT INTO company (id, name, responsability) VALUES (1, \"Telcsa\", \"Programing\")");
+				this.connection.execute_non_select_command("INSERT INTO company (id, name, responsability) VALUES (2, \"Viasa\", \"Accessories\")");
+				this.connection.update_meta_store(null);
+			}
+			catch (Error e) {
+				stdout.printf ("Couln't create temporary database...\nERROR: %s\n", e.message);
+			}
+		}
+		
+		public int t1()
+			throws Error
+		{
+			stdout.printf (">>> NEW TEST: Gda.DataModelIterable & DataModelIterator API tests\n");
+			int fails = 0;
+			var model = this.connection.execute_select_command ("SELECT * FROM user");
+			var itermodel = new DataModelIterable (model);
+
+			stdout.printf ("Iterating over all Values in DataModel\n");
+			foreach (Value v in itermodel) {
+				stdout.printf ("%s\t%s\n", v.type_name (), Gda.value_stringify (v));
+			}
+			
+			stdout.printf ("Choping to get first Value = 1\n");
+			var iter = itermodel.chop (0,0);
+			iter.next ();
+			Value v = iter.get ();
+			if (v.type () == typeof (int))
+				stdout.printf ("ID=%i; Row = %i Col = %i\n", (int) v, 
+							  ((GdaData.DataModelIterator) iter).current_row, 
+							  ((GdaData.DataModelIterator) iter).current_column);
+			else {
+				fails++;
+				stdout.printf ("FAIL:'%i': Expected type = '%s'," +
+							   " but Value is type = '%s'" +
+							   " with value = %s\n" + 
+							   " : Current Row = %i Col = %i\n", 
+							   fails, typeof (int).name (), v.type_name (), 
+							   Gda.value_stringify (v),
+							   ((GdaData.DataModelIterator) iter).current_row, 
+							   ((GdaData.DataModelIterator) iter).current_column);			
+			}
+			
+			stdout.printf ("Choping to get 5th Value = 'Jhon'\n");
+			var iter2 = itermodel.chop (4,0);
+			stdout.printf ("Row = %i Col = %i\n", 
+							   ((GdaData.DataModelIterator) iter2).current_row, 
+							   ((GdaData.DataModelIterator) iter2).current_column);
+			iter2.next ();
+			Value v2 = iter2.get ();
+			if (v2.type () == typeof (string))
+				stdout.printf ("Name = %s; Row = %i Col = %i\n", (string) v2,
+							   ((GdaData.DataModelIterator) iter2).current_row, 
+							   ((GdaData.DataModelIterator) iter2).current_column);
+			else {
+				fails++;
+				stdout.printf ("FAIL:'%i': Expected type = '%s'," +
+							   " but Value is type = '%s'" +
+							   " with value = %s" + 
+							   " : Current Row = %i Col = %i\n", 
+							   fails, typeof (string).name (), v2.type_name (), 
+							   Gda.value_stringify (v2),
+							   ((GdaData.DataModelIterator) iter2).current_row, 
+							   ((GdaData.DataModelIterator) iter2).current_column);			
+			}
+			
+			stdout.printf ("Choping to get 2nd to 5th Values...\n");
+			var iter3 = itermodel.chop (1,4);
+			stdout.printf ("TYPE\t\tVALUE\n");
+			while (iter3.next ()) {
+				Value v3 = iter3.get ();
+				stdout.printf ("%s\t\t%s\n", typeof (int).name (), Gda.value_stringify (v3));
+				if (((GdaData.DataModelIterator) iter3).current_row == 0 
+					&& ((GdaData.DataModelIterator) iter3).current_column == 1) {
+						string name1 = (string) v3;
+						if (name1 != "Daniel") {
+							fails++;
+							break;
+						}
+				}
+				if (((GdaData.DataModelIterator) iter3).current_row == 0 
+					&& ((GdaData.DataModelIterator) iter3).current_column == 2) {
+						string name1 = (string) v3;
+						if (name1 != "Mexico") {
+							fails++;
+							break;
+						}
+				}
+				if (((GdaData.DataModelIterator) iter3).current_row == 1 
+					&& ((GdaData.DataModelIterator) iter3).current_column == 0) {
+						int id1 = (int) v3;
+						if (id1 != 2) {
+							fails++;
+							break;
+						}
+				}
+				if (((GdaData.DataModelIterator) iter3).current_row == 1 
+					&& ((GdaData.DataModelIterator) iter3).current_column == 1) {
+						string name1 = (string) v3;
+						if (name1 != "Jhon") {
+							fails++;
+							break;
+						}
+				}
+			}
+			
+			stdout.printf ("Filtering Values: Any STRING with a letter 'n'...\n");
+			Gee.Predicate <Value?> f = (g) => {
+						bool ret = false;
+						if (g.type () == typeof (string)) {
+							string t = (string) g;
+							if (t.contains ("n"))
+								ret = true;
+							else
+								ret = false;
+						}
+						else
+							ret = false;
+						stdout.printf ("To be Included?: %s, %s\n", 
+										Gda.value_stringify (g), ret == true ? "TRUE" : "FALSE");
+						return ret;
+					};
+			
+			var iter4 = itermodel.filter (f);
+			stdout.printf ("Printing Filtered Values...\n");
+			while (iter4.next ()) {
+				Value v4 = iter4.get ();
+				stdout.printf ("Row: %i, Col: %i\t%s\t\t%s\n", 
+						((GdaData.DataModelIterator) iter4).current_row,
+					    ((GdaData.DataModelIterator) iter4).current_column,
+					    typeof (int).name (), Gda.value_stringify (v4));
+			}
+			
+			/* FIXME: This code doesn't return YIELDED strings maybe becasue Lazy is not correctly build. 
+				In theory stream() function is working correctly*/
+			stdout.printf ("Streaming Values: Any STRING will be YIELDED...\n");
+			Gee.StreamFunc<Value?,string> s = (state, g, lazy) =>	{
+						if (state == Gee.Traversable.Stream.CONTINUE) {
+							Value vs = g.get ();
+							stdout.printf ("Value to YIELD: %s\n", Gda.value_stringify (vs));
+							string ts = "YIELDED Value = " + Gda.value_stringify (vs) + "\n";
+							lazy = new Gee.Lazy<string>.from_value (ts.dup ());
+							stdout.printf (ts);
+							return Gee.Traversable.Stream.YIELD;
+						}
+						return Gee.Traversable.Stream.END;
+					};
+			
+			var iter5 = itermodel.stream<string> (s);
+			stdout.printf ("Printing Streamed Values...\n");
+			while (iter5.next ()) {
+				string sv = iter5.get ();
+				stdout.printf ("%s\n", sv);
+			}	
+				
+			return fails;
+		}
+				
+		public static int main (string[] args) {
+			stdout.printf ("Checking Gda.DataModelIterator implementation...\n");
+			int failures = 0;
+			var app = new Tests ();
+			failures += app.t1 ();
+			return failures != 0 ? 1 : 0;
+		}
+	}
+}
diff --git a/samples/vala/SampleDataObject.vala b/samples/vala/SampleDataObject.vala
new file mode 100644
index 0000000..51bef61
--- /dev/null
+++ b/samples/vala/SampleDataObject.vala
@@ -0,0 +1,177 @@
+/* -*- Mode: Vala; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
+/*
+ * libgdadata Unit Tests
+ * Copyright (C) Daniel Espinosa Ortiz 2011 <esodan gmail com>
+ * 
+ * libgda is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * libgda 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 Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Gda;
+using GdaData;
+
+namespace Sample {
+
+	class Book : GdaData.Object {
+		
+		public Collection accounts {
+			get {
+				int id = (int) this.get_value_id ();
+				string sql = @"SELECT * FROM accounts WHERE book = $id";
+				var accs = this.connection.execute_select (sql);
+				return (Collection) new DataModelIterable (accs);
+			}
+		}
+		
+		public Book () {
+			this._table = "book";
+		}
+		
+		public static Book create ( Gda.Connection cnn, string name, string manager)
+			requires (cnn.is_open ())
+			throws Error
+		{
+			string table = this._table;
+			string sql = @"INSERT INTO $table (name, manager) VALUES ($name, $manager)";
+			cnn.execute_non_select_command(sql);
+			var b = new Book ();
+			b.connection = cnn;
+			b.open (name);
+		}
+		
+		public bool open (string name) 
+			throws Error
+		{
+			string sql = @"SELECT * FROM book WHERE name = $name";
+			var model = this.connection.execute_select (sql);
+			var id = model.get_value_at (model.get_column_index ("id"), 0);
+			this.set_id ("id", id);
+		}
+		
+	}
+	
+	class Account : GdaData.Object {
+		public Book book { get; set; }
+		/* Is possible to create properties to easy access to any field in the database row */
+		public string name { 
+			get {
+				return (string) this.get_value ("name");
+			} 
+			set {
+				return this.set_value ("name", value);
+			}
+		}
+		
+		public Collection transactions {
+			get {
+				int id = (int) this.get_value_id ();
+				string sql = @"SELECT * FROM transactions WHERE account = $id";
+				var accs = this.connection.execute_select (sql);
+				return (Collection) new DataModelIterable (accs);
+			}
+		}
+		
+		public Gda.Numeric balance {
+			get {
+				string sql_debit = @"SELECT sum (debit) FROM transactions WHERE account = $id";
+				string sql_credit = @"SELECT sum (credit) FROM transactions WHERE account = $id";
+				var model_credit = this.connection.execute_select (sql_credit);
+				var model_debit = this.connection.execute_select (sql_debit);
+				var credit = model_credit.get_value_at (0, 0);
+				var debit = model_debit.get_value_at (0, 0);
+				
+			}
+		}
+		
+		public Account () {
+			this._table = "account";
+		}
+		
+		public bool open (string name) {
+			Value n = name;
+			this.set_id (name, n);
+		}
+		
+	}
+	
+	class Transaction : GdaData.Object {
+		public Account account { get; set; }
+		public string description { 
+			get {
+				this.get_value ("description");
+			} 
+			set {
+				this.set_value ("description", value);
+			}
+		}
+		
+		public Gda.Numeric debit { 
+			get {
+				this.get_value ("debit");
+			} 
+			set {
+				this.set_value ("debit", value);
+			}
+		}
+		
+		public Gda.Numeric credit {
+			get {
+				this.get_value ("credit");
+			} 
+			set {
+				this.set_value ("credit", value);
+			}
+		}
+	}
+	
+	class App : GdaData.Object {
+		public Gda.Connection connection;
+		
+		Tests()
+		{
+			try {
+				GLib.FileUtils.unlink("dataobject.db");
+				stdout.printf("Creating Database...\n");
+				this.connection = Connection.open_from_string("SQLite", "DB_DIR=.;DB_NAME=dataobject", null, Gda.ConnectionOptions.NONE);
+				stdout.printf("Creating table 'user'...\n");
+				this.connection.execute_non_select_command("CREATE TABLE book (id int PRIMARY KEY AUTOINCREMENT, name string NOT NULL UNIQUE, manager string)");
+				
+				this.connection.execute_non_select_command("INSERT INTO book (name, manager) VALUES (\"General Book\", \"Jhon\")");
+				
+				stdout.printf("Creating table 'company'...\n");
+				this.connection.execute_non_select_command("CREATE TABLE account (id int PRIMARY KEY AUTOINCREMENT, name string, description string)");
+				this.connection.execute_non_select_command("INSERT INTO account (name) VALUES (\"Incomes\"");
+				this.connection.execute_non_select_command("INSERT INTO account (name) VALUES (\"Expenses\"");
+				this.connection.update_meta_store(null);
+			}
+			catch (Error e) {
+				stdout.printf ("Couln't create temporary database...\nERROR: %s\n", e.message);
+			}
+		}
+		
+		public int get_value ()
+			throws Error
+		{
+			
+			return 0;
+		}
+		
+		public static int main (string[] args) {
+			stdout.printf ("Checking Gda.DataObject implementation...\n");
+			int failures = 0;
+			var app = new Tests ();
+			failures += app.t1 ();
+			return failures != 0 ? 1 : 0;
+		}
+	}
+}
diff --git a/tests/vala/CheckDataModelIterator.vala b/tests/vala/CheckDataModelIterator.vala
index 5b34913..b5d6268 100644
--- a/tests/vala/CheckDataModelIterator.vala
+++ b/tests/vala/CheckDataModelIterator.vala
@@ -50,7 +50,8 @@ namespace Check {
 		public int t1()
 			throws Error
 		{
-			stdout.printf (">>> NEW TEST: Gda.DataModelIterable & DataModelIterator API tests\n");
+			stdout.printf (">>> NEW TEST: Gda.DataModelIterable & DataModelIterator API tests\n"
+						 	+ ">>> Testing Iterable, Traversable Interfaces\n");
 			int fails = 0;
 			var model = this.connection.execute_select_command ("SELECT * FROM user");
 			var itermodel = new DataModelIterable (model);
@@ -194,6 +195,17 @@ namespace Check {
 				
 			return fails;
 		}
+		
+		public int t2 ()
+		{
+			stdout.printf (">>> NEW TEST: Gda.DataModelIterable & DataModelIterator API tests\n"
+							+ ">>> Testing Collection Interface\n");
+			int fails = 0;
+			var model = this.connection.execute_select_command ("SELECT * FROM user");
+			var itermodel = new DataModelIterable (model);
+			
+			
+		}
 				
 		public static int main (string[] args) {
 			stdout.printf ("Checking Gda.DataModelIterator implementation...\n");
diff --git a/tests/vala/Makefile.am b/tests/vala/Makefile.am
index ed1cf9b..0f7f735 100644
--- a/tests/vala/Makefile.am
+++ b/tests/vala/Makefile.am
@@ -29,7 +29,7 @@ VALAFLAGS = \
     --pkg gobject-2.0 \
     --pkg libxml-2.0 \
     --pkg libgda-5.0 \
-    --pkg gdadata-0.2 \
+    --pkg gdadata-5.0 \
     --includedir=$(top_builddir) \
     --vapidir=$(top_srcdir)/libgda \
     --vapidir=$(top_builddir)/libgda/data \
@@ -57,7 +57,7 @@ check_dataobject_CFLAGS= \
     $(NULL)
 check_dataobject_LDADD = \
     $(top_builddir)/libgda/libgda-5.0.la \
-	$(top_builddir)/libgda/data/libgdadata-0.2.la \
+	$(top_builddir)/libgda/data/libgdadata-5.0.la \
 	$(COREDEPS_LIBS)
 
 check_datamodeliterator_SOURCES = $(check_datamodeliterator_VALASOURCES:.vala=.c) check_datamodeliterator.h
@@ -66,7 +66,7 @@ check_datamodeliterator_CFLAGS= \
     $(NULL)
 check_datamodeliterator_LDADD = \
     $(top_builddir)/libgda/libgda-5.0.la \
-	$(top_builddir)/libgda/data/libgdadata-0.2.la \
+	$(top_builddir)/libgda/data/libgdadata-5.0.la \
 	$(COREDEPS_LIBS)
 
 CLEANFILES += \



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]