3
* Cursor for use with a {@link Table}
5
public class TableCursor : GLib.Object, SQLHeavy.RecordSet, SQLHeavy.Cursor {
9
public SQLHeavy.Table table { get; construct; }
14
public int64 offset { get; private set; default = 0; }
17
* The order to sort the result set in
19
public SQLHeavy.SortOrder sort_order { get; construct set; default = SQLHeavy.SortOrder.ASCENDING; }
22
* The column to sort the result set by
24
public string sort_column { get; set; default = "ROWID"; }
29
public int field_count { get { return this.table.field_count; } }
31
private int64 current_id = -1;
33
private SQLHeavy.Query? _query = null;
34
private SQLHeavy.Query query {
36
if ( this._query == null ) {
38
this._query = this.table.queryable.prepare ("SELECT `ROWID` FROM `" + SQLHeavy.escape_string (this.table.name) + "` ORDER BY `" + SQLHeavy.escape_string (this.sort_column) + "` " + ((this.sort_order == SQLHeavy.SortOrder.ASCENDING) ? "ASC" : "DESC") + " LIMIT 1 OFFSET :offset;");
39
} catch ( SQLHeavy.Error e ) {
40
GLib.critical ("Unable to create table cursor: %s", e.message);
51
public new SQLHeavy.Record get () throws SQLHeavy.Error {
52
if ( this.current_id <= 0 )
53
throw new SQLHeavy.Error.RANGE (sqlite_errstr (Sqlite.RANGE));
55
return new SQLHeavy.Row (this.table, this.current_id);
58
private bool move_to_internal (int64 offset) {
63
q.set_int64 (":offset", offset);
64
var res = q.execute ();
65
id = (res.finished) ? -1 : res.fetch_int64 (0);
66
} catch ( SQLHeavy.Error e ) {
67
GLib.critical ("Unable to move cursor: %s", e.message);
72
GLib.debug ("Offset: %lld", offset);
84
public bool move_to (int64 offset) throws SQLHeavy.Error {
85
if ( !this.move_to_internal (offset) )
86
throw new SQLHeavy.Error.RANGE (sqlite_errstr (Sqlite.RANGE));
94
public bool next () throws SQLHeavy.Error {
95
return this.move_to_internal (this.offset + 1);
101
public bool previous () throws SQLHeavy.Error {
102
return this.move_to_internal (this.offset - 1);
108
} catch ( SQLHeavy.Error e ) {
109
GLib.critical ("Unable to create table cursor: %s", e.message);
114
* Create a new cursor for the table
116
* @param table the table to create the cursor for
118
public TableCursor (SQLHeavy.Table table) {
119
GLib.Object (table: table);