Skip to content

ev_postgres

Sudheer edited this page Mar 9, 2023 · 13 revisions

Implementation if Postgresql client prototcol for evlua.

The module uses the underlying evpoco postgresql client to fulfill the functionality of the interface.

Synopsis

The calling program should have access to a running Posgresql database instance. The connection parameters to the database should be configured.

Datatypes needed to interace with the system are either lua datatypes or those defined as part of lua_schema

  • int8_t: userdata, CDATA
  • int16_t: userdata, CDATA
  • int32_t: userdata, CDATA
  • int64_t: userdata, CDATA
  • float: userdata, CDATA
  • string: lua string
  • double: lua number
  • boolean: lua boolean
  • binary data: hex_data_s_type: userdata, CDATA
  • date: dt_s_type: userdata, CDATA
  • datetime: dt_s_type: userdata, CDATA
  • timestamp: dt_s_type: userdata, CDATA
  • duration: dur_s_type: userdata, CDATA
  • numeric: userdata, CDATA (big decimal implementation)
local ev_postgres_db = require("service_utils.db.ev_postgres");

The client implementation provides five classes, viz. ev_postgres_conn, ev_postgres_stmt, ev_postgres_cursor, ev_postgres_cursor_res and ev_postgres_db, these classes represent, connection, statement, cursor, cursor set and database handle respectively.

ev_postgres_db

ev_postgres_db.open_connection(host, port, dbname, user, password);

DESCRIPTION:

Opens a new connection to the Postgresql database server instance

PARAMETERS:

conn: table, connection handle

RETURN:

params: table, parameter data

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_conn

ev_postgres_conn:begin();

DESCRIPTION:

Starts a database transaction using the underlying connection (session)

PARAMETERS:

none

RETURN:

flg: boolean, true - successful, false - unsuccessful
msg: string, error message in case of flg being false

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_conn:commit();

DESCRIPTION:

Commits the insert(s), update(s) and delete(s) done in the transaction

PARAMETERS:

none

RETURN:

flg: boolean, true - successful, false - unsuccessful
msg: string, error message in case of flg being false

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_conn:rollback();

DESCRIPTION:

Rolls back the insert(s), update(s) and delete(s) done in the transaction

PARAMETERS:

none

RETURN:

flg: boolean, true - successful, false - unsuccessful
msg: string, error message in case of flg being false

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_conn:reset_connection_error();

DESCRIPTION:

Resets the connection handle so that it can be reused for other operations

PARAMETERS:

none

RETURN:

none

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_conn:prepare(sql_stmt);

DESCRIPTION:

Prepares the input SQL statement for execution in the database reuses the cached statement if already present and the prepared statment is cached for further usage

PARAMETERS:

sql_statement: string

RETURN:

stmt: table, prepared statement

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_conn:close_open_cursors();

DESCRIPTION:

Closes any open cursors in the database transaction session

PARAMETERS:

none

RETURN:

flg: boolean, true - successful, false - unsuccessful
msg: string, error message in case of flg being false

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_conn:get_systimestamp();

DESCRIPTION:

Gets the current system time, it is expected that the clock on host on which application service is running is synchronized with that of the host on which database is running

PARAMETERS:

none

RETURN:

time: userdata, CDATA of type (char*) representing "datetime|tz"

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_conn:get_sysdate();

DESCRIPTION:

Gets the current system date, it is expected that the clock on host on which application service is running is synchronized with that of the host on which database is running

PARAMETERS:

none

RETURN:

date: userdata, CDATA of type (char*) representing "date|tz"

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_conn:get_sequence_nextval(seq_name);

DESCRIPTION:

Gets the next value of the given sequence definied in the database

PARAMETERS:

seq_name: string, name of the sequence

RETURN:

nextval: userdata, CDATA of type (int64_t)

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_conn:open_cursor(cursor_id, sql_stmt, ...);

DESCRIPTION:

Prepares the input SQL statement for execution and opens a cursor with the SQL statement and identified by the passed cursor_id

PARAMETERS:

cusor_id: string
sql_statement: string

RETURN:

cur: table, cusror handle

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_stmt

ev_postgres_stmt:execute(...);

DESCRIPTION:

Executes the prepared statement using the bind variables passed as arguments

PARAMETERS:

...: vararg, bind variables of various types, which will be used to execute the SQL statement

RETURN:

flg: boolean, true - successful, false - unsuccessful
msg: string, error message in case of flg being false

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_stmt:affected();

DESCRIPTION:

Returns the number of rows affected by the SQL statement that has got executed, this is applicable in case of INSERT, UPDATE and DELETE operations

PARAMETERS:

none

RETURN:

num: number

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_stmt:fetch_result();

DESCRIPTION:

Fetches the next incremental record from the result set, if the record exists

PARAMETERS:

none

RETURN:

rec: table, tuple (array) of result fields

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_stmt:columns();

DESCRIPTION:

Fetches the column names returned by the ev_postgres_stmt.fetch_result operation

PARAMETERS:

none

RETURN:

columns: table, array of column names

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_stmt:map(res_tuple, col_map);

DESCRIPTION:

Maps the data returned by ev_postgre_stmt.fetch_result to the fields mentioned in col_map according to position

PARAMETERS:

res_tuple: table, tuple returned from ev_postgre_stmt.fetch_result col_map: table, tuple of field names

RETURN:

record: table, result object, indexed by field names

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_stmt:close();

DESCRIPTION:

Closes the Postgresql statement and initiates garbage collection

PARAMETERS:

none

RETURN:

none

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_cursor

A cursor is opened using the function ev_postgres_conn:get_sequence_nextval(seq_name)

ev_postgres_cursor:fetch_all();

DESCRIPTION:

Fetches all the records as reclared by the cursor in a single go

PARAMETERS:

none

RETURN:

cur_res: table, cursor result that can be iterated over

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_cursor:fetch_next_set(props);

DESCRIPTION:

Fetches a set of records from the cursor based on parameters given in the argument

  • If props is passed as nil then all the records of the cursor are fetched from the starting position in the cursor
  • If props.num_\recs is is passed as nil or 0, then all the records of the cursor are fetched from the current position in the cursor, else as many records will be fetched
  • If props.offset is is passed as nil or 0, then fetch will be commenced from the first record, else fetch will commence from the mentioned offset.

PARAMETERS:

props: table, {offset, num_recs}

RETURN:

cur_res: table, cursor result that can be iterated over

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_cursor:fetch_rec();

DESCRIPTION:

Fetches an incremental record from the cusror from the current position in the cursor

PARAMETERS:

none

RETURN:

cur_rec: table, tuple of fields as retured based on the SQL query of the cursor.

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_cursor:close();

DESCRIPTION:

Closes the open cursor

PARAMETERS:

none

RETURN:

none

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_cursor_res

The cursor result is obtained by calling the function ev_postgres_cursor:fetch_next_set(props)

ev_postgres_cursor_res:fetch_rec();

DESCRIPTION:

Fetches an incremental record from the cusror from the current position in the cursor

PARAMETERS:

none

RETURN:

cur_rec: table, tuple of fields as retured based on the SQL query of the cursor.

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_cursor_res:map();

DESCRIPTION:

Maps the data returned by ev_postgres_cursor_res:fetch_rec to the fields mentioned in col_map according to position

PARAMETERS:

res_tuple: table, tuple returned from ev_postgre_stmt.fetch_result col_map: table, tuple of field names

RETURN:

record: table, result object, indexed by field names

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

ev_postgres_cursor_res:close();

DESCRIPTION:

Closes the open cursor

PARAMETERS:

none

RETURN:

none

ERROR:

Throws exceptions if any, which can be caught by pcall/xpcall mechanism

Clone this wiki locally