[libgda/libgda-vala: 35/36] Added DataModelIterator. Changed libgdadata to 0.2. Added more unit tests to DataObject.



commit c43e29861a419cfb3de1bb1ae3d2754b5e6effa7
Author: Daniel Espinosa <despinosa src gnome org>
Date:   Thu Dec 1 18:59:28 2011 -0600

    Added DataModelIterator. Changed libgdadata to 0.2. Added more unit tests to DataObject.
    
    * Added DataModelIterator as a Gee.Iterable and Gee.Traversable
    * Changed libgdadata to 0.2 to avoid future conflics on installed versions
    * DataObject have more unit tests to verify changing Table and setting a new ID

 configure.ac                                       |    6 +-
 libgda/data/DataModelIterator.vala                 |  200 ++++++++++++++++++++
 libgda/data/Makefile.am                            |   45 +++--
 .../{libgdadata-1.0.pc.in => libgdadata-0.2.pc.in} |    0
 tests/vala/CheckDataObject.vala                    |   31 +++-
 tests/vala/Makefile.am                             |    4 +-
 6 files changed, 258 insertions(+), 28 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 960077b..6f56202 100644
--- a/configure.ac
+++ b/configure.ac
@@ -510,9 +510,9 @@ then
 	    if test "x$enable_vala_ext" = "xyes"
 	    then
 dnl Check for libgee
-        GEE_REQUIRED="0.6.1"
+        GEE_REQUIRED="0.7.0"
 
-        PKG_CHECK_MODULES(GEE, gee-1.0 >= $GEE_REQUIRED)
+        PKG_CHECK_MODULES(GEE, gee-0.8 >= $GEE_REQUIRED)
         AC_SUBST(GEE_CFLAGS)
         AC_SUBST(GEE_LIBS)
 	    fi
@@ -907,7 +907,7 @@ libgda/sqlite/virtual/libgda-virtual.h
 libgda/sqlite/virtual/Makefile
 libgda/thread-wrapper/Makefile
 libgda/data/Makefile
-libgda/data/libgdadata-1.0.pc
+libgda/data/libgdadata-0.2.pc
 providers/Makefile
 providers/reuseable/Makefile
 providers/reuseable/postgres/Makefile
diff --git a/libgda/data/DataModelIterator.vala b/libgda/data/DataModelIterator.vala
new file mode 100644
index 0000000..077e28d
--- /dev/null
+++ b/libgda/data/DataModelIterator.vala
@@ -0,0 +1,200 @@
+/* -*- Mode: Vala; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
+/*
+ * libgdadata
+ * 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 Gee;
+ using Gda;
+ 
+ namespace GdaData {
+ 	
+ 	/**
+ 	 * Iterator that implements [ link [Gee.Iterator] and [ link [Gee.Traversable]]
+ 	 */
+ 	class DataModelIterator : Object, Gee.Iterator <Value>, Gee.Traversable < Value >
+ 	{
+ 		private Gda.DataModelIter iter;
+ 		private int _current_pos;
+ 		/* Used for Traversable to skip a number of elements in this case rows and columns */
+ 		private int pos_init;
+ 		private int maxpos;
+ 		private Gee.HashMap <int, int> elements;
+ 		private bool filtered;	
+ 		
+ 		
+ 		public int current_col {
+ 			get { return this._current_pos - this.iter.get_row () * this.iter.data_model.get_n_columns (); }
+ 		}
+ 		
+ 		public DataModelIterator (Gda.DataModel model)
+ 		{
+ 			this.iter = model.create_iter ();
+ 			this._current_pos = 0;
+ 			this.pos_init = 0;
+ 			this.maxpos = this.iter.data_model.get_n_columns () * this.iter.data_model.get_n_rows () - 1;
+ 			this.filtered = false;
+ 		}
+ 		
+ 		private DataModelIterator.with_offset (Gda.DataModel model, int pos_init, int maxpos)
+ 		{
+ 			this.iter = model.create_iter ();
+ 			int i = pos_init;
+ 			if (i > maxpos)
+ 				i = maxpos;
+ 			if (maxpos > (model.get_n_columns () *  model.get_n_rows () - 1))
+ 				this.iter.invalidate_contents ();
+ 			
+ 			this._current_pos = i;
+ 			this.pos_init = i;
+ 			this.maxpos = maxpos;
+ 			this.filtered = false;
+ 		}
+ 		
+ 		private DataModelIterator.filtered_elements (Gda.DataModel model, Gee.HashMap <int, int> elements)
+ 		{
+ 			this.iter = model.create_iter ();
+ 			this._current_pos = 0;
+ 			this.pos_init = 0;
+ 			this.maxpos = this.iter.data_model.get_n_columns () * this.iter.data_model.get_n_rows () - 1;
+ 			this.filtered = true;
+ 			this.elements = elements;
+ 		}
+ 		
+ 		public bool valid 
+ 		{ 
+ 			get { return this.iter.is_valid (); }
+ 		}
+ 		
+ 		public bool read_only {	get { return true; } }
+ 		
+ 		public bool next () {
+ 			if (!this.filtered) 
+ 			{
+	 			this._current_pos++;
+	 			if (this._current_pos > this.maxpos) {
+	 				this.iter.invalidate_contents ();
+	 				return false;
+	 			}
+	 			else
+	 				return this.iter.move_to_row (this._current_pos / this.iter.data_model.get_n_columns ());
+ 			}
+ 			else
+ 			{
+ 				while (true) {
+ 					if (this.elements.has_key (++this._current_pos))
+ 						break;
+ 				}
+ 				return this.iter.move_to_row (this._current_pos / this.iter.data_model.get_n_columns ());
+ 			}
+ 		}
+ 		
+ 		public bool has_next () {
+ 			if (this.iter.is_valid ()) {
+ 				int pos = this._current_pos;
+ 				if (++pos > this.maxpos)
+ 					return false;
+ 				else
+ 					return true;
+ 			}
+ 			return false;
+ 		}
+ 		
+ 		/**
+		 * { inheritDoc} 
+		 * 
+		 * { inheritDoc}<< BR >>
+		 * << BR >>
+		 * ''Implementation:'' Gda.DataModel have read only GLib.Value no modification is allowed.
+		 * This function returns a copy of the Value stored in the DataModel.
+		 */
+ 		public Value @get ()
+ 		{
+ 			return this.iter.get_value_at (this._current_pos - 
+ 										this.iter.get_row () * 
+ 											this.iter.data_model.get_n_columns ());
+ 		}
+ 		
+ 		public void remove () {}
+ 		
+ 		/* Traversable */
+ 		public Gee.Iterator<Value?> chop (int offset, int length = -1) 
+ 			requires ( offset >= 0)
+ 		{
+ 			int elements = this.iter.data_model.get_n_columns () * this.iter.data_model.get_n_rows ();
+ 			int maxpos = elements;
+ 			if (length > -1) {
+ 				maxpos = offset + length;
+ 				if (maxpos > elements)
+ 					maxpos = elements;
+ 			}
+ 			return new DataModelIterator.with_offset (this.iter.data_model, offset, maxpos);
+ 		}
+ 		
+		public Gee.Iterator<Value?> filter (owned Gee.Predicate<Value?> f)
+		{
+			var elements = new Gee.HashMap <int,int> ();
+			for (int i = 0; i < this.maxpos; i++) {
+				int row = i / this.iter.data_model.get_n_columns ();
+				int col = i - row * this.iter.data_model.get_n_columns ();
+				Value v = this.iter.data_model.get_value_at (row, col);
+				if (f (v))
+					elements.set (row, col);
+			}
+			return new DataModelIterator.filtered_elements (this.iter.data_model, elements);
+		}
+		
+		public new void @foreach (Gee.ForallFunc<Value?> f)
+		{
+			for (int i = 0; i < this.maxpos; i++) {
+				int row = i / this.iter.data_model.get_n_columns ();
+				int col = i - row * this.iter.data_model.get_n_columns ();
+				Value v = this.iter.data_model.get_value_at (row, col);
+				f(v);
+			}
+		}
+		
+		/**
+		 * { inheritDoc}
+		 * 
+		 * { inheritDoc}<< BR >>
+		 * << BR >>
+		 * ''Implementation:'' This function returns an iterator that can iterate over YIELDED values
+		 * by { link [StreamFunc].
+		 */
+		public Gee.Iterator<Value?> stream<Value> (owned Gee.StreamFunc<Value?,Value?> f)
+		{
+			var l = new Gee.HashMap<int,int> ();
+			for (int i = 0; i < this.maxpos; i++) {
+				int row = i / this.iter.data_model.get_n_columns ();
+				int col = i - row * this.iter.data_model.get_n_columns ();
+				Value v = this.iter.data_model.get_value_at (row, col);
+				var g = new Gee.Lazy<Value>.from_value (v);
+				Gee.Lazy<Value> s;
+				var r = f (Gee.Traversable.Stream.CONTINUE, g, out s);
+				if (r == Gee.Traversable.Stream.END)
+					break;
+				if (r == Stream.YIELD)
+					l.set (this.iter.get_row (), this._current_pos - 
+													this.iter.get_row () * 
+															this.iter.data_model.get_n_columns ());
+			}
+						
+			return new DataModelIterator.filtered_elements (this.iter.data_model, l);
+		}
+ 	}
+ }
+ 
diff --git a/libgda/data/Makefile.am b/libgda/data/Makefile.am
index 4facbfa..0214c29 100644
--- a/libgda/data/Makefile.am
+++ b/libgda/data/Makefile.am
@@ -20,34 +20,35 @@ AM_CFLAGS =\
     $(VALA_CFLAGS) \
     $(NULL)
 
-lib_LTLIBRARIES = libgdadata-1.0.la
+lib_LTLIBRARIES = libgdadata-0.2.la
 
 VALAFLAGS = \
     --vapidir=$(top_srcdir)/libgda \
     --pkg libxml-2.0 \
-    --pkg gee-1.0 \
+    --pkg gee-0.8 \
     --pkg libgda-5.0 \
     $(NULL)
 
-libgdadata_1_0_la_VALASOURCES = \
+libgdadata_0_2_la_VALASOURCES = \
 	DataObject.vala \
 	SelectQuery.vala \
+	DataModelIterator.vala \
 	$(NULL)
 
-libgdadata_1_0_la_SOURCES = \
+libgdadata_0_2_la_SOURCES = \
 	gdadata.vala.stamp \
-	$(libgdadata_1_0_la_VALASOURCES:.vala=.c) \
+	$(libgdadata_0_2_la_VALASOURCES:.vala=.c) \
 	$(NULL)	
 
-gdadata-1.0.vapi gdadata.vala.stamp GdaData-1.0.gir: $(libgdadata_1_0_la_VALASOURCES)
-	$(VALA_COMPILER) $(VALAFLAGS) -C -H libgdadata.h --gir=GdaData-1.0.gir --library gdadata-1.0 $^
+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 $^
 	@touch $@
 
-CLEANFILES+= gdadata-1.0.vapi GdaData-1.0.gir
+CLEANFILES+= gdadata-0.2.vapi GdaData-0.2.gir
 
-libgdadata_1_0_la_LDFLAGS = 
+libgdadata_0_2_la_LDFLAGS = 
 
-libgdadata_1_0_la_LIBADD = \
+libgdadata_0_2_la_LIBADD = \
     $(top_builddir)/libgda/libgda-5.0.la \
 	$(LIBGDA_LIBS) \
 	$(GEE_LIBS) \
@@ -62,7 +63,7 @@ gdadataincludedir = $(includedir)/libgda-$(GDA_ABI_MAJOR_VERSION).$(GDA_ABI_MINO
 gdadatainclude_HEADERS = libgdadata.h
 
 pkgconfigdir = $(libdir)/pkgconfig
-pkgconfig_DATA = libgdadata-1.0.pc
+pkgconfig_DATA = libgdadata-0.2.pc
 
 # GObject Introspection
 
@@ -76,23 +77,29 @@ endif
 
 # GIR files are generated automatically by Valac then is not necessary to scan source code to generate it
 INTROSPECTION_GIRS =
-INTROSPECTION_GIRS += GdaData-1.0.gir
+INTROSPECTION_GIRS += GdaData-0.2.gir
 INTROSPECTION_COMPILER_ARGS = 
 
-GdaData-1.0.typelib: $(INTROSPECTION_GIRS)
+GdaData-0.2.typelib: $(INTROSPECTION_GIRS)
 	$(INTROSPECTION_COMPILER) $(INTROSPECTION_COMPILER_ARGS) --includedir=. $< -o $@
 
 gir_DATA = $(INTROSPECTION_GIRS)
-typelibs_DATA = GdaData-1.0.typelib
+typelibs_DATA = GdaData-0.2.typelib
 
 vapidir = $(VALA_VAPIDIR)
-vapi_DATA = gdadata-1.0.vapi
-
-CLEANFILES += $(INTROSPECTION_GIRS) $(typelibs_DATA) gdadata-1.0.vapi $(libgdadata_1_0_la_VALASOURCES:.vala=.c)
+vapi_DATA = gdadata-0.2.vapi
+
+CLEANFILES += \
+    $(INTROSPECTION_GIRS) \
+    $(typelibs_DATA) \
+    gdadata-0.2.vapi \
+    libgdadata-0.2.pc \
+    $(libgdadata_0_2_la_VALASOURCES:.vala=.c) \
+    $(NULL)
 
 EXTRA_DIST = \
-	libgdadata-1.0.pc.in \
-	$(libgdadata_1_0_la_VALASOURCES:.vala=.c) \
+	libgdadata-0.2.pc \
+	$(libgdadata_0_2_la_VALASOURCES:.vala=.c) \
 	$(vapi_DATA) \
 	$(INTROSPECTION_GIRS) \
 	$(typelibs_DATA) \
diff --git a/libgda/data/libgdadata-1.0.pc.in b/libgda/data/libgdadata-0.2.pc.in
similarity index 100%
rename from libgda/data/libgdadata-1.0.pc.in
rename to libgda/data/libgdadata-0.2.pc.in
diff --git a/tests/vala/CheckDataObject.vala b/tests/vala/CheckDataObject.vala
index af6a2e7..cb2dfea 100644
--- a/tests/vala/CheckDataObject.vala
+++ b/tests/vala/CheckDataObject.vala
@@ -1,7 +1,7 @@
 /* -*- Mode: Vala; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
 /*
- * libgda
- * Copyright (C) Daniel Espinosa Ortiz 2008 <esodan gmail com>
+ * 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
@@ -22,8 +22,6 @@ using GdaData;
 
 namespace Check {
 	class Tests : GdaData.Object {
-		private int fails;
-		
 		Tests()
 		{
 			try {
@@ -125,6 +123,31 @@ namespace Check {
 				stdout.printf ("Couln't UPDATE...\nFAILS: %i\nERROR: %s\n", fails, e.message);
 			}
 			
+			stdout.printf("Setting a new Table... \n");
+			this._table = "company";
+			stdout.printf("Updating values from database using a new table 'company'...\n");
+			try {
+				this.update();
+				stdout.printf("DataObject points to actual stored values, in table '%s':\n", this._table);
+				stdout.printf("%s\n", this._model.dump_as_string());
+			}
+			catch (Error e) {
+				fails++;
+				stdout.printf ("Couln't UPDATE...\nFAILS: %i\nERROR: %s\n", fails, e.message);
+			}
+			
+			v = 2;
+			stdout.printf("Setting ID to %i\n", (int) v);
+			try {
+				this.set_id ("id", v);
+				stdout.printf("DataObject points to actual stored values, in table '%s':\n", this._table);
+				stdout.printf("%s\n", this._model.dump_as_string());
+			}
+			catch (Error e) {
+				fails++;
+				stdout.printf ("Couln't set ID...\nFAILS: %i\nERROR: %s\n", fails, e.message);
+			}
+			
 			return fails;
 		}
 		
diff --git a/tests/vala/Makefile.am b/tests/vala/Makefile.am
index da7b37e..dfdb25d 100644
--- a/tests/vala/Makefile.am
+++ b/tests/vala/Makefile.am
@@ -17,11 +17,11 @@ BUILT_SOURCES = check_dataobject.vala.stamp
 CLEANFILES = check_dataobject.vala.stamp
 
 VALAFLAGS = \
-    --pkg gee-1.0 \
+    --pkg gee-0.8 \
     --pkg gobject-2.0 \
     --pkg libxml-2.0 \
     --pkg libgda-5.0 \
-    --pkg gdadata-1.0 \
+    --pkg gdadata-0.2 \
     --includedir=$(top_builddir) \
     --vapidir=$(top_srcdir)/libgda \
     --vapidir=$(top_builddir)/libgda/data \



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