2016-04-12

TDBC: get resultset columns type

只有在 TDBC::SQLite3 上測試過。目前可以拿到 table columns 的 type 資料,而 resultset 只傳回來 columns name list,利用 table columns 的 type 資料傳回一份對照表。

proc getColumnsType {COLUMNS TABLE_COLUMNS} {
    puts $COLUMNS
    puts $TABLE_COLUMNS
    set result [dict create]
    foreach column $COLUMNS {
        set column_2 [string tolower $column]
        set res [dict get $TABLE_COLUMNS $column_2]
        set type [dict get $res type]
        dict set result $column $type
    }
    
    return $result
}


set table_columns [db columns mylist]

set statement [db prepare {SELECT * FROM mylist}]
set resultset [$statement execute]
set res_columns [$resultset columns]

# test method
puts [getColumnType $res_columns $table_columns]

$statement close


我想我搞懂問題點在哪裡了。如果我操作超過一個表格,那該怎麼做?

如果有時間,我寫一份 TDBC 某個 driver 的研究看看 TDBC 的機制,然後再思考看看該怎麼做。

更新:
看起來我目前不會有操作超過一個表格要 get column type 的情況,所以我想…… 如果有需要再研究好了。


更新:

目前來看,TDBC 實作的策略可以分為二個。

第一個策略,建立 stubs 以後,運用 Tcl 8.6 Tcl_LoadFile,使用 database API
第二個策略,使用原有的 Tcl 套件,然後加上一層 TDBC interface

tdbc::connection

連線到 database, database tables and table columns, primary key..., etc meta data

tdbc::statement

當使用者使用 tdbc::connection prepare 函式,就會建立一個 tdbc::statement 物件。

建立物件時就會使用 tdbc::tokenize (Tcl code) 或者是 Tdbc_TokenizeSql 找出來使用者變數,然後使用 ? (或者是 SQL 語法接受的)取代使用者變數產生 params (有 type 等資料)並且產生 database 可以接受的 SQL 字串

但是這並不是 SQL 語法分析,只是找出 : 或者是 $ 開頭的字串。

tdbc::resultset

當使用者使用 statement object execute method 或者是其它的 command 會觸發 execute method 時就會建立一個 tdbc::resultset 物件。

在建立的時候,就會使用 params 的資料與取得使用者所連結的變數,真正的執行並且取得結果。

So...

如果真的有需要的話,應該是要更改 resultset 相關的部份,另外還要考慮如果不支援這樣操作的資料庫應該要怎麼做。

沒有留言: