~mna/xpgsql

f615fa565fa740eb93cd23a75d30b0911ad5e2e2 — Martin Angers 2 years ago f431d8a v0.5
add sql state error code to return values
3 files changed, 53 insertions(+), 21 deletions(-)

M README.md
A rockspecs/xpgsql-0.5-1.rockspec
M xpgsql.lua
M README.md => README.md +15 -8
@@ 85,18 85,25 @@ error.
### `Connection:query(stmt, ...)`

Executes a query statement and returns the result if it succeeds, or nil, an
error message and the status code. Note that INSERT .. RETURNING must use
Connection:query as it returns values. The statement may contain $1, $2,
etc.  placeholders, they will be replaced by the extra arguments provided to
the method.
error message and the status code (number). If the error is not related to the
connection, then it also returns the string version of the status (e.g. `PGRES_FATAL_ERROR`)
and the SQL state code (e.g. 42P01, see https://www.postgresql.org/docs/current/errcodes-appendix.html).

Note that INSERT .. RETURNING must use Connection:query as it returns
values. The statement may contain $1, $2, etc. placeholders, they will be
replaced by the extra arguments provided to the method.

### `Connection:exec(stmt, ...)`

Executes a non-query statement and returns the result if it succeeds, or
nil, an error message and the status code. Note that INSERT..RETURNING must
use Connection:query as it returns values. The statement may contain $1, $2,
etc.  placeholders, they will be replaced by the extra arguments provided to
the method.
nil, an error message and the status code (number). If the error is not
related to the connection, then it also returns the string version of the
status (e.g. `PGRES_FATAL_ERROR`) and the SQL state code (e.g. 42P01, see
https://www.postgresql.org/docs/current/errcodes-appendix.html).

Note that INSERT..RETURNING must use Connection:query as it returns values.
The statement may contain $1, $2, etc. placeholders, they will be replaced
by the extra arguments provided to the method.

### `Connection:close()`


A rockspecs/xpgsql-0.5-1.rockspec => rockspecs/xpgsql-0.5-1.rockspec +19 -0
@@ 0,0 1,19 @@
package = "xpgsql"
version = "0.5-1"
source = {
   url = "git+ssh://git@git.sr.ht/~mna/xpgsql"
}
description = {
   homepage = "Lua module providing a straightforward API to the luapgsql library.",
   license = "BSD"
}
dependencies = {
   "lua >= 5.3, < 5.5",
   "luapgsql >= 1.6.1-1"
}
build = {
   type = "builtin",
   modules = {
      xpgsql = "xpgsql.lua"
   }
}

M xpgsql.lua => xpgsql.lua +19 -13
@@ 1,7 1,7 @@
local pgsql = require 'pgsql'

local ok, cqpgsql = pcall(require, 'cqueues_pgsql')
local connectfn = ok and cqpgsql.connectdb or pgsql.connectdb
local okreq, cqpgsql = pcall(require, 'cqueues_pgsql')
local connectfn = okreq and cqpgsql.connectdb or pgsql.connectdb

local function closeconn(conn)
  local rawconn = conn._conn


@@ 20,8 20,7 @@ Connection.__index = Connection

local function new_connection(rawconn)
  local o = {_conn = rawconn}
  setmetatable(o, Connection)
  return o
  return setmetatable(o, Connection)
end

-- Closes the connection and frees resources associated with it.


@@ 130,24 129,31 @@ local function exec_or_query(rawconn, stmt, success, ...)
  if status == success then
    return res
  else
    return nil, res:errorMessage(), status
    return nil, res:errorMessage(), status, res:resStatus(status), res:errorField(pgsql.PG_DIAG_SQLSTATE)
  end
end

-- Executes a query statement and returns the result if it succeeds, or nil, an
-- error message and the status code. Note that INSERT .. RETURNING must use
-- Connection:query as it returns values. The statement may contain $1, $2,
-- etc.  placeholders, they will be replaced by the extra arguments provided to
-- the method.
-- error message and the status code (number). If the error is not related to the
-- connection, then it also returns the string version of the status (e.g. PGRES_FATAL_ERROR)
-- and the SQL state code (e.g. 42P01, see https://www.postgresql.org/docs/current/errcodes-appendix.html).
--
-- Note that INSERT .. RETURNING must use Connection:query as it returns
-- values. The statement may contain $1, $2, etc. placeholders, they will be
-- replaced by the extra arguments provided to the method.
function Connection:query(stmt, ...)
  return exec_or_query(self._conn, stmt, pgsql.PGRES_TUPLES_OK, ...)
end

-- Executes a non-query statement and returns the result if it succeeds, or
-- nil, an error message and the status code. Note that INSERT..RETURNING must
-- use Connection:query as it returns values. The statement may contain $1, $2,
-- etc.  placeholders, they will be replaced by the extra arguments provided to
-- the method.
-- nil, an error message and the status code (number). If the error is not
-- related to the connection, then it also returns the string version of the
-- status (e.g. PGRES_FATAL_ERROR) and the SQL state code (e.g. 42P01, see
-- https://www.postgresql.org/docs/current/errcodes-appendix.html).
--
-- Note that INSERT..RETURNING must use Connection:query as it returns values.
-- The statement may contain $1, $2, etc. placeholders, they will be replaced
-- by the extra arguments provided to the method.
function Connection:exec(stmt, ...)
  return exec_or_query(self._conn, stmt, pgsql.PGRES_COMMAND_OK, ...)
end