[libgda] Fixes to RecordCollection.chop() and stream(). Removed warnings. * RecordCollection.chop() is fixed
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda] Fixes to RecordCollection.chop() and stream(). Removed warnings. * RecordCollection.chop() is fixed
- Date: Thu, 20 Sep 2012 23:34:46 +0000 (UTC)
commit 9e2fbf9db3ca27d62d2ae1d550840eacd0176378
Author: Daniel Espinosa <esodan gmail com>
Date: Thu Sep 20 18:21:18 2012 -0500
Fixes to RecordCollection.chop() and stream(). Removed warnings.
* RecordCollection.chop() is fixed appling patch 224866 at bug 684348 to libgee
* Implemented a custom RecordCollection.stream() function
* Updated Unit Test for RecordCollection
* Removing compilation warnings at vala extensions
libgda/data/GdaData-5.0.gir | 15 ---
libgda/data/Makefile.am | 1 +
libgda/data/RecordCollection.vala | 144 ----------------------
libgda/data/RecordCollectionIterator.vala | 189 +++++++++++++++++++++++++++++
libgda/data/SelectQuery.vala | 2 +-
libgda/data/Table.vala | 6 +-
tests/vala/CheckDataModelIterator.vala | 44 +++++--
7 files changed, 227 insertions(+), 174 deletions(-)
---
diff --git a/libgda/data/GdaData-5.0.gir b/libgda/data/GdaData-5.0.gir
index 25caafe..84f1f4d 100644
--- a/libgda/data/GdaData-5.0.gir
+++ b/libgda/data/GdaData-5.0.gir
@@ -212,21 +212,6 @@
</parameter>
</parameters>
</constructor>
- <method name="chop" c:identifier="gda_data_record_collection_chop">
- <return-value transfer-ownership="full">
- <type name="Gee.Iterator" c:type="GeeIterator*">
- <type name="GdaData.DbRecord" c:type="GdaDataDbRecord*"/>
- </type>
- </return-value>
- <parameters>
- <parameter name="offset" transfer-ownership="none">
- <type name="gint" c:type="gint"/>
- </parameter>
- <parameter name="length" transfer-ownership="none">
- <type name="gint" c:type="gint"/>
- </parameter>
- </parameters>
- </method>
<method name="to_string" c:identifier="gda_data_record_collection_to_string">
<return-value transfer-ownership="full">
<type name="utf8" c:type="gchar*"/>
diff --git a/libgda/data/Makefile.am b/libgda/data/Makefile.am
index e19ac2c..0f6da16 100644
--- a/libgda/data/Makefile.am
+++ b/libgda/data/Makefile.am
@@ -53,6 +53,7 @@ db_collection_sources = \
DbSchema.vala \
DbRecordCollection.vala \
RecordCollection.vala \
+ RecordCollectionIterator.vala \
Field.vala \
FieldInfo.vala \
$(NULL)
diff --git a/libgda/data/RecordCollection.vala b/libgda/data/RecordCollection.vala
index ae9559a..ff39abc 100644
--- a/libgda/data/RecordCollection.vala
+++ b/libgda/data/RecordCollection.vala
@@ -131,154 +131,10 @@ namespace GdaData {
return _model.get_n_rows ();
}
}
- // Traversable interface
- public Gee.Iterator<DbRecord> chop (int offset, int length = -1)
- {
- return this.iterator ().chop (offset, length);
- }
//
public string to_string ()
{
return _model.dump_as_string ();
}
}
-
- public class RecordCollectionIterator : Object, Traversable<DbRecord>, Iterator<DbRecord>
- {
- private DataModelIter _iter;
- private DbTable _table;
- private int _length = -1;
- private int init_row = -1;
- private HashMap<int,int> _elements;
- private int filter_pos = -1;
-
- public RecordCollectionIterator (DataModelIter iter, DbTable table)
- {
- _iter = iter;
- _table = table;
- _elements = new HashMap<int,int>();
- }
-
- private RecordCollectionIterator.filtered (DataModelIter iter, DbTable table,
- int init, int offset, int length,
- HashMap<int,int> elements, int filter_pos)
- {
- _iter = iter;
- _table = table;
- _elements = elements;
- this.filter_pos = filter_pos;
- if (init < 0 && offset >= 0) init = 0;
- if (init >= 0 && init < _iter.data_model.get_n_rows ()
- && init + offset >= 0
- && init + offset < _iter.data_model.get_n_rows ())
- {
- _iter.move_to_row (init + offset);
- init_row = _iter.current_row;
- if (length >= 0 && init + length < _iter.data_model.get_n_rows ())
- {
- _length = length;
- }
- }
- else
- init_row = -10; // Invalidate all the interator
- if (_elements.size > 0)
- init_row = -1; // Validate interator due to filtered values
- }
-
- // Traversable Interface
- public Gee.Iterator<DbRecord> chop (int offset, int length = -1)
- {
- var iter = _iter.data_model.create_iter ();
- return new RecordCollectionIterator.filtered (iter, _table, _iter.current_row,
- offset, length, _elements, filter_pos);
- }
- public Gee.Iterator<DbRecord> filter (owned Gee.Predicate<DbRecord> f)
- {
- var elements = new Gee.HashMap <int,int> ();
- int pos = -1;
- while (this.next ()) {
- var r = this.get ();
- if (f(r)) {
- elements.set (++pos, _iter.current_row);
- }
- }
- var iter = _iter.data_model.create_iter ();
- return new RecordCollectionIterator.filtered (iter, _table, -1, 0, -1, elements, -1);
- }
- public new bool @foreach (Gee.ForallFunc<DbRecord> f)
- {
- var r = this.get ();
- bool ret = f(r);
- if (ret && this.next ())
- return true;
- else
- return false;
- }
-
- // Iterator Interface
- public new DbRecord @get ()
- {
- var r = new Record ();
- r.connection = _table.connection;
- r.table = _table;
- try {
- for (int c = 0; c < _iter.data_model.get_n_columns (); c++) {
- r.set_field_value (_iter.data_model.get_column_name (c), _iter.get_value_at (c));
- }
- }
- catch (Error e) { GLib.message (e.message); }
- return r;
- }
- public bool has_next ()
- {
- if ((_length >= 0) && (_iter.current_row + 1 < _length + init_row)
- && (_iter.current_row + 1 > _iter.data_model.get_n_rows ()))
- return false;
- if (_elements.size > 0)
- return filter_pos + 1 < _elements.size ? true : false;
-
- return _iter.current_row + 1 <= _iter.data_model.get_n_rows () ? true : false;
- }
- public bool next ()
- {
- if (this.has_next ()) {
- if (_elements.size > 0) {
- _iter.move_to_row (_elements.get (++filter_pos));
- return true;
- }
- else
- return _iter.move_next ();
- }
- return false;
- }
- public void remove ()
- {
- try { _iter.data_model.remove_row (_iter.current_row); }
- catch (Error e) { GLib.message (e.message); }
- }
- public bool read_only
- {
- get {
- if (_iter.data_model is DataProxy)
- return ((DataProxy)_iter.data_model).is_read_only ();
- else
- return false;
- }
- }
- public bool valid {
- get {
- if (init_row == -10)
- return false;
-
- if (_length >= 0)
- return (_iter.current_row >= init_row)
- && (_iter.current_row < _length + init_row) ? true : false;
- if (_elements.size > 0)
- return filter_pos < _elements.size;
-
- return (_iter.current_row >= 0) &&
- (_iter.current_row < _iter.data_model.get_n_rows ())? true : false;
- }
- }
- }
}
diff --git a/libgda/data/RecordCollectionIterator.vala b/libgda/data/RecordCollectionIterator.vala
new file mode 100644
index 0000000..f2d7cc3
--- /dev/null
+++ b/libgda/data/RecordCollectionIterator.vala
@@ -0,0 +1,189 @@
+/* -*- 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 {
+ public class RecordCollectionIterator : Object, Traversable<DbRecord>, Iterator<DbRecord>
+ {
+ private DataModelIter _iter;
+ private DbTable _table;
+ private int _length = -1;
+ private int init_row = -1;
+ private HashMap<int,int> _elements;
+ private int filter_pos = -1;
+
+ public RecordCollectionIterator (DataModelIter iter, DbTable table)
+ {
+ _iter = iter;
+ _table = table;
+ _elements = new HashMap<int,int>();
+ }
+
+ private RecordCollectionIterator.filtered (DataModelIter iter, DbTable table,
+ int init, int offset, int length,
+ HashMap<int,int> elements, int filter_pos)
+ {
+ _iter = iter;
+ _table = table;
+ _elements = elements;
+ this.filter_pos = filter_pos;
+ if (init < 0 && offset >= 0) init = 0;
+ if (init >= 0 && init < _iter.data_model.get_n_rows ()
+ && init + offset >= 0
+ && init + offset < _iter.data_model.get_n_rows ())
+ {
+ _iter.move_to_row (init + offset);
+ init_row = _iter.current_row;
+ if (length >= 0 && init + length < _iter.data_model.get_n_rows ())
+ {
+ _length = length;
+ }
+ }
+ else
+ init_row = -10; // Invalidate all the interator
+ if (_elements.size > 0)
+ init_row = -1; // Validate interator due to filtered values
+ }
+
+ // Traversable Interface
+ public Iterator<A> stream<A> (owned StreamFunc<DbRecord, A> f) {
+ var l = new Gee.ArrayList<A> ();
+ Traversable.Stream str;
+ Lazy<A>? initial = null;
+ DbRecord r;
+
+ str = f (Stream.YIELD, null, out initial);
+ while (this.next ()) {
+ if (this.valid) {
+ r = this.get ();
+ str = f (Stream.CONTINUE, new Lazy<DbRecord> (() => {return r;}), out initial);
+ switch (str) {
+ case Stream.CONTINUE:
+ break;
+ case Stream.YIELD:
+ if (initial != null) {
+ l.add (initial.value);
+ }
+ break;
+ case Stream.END:
+ return l.iterator();
+ }
+ }
+ }
+ return l.iterator ();
+ }
+
+ public Gee.Iterator<DbRecord> chop (int offset, int length = -1)
+ {
+ var iter = _iter.data_model.create_iter ();
+ return new RecordCollectionIterator.filtered (iter, _table, _iter.current_row,
+ offset, length, _elements, filter_pos);
+ }
+ public Gee.Iterator<DbRecord> filter (owned Gee.Predicate<DbRecord> f)
+ {
+ var elements = new Gee.HashMap <int,int> ();
+ int pos = -1;
+ while (this.next ()) {
+ var r = this.get ();
+ if (f(r)) {
+ elements.set (++pos, _iter.current_row);
+ }
+ }
+ var iter = _iter.data_model.create_iter ();
+ return new RecordCollectionIterator.filtered (iter, _table, -1, 0, -1, elements, -1);
+ }
+ public new bool @foreach (Gee.ForallFunc<DbRecord> f)
+ {
+ var r = this.get ();
+ bool ret = f(r);
+ if (ret && this.next ())
+ return true;
+ else
+ return false;
+ }
+
+ // Iterator Interface
+ public new DbRecord @get ()
+ {
+ var r = new Record ();
+ r.connection = _table.connection;
+ r.table = _table;
+ try {
+ for (int c = 0; c < _iter.data_model.get_n_columns (); c++) {
+ r.set_field_value (_iter.data_model.get_column_name (c), _iter.get_value_at (c));
+ }
+ }
+ catch (Error e) { GLib.message (e.message); }
+ return r;
+ }
+ public bool has_next ()
+ {
+ if ((_length >= 0) && (_iter.current_row + 1 < _length + init_row)
+ && (_iter.current_row + 1 > _iter.data_model.get_n_rows ()))
+ return false;
+ if (_elements.size > 0)
+ return filter_pos + 1 < _elements.size ? true : false;
+
+ return _iter.current_row + 1 <= _iter.data_model.get_n_rows () ? true : false;
+ }
+ public bool next ()
+ {
+ if (this.has_next ()) {
+ if (_elements.size > 0) {
+ _iter.move_to_row (_elements.get (++filter_pos));
+ return true;
+ }
+ else
+ return _iter.move_next ();
+ }
+ return false;
+ }
+ public void remove ()
+ {
+ try { _iter.data_model.remove_row (_iter.current_row); }
+ catch (Error e) { GLib.message (e.message); }
+ }
+ public bool read_only
+ {
+ get {
+ if (_iter.data_model is DataProxy)
+ return ((DataProxy)_iter.data_model).is_read_only ();
+ else
+ return false;
+ }
+ }
+ public bool valid {
+ get {
+ if (init_row == -10)
+ return false;
+
+ if (_length >= 0)
+ return (_iter.current_row >= init_row)
+ && (_iter.current_row < _length + init_row) ? true : false;
+ if (_elements.size > 0)
+ return filter_pos < _elements.size;
+
+ return (_iter.current_row >= 0) &&
+ (_iter.current_row < _iter.data_model.get_n_rows ())? true : false;
+ }
+ }
+ }
+}
diff --git a/libgda/data/SelectQuery.vala b/libgda/data/SelectQuery.vala
index 11001be..d4a7c60 100644
--- a/libgda/data/SelectQuery.vala
+++ b/libgda/data/SelectQuery.vala
@@ -23,7 +23,7 @@ namespace GdaData {
private class SelectQuery : GLib.Object {
- private Gee.ArrayList<string> _fields;
+ //private Gee.ArrayList<string> _fields;
private SqlBuilder _sql;
private string _table;
diff --git a/libgda/data/Table.vala b/libgda/data/Table.vala
index 5d39bcf..5a5aee1 100644
--- a/libgda/data/Table.vala
+++ b/libgda/data/Table.vala
@@ -318,7 +318,7 @@ namespace GdaData
GLib.warning ("Not Implemented!");
return;
// FIXME: ServerOperation Bug or biding Bug
- if (GLib.strcmp (_name,_original_name) != 0) {
+ /*if (GLib.strcmp (_name,_original_name) != 0) {
if (connection.get_provider ()
.supports_operation (connection,
Gda.ServerOperationType.RENAME_TABLE, null)) {
@@ -327,8 +327,6 @@ namespace GdaData
null);
op.set_value_at (_original_name, "/TABLE_DEF_P/TABLE_NAME");
op.set_value_at (name, "/TABLE_DEF_P/TABLE_NEW_NAME");
- /*DEBUG
- */
stdout.printf ("Operation to perform: "+
connection.get_provider ()
.render_operation (connection,op));
@@ -340,7 +338,7 @@ namespace GdaData
}
else {
throw new DbTableError.READ_ONLY ("Table definition is read only");
- }
+ }*/
}
public void append () throws Error
diff --git a/tests/vala/CheckDataModelIterator.vala b/tests/vala/CheckDataModelIterator.vala
index 460827d..8622daa 100644
--- a/tests/vala/CheckDataModelIterator.vala
+++ b/tests/vala/CheckDataModelIterator.vala
@@ -19,6 +19,7 @@
using Gda;
using GdaData;
+using Gee;
namespace Check {
@@ -233,36 +234,59 @@ namespace Check {
public int streaming ()
{
int fails = 0;
- /* FIXME: This code doesn't return YIELDED strings maybe becasue Lazy is not correctly built.
- In theory stream() function is working correctly*/
- stdout.printf (">>> TESTING: Streaming Values: Fist field type STRING will be YIELDED...\n");
+ stdout.printf (">>> TESTING: Streaming Values: First DbRecord's field type STRING will be YIELDED...\n");
var iter = itermodel.stream<string> (
(state, g, out lazy) => {
- if (state == Gee.Traversable.Stream.CONTINUE) {
+ switch(state) {
+ case Gee.Traversable.Stream.YIELD:
+ lazy = null;
+ return Gee.Traversable.Stream.CONTINUE;
+ break;
+ case Gee.Traversable.Stream.CONTINUE:
var r = g.value;
foreach (DbField f in r.fields) {
if (f.value.type () == typeof (string)) {
- stdout.printf ("Value to YIELD: %s\n", Gda.value_stringify (f.value));
- string ts = "YIELDED Value = " + Gda.value_stringify (f.value) + "\n";
- lazy = new Gee.Lazy<string>.from_value (ts.dup ());
+ stdout.printf ("Field (%s) = %s\n", f.name, Gda.value_stringify (f.value));
+ string ts = Gda.value_stringify (f.value);
+ lazy = new Gee.Lazy<string> (() => {return ts.dup ();});
}
}
return Gee.Traversable.Stream.YIELD;
+ break;
}
return Gee.Traversable.Stream.END;
}
);
stdout.printf ("Printing Streamed Values...\n");
+ var l = new Gee.ArrayList<string> ();
while (iter.next ()) {
- stdout.printf (iter.get());
+ string v = iter.get ();
+ l.add (v);
+ stdout.printf (v + "\n");
+ }
+
+ if (!l.contains ("Daniel"))
+ ++fails;
+ if (!l.contains ("Jhon"))
+ ++fails;
+ if (!l.contains ("Jack"))
+ ++fails;
+ if (!l.contains ("Elsy"))
+ ++fails;
+ if (!l.contains ("Mayo"))
+ ++fails;
+
+ if (fails > 0) {
+ stdout.printf ("----- FAIL" + (++fails).to_string () + "\n");
}
- stdout.printf ("\n");
+ else
+ stdout.printf ("+++++ PASS\n");
return fails;
}
public int InitIter()
throws Error
{
- stdout.printf (">>> TESTING: Initializing DbRecordCollection/RecordCollection\n");
+ stdout.printf (">>> INITIALIZING: DbRecordCollection/RecordCollection\n");
int fails = 0;
var model = this.connection.execute_select_command ("SELECT * FROM user");
stdout.printf ("Setting up Table...");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]