Class: Fresco::Db::Postgres

Inherits:
Object
  • Object
show all
Defined in:
lib/fresco/runtime/db_postgres.rb

Overview

Thin Ruby cover over the Postgres FFI module. Same shape as Fresco::Db::SQLite — identical method names, identical return conventions (0/-1 status, 1/0/-1 step, Int/Str cols, etc.).

Differences worth knowing per-adapter:

- URL-based open instead of file path. Pass a libpq conninfo
  string ("postgres://user@host/db" or "host=... user=...").
- last_rowid runs `SELECT lastval()` — only valid when the
  prior statement triggered a sequence. For predictable INSERT
  id reads, use `INSERT ... RETURNING id` + step + col_int(0).
- Placeholders are `$1, $2, ...` not `?`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePostgres

Returns a new instance of Postgres.



59
60
61
# File 'lib/fresco/runtime/db_postgres.rb', line 59

def initialize
  @dbh = -1
end

Instance Attribute Details

#dbhObject

Returns the value of attribute dbh.



57
58
59
# File 'lib/fresco/runtime/db_postgres.rb', line 57

def dbh
  @dbh
end

Instance Method Details

#bind_int(cid = 0, idx = 0, value = 0) ⇒ Object



103
104
105
# File 'lib/fresco/runtime/db_postgres.rb', line 103

def bind_int(cid = 0, idx = 0, value = 0)
  Pg.fresco_pg_bind_int(cid, idx, value)
end

#bind_str(cid = 0, idx = 0, value = "") ⇒ Object



99
100
101
# File 'lib/fresco/runtime/db_postgres.rb', line 99

def bind_str(cid = 0, idx = 0, value = "")
  Pg.fresco_pg_bind_str(cid, idx, value)
end

#closeObject



73
74
75
76
77
78
79
# File 'lib/fresco/runtime/db_postgres.rb', line 73

def close
  if @dbh >= 0
    Pg.fresco_pg_close(@dbh)
    @dbh = -1
  end
  0
end

#col_count(cid = 0) ⇒ Object



113
# File 'lib/fresco/runtime/db_postgres.rb', line 113

def col_count(cid = 0);        Pg.fresco_pg_col_count(cid);       end

#col_int(cid = 0, idx = 0) ⇒ Object



112
# File 'lib/fresco/runtime/db_postgres.rb', line 112

def col_int(cid = 0, idx = 0); Pg.fresco_pg_col_int(cid, idx);    end

#col_str(cid = 0, idx = 0) ⇒ Object



111
# File 'lib/fresco/runtime/db_postgres.rb', line 111

def col_str(cid = 0, idx = 0); Pg.fresco_pg_col_str(cid, idx);    end

#exec(sql = "") ⇒ Object

Run a no-bind statement (DDL, BEGIN/COMMIT, queries with literal constants). Returns true on success. For any user-supplied value always use prepare + bind + step + finalize.



84
85
86
87
# File 'lib/fresco/runtime/db_postgres.rb', line 84

def exec(sql = "")
  return false if @dbh < 0
  Pg.fresco_pg_exec(@dbh, sql) == 0
end

#finalize(cid = 0) ⇒ Object



114
# File 'lib/fresco/runtime/db_postgres.rb', line 114

def finalize(cid = 0);         Pg.fresco_pg_finalize(cid);        end

#first_int(sql = "", p1 = "") ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fresco/runtime/db_postgres.rb', line 140

def first_int(sql = "", p1 = "")
  return 0 if @dbh < 0
  cid = Pg.fresco_pg_prepare(@dbh, sql)
  return 0 if cid < 0
  if p1.length > 0
    Pg.fresco_pg_bind_str(cid, 1, p1)
  end
  result = 0
  if Pg.fresco_pg_step(cid) == 1
    result = Pg.fresco_pg_col_int(cid, 0)
  end
  Pg.fresco_pg_finalize(cid)
  result
end

#first_str(sql = "", p1 = "") ⇒ Object

Single-row single-column convenience. Same semantics as Fresco::Db::SQLite#first_str — pass ‘“”` for no bind. Always finalises the cursor before returning.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fresco/runtime/db_postgres.rb', line 125

def first_str(sql = "", p1 = "")
  return "" if @dbh < 0
  cid = Pg.fresco_pg_prepare(@dbh, sql)
  return "" if cid < 0
  if p1.length > 0
    Pg.fresco_pg_bind_str(cid, 1, p1)
  end
  result = ""
  if Pg.fresco_pg_step(cid) == 1
    result = Pg.fresco_pg_col_str(cid, 0)
  end
  Pg.fresco_pg_finalize(cid)
  result
end

#last_rowidObject



117
118
119
120
# File 'lib/fresco/runtime/db_postgres.rb', line 117

def last_rowid
  return -1 if @dbh < 0
  Pg.fresco_pg_last_insert_rowid(@dbh)
end

#open(url = "") ⇒ Object

Returns true on success, false on failure. URL accepts any libpq conninfo string. Calling #open twice on the same instance leaks the prior handle — close first.



66
67
68
69
70
71
# File 'lib/fresco/runtime/db_postgres.rb', line 66

def open(url = "")
  h = Pg.fresco_pg_open(url)
  return false if h < 0
  @dbh = h
  true
end

#prepare(sql = "") ⇒ Object

Open a cursor on a parameterised statement. Returns an Int cursor id (>= 1) on success, -1 on failure. The query doesn’t actually hit Postgres until the first #step — bind_str/bind_int calls between prepare and step accumulate the values that will be passed to PQexecParams.



94
95
96
97
# File 'lib/fresco/runtime/db_postgres.rb', line 94

def prepare(sql = "")
  return -1 if @dbh < 0
  Pg.fresco_pg_prepare(@dbh, sql)
end

#reset(cid = 0) ⇒ Object



115
# File 'lib/fresco/runtime/db_postgres.rb', line 115

def reset(cid = 0);            Pg.fresco_pg_reset(cid);           end

#step(cid = 0) ⇒ Object

1 -> row available, 0 -> done, -1 -> error or invalid cursor. The first call fires PQexecParams with accumulated binds; subsequent calls walk the result one row at a time.



110
# File 'lib/fresco/runtime/db_postgres.rb', line 110

def step(cid = 0);             Pg.fresco_pg_step(cid);            end