OPTIONS "C_CALL LABELCHECK NOSOURCE"
NUMERIC DIGITS 10
LIBRARY rexxgui, rxodbc
PARSE SOURCE . . file_name
file_name = FILESPEC('D', file_name) || FILESPEC('P', file_name) || "ODBC_defaults.txt"
i = LOADTEXT("MS.", file_name, 'TLB')
IF i == 1 THEN DO i = 1 TO ms.0
IF LEFT(ms.i, 1) \== '*' THEN INTERPRET ms.i
END
ELSE defaultdatabase = 'SQLite3 Datasource'
odbcerr = "ERROR"
odbcheading = 1
DO
odbcallocenv()
CATCH ERROR
CONDITION('M')
RETURN
END
guierr = "SYNTAX"
guiheading = 1
odbc_operation = "Select"
odbc_allcols = 1
guicreatewindow('NORMAL')
database = 0
datasrcname = choose_datasource()
IF datasrcname == "" THEN SIGNAL RETURN
again:
DO FOREVER
guigetmsg()
IF EXISTS('GuiObject') == 0 THEN DO
IF EXISTS('GuiSignal') THEN DO
END
END
ELSE DO
IF EXISTS('GuiSignal') == 0 THEN DROP (guiobject)
ELSE SELECT guiobject
WHEN 0 THEN NOP
OTHERWISE
END
END
CATCH SYNTAX
IF guiinfo() \== "" THEN DO
CONDITION('M')
SIGNAL again
END
CATCH HALT
FINALLY
guidestroywindow()
END
RETURN
perform_sqlcode:
guigetctlvalue("ODBC_AllCols")
guigetctlvalue("ODBC_Operation")
UPPER odbc_operation
guigetctlvalue("ODBC_TableName")
sql_all_colname.0 = 0
IF odbc_operation == 'SELECT' | odbc_operation == 'INSERT' THEN DO
IF fetch_all_columns() \== 0 THEN RETURN
END
IF odbc_allcols == 2 THEN DO
IF sql_pick_columns() \== 0 THEN SIGNAL perform_sqlcode_end
END
ELSE
sql_selcols.0 = ""
SELECT odbc_operation
WHEN 'DELETE' THEN sql_delete_code()
WHEN 'SELECT' THEN sql_select_code()
WHEN 'INSERT' THEN sql_insert_row()
WHEN 'UPDATE' THEN sql_update_code()
WHEN 'CREATE TABLE' THEN sql_create_table()
WHEN 'DROP TABLE' THEN sql_drop_table()
OTHERWISE DO
guisay("Need to add code to handle a" odbc_operation "command.", "OK|STOP")
RETURN
END
END
perform_sqlcode_end:
odbcfreestmt()
RETURN
sql_pick_columns:
createobject("ODBC_PickColumns.rex")
DO UNTIL guiobject == 'ODBC_PICKCOLUMNS'
guigetmsg()
IF guiobject == 'ODBC_PICKCOLUMNS' THEN DO
IF guisignal == "OK" THEN DO
odbc_pickcolumns~fillstem(sql_selcols.)
RETURN 0
END
END
FINALLY
DROP odbc_pickcolumns
END
RETURN 1
sql_insert_row:
createobject("ODBC_GetInsertCmd.rex")
againpickrow:
DO UNTIL guiobject == 'ODBC_GETINSERTCMD'
guigetmsg()
IF guiobject == 'ODBC_GETINSERTCMD' THEN DO
IF EXISTS("GuiSignal") THEN DO
odbcexecute(guisignal)
CATCH ERROR
guisay("Insert row failed:" || '0D0A'x || CONDITION('D'), "OK|STOP")
END
END
FINALLY
DROP odbc_getinsertcmd
END
RETURN
fetch_all_columns: PROCEDURE EXPOSE datasrcname odbc_databasename odbc_tablename odbc_operation sql_all_colname. sql_coltype. sql_length. sql_scale.
SELECT
WHEN POS('DB2', datasrcname) \== 0 THEN DO
IF odbc_operation == 'INSERT' THEN
sql_statement = "Select name,coltype,length,scale from sysibm.syscolumns "
ELSE DO
sql_statement = "Select name from sysibm.syscolumns ",
"where tbcreator = '"odbc_databasename"'",
"and tbname = '" || TRANSLATE(odbc_tablename) || "'",
"order by colno asc"
END
END
WHEN POS('SQLITE', datasrcname) \== 0 THEN DO
sql_statement = "Select sql from sqlite_master where ((type = 'table') and (name = '"odbc_tablename"'))",
"and sql like 'CREATE TABLE %' "
END
OTHERWISE DO
guisay("Must write code to format an SQL command to fetch the column names for" datasrcname, "OK|STOP")
RETURN 1
END
END
DO
odbcexecute(sql_statement, "columns")
DO WHILE odbcfetch("data", columns) \== "DONE"
sql_all_colname.0 = sql_all_colname.0 + 1
sql_all_colname.[sql_all_colname.0] = data.1
extract_col_names()
IF POS('SQLITE', datasrcname) == 0 THEN DO
IF odbc_operation == 'INSERT' THEN DO
sql_coltype.[sql_all_colname.0] = data.4
sql_length.[sql_all_colname.0] = data.5
sql_scale.[sql_all_colname.0] = data.7
END
END
END
IF sql_all_colname.0 == 1 THEN DO
err = 100
guisay(odbc_tablename "not found for system" odbc_databasename, "OK|STOP")
END
ELSE err = 0
CATCH ERROR
guisay("Fetch columns failed:" || '0D0A'x || CONDITION('D'), "OK|STOP")
err = 1
END
odbcfreestmt()
RETURN err
extract_col_names:
IF POS('SQLITE', datasrcname) \== 0 THEN SIGNAL extract_sqlite_columns
RETURN
extract_sqlite_columns:
sql_all_colname.1 = CHANGESTR('0D0A'x, sql_all_colname.1, ' ')
PARSE VAR sql_all_colname.1 . '(' col_defs
sql_all_colname.0 = 0
DO WHILE col_defs \= ""
PARSE VAR col_defs next_col ',' col_defs
IF POS('(', next_col) \== 0 & POS(')', next_col) == 0 THEN DO
PARSE VAR col_defs temp ',' col_defs
next_col = next_col || ',' || temp
END
IF next_col = "" THEN LEAVE
PARSE VAR next_col col_name temp
sql_all_colname.0 = sql_all_colname.0 + 1
sql_all_colname.[sql_all_colname.0] = STRIP(col_name)
IF odbc_operation == 'INSERT' THEN sql_coltype.[sql_all_colname.0] = STRIP(temp)
END
RETURN
sql_delete_code: PROCEDURE EXPOSE stmt odbc_tablename
guigetctlvalue("ODBC_Statement")
IF odbc_statement = "" THEN
IF guisay('No selection specified - do you REALLY want to delete ALL rows ?', "YES") \== 'YES' THEN RETURN
odbc_statement = 'Delete from' odbc_tablename odbc_statement
DO
odbcexecute(odbc_statement)
CATCH ERROR
guisay("Delete failed:" || '0D0A'x || CONDITION('D'), "OK|STOP")
END
RETURN
sql_update_code: PROCEDURE EXPOSE stmt odbc_tablename
guigetctlvalue("ODBC_Statement")
IF odbc_statement = "" THEN DO
guisay('No selection specified. Nothing to update.', "OK|INFO")
RETURN
END
odbc_statement = 'Update' odbc_tablename odbc_statement
DO
odbcexecute(odbc_statement)
CATCH ERROR
guisay("Update failed:" || '0D0A'x || CONDITION('D'), "OK|STOP")
END
RETURN
sql_create_table: PROCEDURE EXPOSE datasrcname odbc_tablename
SELECT
WHEN POS('DB2', datasrcname) \== 0 THEN DO
sw_db = 1
odbc_statement = 'Create table' database || '.' || odbc_tablename
END
WHEN POS('SQLITE', datasrcname) \== 0 THEN DO
sw_db = 2
odbc_statement = 'Create table' odbc_tablename
END
OTHERWISE DO
guisay("Must write code to format an SQL command to create a table for" datasrcname, "OK|STOP")
RETURN
END
END
odbc_statement = odbc_statement '( keycol1 smallint not null unique ,timecol time ,datecol date ,timestampcol timestamp ,charcol char(10)'
SELECT sw_db
WHEN 1 THEN
odbc_statement = odbc_statement ',deccol decimal(10,2)'
WHEN 2 THEN
odbc_statement = odbc_statement ',deccol int(10,2)'
OTHERWISE DO
guisay("Must write code to format an integer in an SQL command for" datasrcname, "OK|STOP")
RETURN
END
END
odbc_statement = odbc_statement || ')'
DO
odbcexecute(odbc_statement)
guisay('Table created with the definition:' || '0D0A'x || odbc_statement, "OK|INFO")
CATCH ERROR
guisay("Create table failed:" || '0D0A'x || CONDITION('D'), "OK|STOP")
END
RETURN
sql_drop_table: PROCEDURE EXPOSE datasrcname odbc_tablename
SELECT
WHEN POS('DB2', datasrcname) \== 0 THEN
odbc_statement = 'Drop table' database || '.' || odbc_tablename
WHEN POS('SQLITE', datasrcname) \== 0 THEN
odbc_statement = 'Drop table' odbc_tablename
OTHERWISE DO
guisay("Must write code to format an SQL command to create a table for" datasrcname, "OK|STOP")
RETURN
END
END
IF guisay('Do you REALLY want to drop this table?', "YES") == 'YES' THEN DO
odbcexecute(odbc_statement)
guisay('Table dropped.', "OK|INFO")
CATCH ERROR
guisay("DROP TABLE failed:" || '0D0A'x || CONDITION('D'), "OK|STOP")
END
RETURN
sql_select_code: PROCEDURE EXPOSE datasrcname odbc_tablename sql_all_colname. sql_selcols.
guiremovectltext("ODBC_Result")
guigetctlvalue("ODBC_Statement")
IF sql_selcols.0 == "" THEN
selected_cols = '*'
ELSE DO
selected_cols = ""
DO i = 1 TO sql_selcols.0
n = sql_selcols.i + 1
selected_cols = selected_cols || ',' sql_all_colname.n
END
selected_cols = SUBSTR(selected_cols, 2)
END
SELECT
WHEN POS('DB2', datasrcname) \== 0 THEN
sql_select = "Select" selected_cols "from" database || "." || odbc_tablename
WHEN POS('SQLITE', datasrcname) \== 0 THEN
sql_select = "Select" selected_cols "from" odbc_tablename
OTHERWISE DO
guisay("Must write code to format an SQL command to select rows for" datasrcname, "OK|STOP")
RETURN
END
END
IF odbc_statement \= "" THEN sql_select = sql_select odbc_statement
rows = 0
DO
odbcexecute(sql_select, "columns")
DO i = 1 TO columns
odbccolattribute("sql_sizes." || i, i, "DISPLAY")
sql_sizes.i = sql_sizes.i + 2
END
selected_cols = ""
IF sql_selcols.0 == "" THEN DO i = 1 TO columns
IF LENGTH(sql_all_colname.i) > sql_sizes.i THEN sql_sizes.i = LENGTH(sql_all_colname.i) + 2
selected_cols = selected_cols CENTER(sql_all_colname.i, sql_sizes.i)
END
ELSE DO i = 1 TO sql_selcols.0
n = sql_selcols.i + 1
IF LENGTH(sql_all_colname.n) > sql_sizes.i THEN sql_sizes.i = LENGTH(sql_all_colname.n) + 2
selected_cols = selected_cols CENTER(sql_all_colname.n, sql_sizes.i)
END
guisendmsg("ODBC_Result", "ADDSTRING", , selected_cols)
guisendmsg("ODBC_Result", "ADDSTRING", , COPIES("=", LENGTH(selected_cols)))
DO WHILE odbcfetch("data", columns) \== "DONE" & rows < 51
rows = rows + 1
selected_cols = ""
DO i = 1 TO columns
selected_cols = selected_cols CENTER(STRIP(data.i), sql_sizes.i)
guisendmsg("ODBC_Result", "ADDSTRING", , selected_cols)
END
END
CATCH ERROR
guisay("SELECT failed:" || "0D0A"x || CONDITION('D'), "OK|STOP")
rows = 0
END
odbcfreestmt()
IF rows == 1 THEN guisay('No rows found in table.', "OK|INFO")
RETURN
sql_show_tables: PROCEDURE EXPOSE datasrcname
SELECT
WHEN POS('DB2', datasrcname) \== 0 THEN DO
sw_db = 1
sql_select = "Select name from sysibm.systables where creator = '" || TRANSLATE(USERID()),
|| "' and type = 'T' order by name asc"
END
WHEN POS('SQLITE', datasrcname) \== 0 THEN DO
sw_db = 2
sql_select = "Select sql from sqlite_master where type = 'table' and sql like 'CREATE TABLE %' "
END
OTHERWISE DO
guisay("Must write code to format an SQL command to query table names for" datasrcname, "OK|STOP")
RETURN
END
END
DO
odbcexecute(sql_select, "columns")
guiremovectltext("ODBC_TableName")
DO WHILE odbcfetch("data", columns) \== "DONE"
table_name = ""
SELECT sw_db
WHEN 1 THEN
IF POS('_', data.1) == 0 THEN table_name = data.1
WHEN 2 THEN DO
PARSE VAR data.1 . 'TABLE ' table_name '(' .
table_name = STRIP(table_name)
END
OTHERWISE DO
guisay("Must write code to extract the table name for" datasrcname, "OK|STOP")
RETURN
END
END
IF table_name \= "" THEN guisendmsg("ODBC_TableName", "ADDSTRING", , table_name)
END
guisendmsg("ODBC_TableName", "SETCURSEL", 0 )
CATCH ERROR
guisay("Show tables failed:" || "0D0A"x || CONDITION('D'), "OK|STOP")
END
odbcfreestmt()
RETURN
connect_to_database: PROCEDURE EXPOSE guiwindow stmt datasrcname obdc_databasename
user = USERID()
IF POS('SQLITE', datasrcname) \== 0 THEN DO
guigetctlvalue("ODBC_DatabaseName")
IF odbc_databasename == "" THEN DO
guisay("Enter a database name first.")
RETURN 0
END
odbc_databasename = TRANSLATE(STRIP(odbc_databasename))
IF FILESPEC('E', odbc_databasename) \== '.DB' THEN odbc_databasename = odbc_databasename || '.DB'
full = "DSN=" || datasrcname || ";Database=" || odbc_databasename ";UID=" || user || ";"
END
ELSE
full = "DSN=" || datasrcname || ";UID=" || user || ";"
DO
odbcallocdatabase("database", "full", "Stmt", guiwindow)
CATCH ERROR
err = CONDITION('D')
SELECT
WHEN POS("PASSWORD INVALID",err) \== 0 THEN
guisay("Invalid password. Try again", 'OK|STOP')
WHEN POS("USERID INVALID",err) \== 0 THEN
guisay("Invalid user id. Try again", 'OK|STOP')
OTHERWISE
guisay("Connect to database failed:" || '0D0A'x || err, 'OK|STOP')
END
RETURN 0
END
RETURN database
choose_datasource: PROCEDURE EXPOSE environment defaultdatabase
createobject("ODBC_PickSource.rex", , , defaultdatabase)
choose_datasource_again:
DO UNTIL guiobject == 'ODBC_PICKSOURCE'
guigetmsg()
IF guiobject == 'ODBC_PICKSOURCE' THEN DO
IF guisignal == "" THEN SIGNAL choose_datasource_bad
IF EXISTS("GuiSignal") THEN DO
datasrcname = TRANSLATE(guisignal)
DROP odbc_picksource
guiaddctltext("ODBC_driver", datasrcname)
RETURN datasrcname
END
END
CATCH SYNTAX
IF guiinfo() \== "" THEN DO
CONDITION('M')
SIGNAL choose_datasource_again
END
END
choose_datasource_bad:
DROP odbc_picksource
RETURN ""
wm_click_odbc_browse:
IF database == 0 THEN DO
database = connect_to_database()
IF database = 0 THEN RETURN
END
sql_show_tables()
RETURN
wm_click_odbc_cancel:
guisendmsg(guiwindow, "CLOSE")
RETURN
wm_click_odbc_ok:
IF database == 0 THEN DO
database = connect_to_database()
IF database = 0 THEN RETURN
END
guigetctlplacement("ODBC_Operation",,,,,'test')
IF test == "DISABLE" THEN DO
guisetctlplacement("ODBC_Operation",,,,,"ENABLE")
guisetctlplacement("ODBC_TableName",,,,,"ENABLE")
guisetctlplacement("ODBC_Statement",,,,,"ENABLE")
guisetctlplacement("ODBC_Browse",,,,,"ENABLE")
wm_select_odbc_operation()
END
ELSE perform_sqlcode()
RETURN
wm_select_odbc_operation:
guigetctlvalue("ODBC_Operation")
UPPER odbc_operation
IF odbc_operation == 'DELETE' | odbc_operation == 'UPDATE' | odbc_operation == 'DROP TABLE' | odbc_operation == 'CREATE TABLE' THEN DO
odbc_allcols = 1
guisetctlvalue("ODBC_AllCols")
guisetctlplacement("ODBC_AllCols", , , , , "DISABLE")
guisetctlplacement("ODBC_SelCols", , , , , "DISABLE")
END
ELSE DO
guisetctlplacement("ODBC_AllCols", , , , , "ENABLE")
guisetctlplacement("ODBC_SelCols", , , , , "ENABLE")
END
IF odbc_operation == 'DELETE' | odbc_operation == 'UPDATE' | odbc_operation == 'SELECT' THEN
guisetctlplacement("ODBC_Statement", , , , , "ENABLE")
ELSE
guisetctlplacement("ODBC_Statement", , , , , "DISABLE")
RETURN |
|