proc vmctps_store_kddh_msg_labdaq_kddh_orm { args } {

global HciConnName ;# Name of thread

keylget args MODE mode ;# Fetch mode

set ctx "" ; keylget args CONTEXT ctx ;# Fetch tps caller context

set uargs {} ; keylget args ARGS uargs ;# Fetch user-supplied args

set debug 0 ; ;# Fetch user argument DEBUG and

catch {keylget uargs DEBUG debug} ;# assume uargs is a keyed list

set module "vmctps_store_kddh_msg_labdaq_kddh_orm/$HciConnName/$ctx" ;# Use this before every echo/puts,

;# it describes where the text came from

set dispList {} ;# Nothing to return

switch -exact -- $mode {

start {

# Perform special init functions

# N.B.: there may or may not be a MSGID key in args

if { $debug } {

puts stdout "$module: Starting in debug mode..."

}

}

run {

# 'run' mode always has a MSGID; fetch and process it

echo

echo tcl vmctps_storemsgid_prm_kddh_orm

echo

keylget args MSGID mh

# get msg

set msg [msgget $mh]

set debug 1

package require odbc

set usr "xxx"

set pass "xxxx"

set dsn "xxxxxxxx"

set stored_procedure "sp_xxxxx"

set table "xxxxx"

echo [odbc SQLAllocHandle SQL_HANDLE_ENV SQL_NULL_HANDLE henv]

# Set the most current ODBC version (3.0)

echo [odbc SQLSetEnvAttr $henv SQL_ATTR_ODBC_VERSION SQL_OV_ODBC3 0]

# Allocate connection handle

set err [odbc SQLAllocHandle SQL_HANDLE_DBC $henv hdbc]

if {$debug} {puts "DEBUG: SQLAllocHandle err: $err"}

# Make a connection

echo test connect: [set err [odbc SQLConnect $hdbc $dsn SQL_NTS $usr SQL_NTS $pass SQL_NTS]]

if {$debug} {puts "DEBUG: SQLConnect err: $err"}

#If connection failed... will attempt to connect 3 times (5sec sleep between each retry)

set retries 3

set sleep_int 5

while {$retries & [cequal $err SQL_ERROR]} {

sleep $sleep_int

echo retries $retries

echo sleep_int $sleep_int

# Make a connection

set err [odbc SQLConnect $hdbc $dsn SQL_NTS $usr SQL_NTS $pass SQL_NTS]

incr retries -1

}

catch {odbc SQLFreeStmt $hstmt SQL_DROP}

set err [odbc SQLAllocHandle SQL_HANDLE_STMT $hdbc hstmt]

#if {$err ne "SQL_SUCCESS"} {return ""}

if {$debug} {puts "DEBUG: SQLAllocHandle err: $err"}

# Prepare & Execute - SQL statement

set err [odbc SQLExecDirect $hstmt $stored_procedure SQL_NTS]

set result {}

set ctrid ""

set KDDH_Message ""

set InsertDateTime ""

set Message_Sent ""

echo bind1 [odbc SQLBindCol $hstmt 1 SQL_C_NUMBERIC ctrid 255 255]

echo bind1 [odbc SQLBindCol $hstmt 2 SQL_C_VARCHAR KDDH_Message 8000 8000]

echo bind1 [odbc SQLBindCol $hstmt 3 SQL_C_NUMERIC Message_Sent 255 255]

echo bind1 [odbc SQLBindCol $hstmt 4 SQL_C_TIMESTAMP InsertDateTime 255 255]

set i 0

set err [odbc SQLFetch $hstmt]

# if string {[!equal $err "SQL_NO_DATA_FOUND"]}

# echo "We have a MAJOR issue because there was 'No Data Found' at this time"

while {$err == "SQL_SUCCESS" || $err == "SQL_SUCCESS_WITH_INFO"} {

incr i

set err [odbc SQLFetch $hstmt]

}

echo counter $i

if {$err == "SQL_NO_DATA_FOUND"} {

set insert "INSERT INTO KDDH_Message_Hold (KDDH_Message,Message_Sent) VALUES('$msg','0')"

set err [odbc SQLCloseCursor $hstmt]

set err [odbc SQLExecDirect $hstmt $insert SQL_NTS]

echo

echo added the information to table

echo

# set err2 [odbc SQLGetDiagRec SQL_HANDLE_STMT $hstmt 1 SqlState NativeError MessageText 511 TextLength]

# echo err2 $err2

# echo sqlstate $SqlState

# echo nativeerror $NativeError

# echo messagetext $MessageText

}

# set xlateOutVals $result

# echo result: |$result|

# echo xlateoutvals: |[lindex $xlateOutVals 0]|

# Terminate connection/statement handle

catch {odbc SQLDisconnect $hdbc}

catch {odbc SQLFreeHandle SQL_HANDLE_DBC $hdbc}

catch {odbc SQLFreeHandle SQL_HANDLE_ENV $henv}

# lappend dispList "CONTINUE $mh"

}

}

time {

# Timer-based processing

# N.B.: there may or may not be a MSGID key in args

}

#shutdown {

# Doing some clean-up work

#}

#default {

# error "Unknown mode '$mode' in $module"

#}

}

return $dispList

}