#! /bin/sh
#------------------------------------------------------------------------------
# /var/install/include/mysqllib-2 - script interface for MySQL lib
#
# Creation:     2008-04-12 dv
# Last update:  $Id: mysqllib-2.in 54319 2018-04-28 18:33:52Z dv $
#
# Copyright (c) 2008-@@YEAR@@ the eisfair-team, team(at)eisfair(dot)org
#
# 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.
#----------------------------------------------------------------------------

MY_API_SERVERCONNECT=10
MY_API_SERVERDISCONNECT=20
MY_API_SERVERISCONNECTED=30
MY_API_SERVERGETERROR=40
MY_API_SERVERPASSWD=50
MY_API_SERVERUSER=60
MY_API_SERVERHOST=70
MY_API_SERVERPORT=80
MY_API_SERVERDUPTO=100
MY_API_QUERYSQL=110
MY_API_EXECSQL=120
MY_API_SERVERDEFAULT=200
MY_API_RESULTSTATUS=300
MY_API_RESULTNUMROWS=310
MY_API_RESULTNUMCOLUMNS=320
MY_API_RESULTCOLUMNNAME=330
MY_API_RESULTCOLUMNSIZE=340
MY_API_RESULTFETCH=350
MY_API_RESULTDATA=360
MY_API_RESULTISNULL=370
MY_API_RESULTRESET=380
MY_API_RESULTFREE=390
MY_API_RESULTTOLIST=400

my_module_offs="99000"

SQL_COMMAND_OK=0
SQL_DATA_READY=1
SQL_ERROR=2

#------------------------------------------------------------------------------
# Load MySQl module
# Expects: <nothing>
# Returns: <nothing> but success or failure
#------------------------------------------------------------------------------
my_initmodule()
{
    local prefix=/usr
    local exec_prefix=/usr

    cui_load_addon "/usr/lib/cui-addons/libmysql-cui-addon.so.2.0.2"
    if [ "$p2" != 0 ]
    then
        my_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)
#------------------------------------------------------------------------------
my_server_connect()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_SERVERCONNECT}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Close connection
# Expects: $1  <-- Handle         : connection handle
# Returns: nothing
#------------------------------------------------------------------------------
my_server_disconnect()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_SERVERDISCONNECT}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Is the connection established?
# Expects: $1  <-- Handle         : connection handle
# Returns: $p2 --> Value          : 1 if connected else 0
#------------------------------------------------------------------------------
my_server_isconnected()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_SERVERISCONNECTED}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Get error message
# Expects: $1  <-- Handle         : connection handle
# Returns: $p2 --> Value          : error message
#------------------------------------------------------------------------------
my_server_geterror()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_SERVERGETERROR}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Get password of this connection
# Expects: $1  <-- Handle         : connection handle
# Returns: $p2 --> Value          : conncetion password
#------------------------------------------------------------------------------
my_server_passwd()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_SERVERPASSWD}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Get user of this connection
# Expects: $1  <-- Handle         : connection handle
# Returns: $p2 --> Value          : conncetion user
#------------------------------------------------------------------------------
my_server_user()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_SERVERUSER}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Get host of this connection
# Expects: $1  <-- Handle         : connection handle
# Returns: $p2 --> Value          : conncetion host
#------------------------------------------------------------------------------
my_server_host()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_SERVERHOST}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Get port of this connection
# Expects: $1  <-- Handle         : connection handle
# Returns: $p2 --> Value          : conncetion port
#------------------------------------------------------------------------------
my_server_port()
{
    cui_send "C" "$[${my_module_offs} + ${MY_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
#------------------------------------------------------------------------------
my_server_dupto()
{
    cui_send "C" "$[${my_module_offs} + ${MY_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
#------------------------------------------------------------------------------
my_query_sql()
{
    cui_send "C" "$[${my_module_offs} + ${MY_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
#------------------------------------------------------------------------------
my_exec_sql()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_EXECSQL}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Return standard connection
# Expects: nothing
# Returns: $p2 --> Handle         : connection handle
#------------------------------------------------------------------------------
my_server_default()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_SERVERDEFAULT}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Return result status
# Expects: $1  <-- Handle         : result handle
# Returns: $p2 --> Value          : execution status (SQL_XXX...)
#------------------------------------------------------------------------------
my_result_status()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_RESULTSTATUS}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Return number of result rows
# Expects: $1  <-- Handle         : result handle
# Returns: $p2 --> Value          : number of rows
#------------------------------------------------------------------------------
my_result_numrows()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_RESULTNUMROWS}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Return number of result columns
# Expects: $1  <-- Handle         : result handle
# Returns: $p2 --> Value          : number of columns
#------------------------------------------------------------------------------
my_result_numcolumns()
{
    cui_send "C" "$[${my_module_offs} + ${MY_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
#------------------------------------------------------------------------------
my_result_columnname()
{
    cui_send "C" "$[${my_module_offs} + ${MY_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
#------------------------------------------------------------------------------
my_result_columnsize()
{
    cui_send "C" "$[${my_module_offs} + ${MY_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
#------------------------------------------------------------------------------
my_result_fetch()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_RESULTFETCH}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Return result data
# Expects: $1  <-- Handle         : result handle
#          $2  <-- Value          : column index (0 ... n)
# Returns: $p2 --> String         : Data
#------------------------------------------------------------------------------
my_result_data()
{
    cui_send "C" "$[${my_module_offs} + ${MY_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
#------------------------------------------------------------------------------
my_result_isnull()
{
    cui_send "C" "$[${my_module_offs} + ${MY_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
#------------------------------------------------------------------------------
my_result_reset()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_RESULTRESET}]" "$@"
    cui_wait_ack
    return $?
}


#------------------------------------------------------------------------------
# Free result set
# Expects: $1  <-- Handle         : result handle
# Returns: nothing
#------------------------------------------------------------------------------
my_result_free()
{
    cui_send "C" "$[${my_module_offs} + ${MY_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
#------------------------------------------------------------------------------
my_result_tolist()
{
    cui_send "C" "$[${my_module_offs} + ${MY_API_RESULTTOLIST}]" "$@"
    cui_wait_ack
    return $?
}


#----------------------------------------------------------------------------
# end
#----------------------------------------------------------------------------