[libgda/libgda-vala] Added equivalent() and compatible() methods to DbTable and DbFieldInfo
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda/libgda-vala] Added equivalent() and compatible() methods to DbTable and DbFieldInfo
- Date: Fri, 17 May 2013 02:34:49 +0000 (UTC)
commit b8960d88f7989efba6c619719d0534997828f415
Author: Daniel Espinosa <esodan gmail com>
Date: Thu May 16 09:03:53 2013 -0500
Added equivalent() and compatible() methods to DbTable and DbFieldInfo
libgda/data/DbFieldInfo.vala | 38 ++++++++++-
libgda/data/DbTable.vala | 33 ++++++++++
libgda/data/GdaData-5.0.gir | 140 ++++++++++++++++++++++++++++++++++++++++++
tests/vala/CheckTable.vala | 50 +++++++++++++--
4 files changed, 251 insertions(+), 10 deletions(-)
---
diff --git a/libgda/data/DbFieldInfo.vala b/libgda/data/DbFieldInfo.vala
index d7d0952..5e95c6d 100644
--- a/libgda/data/DbFieldInfo.vala
+++ b/libgda/data/DbFieldInfo.vala
@@ -2,17 +2,17 @@
/*
* libgdavala
* 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/>.
*/
@@ -37,6 +37,36 @@ namespace GdaData {
// Check clause expression
// public abstract DbSqlExpression check_expression { get; set; }
+ public virtual bool compatible (DbFieldInfo field)
+ {
+ if (value_type != field.value_type)
+ return false;
+ if (Attribute.CAN_BE_NULL in attributes)
+ if (!(Attribute.CAN_BE_NULL in field.attributes))
+ return false;
+ return true;
+ }
+
+ public virtual bool equivalent (DbFieldInfo field)
+ {
+ if (name != field.name)
+ return false;
+ if (value_type != field.value_type)
+ return false;
+ if (Attribute.PRIMARY_KEY in attributes)
+ if (!(Attribute.PRIMARY_KEY in field.attributes))
+ return false;
+ if (Attribute.UNIQUE in attributes)
+ if (!(Attribute.UNIQUE in field.attributes))
+ return false;
+ if (Attribute.CHECK in attributes)
+ if (!(Attribute.CHECK in field.attributes))
+ return false;
+ if (Attribute.CAN_BE_NULL in attributes)
+ if (!(Attribute.CAN_BE_NULL in field.attributes))
+ return false;
+ return true;
+ }
// Constrains
public abstract ForeignKey fkey { get; set; }
@@ -259,7 +289,7 @@ namespace GdaData {
return Rule.NONE;
}
- }
+ }
}
}
}
diff --git a/libgda/data/DbTable.vala b/libgda/data/DbTable.vala
index 71db1bd..6a5f409 100644
--- a/libgda/data/DbTable.vala
+++ b/libgda/data/DbTable.vala
@@ -36,6 +36,39 @@ namespace GdaData
public abstract void set_field (DbFieldInfo field) throws Error;
public abstract DbFieldInfo get_field (string name) throws Error;
+ public virtual bool compatible (DbTable table)
+ {
+ foreach (DbFieldInfo f in fields) {
+ foreach (DbFieldInfo ft in table.fields) {
+ if (ft.name == f.name) {
+ if (!f.compatible (ft)) {
+ return false;
+ break;
+ }
+ }
+ }
+ }
+ return true;
+ }
+
+ public virtual bool equivalent (DbTable table)
+ {
+ // At least all fields are located in table and are equivalent
+ foreach (DbFieldInfo f in fields) {
+ bool found = false;
+ foreach (DbFieldInfo ft in table.fields) {
+ if (f.name == ft.name) {
+ found = true;
+ if (!f.equivalent (ft))
+ return false;
+ break;
+ }
+ }
+ if (!found)
+ return false;
+ }
+ return true;
+ }
public enum TableType {
NONE,
diff --git a/libgda/data/GdaData-5.0.gir b/libgda/data/GdaData-5.0.gir
index e588e74..2ddb362 100644
--- a/libgda/data/GdaData-5.0.gir
+++ b/libgda/data/GdaData-5.0.gir
@@ -502,6 +502,46 @@
</parameter>
</parameters>
</virtual-method>
+ <method name="compatible" c:identifier="gda_data_db_table_compatible">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="table" transfer-ownership="none">
+ <type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
+ </parameter>
+ </parameters>
+ </method>
+ <virtual-method name="compatible" invoker="compatible">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="table" transfer-ownership="none">
+ <type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
+ </parameter>
+ </parameters>
+ </virtual-method>
+ <method name="equivalent" c:identifier="gda_data_db_table_equivalent">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="table" transfer-ownership="none">
+ <type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
+ </parameter>
+ </parameters>
+ </method>
+ <virtual-method name="equivalent" invoker="equivalent">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="table" transfer-ownership="none">
+ <type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
+ </parameter>
+ </parameters>
+ </virtual-method>
<property name="catalog" writable="1">
<type name="GdaData.DbCatalog" c:type="GdaDataDbCatalog*"/>
</property>
@@ -571,6 +611,36 @@
</parameters>
</callback>
</field>
+ <field name="compatible">
+ <callback name="compatible" c:type="compatible">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="self" transfer-ownership="none">
+ <type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
+ </parameter>
+ <parameter name="table" transfer-ownership="none">
+ <type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
+ </parameter>
+ </parameters>
+ </callback>
+ </field>
+ <field name="equivalent">
+ <callback name="equivalent" c:type="equivalent">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="self" transfer-ownership="none">
+ <type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
+ </parameter>
+ <parameter name="table" transfer-ownership="none">
+ <type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
+ </parameter>
+ </parameters>
+ </callback>
+ </field>
</record>
<enumeration name="DbTableTableType" c:type="GdaDataDbTableTableType"
glib:type-name="GdaDataDbTableTableType" glib:get-type="gda_data_db_table_table_type_get_type">
<member name="none" c:identifier="GDA_DATA_DB_TABLE_TABLE_TYPE_NONE" value="0"/>
@@ -1092,6 +1162,46 @@
</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"/>
+ <method name="compatible" c:identifier="gda_data_db_field_info_compatible">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="field" transfer-ownership="none">
+ <type name="GdaData.DbFieldInfo" c:type="GdaDataDbFieldInfo*"/>
+ </parameter>
+ </parameters>
+ </method>
+ <virtual-method name="compatible" invoker="compatible">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="field" transfer-ownership="none">
+ <type name="GdaData.DbFieldInfo" c:type="GdaDataDbFieldInfo*"/>
+ </parameter>
+ </parameters>
+ </virtual-method>
+ <method name="equivalent" c:identifier="gda_data_db_field_info_equivalent">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="field" transfer-ownership="none">
+ <type name="GdaData.DbFieldInfo" c:type="GdaDataDbFieldInfo*"/>
+ </parameter>
+ </parameters>
+ </method>
+ <virtual-method name="equivalent" invoker="equivalent">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="field" transfer-ownership="none">
+ <type name="GdaData.DbFieldInfo" c:type="GdaDataDbFieldInfo*"/>
+ </parameter>
+ </parameters>
+ </virtual-method>
<method name="to_string" c:identifier="gda_data_db_field_info_to_string">
<return-value transfer-ownership="full">
<type name="utf8" c:type="gchar*"/>
@@ -1134,6 +1244,36 @@
<field name="parent_iface">
<type name="GObject.TypeInterface" c:type="GTypeInterface"/>
</field>
+ <field name="compatible">
+ <callback name="compatible" c:type="compatible">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="self" transfer-ownership="none">
+ <type name="GdaData.DbFieldInfo"
c:type="GdaDataDbFieldInfo*"/>
+ </parameter>
+ <parameter name="field" transfer-ownership="none">
+ <type name="GdaData.DbFieldInfo"
c:type="GdaDataDbFieldInfo*"/>
+ </parameter>
+ </parameters>
+ </callback>
+ </field>
+ <field name="equivalent">
+ <callback name="equivalent" c:type="equivalent">
+ <return-value transfer-ownership="full">
+ <type name="gboolean" c:type="gboolean"/>
+ </return-value>
+ <parameters>
+ <parameter name="self" transfer-ownership="none">
+ <type name="GdaData.DbFieldInfo"
c:type="GdaDataDbFieldInfo*"/>
+ </parameter>
+ <parameter name="field" transfer-ownership="none">
+ <type name="GdaData.DbFieldInfo"
c:type="GdaDataDbFieldInfo*"/>
+ </parameter>
+ </parameters>
+ </callback>
+ </field>
<field name="to_string">
<callback name="to_string" c:type="to_string">
<return-value transfer-ownership="full">
diff --git a/tests/vala/CheckTable.vala b/tests/vala/CheckTable.vala
index 2e2fd9b..83ab799 100644
--- a/tests/vala/CheckTable.vala
+++ b/tests/vala/CheckTable.vala
@@ -378,10 +378,10 @@ namespace Check {
field2.fkey = fk;
t.set_field (field2);
}
-/*
- public int equal () throws Error
+
+ public int equivalent () throws Error
{
- stdout.printf("\n\n\n>>>>>>>>>>>>>>> NEW TEST: Gda.DbTable - Equal...\n");
+ stdout.printf("\n\n\n>>>>>>>>>>>>>>> NEW TEST: Gda.DbTable - equivalent...\n");
var t = new Table ();
t.name = "created_table";
create_table_definition (t);
@@ -402,7 +402,8 @@ namespace Check {
stdout.printf (@"Can't access to database "+
"table: $(a.name) : ERROR: $(e.message)\n");
}
- if (t != a) {
+ // FIXME: This must fail- see equivalent implementation
+ if (!t.equivalent (a)) {
stdout.printf (@"Fields in PRE-DEFINED table: $(t.name)\n");
foreach (DbFieldInfo f in t.fields)
stdout.printf (f.to_string () + "\n");
@@ -414,9 +415,45 @@ namespace Check {
}
stdout.printf (">>>>>>>> TEST PASS <<<<<<<<<<<\n");
return 0;
+ }
+ public int compatible () throws Error
+ {
+ stdout.printf("\n\n\n>>>>>>>>>>>>>>> NEW TEST: Gda.DbTable - compatible...\n");
+ var t = new Table ();
+ t.name = "created_table";
+ create_table_definition (t);
+ var a = new Table ();
+ a.name = "created_table";
+ a.connection = connection;
+ a.update_meta = true;
+ a.update ();
+ try {
+ var rs = a.records;
+ stdout.printf (@"Records in DATABASE table: $(a.name)\n");
+ foreach (DbRecord r in rs) { stdout.printf (@"$(r)\n"); }
+ stdout.printf (@"Fields in DATABASE table: $(a.name)\n");
+ foreach (DbFieldInfo f2 in a.fields)
+ stdout.printf (f2.to_string () + "\n");
+ }
+ catch (Error e) {
+ stdout.printf (@"Can't access to database "+
+ "table: $(a.name) : ERROR: $(e.message)\n");
+ }
+ if (!t.compatible (a)) {
+ stdout.printf (@"Fields in PRE-DEFINED table: $(t.name)\n");
+ foreach (DbFieldInfo f in t.fields)
+ stdout.printf (f.to_string () + "\n");
+ stdout.printf (@"\nFields in DATABASE table: $(a.name)\n");
+ foreach (DbFieldInfo f2 in a.fields)
+ stdout.printf (f2.to_string () + "\n");
+ stdout.printf (">>>>>>>> FAIL <<<<<<<<<<<\n");
+ return 1;
+ }
+ stdout.printf (">>>>>>>> TEST PASS <<<<<<<<<<<\n");
+ return 0;
}
-*/
+
public int append () throws Error
{
stdout.printf("\n\n\n>>>>>>>>>>>>>>> NEW TEST: Gda.DbTable - Append...\n");
@@ -588,7 +625,8 @@ namespace Check {
failures += app.records ();
//failures += app.expression ();
failures += app.append ();
- //failures += app.equal ();
+ failures += app.equivalent ();
+ failures += app.compatible ();
failures += app.drop ();
//failures += app.save ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]