[libgda/libgda-vala] DataModelIterator implements Gee.AbstractCollection. GdaData.Object API changes make table field pub



commit 7b1fc0b6ed5a3c8b031a3fbf1eadfec7f37c0e2e
Author: Daniel Espinosa <despinosa src gnome org>
Date:   Wed Dec 14 12:37:55 2011 -0600

    DataModelIterator implements Gee.AbstractCollection. GdaData.Object API changes make table field public and abstract read only property.
    
    * DataModelIterator implements Gee.AbstractCollection instead of
    Gee.Collection, Gee.Iterable and Gee.Traversable
    * GdaData.Object expose 'table' as public abstract property and is declared as
    abstract class. Derived classes must implement table to return the table used
    to get data from.
    * Fixed some build problems. Remains a typelib compilation problem.

 libgda/data/DataModelIterator.vala |  188 ++++++++++++++++++------------------
 libgda/data/DataObject.vala        |   10 +-
 libgda/data/Makefile.am            |   10 ++-
 libgda/gda-data-model.c            |    1 -
 samples/vala/SampleDataObject.vala |   18 ++--
 tests/vala/CheckDataObject.vala    |   24 +++--
 6 files changed, 129 insertions(+), 122 deletions(-)
---
diff --git a/libgda/data/DataModelIterator.vala b/libgda/data/DataModelIterator.vala
index 5b70257..d564cc2 100644
--- a/libgda/data/DataModelIterator.vala
+++ b/libgda/data/DataModelIterator.vala
@@ -22,8 +22,7 @@
  
  namespace GdaData {
  	
- 	public class DataModelIterable : GLib.Object, Gee.Traversable <Value?>, 
- 										Gee.Iterable <Value?>, Gee.Collection <Value?>, Gda.DataModel
+ 	public class DataModelIterable : Gee.AbstractCollection<Value?>, Gda.DataModel
  	{
  		private Gda.DataModel model;
  		
@@ -37,20 +36,20 @@
  			get { return typeof (GLib.Value); } 
  		}
  		
- 		public Iterator<Value?> iterator ()
+ 		public override Iterator<Value?> iterator ()
  		{
  			return new DataModelIterator (this.model);
  		}
  		
  		// Traversable Interface
  		
-		public Gee.Iterator<Value?> chop (int offset, int length = -1)
+		public override Gee.Iterator<Value?> chop (int offset, int length = -1)
  		{
  			var iter = new DataModelIterator (this.model);
  			return iter.chop (offset, length);
  		}
  		
-		public Gee.Iterator<Value?> filter (owned Gee.Predicate<Value?> f)
+		public override Gee.Iterator<Value?> filter (owned Gee.Predicate<Value?> f)
 		{
 			var iter = new DataModelIterator (this.model);
  			return iter.filter (f);
@@ -58,25 +57,29 @@
 		
 		public new void @foreach (Gee.ForallFunc<Value?> f)
 		{
-			for (int i = 0; i < this.model.get_n_rows (); i++) {
-				for (int j = 0; j < this.model.get_n_columns (); j++) {
-					Value v = this.model.get_value_at (i, j);
-					f(v);
-				}				
-			}
+			try {
+				for (int i = 0; i < this.model.get_n_rows (); i++) {
+					for (int j = 0; j < this.model.get_n_columns (); j++) {
+						Value v = this.model.get_value_at (i, j);
+						f(v);
+					}				
+				}
+			} catch {}
 		}
 		
-		public Gee.Iterator<A> stream<A> (owned Gee.StreamFunc<Value?,A> f)
+		public override Gee.Iterator<A> stream<A> (owned Gee.StreamFunc<Value?,A> f)
 		{
 			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;
+		public override bool add (Value? item) {
+			try {
+				int i = this.model.append_row ();
+				if (i >= 0)
+					return true;
+			} catch {}
 			
 			return false;
 		}
@@ -90,26 +93,28 @@
 		 * <<BR>>
 		 * If the collection is a database proxy, you need to apply to make changes permanently.
 		 */
-		public bool add_all (Gee.Collection<Value?> collection)
+		public override 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);
+			try {
+				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;
 				}
-				catch {
-					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;
+					}
 				}
-			}
+			} catch {}
 			return true;
 		}
 		
@@ -120,25 +125,29 @@
 		 * <<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 override void clear () {
+			try {
+				for (int i = 0; i < this.model.get_n_rows (); i++ ) {
+					this.model.remove_row (i);
+				}
+			} catch {}
 		}
 		
-		public bool contains (Value? item)
+		public override 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;
+			try {
+				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;
+					}
 				}
-			}
+			} catch {}
 			return false;
 		}
 		
-		public bool contains_all (Gee.Collection<Value?> collection)
+		public override bool contains_all (Gee.Collection<Value?> collection)
 		{
 			bool ret = true;
 			foreach (Value v in collection) {
@@ -162,7 +171,7 @@
 		 * <<BR>>
 		 * If the collection is a database proxy, you need to apply to make changes permanently.
 		 */
-		public bool remove (Value? item) {
+		public override 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 {
@@ -192,7 +201,7 @@
 		 * <<BR>>
 		 * If the collection is a database proxy, you need to apply to make changes permanently.
 		 */
-		public bool remove_all (Gee.Collection<Value?> collection)
+		public override bool remove_all (Gee.Collection<Value?> collection)
 		{
 			foreach (Value v in collection) {
 				for (int r = 0; r < this.model.get_n_rows (); r++) {
@@ -229,7 +238,7 @@
 		 * <<BR>>
 		 * If the collection is a database proxy, you need to apply to make changes permanently.
 		 */
-		public bool retain_all (Gee.Collection<Value?> collection)
+		public override bool retain_all (Gee.Collection<Value?> collection)
 		{
 			foreach (Value v in collection) {
 				for (int r = 0; r < this.model.get_n_rows (); r++) {
@@ -255,27 +264,7 @@
 			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 { 
+		public override bool is_empty { 
 			get {
 				if (this.model.get_n_rows () <= 0)
 					return true;
@@ -283,19 +272,24 @@
 				return false;
 			} 
 		}
-		public bool read_only { 
+		public override bool read_only { 
 			get {
-				// FIXME: Check if is a Proxy DataModel can be modified to return true
+				if (this.model is Gda.DataProxy)
+					return ((Gda.DataProxy) this.model).is_read_only ();
+				
 				return true;
 			}
 		}
-		public Gee.Collection<Value?> read_only_view { 
+		public override Gee.Collection<Value?> read_only_view { 
 			owned get {
-				return (Gee.Collection<Value?>) ((GLib.Object) this).ref ();
+				if (this.model is Gda.DataProxy)
+					return (Gee.Collection<Value?>) 
+								new DataModelIterable (((DataProxy)this.model).get_proxied_model ());
+				return (Gee.Collection<Value?>) new DataModelIterable (this.model);
 			}
 		}
 		
-		public int size { 
+		public override int size { 
 			get {
 				return this.model.get_n_columns () * this.model.get_n_rows ();
 			} 
@@ -314,10 +308,6 @@
 			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 ();
 		}
@@ -358,6 +348,10 @@
 			return this.model.i_iter_set_value (iter, col, value);
 		}
 		
+		public void i_set_notify (bool do_notify_changes)
+		{
+			this.model.i_set_notify (do_notify_changes);
+		}
 		public bool remove_row (int row) throws GLib.Error {
 			return this.model.remove_row (row);
 		}
@@ -529,12 +523,15 @@
 		
 		public new void @foreach (Gee.ForallFunc<Value?> f)
 		{
-			for (int i = this.pos_init; 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);
+			try {
+				for (int i = this.pos_init; 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);
+				}
 			}
+			catch {}
 		}
 		
 		/**
@@ -548,20 +545,21 @@
 		public Gee.Iterator<A> stream<A> (owned Gee.StreamFunc<Value?, A> f)
 		{
 			var l = new Gee.ArrayList<A> ();
-			for (int i = this.pos_init; 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 (col, row);
-				var g = new Gee.Lazy<Value?>.from_value (v);
-				Gee.Lazy<A> s;
-				var r = f (Gee.Traversable.Stream.CONTINUE, g, out s);
-				if (r == Gee.Traversable.Stream.END)
-					break;
-				if (r == Gee.Traversable.Stream.YIELD) {
-					l.add (s);
+			try {
+				for (int i = this.pos_init; 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 (col, row);
+					var g = new Gee.Lazy<Value?>.from_value (v);
+					Gee.Lazy<A> s;
+					var r = f (Gee.Traversable.Stream.CONTINUE, g, out s);
+					if (r == Gee.Traversable.Stream.END)
+						break;
+					if (r == Gee.Traversable.Stream.YIELD) {
+						l.add (s);
+					}
 				}
-			}
-						
+			} catch {}		
 			return l.iterator<A> ();
 		}
  	}
diff --git a/libgda/data/DataObject.vala b/libgda/data/DataObject.vala
index 133cf72..c0a3159 100644
--- a/libgda/data/DataObject.vala
+++ b/libgda/data/DataObject.vala
@@ -28,7 +28,7 @@ namespace GdaData {
         private Value? _id_value;
         private DataModel? _model;
         
-        protected string? _table;
+        public abstract string table { get; }
         
         public DataModelIterable record {
         	owned get {
@@ -53,7 +53,7 @@ namespace GdaData {
          */
         public void set_id (string field, Value v)
         	throws Error
-        	requires (this._table != "")
+        	requires (this.table != "")
         {
         	this._field_id = field;
         	this._id_value = v;
@@ -84,18 +84,18 @@ namespace GdaData {
         
         public void update ()
         	throws Error
-        	requires (this._table != "")
+        	requires (this.table != "")
         {
         	set_id (this._field_id, this._id_value);
         }
         
         private SqlBuilder build ()
-        	requires (this._table != null || this._table != "")
+        	requires (this.table != null || this.table != "")
         	requires (this._field_id != null || this._field_id != "")
         	requires (this._id_value != null)
         {
         	var q = new SqlBuilder (SqlStatementType.SELECT);
-        	q.select_add_target (this._table, null);
+        	q.select_add_target (this.table, null);
         	var f_id = q.add_id (this._field_id);
 			var e_id = q.add_expr_value (null, this._id_value);
 			var c_id = q.add_cond (SqlOperatorType.EQ, f_id, e_id, 0);
diff --git a/libgda/data/Makefile.am b/libgda/data/Makefile.am
index afe6264..7f39519 100644
--- a/libgda/data/Makefile.am
+++ b/libgda/data/Makefile.am
@@ -80,10 +80,14 @@ endif
 # GIR files are generated automatically by Valac then is not necessary to scan source code to generate it
 INTROSPECTION_GIRS =
 INTROSPECTION_GIRS += GdaData-5.0.gir
-INTROSPECTION_COMPILER_ARGS = 
+INTROSPECTION_COMPILER_ARGS = \
+    --includedir=. \
+    --includedir=$(top_srcdir)/libgda \
+    --includedir=$(top_srcdir)/libgda/data \
+    $(NULL)
 
-GdaData-0.2.typelib: $(INTROSPECTION_GIRS)
-	$(INTROSPECTION_COMPILER) $(INTROSPECTION_COMPILER_ARGS) --includedir=. $< -o $@
+GdaData-5.0.typelib: $(INTROSPECTION_GIRS)
+	$(INTROSPECTION_COMPILER) $(INTROSPECTION_COMPILER_ARGS)  $< -o $@
 
 gir_DATA = $(INTROSPECTION_GIRS)
 typelibs_DATA = GdaData-5.0.typelib
diff --git a/libgda/gda-data-model.c b/libgda/gda-data-model.c
index b0a9306..13721bd 100644
--- a/libgda/gda-data-model.c
+++ b/libgda/gda-data-model.c
@@ -362,7 +362,6 @@ gda_data_model_reset (GdaDataModel *model)
  * re-enable notifications again, you should call the
  * #gda_data_model_thaw function.
  *
- * Virtual: i_set_notify
  */
 void
 gda_data_model_freeze (GdaDataModel *model)
diff --git a/samples/vala/SampleDataObject.vala b/samples/vala/SampleDataObject.vala
index 51bef61..ec96d33 100644
--- a/samples/vala/SampleDataObject.vala
+++ b/samples/vala/SampleDataObject.vala
@@ -24,6 +24,10 @@ namespace Sample {
 
 	class Book : GdaData.Object {
 		
+		public override string table { 
+			get { return "book"; }
+		}
+		
 		public Collection accounts {
 			get {
 				int id = (int) this.get_value_id ();
@@ -33,10 +37,6 @@ namespace Sample {
 			}
 		}
 		
-		public Book () {
-			this._table = "book";
-		}
-		
 		public static Book create ( Gda.Connection cnn, string name, string manager)
 			requires (cnn.is_open ())
 			throws Error
@@ -61,6 +61,9 @@ namespace Sample {
 	}
 	
 	class Account : GdaData.Object {
+		public override string table { 
+			get { return "account"; }
+		}
 		public Book book { get; set; }
 		/* Is possible to create properties to easy access to any field in the database row */
 		public string name { 
@@ -93,10 +96,6 @@ namespace Sample {
 			}
 		}
 		
-		public Account () {
-			this._table = "account";
-		}
-		
 		public bool open (string name) {
 			Value n = name;
 			this.set_id (name, n);
@@ -105,6 +104,9 @@ namespace Sample {
 	}
 	
 	class Transaction : GdaData.Object {
+		public override string table { 
+			get { return "transaction"; }
+		}
 		public Account account { get; set; }
 		public string description { 
 			get {
diff --git a/tests/vala/CheckDataObject.vala b/tests/vala/CheckDataObject.vala
index cb2dfea..7689b95 100644
--- a/tests/vala/CheckDataObject.vala
+++ b/tests/vala/CheckDataObject.vala
@@ -22,6 +22,11 @@ using GdaData;
 
 namespace Check {
 	class Tests : GdaData.Object {
+		private string t;
+		public override string table { 
+			set construct { this.t = "user"; }
+			get { return this.t; }
+		}
 		Tests()
 		{
 			try {
@@ -49,7 +54,6 @@ namespace Check {
 		{
 			stdout.printf(">>> NEW TEST: Gda.DataObject API tests\n");
 			int fails = 0;
-			this._table = "user";
 			Value v = 1;
 			stdout.printf("Setting ID to %i\n", (int) v);
 			try {
@@ -65,7 +69,7 @@ namespace Check {
 				stdout.printf ("Couln't set ID...\nFAILS: %i\nERROR: %s\n", fails, e.message);
 			}
 			
-			stdout.printf("DataObject points to, in table '%s':\n", this._table);
+			stdout.printf("DataObject points to, in table '%s':\n", this.table);
 			stdout.printf("%s\n", this._model.dump_as_string());
 			
 			stdout.printf("Getting ID value...\n");
@@ -90,13 +94,13 @@ namespace Check {
 			stdout.printf("Setting value at 'name'...\n");
 			Value n = "Daniel Espinosa";
 			this.set_value ("name", n);
-			stdout.printf("DataObject points to in memory modified value, in table '%s':\n", this._table);
+			stdout.printf("DataObject points to in memory modified value, in table '%s':\n", this.table);
 			stdout.printf("%s\n", this._model.dump_as_string());
 			
 			stdout.printf("Saving changes...\n");
 			try {
 				this.save();
-				stdout.printf("DataObject points to modified value, in table '%s':\n", this._table);
+				stdout.printf("DataObject points to modified value, in table '%s':\n", this.table);
 				stdout.printf("%s\n", this._model.dump_as_string());
 			}
 			catch (Error e) {
@@ -109,13 +113,13 @@ namespace Check {
 			}
 			catch (Error e) {
 				fails++;
-				stdout.printf ("Couln't manual update table '%s'...\nFAILS: %i\nERROR: %s\n", this._table, fails, e.message);
+				stdout.printf ("Couln't manual update table '%s'...\nFAILS: %i\nERROR: %s\n", this.table, fails, e.message);
 			}
 			
 			stdout.printf("Updating values from database...\n");
 			try {
 				this.update();
-				stdout.printf("DataObject points to actual stored values, in table '%s':\n", this._table);
+				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) {
@@ -123,12 +127,12 @@ 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("No Common Operation: Setting a new Table... \n");
+			this._t = "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("DataObject points to actual stored values, in table '%s':\n", this.table);
 				stdout.printf("%s\n", this._model.dump_as_string());
 			}
 			catch (Error e) {
@@ -140,7 +144,7 @@ namespace Check {
 			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("DataObject points to actual stored values, in table '%s':\n", this.table);
 				stdout.printf("%s\n", this._model.dump_as_string());
 			}
 			catch (Error e) {



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