[libgda] Added equal() and to_string() to DbField. equal() and copy() to DbRecord. * Added DbRecord.copy () *
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda] Added equal() and to_string() to DbField. equal() and copy() to DbRecord. * Added DbRecord.copy () *
- Date: Thu, 11 Apr 2013 03:52:14 +0000 (UTC)
commit cb60405b66aa959284f613ef14a0e73e4b8c83b5
Author: Daniel Espinosa <esodan gmail com>
Date: Wed Apr 10 22:45:49 2013 -0500
Added equal() and to_string() to DbField. equal() and copy() to DbRecord.
* Added DbRecord.copy ()
* Updated bindings and GI for GdaData
* Removed to_string () from Field
libgda/data/DbField.vala | 70 +++++++++++++++++++--
libgda/data/DbRecord.vala | 35 +++++++++++
libgda/data/Field.vala | 1 -
libgda/data/GdaData-5.0.gir | 130 +++++++++++++++++++++++++++++++++++++-
tests/vala/CheckDataObject.vala | 26 +++++++-
5 files changed, 248 insertions(+), 14 deletions(-)
---
diff --git a/libgda/data/DbField.vala b/libgda/data/DbField.vala
index 41cc73f..e21ec1b 100644
--- a/libgda/data/DbField.vala
+++ b/libgda/data/DbField.vala
@@ -28,9 +28,7 @@ namespace GdaData
public abstract string name { get; set; }
public abstract string column_name { get; }
public abstract DbField.Attribute attributes { get; }
-
- public abstract string to_string ();
-
+
[Flags]
public enum Attribute {
NONE,
@@ -43,9 +41,67 @@ namespace GdaData
DATA_NON_VALID,
HAS_VALUE_ORIG,
NO_MODIF,
- UNUSED
+ UNUSED;
+
+ public static Attribute[] items () {
+ return {
+ NONE,
+ IS_NULL,
+ CAN_BE_NULL,
+ IS_DEFAULT,
+ CAN_BE_DEFAULT,
+ IS_UNCHANGED,
+ ACTIONS_SHOWN,
+ DATA_NON_VALID,
+ HAS_VALUE_ORIG,
+ NO_MODIF,
+ UNUSED
+ };
+ }
+
+ public string to_string () {
+ switch (this) {
+ case NONE:
+ return "None";
+ case IS_NULL:
+ return "IsNull";
+ case CAN_BE_NULL:
+ return "CanBeNull";
+ case IS_DEFAULT:
+ return "IsDefault";
+ case CAN_BE_DEFAULT:
+ return "CanBeDefault";
+ case IS_UNCHANGED:
+ return "IsUnchanged";
+ case ACTIONS_SHOWN:
+ return "ActionsShown";
+ case DATA_NON_VALID:
+ return "DataNonValid";
+ case HAS_VALUE_ORIG:
+ return "HasValueOrig";
+ case NO_MODIF:
+ return "NoModif";
+ case UNUSED:
+ return "CanBeNull";
+ default:
+ assert_not_reached();
+ }
+ }
+ }
+
+ public virtual bool equal (DbField field) {
+ if (field.name != name)
+ return false;
+ if (field.column_name != column_name)
+ return false;
+ var attributes = Attribute.items ();
+ foreach (Attribute att in attributes) {
+ if (att in field.attributes && !(att in attributes))
+ return false;
+ }
+ return true;
}
-
+
public static Value? value_from_string (string as_string, Type type)
{
// FIXME: No all basic types have support to parse from string se bug 669278
@@ -83,5 +139,9 @@ namespace GdaData
return as_string;
}
+
+ public virtual string to_string () {
+ return @"([$name],[$column_name],[%s],[$(attributes)])".printf (Gda.value_stringify
(value));
+ }
}
}
diff --git a/libgda/data/DbRecord.vala b/libgda/data/DbRecord.vala
index 4fea153..4278ab6 100644
--- a/libgda/data/DbRecord.vala
+++ b/libgda/data/DbRecord.vala
@@ -38,5 +38,40 @@ namespace GdaData
public abstract void set_key_value (string field, Value? val) throws Error;
public abstract DbField get_key (string name) throws Error;
public abstract string to_string ();
+
+ public virtual void copy (DbRecord record) throws Error {
+ foreach (DbField f in record.fields) {
+ this.set_field (f);
+ }
+ foreach (DbField k in record.keys) {
+ this.set_key (k);
+ }
+ this.connection = record.connection;
+ this.table = record.table;
+ }
+
+ public virtual bool equal (DbRecord record) throws Error {
+ if (record.table != null && this.table != null)
+ if (this.table.name != record.table.name)
+ return false;
+
+ foreach (DbField f in record.fields) {
+ try {
+ var tmp = this.get_field (f.name);
+ if (!f.equal (tmp))
+ return false;
+ }
+ catch { return false; }
+ }
+ foreach (DbField k in record.keys) {
+ try {
+ var ktmp = this.get_key (k.name);
+ if (!k.equal (ktmp))
+ return false;
+ }
+ catch { return false; }
+ }
+ return true;
+ }
}
}
diff --git a/libgda/data/Field.vala b/libgda/data/Field.vala
index 8b3f0ed..ec10865 100644
--- a/libgda/data/Field.vala
+++ b/libgda/data/Field.vala
@@ -43,7 +43,6 @@ namespace GdaData
public DbField.Attribute attributes {
get { return _attributes; }
}
- public string to_string () { return Gda.value_stringify (val); }
public Field (string col_name, DbField.Attribute attr)
{
_column_name = col_name;
diff --git a/libgda/data/GdaData-5.0.gir b/libgda/data/GdaData-5.0.gir
index 84f1f4d..c72c9b1 100644
--- a/libgda/data/GdaData-5.0.gir
+++ b/libgda/data/GdaData-5.0.gir
@@ -761,6 +761,46 @@
<type name="utf8" c:type="gchar*"/>
</return-value>
</virtual-method>
+ <method name="copy" c:identifier="gda_data_db_record_copy" throws="1">
+ <return-value transfer-ownership="full">
+ <type name="none"/>
+ </return-value>
+ <parameters>
+ <parameter name="record" transfer-ownership="none">
+ <type name="GdaData.DbRecord" c:type="GdaDataDbRecord*"/>
+ </parameter>
+ </parameters>
+ </method>
+ <virtual-method name="copy" invoker="copy" throws="1">
+ <return-value transfer-ownership="full">
+ <type name="none"/>
+ </return-value>
+ <parameters>
+ <parameter name="record" transfer-ownership="none">
+ <type name="GdaData.DbRecord" c:type="GdaDataDbRecord*"/>
+ </parameter>
+ </parameters>
+ </virtual-method>
+ <method name="equal" c:identifier="gda_data_db_record_equal" throws="1">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="record" transfer-ownership="none">
+ <type name="GdaData.DbRecord" c:type="GdaDataDbRecord*"/>
+ </parameter>
+ </parameters>
+ </method>
+ <virtual-method name="equal" invoker="equal" throws="1">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="record" transfer-ownership="none">
+ <type name="GdaData.DbRecord" c:type="GdaDataDbRecord*"/>
+ </parameter>
+ </parameters>
+ </virtual-method>
<property name="table" writable="1" construct="1">
<type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
</property>
@@ -902,18 +942,58 @@
</parameters>
</callback>
</field>
+ <field name="copy">
+ <callback name="copy" c:type="copy" throws="1">
+ <return-value transfer-ownership="full">
+ <type name="none"/>
+ </return-value>
+ <parameters>
+ <parameter name="self" transfer-ownership="none">
+ <type name="GdaData.DbRecord" c:type="GdaDataDbRecord*"/>
+ </parameter>
+ <parameter name="record" transfer-ownership="none">
+ <type name="GdaData.DbRecord" c:type="GdaDataDbRecord*"/>
+ </parameter>
+ </parameters>
+ </callback>
+ </field>
+ <field name="equal">
+ <callback name="equal" c:type="equal" throws="1">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="self" transfer-ownership="none">
+ <type name="GdaData.DbRecord" c:type="GdaDataDbRecord*"/>
+ </parameter>
+ <parameter name="record" transfer-ownership="none">
+ <type name="GdaData.DbRecord" c:type="GdaDataDbRecord*"/>
+ </parameter>
+ </parameters>
+ </callback>
+ </field>
</record>
<interface name="DbField" c:type="GdaDataDbField" glib:type-name="GdaDataDbField"
glib:get-type="gda_data_db_field_get_type" glib:type-struct="DbFieldIface">
<prerequisite name="GObject.Object"/>
- <method name="to_string" c:identifier="gda_data_db_field_to_string">
+ <method name="equal" c:identifier="gda_data_db_field_equal">
<return-value transfer-ownership="full">
- <type name="utf8" c:type="gchar*"/>
+ <type name="gboolean" c:type="gboolean"/>
</return-value>
+ <parameters>
+ <parameter name="field" transfer-ownership="none">
+ <type name="GdaData.DbField" c:type="GdaDataDbField*"/>
+ </parameter>
+ </parameters>
</method>
- <virtual-method name="to_string" invoker="to_string">
+ <virtual-method name="equal" invoker="equal">
<return-value transfer-ownership="full">
- <type name="utf8" c:type="gchar*"/>
+ <type name="gboolean" c:type="gboolean"/>
</return-value>
+ <parameters>
+ <parameter name="field" transfer-ownership="none">
+ <type name="GdaData.DbField" c:type="GdaDataDbField*"/>
+ </parameter>
+ </parameters>
</virtual-method>
<function name="value_from_string" c:identifier="gda_data_db_field_value_from_string">
<return-value transfer-ownership="full" allow-none="1">
@@ -928,6 +1008,16 @@
</parameter>
</parameters>
</function>
+ <method name="to_string" c:identifier="gda_data_db_field_to_string">
+ <return-value transfer-ownership="full">
+ <type name="utf8" c:type="gchar*"/>
+ </return-value>
+ </method>
+ <virtual-method name="to_string" invoker="to_string">
+ <return-value transfer-ownership="full">
+ <type name="utf8" c:type="gchar*"/>
+ </return-value>
+ </virtual-method>
<property name="value" writable="1">
<type name="GObject.Value" c:type="GValue*"/>
</property>
@@ -945,6 +1035,21 @@
<field name="parent_iface">
<type name="GObject.TypeInterface" c:type="GTypeInterface"/>
</field>
+ <field name="equal">
+ <callback name="equal" c:type="equal">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="self" transfer-ownership="none">
+ <type name="GdaData.DbField" c:type="GdaDataDbField*"/>
+ </parameter>
+ <parameter name="field" transfer-ownership="none">
+ <type name="GdaData.DbField" c:type="GdaDataDbField*"/>
+ </parameter>
+ </parameters>
+ </callback>
+ </field>
<field name="to_string">
<callback name="to_string" c:type="to_string">
<return-value transfer-ownership="full">
@@ -971,6 +1076,23 @@
<member name="no_modif" c:identifier="GDA_DATA_DB_FIELD_ATTRIBUTE_NO_MODIF" value="512"/>
<member name="unused" c:identifier="GDA_DATA_DB_FIELD_ATTRIBUTE_UNUSED" value="1024"/>
</bitfield>
+ <function name="db_field_attribute_items" c:identifier="gda_data_db_field_attribute_items">
+ <return-value transfer-ownership="full">
+ <array length="0">
+ <type name="GdaData.DbFieldAttribute" c:type="GdaDataDbFieldAttribute"/>
+ </array>
+ </return-value>
+ <parameters>
+ <parameter name="result_length1" direction="out" transfer-ownership="none">
+ <type name="gint" c:type="gint"/>
+ </parameter>
+ </parameters>
+ </function>
+ <function name="db_field_attribute_to_string" c:identifier="gda_data_db_field_attribute_to_string">
+ <return-value transfer-ownership="full">
+ <type name="utf8" c:type="gchar*"/>
+ </return-value>
+ </function>
<interface name="DbFieldInfo" c:type="GdaDataDbFieldInfo" glib:type-name="GdaDataDbFieldInfo"
glib:get-type="gda_data_db_field_info_get_type" glib:type-struct="DbFieldInfoIface">
<prerequisite name="GObject.Object"/>
<function name="attribute_from_string"
c:identifier="gda_data_db_field_info_attribute_from_string">
diff --git a/tests/vala/CheckDataObject.vala b/tests/vala/CheckDataObject.vala
index 6fee176..c17b835 100644
--- a/tests/vala/CheckDataObject.vala
+++ b/tests/vala/CheckDataObject.vala
@@ -30,7 +30,7 @@ namespace Check {
class Tests : GLib.Object {
public Gda.Connection connection { get; set; }
- Tests()
+ Tests ()
{
try {
GLib.FileUtils.unlink("dataobject.db");
@@ -52,7 +52,7 @@ namespace Check {
}
}
- public int t1()
+ public int t1( )
throws Error
{
stdout.printf(">>> NEW TEST: GdaData.DbRecord API tests\n");
@@ -193,11 +193,29 @@ namespace Check {
fails++;
stdout.printf ("FAIL: %i\nCouln't set ID...ERROR: %s\n", fails, e.message);
}
- r.t = "user"; // Reset to default
+
+ stdout.printf ("Coping Record ...");
+ var nr = new Check.Record ();
+ nr.copy (r);
+ if (!nr.equal (r)) {
+ fails++;
+ stdout.printf ("FAIL: %i\n No copy or equal works correctly\n", fails);
+ stdout.printf ("DUMP (from):\n");
+ foreach (DbField f in r.fields) {
+ stdout.printf ("%s\n", f.to_string ());
+ }
+ stdout.printf ("DUMP (to):\n");
+ foreach (DbField f2 in nr.fields) {
+ stdout.printf ("%s\n", f2.to_string ());
+ }
+ }
+ else
+ stdout.printf ("PASS\n");
+
return fails;
}
- public int t2()
+ public int t2 ()
throws Error
{
stdout.printf(">>> NEW TEST: Gda.DbRecord - Adding/Deleting objects to DB...\n");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]