GNOME Data Access Library Example

Example

The next example, requires you have a SQLite database located in the same directory you are running the program. The database must be called "test.db".

/* file: test-gda.vala */
using Gda;

namespace Test {
        class SelectQuery : Object {
            public string field { set; get; default = "*"; }
            public string table { set; get; default = "test"; }
            public Connection connection { set; get; }
            
            public DataModel get_table_contents () 
                throws Error 
                requires (this.connection.is_opened())
            {
                stdout.printf("Building query...\n");
                /* Build Select Query */
                var b = new Gda.SqlBuilder(Gda.SqlStatementType.SELECT);
                b.select_add_field (this.field, null, null);
            b.select_add_target(this.table, null);
            var s = b.get_statement();
            stdout.printf("Executing...\n");
            return this.connection.statement_execute_select(s, null);
            }
        }
        
        class DataBase : Object {
                /* Using defaults will search a SQLite database located at current directory called test.db */
                public string provider { set; get; default = "SQLite"; }
                public string constr { set; get; default = "SQLite://DB_DIR=.;DB_NAME=test"; }
                public Gda.Connection cnn;
                
                public void open () throws Error {
                        stdout.printf("Opening Database connection...\n");
                        this.cnn = Gda.Connection.open_from_string (null, this.constr, null, Gda.ConnectionOptions.NONE);
                }

                /* Create a tables and populate them */
                public void create_tables () 
                        throws Error
                        requires (this.cnn.is_opened())
                {
                        stdout.printf("Creating and populating data...\n");
                        this.run_query("CREATE TABLE test (description string, notes string)");
                        this.run_query("INSERT INTO test (description, notes) VALUES (\"Test description 1\", \"Some notes\")");
                        this.run_query("INSERT INTO test (description, notes) VALUES (\"Test description 2\", \"Some additional notes\")");
                        
                        this.run_query("CREATE TABLE table1 (city string, notes string)");
                        this.run_query("INSERT INTO table1 (city, notes) VALUES (\"Mexico\", \"Some place to live\")");
                        this.run_query("INSERT INTO table1 (city, notes) VALUES (\"New York\", \"A new place to live\")");
                }
                
                public int run_query (string query) 
                        throws Error
                        requires (this.cnn.is_opened())
                {
                        stdout.printf("Executing query: [%s]\n", query);
                        return this.cnn.execute_non_select_command (query);
                }
        }
        
        class App : Object {
            public DataBase db;
            
            public App () {
                this.db = new DataBase();
            }
            
            public void test_initdb () 
                throws Error
            {
                stdout.printf("Test: Opening and initializing Database ...\n");
                /* Opening and initializing DB */
                try {
                        db.open();
                        db.create_tables();
                }
                catch (Error e) {
                        stdout.printf("ERROR: '%s'\n", e.message);
                }
            }
            
            public void test_builder() 
                throws Error 
                requires (this.db.cnn.is_opened())
            {
                stdout.printf("Test: GdaSqlBuilder...\n");
                var q = new SelectQuery();
                q.connection = this.db.cnn;
                
                /* Select * from test */
                this.show_data(q);
                
                /* Select notes from test */
                q.field = "notes";
                this.show_data(q);
                
                /* Select notes from table1 */
                q.table = "table1";
                this.show_data(q);
                
                /* Select * from table1 */
                q.field = "*";
                this.show_data(q);
            }
            
            public void show_data (SelectQuery q) 
                throws Error 
                requires (this.db.cnn.is_opened())
            {
                try {
                    var m = q.get_table_contents();
                    stdout.printf("Table: '%s'\n%s", q.table, m.dump_as_string());
                }
                catch  (GLib.Error e){
                    stdout.printf("ERROR: '%s'\n", e.message);
                }
            }
            
            public static int main (string[] args) {
                stdout.printf("GNOME Data Access Vala Demo\n");
                var a = new App();
                try {
                        a.test_initdb();
                    }
                    catch  (GLib.Error e){
                    stdout.printf("ERROR: '%s'\n", e.message);
                }
                try {
                        a.test_builder();
                    }
                    catch  (GLib.Error e){
                    stdout.printf("ERROR: '%s'\n", e.message);
                }
                return 0;
            }
        }
}

Compile

You must use latest libgda VAPI file from 4.2.10 and 5.0 versions and above.

For GDA 5.0, you can use:

valac --pkg libgda-5.0 --pkg libxml-2.0 test-gda.vala

Run

./test-gda

Projects/Vala/GDA (last edited 2013-11-22 16:48:27 by WilliamJonMcCann)