~ubuntu-branches/debian/jessie/sqlheavy/jessie

« back to all changes in this revision

Viewing changes to sqlheavy/sqlheavy-table-cursor.vala

  • Committer: Package Import Robot
  • Author(s): Devid Antonio Filoni
  • Date: 2011-10-23 15:43:22 UTC
  • Revision ID: package-import@ubuntu.com-20111023154322-unzj3fz2aj4g6cl8
Tags: upstream-0.1.0
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace SQLHeavy {
 
2
  /**
 
3
   * Cursor for use with a {@link Table}
 
4
   */
 
5
  public class TableCursor : GLib.Object, SQLHeavy.RecordSet, SQLHeavy.Cursor {
 
6
    /**
 
7
     * The table
 
8
     */
 
9
    public SQLHeavy.Table table { get; construct; }
 
10
 
 
11
    /**
 
12
     * The current offset
 
13
     */
 
14
    public int64 offset { get; private set; default = 0; }
 
15
 
 
16
    /**
 
17
     * The order to sort the result set in
 
18
     */
 
19
    public SQLHeavy.SortOrder sort_order { get; construct set; default = SQLHeavy.SortOrder.ASCENDING; }
 
20
 
 
21
    /**
 
22
     * The column to sort the result set by
 
23
     */
 
24
    public string sort_column { get; set; default = "ROWID"; }
 
25
 
 
26
    /**
 
27
     * {@inheritDoc}
 
28
     */
 
29
    public int field_count { get { return this.table.field_count; } }
 
30
 
 
31
    private int64 current_id = -1;
 
32
 
 
33
    private SQLHeavy.Query? _query = null;
 
34
    private SQLHeavy.Query query {
 
35
      get {
 
36
        if ( this._query == null ) {
 
37
          try {
 
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);
 
41
          }
 
42
        }
 
43
 
 
44
        return this._query;
 
45
      }
 
46
    }
 
47
 
 
48
    /**
 
49
     * {@inheritDoc}
 
50
     */
 
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));
 
54
 
 
55
      return new SQLHeavy.Row (this.table, this.current_id);
 
56
    }
 
57
 
 
58
    private bool move_to_internal (int64 offset) {
 
59
      int64 id = -1;
 
60
 
 
61
      try {
 
62
        var q = this.query;
 
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);
 
68
        return false;
 
69
      }
 
70
 
 
71
      if ( id > 0 ) {
 
72
        GLib.debug ("Offset: %lld", offset);
 
73
        this.current_id = id;
 
74
        this.offset = offset;
 
75
        return true;
 
76
      } else {
 
77
        return false;
 
78
      }
 
79
    }
 
80
 
 
81
    /**
 
82
     * {@inheritDoc}
 
83
     */
 
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));
 
87
 
 
88
      return true;
 
89
    }
 
90
 
 
91
    /**
 
92
     * {@inheritDoc}
 
93
     */
 
94
    public bool next () throws SQLHeavy.Error {
 
95
      return this.move_to_internal (this.offset + 1);
 
96
    }
 
97
 
 
98
    /**
 
99
     * {@inheritDoc}
 
100
     */
 
101
    public bool previous () throws SQLHeavy.Error {
 
102
      return this.move_to_internal (this.offset - 1);
 
103
    }
 
104
 
 
105
    construct {
 
106
      try {
 
107
        this.next ();
 
108
      } catch ( SQLHeavy.Error e ) {
 
109
        GLib.critical ("Unable to create table cursor: %s", e.message);
 
110
      }
 
111
    }
 
112
 
 
113
    /**
 
114
     * Create a new cursor for the table
 
115
     *
 
116
     * @param table the table to create the cursor for
 
117
     */
 
118
    public TableCursor (SQLHeavy.Table table) {
 
119
      GLib.Object (table: table);
 
120
    }
 
121
  }
 
122
}