# DBENG (Bbuuzzb) io code error message module.
# Rick Smereka, Copyright (C) 2005.

# 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.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, get a copy via the Internet at
# http://gnu.org/copyleft/gpl.html or write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA

# You can contact the author via email at rsmereka@future-lab.com

# Initial tcl version Apr/2005, Rick Smereka

proc db_io_code_string {io_stat} {
   # Interpret 'io_stat' and return an explanation of the code
   # in English.
   global DBENG_OK DBENG_EXISTS DBENG_NOT_EXIST DBENG_RECORD_CHANGED
   global DBENG_NO_RECORD DBENG_EOF DBENG_NO_SUCH_FIELD DBENG_INVALID_FILE_NAME
   global DBENG_UNABLE_TO_OPEN DBENG_WRITE_FAIL DBENG_NO_EXISTING_RECORD
   global DBENG_CANNOT_CREATE_TABLE DBENG_MAX_OPEN_TABLE DBENG_NO_SUCH_FILE
   global DBENG_RECORD_TOO_LARGE DBENG_MEMORY_FAIL DBENG_INVALID_FUNCTION
   global DBENG_VC_ERROR DBENG_ACCESS_DENIED DBENG_FUNCTION_NOT_IMPLEMENTED
   global DBENG_PUT_ERROR DBENG_NO_SUCH_SUBFIELD DBENG_NO_SUCH_SUBSUBFIELD
   global DBENG_FILENAME_TOO_LONG DBENG_NO_SUCH_TID DBENG_TABLE_LOCKED
   global DBENG_RENAME_ERROR DBENG_INTERNAL_ERROR DBENG_FILE_DATA_ERROR
   global DBENG_CONFIG_INVALID_PARAMETER DBENG_CONFIG_PARAMETER_MISSING
   global DBENG_CONFIG_UNABLE_TO_OPEN DBENG_CONFIG_TMP_PATH_SIZE
   global DBENG_CONFIG_LOG_SIZE DBENG_CONFIG_SESSION_TABLE_SIZE
   global DBENG_CONFIG_INVALID_TMP_PATH DBENG_CONFIG_INVALID_LOG
   global DBENG_CONFIG_INVALID_SESSION_TABLE DBENG_CONFIG_INVALID_CATALOG
   global DBENG_SOCLOC_NO_INIT DBENG_NO_SERVER DBENG_FAILOVER DBENG_BAD_IP
   global DBENG_NO_CATALOG_ENTRY DBENG_CATALOG_ENTRY_EXISTS
   global DBENG_RECORD_POSITION_CHANGED DBENG_REPLICATION_NOT_ACTIVE
   global DBENG_NO_REPLICATION_ENTRY DBENG_TABLE_IN_USE DBENG_NO_CATALOG
   global DBENG_NO_INIT DBENG_NO_CATALOG

   switch $io_stat \
      $DBENG_OK {set ret "ok"} \
      $DBENG_EXISTS {set ret "already exists"} \
      $DBENG_NOT_EXIST {set ret "does not exist"} \
      $DBENG_RECORD_CHANGED {set ret "record changed"} \
      $DBENG_NO_RECORD {set ret "no current record"} \
      $DBENG_EOF {set ret "end of file"} \
      $DBENG_NO_SUCH_FIELD {set ret "no such field"} \
      $DBENG_INVALID_FILE_NAME {set ret "invalid file name"} \
      $DBENG_UNABLE_TO_OPEN {set ret "unable to open"} \
      $DBENG_WRITE_FAIL {set ret "write fail, disk full?"} \
      $DBENG_NO_EXISTING_RECORD {set ret "no existing record"} \
      $DBENG_CANNOT_CREATE_TABLE {set ret "cannot create table"} \
      $DBENG_MAX_OPEN_TABLE {set ret "already at max. open tables"} \
      $DBENG_NO_SUCH_FILE {set ret "table does not exist"} \
      $DBENG_RECORD_TOO_LARGE {set ret "record size too large"} \
      $DBENG_MEMORY_FAIL {set ret "memory allocation failure"} \
      $DBENG_INVALID_FUNCTION {set ret "invalid database function"} \
      $DBENG_VC_ERROR {set ret "socket communication error"} \
      $DBENG_ACCESS_DENIED {set ret "access denied"} \
      $DBENG_FUNCTION_NOT_IMPLEMENTED {set ret "function not yet implemented"} \
      $DBENG_PUT_ERROR {set ret "error putting data into record"} \
      $DBENG_NO_SUCH_SUBFIELD {set ret "no such subfield"} \
      $DBENG_NO_SUCH_SUBSUBFIELD {set ret "no such subsubfield"} \
      $DBENG_FILENAME_TOO_LONG {set ret "filename too long"} \
      $DBENG_NO_SUCH_TID {set ret "unknown table ID (tid)"} \
      $DBENG_TABLE_LOCKED {set ret "table is locked"} \
      $DBENG_RENAME_ERROR {set ret "error renaming table"} \
      $DBENG_INTERNAL_ERROR {set ret "unexpected internal error"} \
      $DBENG_FILE_DATA_ERROR {set ret "error in file data format"} \
      $DBENG_CONFIG_INVALID_PARAMETER {set ret "invalid config parameter"} \
      $DBENG_CONFIG_PARAMETER_MISSING {set ret "config parameter missing"} \
      $DBENG_CONFIG_UNABLE_TO_OPEN {set ret "unable to open config file"} \
      $DBENG_CONFIG_TMP_PATH_SIZE {set ret "temp files path is too long"} \
      $DBENG_CONFIG_LOG_SIZE {set ret "log name is too long"} \
      $DBENG_CONFIG_SESSION_TABLE_SIZE {set ret "session table path/name \
                                        is too long"} \
      $DBENG_CONFIG_INVALID_TMP_PATH {set ret "temp files path is invalid"} \
      $DBENG_CONFIG_INVALID_LOG {set ret "log name is invalid"} \
      $DBENG_CONFIG_INVALID_SESSION_TABLE {set ret "session table path/name \
                                           is invalid"} \
      $DBENG_CONFIG_INVALID_CATALOG {set ret "catalog path/name is invalid"} \
      $DBENG_SOCLOC_NO_INIT {set ret "socloc not initialized"} \
      $DBENG_NO_SERVER {set ret "no Bbuuzzb server available"} \
      $DBENG_FAILOVER {set ret "failover:new connection established"} \
      $DBENG_BAD_IP {set ret "bad IP address"} \
      $DBENG_NO_CATALOG_ENTRY {set ret "no catalog entry"} \
      $DBENG_CATALOG_ENTRY_EXISTS {set ret "catalog entry already exists"} \
      $DBENG_RECORD_POSITION_CHANGED {set ret "start rec pos \
                                      changed(table packed?)"} \
      $DBENG_REPLICATION_NOT_ACTIVE {set ret "table replication not active"} \
      $DBENG_NO_REPLICATION_ENTRY {set ret "no table replication entry"} \
      $DBENG_TABLE_IN_USE {set ret "table in use by other tid(s)"} \
      $DBENG_NO_CATALOG {set ret "no catalog name"} \
      $DBENG_NO_INIT {set ret "bbuuzzb API not initialized"} \
      $DBENG_NO_CATALOG {set ret "no catalog present"} \
      default {set ret "unknown error code"}
   return $ret
}

proc db_io_code_log {mname desc io_code} {
   # Send a message to the log manager (which must already be active).
   # The format of the message is:

   #    mname:desc,rc=io_code(string)

   # Where 'mname' is the procedure name, 'desc' is a desciption of
   # the fault and 'io_code' is a valid 'DBENG' I/O error code.
   set mes [db_io_code_string $io_code]
   set tmp [format "%s:%s,rc=%s" $mname $desc $mes]
   logman $tmp
}
