[libgda] Fix libgdadata installation issues. Simplifing example for GdaData.Object.



commit 722dc768a2b7e5a11822cf32e93b37db6715f04c
Author: Daniel Espinosa <despinosa src gnome org>
Date:   Wed Dec 21 10:13:40 2011 -0600

    Fix libgdadata installation issues. Simplifing example for GdaData.Object.

 libgda/data/DataObject.vala        |    2 +-
 libgda/data/GdaData-5.0.gir        |    2 +-
 libgda/data/Makefile.am            |    8 +-
 libgda/data/libgdadata-5.0.pc.in   |    2 +-
 samples/vala/SampleDataObject.vala |  216 ++++++++++++++----------------------
 5 files changed, 91 insertions(+), 139 deletions(-)
---
diff --git a/libgda/data/DataObject.vala b/libgda/data/DataObject.vala
index c7bf197..940db13 100644
--- a/libgda/data/DataObject.vala
+++ b/libgda/data/DataObject.vala
@@ -20,7 +20,7 @@
 using Gda;
 
 [CCode (gir_namespace = "GdaData", gir_version = "5.0")]
-[CCode (cheader_filename="libgdadata.h")]
+[CCode (cheader_filename="libgda/libgdadata.h")]
 namespace GdaData {
 
     public abstract class Object : GLib.Object {
diff --git a/libgda/data/GdaData-5.0.gir b/libgda/data/GdaData-5.0.gir
index 6d7b824..70108fc 100644
--- a/libgda/data/GdaData-5.0.gir
+++ b/libgda/data/GdaData-5.0.gir
@@ -8,7 +8,7 @@
 <namespace name="GdaData" version="5.0" c:prefix="GdaData">
 	<annotation key="ccode.gir-version" value="5.0"/>
 	<annotation key="ccode.gir-namespace" value="GdaData"/>
-	<annotation key="ccode.cheader-filename" value="libgdadata.h"/>
+	<annotation key="ccode.cheader-filename" value="libgda/libgdadata.h"/>
 	<class name="Object" c:type="GdaDataObject" glib:type-name="GdaDataObject" glib:get-type="gda_data_object_get_type" glib:type-struct="ObjectClass" parent="GObject.Object" abstract="1">
 		<field name="parent_instance">
 			<type name="GObject.Object" c:type="GObject"/>
diff --git a/libgda/data/Makefile.am b/libgda/data/Makefile.am
index 3cf371d..1b7210c 100644
--- a/libgda/data/Makefile.am
+++ b/libgda/data/Makefile.am
@@ -42,6 +42,11 @@ libgdadata_5_0_la_SOURCES = \
 	$(libgdadata_5_0_la_VALASOURCES:.vala=.c) \
 	$(NULL)	
 
+vapi: gdadata-5.0.vapi
+
+vapidir = $(VAPIDIR)
+vapi_DATA = gdadata-5.0.vapi
+
 gdadata-5.0.vapi gdadata.vala.stamp GdaData-5.0.gir: $(libgdadata_5_0_la_VALASOURCES)
 	$(VALA_COMPILER) $(VALAFLAGS) -C -H libgdadata.h --gir=GdaData-5.0.gir --library gdadata-5.0 $^
 	@touch $@
@@ -92,9 +97,6 @@ GdaData-5.0.typelib: $(INTROSPECTION_GIRS)
 gir_DATA = $(INTROSPECTION_GIRS)
 typelibs_DATA = GdaData-5.0.typelib
 
-vapidir = $(VALA_VAPIDIR)
-vapi_DATA = gdadata-5.0.vapi
-
 CLEANFILES += \
     $(typelibs_DATA) \
     gdadata-5.0.vapi \
diff --git a/libgda/data/libgdadata-5.0.pc.in b/libgda/data/libgdadata-5.0.pc.in
index 151c43c..044f291 100644
--- a/libgda/data/libgdadata-5.0.pc.in
+++ b/libgda/data/libgdadata-5.0.pc.in
@@ -10,4 +10,4 @@ Description: Gda Data Library. Data oriented GObject implementations using Vala
 Version: @VERSION@
 Requires: gobject-2.0 gthread-2.0 libxml-2.0 libgda-5.0 libgee-0.8
 Libs: -L${libdir} -lgdadata- GDA_ABI_MAJOR_VERSION@  GDA_ABI_MINOR_VERSION@
-Cflags: -I${includedir}/libgdadata- GDA_ABI_MAJOR_VERSION@  GDA_ABI_MINOR_VERSION@ -I${includedir}/libgda- GDA_ABI_MAJOR_VERSION@  GDA_ABI_MINOR_VERSION@/libgda @GDA_DEBUG_FLAGS@
+Cflags: -I${includedir}/libgda- GDA_ABI_MAJOR_VERSION@  GDA_ABI_MINOR_VERSION@ @GDA_DEBUG_FLAGS@
diff --git a/samples/vala/SampleDataObject.vala b/samples/vala/SampleDataObject.vala
index 5d6fee3..d4b3da9 100644
--- a/samples/vala/SampleDataObject.vala
+++ b/samples/vala/SampleDataObject.vala
@@ -22,137 +22,67 @@ using GdaData;
 
 namespace Sample {
 
-	class Book : GdaData.Object {
+	class DbRecord : GdaData.Object {
+		private static string dbtable = "user";
 		
+		/**
+		 * On derived classes you must implement this property.
+		 * Is intended that implementors set table and don't change it in the object live time
+		 * in order to keep a good reference to the data the object refers to.
+		 */
 		public override string table { 
-			get { return "book"; }
-			construct {}
+			get { return this.dbtable; }
 		}
 		
-		public Collection accounts {
+		/**
+		 * Wrapping database fields.
+		 * You can define properties that use database stored values.
+		 * Fields are not limited on the ones defined on the class,
+		 * you can always access any other in the database using
+		 * get_value(string) function.
+		 */
+		public string functions {
 			get {
-				int id = (int) this.get_value_id ();
-				string sql = @"SELECT * FROM accounts WHERE book = $id";
-				var accs = this.connection.execute_select (sql);
-				return (Collection) new DataModelIterable (accs);
+					return (string) this.get_value ("functions");
 			}
-		}
-				
-		public bool open (string name) 
-			throws Error
-		{
-			string sql = @"SELECT * FROM book WHERE name = $name";
-			var model = this.connection.execute_select (sql);
-			var id = model.get_value_at (model.get_column_index ("id"), 0);
-			this.set_id ("id", id);
-		}
-		
-	}
-	
-	class Account : GdaData.Object {
-		public override string table { 
-			get { return "account"; }
-			construct {}
-		}
-		public Book book { get; set; }
-		/* Is possible to create properties to easy access to any field in the database row */
-		public string name { 
-			get {
-				return (string) this.get_value ("name");
-			} 
 			set {
-				return this.set_value ("name", value);
+				try {
+					this.set_value ("functions", value);
+				}
+				catch {}
 			}
 		}
 		
-		public Collection transactions {
+		public string name {
 			get {
-				int id = (int) this.get_value_id ();
-				string sql = @"SELECT * FROM transactions WHERE account = $id";
-				var accs = this.connection.execute_select (sql);
-				return (Collection) new DataModelIterable (accs);
-			}
-		}
-		
-		public double balance {
-			get {
-				string sql_debit = @"SELECT sum (debit) FROM transactions WHERE account = $id";
-				string sql_credit = @"SELECT sum (credit) FROM transactions WHERE account = $id";
-				var model_credit = this.connection.execute_select (sql_credit);
-				var model_debit = this.connection.execute_select (sql_debit);
-				var credit = model_credit.get_value_at (0, 0);
-				var debit = model_debit.get_value_at (0, 0);
-				return (double) credit - (double) debit;
-			}
-		}
-		
-		public bool open (string name) {
-			Value n = name;
-			this.set_id (name, n);
-		}
-		
-	}
-	
-	class Transaction : GdaData.Object {
-		public override string table { 
-			get { return "transaction"; }
-			construct {}
-		}
-		public Account account { 
-			get {
-				var acc = new Account ();
-				Value idacc = this.get_value ("account");
-				acc.set_id ("id", idacc);
-				return acc;
-			}
-			set {
-				Account acc = value;
-				var id = acc.get_value ("id");
-				this.set_value ("account", id);
+				return (string) this.get_value ("name");
 			}
-		}
-		public string description { 
-			get {
-				this.get_value ("description");
-			} 
+			
 			set {
-				this.set_value ("description", value);
+				try {
+					this.set_value ("name", value);
+				}
+				catch {}
 			}
 		}
 		
-		public double debit { 
-			get {
-				var credit = (bool) this.get_value ("credit");
-				if (!credit)
-					return (double) this.get_value ("amount");
-				else
-					return 0;
-			} 
-			set {
-				this.set_value ("amount", value);
-				bool credit = false;
-				Value v = credit;
-				this.set_value ("debit", v);
-			}
+		/**
+		 * This function is a wrapper to set the id field
+		 * and id value used to retrieve a record from the
+		 * database.
+		 * 
+		 * In this example 'user' table have two keys, id and
+		 * name (declared as UNIQUE), then we can use the last
+		 * to identify any record.
+		 */
+		public void open (Value name) 
+			throws Error
+		{
+			this.set_id ("name", name);
 		}
 		
-		public double credit { 
-			get {
-				var credit = (bool) this.get_value ("credit");
-				if (credit)
-					return (double) this.get_value ("amount");
-				else
-					return -1;
-			} 
-			set {
-				this.set_value ("amount", value);
-				bool credit = true;
-				Value v = credit;
-				this.set_value ("debit", v);
-			}
-		}
 	}
-	
+
 	class App : GLib.Object {
 		public Gda.Connection connection;
 		
@@ -161,37 +91,57 @@ namespace Sample {
 			try {
 				GLib.FileUtils.unlink("dataobject.db");
 				stdout.printf("Creating Database...\n");
-				this.connection = Connection.open_from_string("SQLite", "DB_DIR=.;DB_NAME=dataobject", null, Gda.ConnectionOptions.NONE);
+				this.connection = Connection.open_from_string("SQLite", 
+									"DB_DIR=.;DB_NAME=dataobject", null, 
+									Gda.ConnectionOptions.NONE);
 				stdout.printf("Creating table 'user'...\n");
-				this.connection.execute_non_select_command("CREATE TABLE book (id int PRIMARY KEY AUTOINCREMENT, name string NOT NULL UNIQUE, manager string)");
-				
-				this.connection.execute_non_select_command("INSERT INTO book (id, name, manager) VALUES (1, \"General Book\", \"Jhon\")");
+				this.connection.execute_non_select_command("CREATE TABLE user (id int PRIMARY KEY AUTOINCREMENT, name string UNIQUE, functions string, security_number int)");
+				this.connection.execute_non_select_command("INSERT INTO user (id, name, functions) VALUES (1, \"Martin Stewart\", \"Programmer, QA\", 2334556");
+				this.connection.execute_non_select_command("INSERT INTO user (id, name, functions) VALUES (2, \"Jane Castle\", \"Accountant\", 3002884");
 				
-				stdout.printf("Creating table 'company'...\n");
-				this.connection.execute_non_select_command("CREATE TABLE account (id int PRIMARY KEY AUTOINCREMENT, name string UNIQUE, description string)");
-				this.connection.execute_non_select_command("INSERT INTO account (id, name) VALUES (1, \"Incomes\"");
-				this.connection.execute_non_select_command("INSERT INTO account (id, name) VALUES (2, \"Expenses\"");
-				this.connection.execute_non_select_command("INSERT INTO account (id, name) VALUES (3, \"Bank\"");
-				this.connection.execute_non_select_command("CREATE TABLE transaction (id int PRIMARY KEY AUTOINCREMENT, credit int, debit int, description string, amount double)");
-				this.connection.execute_non_select_command("INSERT INTO account (id, credit, debit, description, amount, credit) VALUES (1, 3, 1, \"Salary\", 3100.0, TRUE");
-				this.connection.execute_non_select_command("INSERT INTO account (id, credit, debit, description, amount, credit) VALUES (2, 2, 3, \"Expenses of the week\"");
 				this.connection.update_meta_store(null);
 			}
 			catch (Error e) {
-				stdout.printf ("Couln't create temporary database...\nERROR: %s\n", e.message);
+				stdout.printf ("Can't create temporary database...\nERROR: " + e.message + "\n");
 			}
 		}
 		
-				
+		public void modify_record (string name)
+			throws Error
+		{
+			var rcd = new DbRecord ();
+			rcd.open (name);
+			
+			stdout.printf ("Modifing user: " + rcd.name);
+			foreach (Value v in rcd.record) {
+				stdout.printf ("Field Value: " + Gda.value_stringify (v));
+			}
+			
+			// Changing functions
+			rcd.functions += ", Hardware Maintenance";
+			
+			// Updating Name
+			rcd.name += " PhD.";
+			
+			// Changing non class property value in the record
+			// You must know the field name refer to
+			string v = (string) rcd.get_value ("security_number");
+			stdout.printf ("Taken value from a field in the DB: " + Gda.value_stringify (v));
+		}
+			
 		public static int main (string[] args) {
-			stdout.printf ("Checking Gda.DataObject implementation...\n");
-			int failures = 0;
+			stdout.printf ("Gda.DataObject Example...\n");
 			var app = new App ();
-			app.get_balance ("Incomes");
-			app.get_balance ("Expenses");
-			app.get_balance ("Bank");
-			app.get_operations ("Bank");
-			return failures != 0 ? 1 : 0;
+			try {
+				app.modify_record ("Martin Stewart");
+				app.modify_record ("Jane Castle");
+				return 0;
+			}
+			catch (Error e) {
+				stdout.printf ("Can't modify record\nERROR: " + e.message + "\n");
+			}
+			
+			return 0;
 		}
 	}
 }



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