| 1 |
/* valac --debug --pkg midgard2 -o midgard-vala-example example.vala --vapidir=./ */ |
|---|
| 2 |
|
|---|
| 3 |
using GLib; |
|---|
| 4 |
using Midgard; |
|---|
| 5 |
|
|---|
| 6 |
namespace MidgardValaExample { |
|---|
| 7 |
|
|---|
| 8 |
void main() { |
|---|
| 9 |
|
|---|
| 10 |
Midgard.init(); |
|---|
| 11 |
|
|---|
| 12 |
Midgard.Config config = new Midgard.Config(); |
|---|
| 13 |
try { |
|---|
| 14 |
config.read_file ("midgard_test", true); |
|---|
| 15 |
} catch ( GLib.Error e) { |
|---|
| 16 |
|
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
Midgard.Connection cnc = new Midgard.Connection(); |
|---|
| 20 |
if (!cnc.open_config (config)) |
|---|
| 21 |
GLib.error ("Not connected to database \n"); |
|---|
| 22 |
|
|---|
| 23 |
/* cnc.set_loglevel ("debug", null); */ |
|---|
| 24 |
|
|---|
| 25 |
Midgard.Object page = new Midgard.Object (cnc, "midgard_page", null); |
|---|
| 26 |
page.set ("name", "Hello Vala"); |
|---|
| 27 |
if (page.create()) { |
|---|
| 28 |
string pname; |
|---|
| 29 |
page.get ("name", out pname); |
|---|
| 30 |
Midgard.Timestamp created = page.metadata.created; |
|---|
| 31 |
GLib.print ("Created new page '%s' identified by guid %s at %s \n", pname, page.guid, created.get_string()); |
|---|
| 32 |
} |
|---|
| 33 |
else { |
|---|
| 34 |
GLib.print ("Couldn't create new page because: %s \n", cnc.get_error_string()); |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
Midgard.QueryBuilder builder = new Midgard.QueryBuilder (cnc, "midgard_page"); |
|---|
| 38 |
unowned GLib.Object[] objects = builder.execute (); |
|---|
| 39 |
|
|---|
| 40 |
foreach (GLib.Object object in objects){ |
|---|
| 41 |
|
|---|
| 42 |
string guid; |
|---|
| 43 |
object.get ("guid", out guid); |
|---|
| 44 |
GLib.print ("Object %s \n", guid); |
|---|
| 45 |
} |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|