This is an automated email from the ASF dual-hosted git repository. mxmanghi pushed a commit to branch tdbc in repository https://gitbox.apache.org/repos/asf/tcl-rivet.git
The following commit(s) were added to refs/heads/tdbc by this push: new eaf250f add formatters class eaf250f is described below commit eaf250fca79b1f24505ebe6661fac55c4e730d99 Author: Massimo Manghi <mxman...@apache.org> AuthorDate: Sat May 18 18:21:59 2024 +0200 add formatters class --- rivet/packages/dio/dio.tcl | 48 +++----------- rivet/packages/dio/dio_Mysql.tcl | 71 +------------------- rivet/packages/dio/formatters.tcl | 136 ++++++++++++++++++++++++++++++++++++++ rivet/packages/dio/pkgIndex.tcl | 13 ++-- 4 files changed, 156 insertions(+), 112 deletions(-) diff --git a/rivet/packages/dio/dio.tcl b/rivet/packages/dio/dio.tcl index 69b7c50..ebc151c 100644 --- a/rivet/packages/dio/dio.tcl +++ b/rivet/packages/dio/dio.tcl @@ -59,9 +59,8 @@ proc handle {interface args} { # quote - given a string, return the same string with any single # quote characters preceded by a backslash # - method quote {string} { - regsub -all {'} $string {\'} string - return $string + method quote {a_string} { + return [DIO::formatters::quote $a_string] } # @@ -603,16 +602,14 @@ proc handle {interface args} { # - public method select_special_field {table_name field_name} { - if {[dict exists $special_fields $table_name $field_name]} { - return [dict get $special_fields $table_name $field_name] - } else { - return "" - } + protected method set_field_formatter {formatter_class} { + $special_field_formatter destroy + + set special_field_formatter [$formatter_class ::DIO::formatters::#auto] } - public method build_special_field {table_name field_name val} { - return [$special_field_formatter build $table_name $field_name $val] + public method build_special_field {table_name field_name val {convert_to {}}} { + return [$special_field_formatter build $table_name $field_name $val $convert_to] } public method register_special_field {table_name field_name type} { @@ -625,7 +622,7 @@ proc handle {interface args} { $this register_special_field $table_name $field_name $type } - public method makeDBFieldValue {table_name field_name val} { + public method makeDBFieldValue {table_name field_name val {convert_to {}}} { return [$this build_special_field $table_name $field_name $val] } @@ -655,7 +652,7 @@ proc handle {interface args} { method host {{string ""}} { return [configure_variable host $string] } method port {{string ""}} { return [configure_variable port $string] } - private variable special_fields_formatter [FieldFormatter #auto] + private variable special_fields_formatter [::DIO::formatters::RootFormatter #auto] public variable interface "" public variable errorinfo "" @@ -893,31 +890,6 @@ proc handle {interface args} { } ; ## ::itcl::class Result -# FieldFormatter -# -# we devolve the role of special field formatter to this -# class. By design this is more sensible with respect to -# the current approach of having such method in subclasses -# because it allows to reuse the functionality of this -# class in other DBMS connector. - -# this class must be subclassed for each database type - -::itcl::class FieldFormatter { - - private variable special_fields [dict create] - - public method register {table_name field_name ftype} { - dict set special_fields $table_name $field_name $ftype - } - - public method build {table_name field_name val} { - return "'[quote $val]'" - } - -} ; ## ::itcl::class FieldFormatter - - } ; ## namespace eval DIO package provide DIO 1.2 diff --git a/rivet/packages/dio/dio_Mysql.tcl b/rivet/packages/dio/dio_Mysql.tcl index e75c264..9bb044d 100644 --- a/rivet/packages/dio/dio_Mysql.tcl +++ b/rivet/packages/dio/dio_Mysql.tcl @@ -39,6 +39,9 @@ namespace eval DIO { } set db $user } + + $this set_field_formatter ::DIO::formatters::Mysql + } destructor { @@ -125,74 +128,6 @@ namespace eval DIO { return $conn } - method build_special_field {table_name field_name val {convert_to {}}} { - - switch [$this select_special_field $table_name $field_name] { - DATE { - set secs [clock scan $val] - set my_val [clock format $secs -format {%Y-%m-%d}] - return "DATE_FORMAT('$my_val','%Y-%m-%d')" - } - DATETIME { - set secs [clock scan $val] - set my_val [clock format $secs -format {%Y-%m-%d %T}] - return "DATE_FORMAT('$my_val','%Y-%m-%d %T')" - } - NOW { - - # we try to be coherent with the original purpose of this method whose - # goal is endow the class with a uniform way to handle timestamps. - # E.g.: Package session expects this case to return a timestamp in seconds - # so that differences with timestamps returned by [clock seconds] - # can be done and session expirations are computed consistently. - # (Bug #53703) - - switch $convert_to { - - SECS { - if {[::string compare $val "now"] == 0} { - -# set secs [clock seconds] -# set my_val [clock format $secs -format {%Y%m%d%H%M%S}] -# return $my_val - - return [clock seconds] - - } else { - return "UNIX_TIMESTAMP($field_name)" - } - } - default { - - if {[::string compare $val, "now"] == 0} { - set secs [clock seconds] - } else { - set secs [clock scan $val] - } - - # this is kind of going back and forth from the same - # format, - - #set my_val [clock format $secs -format {%Y-%m-%d %T}] - return "FROM_UNIXTIME('$secs')" - } - } - } - NULL { - if {[::string toupper $val] == "NULL"} { - return $val - } else { - return "'[quote $val]'" - } - } - default { - # no special code for that type!! - return "'[quote $val]'" - } - } - - } - public variable db "" { if {[info exists conn] && [mysqlping $conn]} { mysqluse $conn $db diff --git a/rivet/packages/dio/formatters.tcl b/rivet/packages/dio/formatters.tcl new file mode 100644 index 0000000..a54c361 --- /dev/null +++ b/rivet/packages/dio/formatters.tcl @@ -0,0 +1,136 @@ +# formatters.tcl -- connector for tdbc, the Tcl database abstraction layer +# +# Copyright 2024 The Apache Software Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +namespace eval DIO::formatters { + + # + # quote - given a string, return the same string with any single + # quote characters preceded by a backslash + # + proc quote {a_string} { + regsub -all {'} $a_string {\'} a_string + return $a_string + } + + # ::itcl::class FieldFormatter + # + # we devolve the role of special field formatter to this + # class. By design this is more sensible with respect to + # the current approach of having such method in subclasses + # because it allows to reuse the functionality of this + # class in other DBMS connector. + + # this class must be subclassed for each database type + + ::itcl::class RootFormatter { + + private variable special_fields [dict create] + + + public method register {table_name field_name ftype} { + dict set special_fields $table_name $field_name $ftype + } + + public method build {table_name field_name val convert_to} { + if {[dict exists $special_fields $table_name $field_name]} { + set field_type [dict get $special_fields $table_name $field_name] + + if {[catch { + set field_value [$this $field_type $table_name $field_name $val $convert_to] + } e einfo]} { + return "'[quote $val]'" + } + + return $field_value + } else { + return "'[quote $val]'" + } + } + + } ; ## ::itcl::class FieldFormatter + + ::itcl::class Mysql { + inherit RootFormatter + + public method DATE {table_name field_name val convert_to} { + set secs [clock scan $val] + set my_val [clock format $secs -format {%Y-%m-%d}] + return "DATE_FORMAT('$my_val','%Y-%m-%d')" + } + + public method DATETIME {table_name field_name val convert_to} { + set secs [clock scan $val] + set my_val [clock format $secs -format {%Y-%m-%d %T}] + return "DATE_FORMAT('$my_val','%Y-%m-%d %T')" + } + + public method NOW {table_name field_name val convert_to} { + + # we try to be coherent with the original purpose of this method whose + # goal is endow the class with a uniform way to handle timestamps. + # E.g.: Package session expects this case to return a timestamp in seconds + # so that differences with timestamps returned by [clock seconds] + # can be done and session expirations are computed consistently. + # (Bug #53703) + + switch $convert_to { + SECS { + if {[::string compare $val "now"] == 0} { + +# set secs [clock seconds] +# set my_val [clock format $secs -format {%Y%m%d%H%M%S}] +# return $my_val + + return [clock seconds] + + } else { + return "UNIX_TIMESTAMP($field_name)" + } + } + default { + + if {[::string compare $val, "now"] == 0} { + set secs [clock seconds] + } else { + set secs [clock scan $val] + } + + # this is kind of going back and forth from the same + # format, + + #set my_val [clock format $secs -format {%Y-%m-%d %T}] + return "FROM_UNIXTIME('$secs')" + } + } + } + + public method NULL {table_name field_name val convert_to} { + if {[::string toupper $val] == "NULL"} { + return $val + } else { + return "'[quote $val]'" + } + } + + } + + + + +} ; ## namespace eval DIO + +package provide dio::formatters 1.0 diff --git a/rivet/packages/dio/pkgIndex.tcl b/rivet/packages/dio/pkgIndex.tcl index 29a98a1..e66fed1 100644 --- a/rivet/packages/dio/pkgIndex.tcl +++ b/rivet/packages/dio/pkgIndex.tcl @@ -8,10 +8,11 @@ # script is sourced, the variable $dir must contain the # full path name of this file's directory. -package ifneeded DIO 1.2 [list source [file join $dir dio.tcl]] -package ifneeded DIODisplay 1.0 [list source [file join $dir diodisplay.tcl]] -package ifneeded dio_Mysql 0.4 [list source [file join $dir dio_Mysql.tcl]] -package ifneeded dio_Oracle 0.3 [list source [file join $dir dio_Oracle.tcl]] +package ifneeded DIO 1.2 [list source [file join $dir dio.tcl]] +package ifneeded DIODisplay 1.0 [list source [file join $dir diodisplay.tcl]] +package ifneeded dio_Mysql 0.4 [list source [file join $dir dio_Mysql.tcl]] +package ifneeded dio_Oracle 0.3 [list source [file join $dir dio_Oracle.tcl]] package ifneeded dio_Postgresql 0.2 [list source [file join $dir dio_Postgresql.tcl]] -package ifneeded dio_Sqlite 0.2 [list source [file join $dir dio_Sqlite.tcl]] -package ifneeded dio_Tdbc 0.2 [list source [file join $dir dio_Tdbc.tcl]] +package ifneeded dio_Sqlite 0.2 [list source [file join $dir dio_Sqlite.tcl]] +package ifneeded dio_Tdbc 0.2 [list source [file join $dir dio_Tdbc.tcl]] +package ifneeded dio::formatters 1.0 [list source [file join $dir formatters.tcl]] --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@tcl.apache.org For additional commands, e-mail: commits-h...@tcl.apache.org