Class: Tep::SQLite

Inherits:
Object
  • Object
show all
Defined in:
lib/tep/sqlite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSQLite

Returns a new instance of SQLite.



90
91
92
# File 'lib/tep/sqlite.rb', line 90

def initialize
  @dbh = -1
end

Instance Attribute Details

#dbhObject

‘:dbh` (rather than the natural `:handle`) – spinel widens poly dispatch return types when a method name is shared across classes with different signatures. `Tep::Handler#handle(req, res)` is the heart of the framework and returns String; an attr_accessor `handle` on Tep::SQLite would emit a 0-arg / int-return arm, widening the dispatch’s return type to poly and cascading through ‘set_body_if_empty(s)` -> `Response#body` -> the sphttp_write_str(int, const char *) call. (See the gemini-bot commentary in spinel PR #391.)



88
89
90
# File 'lib/tep/sqlite.rb', line 88

def dbh
  @dbh
end

Instance Method Details

#bind_int(idx, value) ⇒ Object



158
# File 'lib/tep/sqlite.rb', line 158

def bind_int(idx, value); Sqlite.tep_sqlite_bind_int(idx, value); end

#bind_str(idx, value) ⇒ Object



157
# File 'lib/tep/sqlite.rb', line 157

def bind_str(idx, value); Sqlite.tep_sqlite_bind_str(idx, value); end

#closeObject



106
107
108
109
110
111
112
# File 'lib/tep/sqlite.rb', line 106

def close
  if @dbh >= 0
    Sqlite.tep_sqlite_close(@dbh)
    @dbh = -1
  end
  0
end

#col_countObject



164
# File 'lib/tep/sqlite.rb', line 164

def col_count;  Sqlite.tep_sqlite_col_count;  end

#col_int(i) ⇒ Object



163
# File 'lib/tep/sqlite.rb', line 163

def col_int(i); Sqlite.tep_sqlite_col_int(i); end

#col_str(i) ⇒ Object



162
# File 'lib/tep/sqlite.rb', line 162

def col_str(i); Sqlite.tep_sqlite_col_str(i); end

#exec(sql) ⇒ Object

Run a statement that returns no rows (CREATE / INSERT / UPDATE / DELETE / PRAGMA / BEGIN / COMMIT). Returns true on success. No bind in this form – inline literal SQL is fine for DDL and constants; for any user-supplied value use prepare + bind + step + finalize.



119
120
121
122
123
124
# File 'lib/tep/sqlite.rb', line 119

def exec(sql)
  if @dbh < 0
    return false
  end
  Sqlite.tep_sqlite_exec(@dbh, sql) == 0
end

#finalizeObject



165
# File 'lib/tep/sqlite.rb', line 165

def finalize;   Sqlite.tep_sqlite_finalize;   end

#first_int(sql, p1) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/tep/sqlite.rb', line 197

def first_int(sql, p1)
  if @dbh < 0
    return 0
  end
  if Sqlite.tep_sqlite_prepare(@dbh, sql) != 0
    return 0
  end
  if p1.length > 0
    Sqlite.tep_sqlite_bind_str(1, p1)
  end
  result = 0
  if Sqlite.tep_sqlite_step == 1
    result = Sqlite.tep_sqlite_col_int(0)
  end
  Sqlite.tep_sqlite_finalize
  result
end

#first_str(sql, p1) ⇒ Object

Convenience: prepare a single-row, single-column query, bind one optional string param (pass “” for “no param”), step once, return col. Always finalises the cursor before returning so the caller doesn’t have to.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/tep/sqlite.rb', line 179

def first_str(sql, p1)
  if @dbh < 0
    return ""
  end
  if Sqlite.tep_sqlite_prepare(@dbh, sql) != 0
    return ""
  end
  if p1.length > 0
    Sqlite.tep_sqlite_bind_str(1, p1)
  end
  result = ""
  if Sqlite.tep_sqlite_step == 1
    result = Sqlite.tep_sqlite_col_str(0)
  end
  Sqlite.tep_sqlite_finalize
  result
end

#last_rowidObject



168
169
170
171
172
173
# File 'lib/tep/sqlite.rb', line 168

def last_rowid
  if @dbh < 0
    return -1
  end
  Sqlite.tep_sqlite_last_insert_rowid(@dbh)
end

#open(path) ⇒ Object

Returns true on success, false on failure. Path may be a real file or ‘:memory:` for an anonymous in-memory db. Multiple opens on the same instance leak the prior handle; close first.



97
98
99
100
101
102
103
104
# File 'lib/tep/sqlite.rb', line 97

def open(path)
  h = Sqlite.tep_sqlite_open(path)
  if h < 0
    return false
  end
  @dbh = h
  true
end

#prepare(sql) ⇒ Object

Open a cursor on a parameterised query. Subsequent bind_str / bind_int calls fill in ‘?` markers (1-indexed). Always pair with `finalize` once iteration is done.



129
130
131
132
133
134
# File 'lib/tep/sqlite.rb', line 129

def prepare(sql)
  if @dbh < 0
    return false
  end
  Sqlite.tep_sqlite_prepare(@dbh, sql) == 0
end

#prepare_cached(sql) ⇒ Object

Cached variant. Same surface as ‘prepare`, but the underlying `sqlite3_stmt *` is memoised per-(db, sql); subsequent calls with the same SQL string reuse the prepared statement, paying the parse cost only once per process. Pair with `finalize` as usual; on the cached path `finalize` becomes `sqlite3_reset + sqlite3_clear_bindings` (the slot stays alive). The cache is bounded (currently 64 distinct SQL strings per process); apps that exceed the bound fall through to uncached prepare so correctness is preserved.

Use for hot-path SQL where the string is known + fixed at codegen / boot time. Apps that build SQL with varying whitespace miss the cache (match is literal); format consistently.



150
151
152
153
154
155
# File 'lib/tep/sqlite.rb', line 150

def prepare_cached(sql)
  if @dbh < 0
    return false
  end
  Sqlite.tep_sqlite_prepare_cached(@dbh, sql) == 0
end

#resetObject



166
# File 'lib/tep/sqlite.rb', line 166

def reset;      Sqlite.tep_sqlite_reset;      end

#stepObject

1 -> row available, 0 -> done (no more rows), -1 -> error.



161
# File 'lib/tep/sqlite.rb', line 161

def step;       Sqlite.tep_sqlite_step;       end