Record class is an implementation of DbRecord interface. It uses GDA to get access to a database's row in a table.
In order to load data, you need to set a Gda.Connection, a table and a key. Then update() method execute a SELECT command using the key to find the required row in the table.
var r = new Record (); var t = new Table (); t.name = "customer"; r.table = t; r.connection = connection; r.set_key_value ("id", 1); r.update ();
In the above code, connection is opened before to be set to Record.connection property. You need to setup a Table class to set to Record.table property, but just set Table.name property to the table's name is required. Use Record.set_key_value() method to set the value to the column used in the table as the primary key and to allow update() method to find the correct row (the WHERE clause in SELECT statement), if more than one key exists you must set it; you must know the column and type of value to set.
var r = new Record (); var t = new Table (); t.name = "customer"; r.table = t; r.connection = connection; r.set_field_value ("name", "Clausse Rayman"); r.set_field_value ("company", "Packing Sources, Ltd."); r.append ();
In the above code a new row will be added. Create a new Record object, set a table to add a new row to; use Record.set_field_value() to set values to columns in the table, you must know columns and data type to set. At the end call Record.save() to add the new row.
Record.set_field_value() doesn't know if the columns and type is correct, just store the value to used in an INSERT statement; if key is set by database engine it will be added automatically, if not you must set it in order to execute save() with no errors.
Once you have set a key and a table to a Record object, you can call Record.set_field_value() to change row's column's values, once done, you can call Record.save() to call an UPDATE command and update database's row/column values.
var r = new Record (); r.connection = connection; r.set_key_value ("id", 1); r.set_field_value ("name", "Jack Swan"); r.save ();
If you want to delete a row in a database's table, just create a new Record object, set a key, a table and a connection, then call Record.drop().
var r = new Record (); r.connection = connection; var t = new Table (); t.name = "table_name"; r.table = t; r.set_key_value ("id", 1); r.drop ();
Record class could be used as base for others. Is useful to wrap Record.set_field_value() into your class property to hide database access.
Use try{} catch{} to avoid warnings for unhandled error
class MyRecord : Record { public MyRecord () { /* Your init code */ } public string name { get { try { return this.get_value ("name"); } catch (Error e) { GLib.warning ("ERROR on getting value from name property: " + e.message) } } set { try { return this.set_field_value ("name", value); } catch (Error e) { GLib.warning ("ERROR on setting value to name property: " + e.message) } } } }
The above code declares a MyRecord.name property witch uses Record.get_value() and Record.set_field_value() from its base class, to get and set its value.
You can iterate over Record's fields using Vala foreach statement, by using Record.fields property; it's a Gee.Collection of DbField objects, then you can use:
foreach (DbField f in record.fields) { /* work with DbField object */ }
The same apply for all keys you set by using Record.keys property.
Got a comment? Spotted an error? Found the instructions unclear? Send feedback about this page.