#! /bin/sh #------------------------------------------------------------------------------ # /var/install/include/pgsqllib - script interface for postgres libpq # # Copyright (c) 2008 eisfair-Team # # Creation: 2008-04-12 dv # Last update: $Id: pgsqllib-2 33436 2013-04-10 20:34:56Z dv $ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- PG_API_SERVERCONNECT=10 PG_API_SERVERDISCONNECT=20 PG_API_SERVERISCONNECTED=30 PG_API_SERVERGETERROR=40 PG_API_SERVERPASSWD=50 PG_API_SERVERUSER=60 PG_API_SERVERHOST=70 PG_API_SERVERPORT=80 PG_API_SERVERDUPTO=100 PG_API_QUERYSQL=110 PG_API_EXECSQL=120 PG_API_SERVERDEFAULT=200 PG_API_RESULTSTATUS=300 PG_API_RESULTNUMROWS=310 PG_API_RESULTNUMCOLUMNS=320 PG_API_RESULTCOLUMNNAME=330 PG_API_RESULTCOLUMNSIZE=340 PG_API_RESULTFETCH=350 PG_API_RESULTFIRST=351 PG_API_RESULTPREVIOUS=352 PG_API_RESULTNEXT=353 PG_API_RESULTLAST=354 PG_API_RESULTDATA=360 PG_API_RESULTISNULL=370 PG_API_RESULTRESET=380 PG_API_RESULTFREE=390 PG_API_RESULTTOLIST=400 pg_module_offs="99000" SQL_COMMAND_OK=0 SQL_DATA_READY=1 SQL_ERROR=2 #------------------------------------------------------------------------------ # Load postgres module # Expects: # Returns: but success or failure #------------------------------------------------------------------------------ function pg_initmodule() { local prefix=@prefix@ local exec_prefix=@exec_prefix@ cui_load_addon "@libdir@/cui-addons/@PACKAGE_NAME@.so.@PACKAGE_VERSION@" if [ "$p2" != 0 ] then pg_module_offs="$p2" return 0 else return 1 fi } #------------------------------------------------------------------------------ # Establish connection # Expects: $1 <-- String : Host # $2 <-- String : Port # $3 <-- String : User # $4 <-- String : Password # $5 <-- String : Database # Returns: $p2 --> Handle : Connection Handle (0 if error) #------------------------------------------------------------------------------ function pg_server_connect() { cui_send "C" "$[${pg_module_offs} + ${PG_API_SERVERCONNECT}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Close connection # Expects: $1 <-- Handle : connection handle # Returns: nothing #------------------------------------------------------------------------------ function pg_server_disconnect() { cui_send "C" "$[${pg_module_offs} + ${PG_API_SERVERDISCONNECT}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Is the connection established? # Expects: $1 <-- Handle : connection handle # Returns: $p2 --> Value : 1 if connected else 0 #------------------------------------------------------------------------------ function pg_server_isconnected() { cui_send "C" "$[${pg_module_offs} + ${PG_API_SERVERISCONNECTED}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Get error message # Expects: $1 <-- Handle : connection handle # Returns: $p2 --> Value : error message #------------------------------------------------------------------------------ function pg_server_geterror() { cui_send "C" "$[${pg_module_offs} + ${PG_API_SERVERGETERROR}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Get password of this connection # Expects: $1 <-- Handle : connection handle # Returns: $p2 --> Value : conncetion password #------------------------------------------------------------------------------ function pg_server_passwd() { cui_send "C" "$[${pg_module_offs} + ${PG_API_SERVERPASSWD}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Get user of this connection # Expects: $1 <-- Handle : connection handle # Returns: $p2 --> Value : conncetion user #------------------------------------------------------------------------------ function pg_server_user() { cui_send "C" "$[${pg_module_offs} + ${PG_API_SERVERUSER}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Get host of this connection # Expects: $1 <-- Handle : connection handle # Returns: $p2 --> Value : conncetion host #------------------------------------------------------------------------------ function pg_server_host() { cui_send "C" "$[${pg_module_offs} + ${PG_API_SERVERHOST}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Get port of this connection # Expects: $1 <-- Handle : connection handle # Returns: $p2 --> Value : conncetion port #------------------------------------------------------------------------------ function pg_server_port() { cui_send "C" "$[${pg_module_offs} + ${PG_API_SERVERPORT}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Establish a second connection to the current server # Expects: $1 <-- Handle : connection handle # $2 <-- String : database name # Returns: $p2 --> Handle : new connection handle #------------------------------------------------------------------------------ function pg_server_dupto() { cui_send "C" "$[${pg_module_offs} + ${PG_API_SERVERDUPTO}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Execute a SQL query and return a result set # Expects: $1 <-- Handle : connection handle # $2 <-- String : SQL statement # Returns: $p2 --> Handle : result set handle #------------------------------------------------------------------------------ function pg_query_sql() { cui_send "C" "$[${pg_module_offs} + ${PG_API_QUERYSQL}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Execute a SQL command and return success of failure # Expects: $1 <-- Handle : connection handle # $2 <-- String : SQL statement # Returns: $p2 --> Handle : 1 = success, 0 = failure #------------------------------------------------------------------------------ function pg_exec_sql() { cui_send "C" "$[${pg_module_offs} + ${PG_API_EXECSQL}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Return standard connection # Expects: nothing # Returns: $p2 --> Handle : connection handle #------------------------------------------------------------------------------ function pg_server_default() { cui_send "C" "$[${pg_module_offs} + ${PG_API_SERVERDEFAULT}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Return result status # Expects: $1 <-- Handle : result handle # Returns: $p2 --> Value : execution status (SQL_XXX...) #------------------------------------------------------------------------------ function pg_result_status() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTSTATUS}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Return number of result rows # Expects: $1 <-- Handle : result handle # Returns: $p2 --> Value : number of rows #------------------------------------------------------------------------------ function pg_result_numrows() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTNUMROWS}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Return number of result columns # Expects: $1 <-- Handle : result handle # Returns: $p2 --> Value : number of columns #------------------------------------------------------------------------------ function pg_result_numcolumns() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTNUMCOLUMNS}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Return column name # Expects: $1 <-- Handle : result handle # $2 <-- Value : column index (0 ... n) # Returns: $p2 --> String : column name #------------------------------------------------------------------------------ function pg_result_columnname() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTCOLUMNNAME}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Return column size # Expects: $1 <-- Handle : result handle # $2 <-- Value : column index (0 ... n) # Returns: $p2 --> Value : column size #------------------------------------------------------------------------------ function pg_result_columnsize() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTCOLUMNSIZE}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Fetch result data row (move cursor) # Expects: $1 <-- Handle : result handle # Returns: $p2 --> Value : 1 = data available, 0 = no more data #------------------------------------------------------------------------------ function pg_result_fetch() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTFETCH}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Fetch first row of result data (set cursor) # Expects: $1 <-- Handle : result handle # Returns: $p2 --> Value : 1 = data available, 0 = no more data #------------------------------------------------------------------------------ function pg_result_first() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTFIRST}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Fetch previous row of result data (move cursor) # Expects: $1 <-- Handle : result handle # Returns: $p2 --> Value : 1 = data available, 0 = no more data #------------------------------------------------------------------------------ function pg_result_previous() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTPREVIOUS}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Fetch next row of result data (move cursor) # Expects: $1 <-- Handle : result handle # Returns: $p2 --> Value : 1 = data available, 0 = no more data #------------------------------------------------------------------------------ function pg_result_next() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTNEXT}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Fetch last row of result data (set cursor) # Expects: $1 <-- Handle : result handle # Returns: $p2 --> Value : 1 = data available, 0 = no more data #------------------------------------------------------------------------------ function pg_result_last() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTLAST}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Return result data # Expects: $1 <-- Handle : result handle # $2 <-- Value : column index (0 ... n) # Returns: $p2 --> String : Data #------------------------------------------------------------------------------ function pg_result_data() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTDATA}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Check if value is NULL # Expects: $1 <-- Handle : result handle # $2 <-- Value : column index (0 ... n) # Returns: $p2 --> Value : 1 = is null, 0 is not null #------------------------------------------------------------------------------ function pg_result_isnull() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTISNULL}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Reset result column cursor to first data row # Expects: $1 <-- Handle : result handle # $2 <-- Value : column index (0 ... n) # Returns: $p2 --> Value : 1 = is null, 0 is not null #------------------------------------------------------------------------------ function pg_result_reset() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTRESET}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Free result set # Expects: $1 <-- Handle : result handle # Returns: nothing #------------------------------------------------------------------------------ function pg_result_free() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTFREE}]" "$@" cui_wait_ack return $? } #------------------------------------------------------------------------------ # Transfer result set to list view (selects entry that matches keyword in # column $3) # Expects: $1 <-- Handle : result handle # $2 <-- Handle : listview window handle # $3 <-- Index : column no (zero based) for keyword matching # $4 <-- String : text for keyword matching # Returns: nothing #------------------------------------------------------------------------------ function pg_result_tolist() { cui_send "C" "$[${pg_module_offs} + ${PG_API_RESULTTOLIST}]" "$@" cui_wait_ack return $? } #---------------------------------------------------------------------------- # End #----------------------------------------------------------------------------