You are viewing a plain text version of this content. The canonical link for it is here.
Posted to rivet-dev@tcl.apache.org by da...@apache.org on 2002/04/18 02:56:57 UTC
cvs commit: tcl-rivet/rivet/packages/dio dio.tcl diodisplay.tcl
damonc 02/04/17 17:56:57
Modified: rivet/packages/dio dio.tcl diodisplay.tcl
Log:
Added an interface variable and corresponding function.
Added a sql_limit_syntax command. This is a feeble attempt at trying
to make some of the different SQL servers a little more generic through
a Tcl interface.
Fixed problem in Mysql's connection routine.
Revision Changes Path
1.2 +25 -4 tcl-rivet/rivet/packages/dio/dio.tcl
Index: dio.tcl
===================================================================
RCS file: /home/cvs/tcl-rivet/rivet/packages/dio/dio.tcl,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- dio.tcl 17 Apr 2002 19:09:40 -0000 1.1
+++ dio.tcl 18 Apr 2002 00:56:57 -0000 1.2
@@ -1,8 +1,6 @@
catch {package require Tclx}
package require Itcl
-package provide DIO 1.0
-
namespace eval ::DIO {
proc handle {interface args} {
@@ -321,6 +319,7 @@
##
## Functions to get and set public variables.
##
+ method interface {{string ""}} { configure_variable interface $string }
method errorinfo {{string ""}} { configure_variable errorinfo $string }
method db {{string ""}} { configure_variable db $string }
method table {{string ""}} { configure_variable table $string }
@@ -332,6 +331,7 @@
method host {{string ""}} { configure_variable host $string }
method port {{string ""}} { configure_variable port $string }
+ public variable interface ""
public variable errorinfo ""
public variable db ""
@@ -566,6 +566,12 @@
return [$this string "select last_value from $sequence"]
}
+ method sql_limit_syntax {limit {offset ""}} {
+ set sql " LIMIT $limit"
+ if {![lempty $offset]} { append sql " OFFSET $offset" }
+ return $sql
+ }
+
## If they change DBs, we need to close the connection and re-open it.
public variable db "" {
if {[info exists conn]} {
@@ -574,6 +580,7 @@
}
}
+ public variable interface "Postgresql"
private variable conn
} ; ## ::itcl::class Postgresql
@@ -619,7 +626,11 @@
inherit Database
constructor {args} {eval configure $args} {
- package require Mysqltcl
+ if {[catch {package require Mysqltcl}] \
+ && [catch {package require mysql}]} {
+ return -code error "No MySQL Tcl package available"
+ }
+
eval configure $args
if {[lempty $db]} {
@@ -639,8 +650,8 @@
if {![lempty $user]} { lappend command -user $user }
if {![lempty $pass]} { lappend command -pass $pass }
- if {![lempty $host]} { lappend command -host $host }
if {![lempty $port]} { lappend command -port $port }
+ if {![lempty $host]} { lappend command $host }
if {[catch $command error]} { return -code error $error }
@@ -684,12 +695,20 @@
return $string
}
+ method sql_limit_syntax {limit {offset ""}} {
+ if {[lempty $offset]} {
+ return " LIMIT $limit"
+ }
+ return " LIMIT [expr $offset - 1],$limit"
+ }
+
public variable db "" {
if {[info exists conn]} {
mysqluse $conn $db
}
}
+ public variable interface "Mysql"
private variable conn
} ; ## ::itcl::class Mysql
@@ -712,3 +731,5 @@
} ; ## ::itcl::class MysqlResult
} ; ## namespace eval DIO
+
+package provide DIO 1.0
1.2 +148 -25 tcl-rivet/rivet/packages/dio/diodisplay.tcl
Index: diodisplay.tcl
===================================================================
RCS file: /home/cvs/tcl-rivet/rivet/packages/dio/diodisplay.tcl,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- diodisplay.tcl 17 Apr 2002 19:09:40 -0000 1.1
+++ diodisplay.tcl 18 Apr 2002 00:56:57 -0000 1.2
@@ -18,6 +18,8 @@
if {[lempty $form]} {
set form [namespace which [::form #auto -defaults response]]
}
+
+ set document [env DOCUMENT_NAME]
}
destructor {
@@ -135,16 +137,70 @@
puts "</TR>"
}
+ method page_buttons {} {
+ if {$pagesize <= 0} { return }
+
+ if {![info exists response(page)]} { set response(page) 0 }
+
+ puts "<TABLE WIDTH=\"$rowwidth\">"
+ puts "<TR>"
+ if {$response(page) != 0} {
+ puts "<TD ALIGN=LEFT>"
+ set f [::form #auto -defaults response]
+ $f start
+ $f hidden mode
+ $f hidden query
+ $f hidden sortBy
+ $f hidden page -value [expr $response(page) - 1]
+ $f submit submit -value "Back"
+ $f end
+ puts "</TD>"
+ } else {
+ puts "<TD></TD>"
+ }
+ if {[$DIOResult numrows] >= $pagesize} {
+ puts "<TD ALIGN=RIGHT>"
+ set f [::form #auto -defaults response]
+ $f start
+ $f hidden mode
+ $f hidden query
+ $f hidden sortBy
+ $f hidden page -value [expr $response(page) + 1]
+ $f submit submit -value "Next"
+ $f end
+ puts "</TD>"
+ } else {
+ puts "<TD></TD>"
+ }
+ puts "</TR>"
+ puts "</TABLE>"
+ }
+
+
method rowheader {} {
set fieldList $fields
if {![lempty $rowfields]} { set fieldList $rowfields }
+ puts <P>
+
+ if {$topnav} { page_buttons }
+
puts "<TABLE BORDER WIDTH=\"$rowwidth\">"
puts "<TR>"
foreach field $fieldList {
- puts "<TD><B>"
- if {[catch { puts [$field text] }]} { puts $field }
- puts "</TD>"
+ set text [$field text]
+ ## If sorting is turned off, or this field is not in the
+ ## sortfields, we don't display the sort option.
+ if {!$allowsort || \
+ (![lempty $sortfields] && [lsearch $sortfields $field] < 0)} {
+ set html $text
+ } else {
+ set html {<A HREF="}
+ append html "$document?mode=$response(mode)"
+ append html "&query=$response(query)"
+ append html "&sortBy=$field\">$text</A>"
+ }
+ puts "<TD><B>$html</TD>"
}
puts "<TD><CENTER><B>Functions</TD>"
@@ -153,6 +209,8 @@
method rowfooter {} {
puts "</TABLE>"
+
+ if {$bottomnav} { page_buttons }
}
## Define a new function.
@@ -168,8 +226,8 @@
set class DIODisplayField
if {[info exists data(type)]} {
- if {![lempty [::itcl::find classes *DIODisplayField$data(type)]]} {
- set class DIODisplayField$data(type)
+ if {![lempty [::itcl::find classes *DIODisplayField_$data(type)]]} {
+ set class DIODisplayField_$data(type)
}
}
@@ -216,9 +274,9 @@
}
method DisplayRequest {req} {
- set res [$DIO exec $req]
+ set DIOResult [$DIO exec $req]
- if {[$res numrows] <= 0} {
+ if {[$DIOResult numrows] <= 0} {
puts "Could not find any matching records."
abort_page
return
@@ -226,13 +284,14 @@
rowheader
- $res forall -array a {
+ $DIOResult forall -array a {
showrow a
}
rowfooter
- $res destroy
+ $DIOResult destroy
+ set DIOResult ""
}
method Main {} {
@@ -273,6 +332,23 @@
puts "</TABLE>"
}
+ method sql_order_by_syntax {} {
+ if {[info exists response(sortBy)] && ![lempty $response(sortBy)]} {
+ return " ORDER BY $response(sortBy)"
+ }
+ }
+
+ method sql_limit_syntax {} {
+ if {$pagesize <= 0} { return }
+
+ set offset ""
+ if {[info exists response(page)]} {
+ set offset [expr $response(page) * $pagesize]
+ }
+ return [$DIO sql_limit_syntax $pagesize $offset]
+ }
+
+
method Search {} {
set searchField $FieldTextMap($response(searchBy))
set table [$DIO table]
@@ -280,6 +356,10 @@
set req "SELECT * FROM $table
WHERE $searchField LIKE '[$DIO quote $response(query)]'"
+ append req [sql_order_by_syntax]
+
+ append req [sql_limit_syntax]
+
DisplayRequest $req
}
@@ -288,6 +368,10 @@
set req "SELECT * FROM $table"
+ append req [sql_order_by_syntax]
+
+ append req [sql_limit_syntax]
+
DisplayRequest $req
}
@@ -346,7 +430,7 @@
set_field_values response
get_field_values storeArray
store storeArray
- headers redirect [env DOCUMENT_NAME]
+ headers redirect $document
}
method Delete {} {
@@ -393,7 +477,7 @@
method DoDelete {} {
delete $response(query)
- headers redirect [env DOCUMENT_NAME]
+ headers redirect $document
}
method Details {} {
@@ -409,7 +493,7 @@
}
proc Cancel {} {
- headers redirect [env DOCUMENT_NAME]
+ headers redirect $document
}
###
@@ -447,19 +531,31 @@
}
method DIO {{string ""}} { configure_variable DIO $string }
+ method DIOResult {{string ""}} { configure_variable DIOResult $string }
method errorhandler {{string ""}} {configure_variable errorhandler $string }
+
method title {{string ""}} { configure_variable title $string }
method functions {{string ""}} { configure_variable functions $string }
method pagesize {{string ""}} { configure_variable pagesize $string }
method form {{string ""}} { configure_variable form $string }
method cleanup {{string ""}} { configure_variable cleanup $string }
- method rowfunctions {{string ""}} {configure_variable rowfunctions $string}
- method rowwidth {{string ""}} {configure_variable rowwidth $string}
method confirmdelete {{string ""}} {
configure_variable confirmdelete $string
}
+ method allowsort {{string ""}} { configure_variable allowsort $string }
+ method sortfields {{string ""}} { configure_variable sortfields $string }
+ method background {{string ""}} { configure_variable background $string }
+ method border {{string ""}} { configure_variable border $string }
+ method bordercolor {{string ""}} { configure_variable bordercolor $string }
+ method topnav {{string ""}} { configure_variable topnav $string }
+ method bottomnav {{string ""}} { configure_variable bottomnav $string }
+
+ method rowfunctions {{string ""}} {configure_variable rowfunctions $string}
+ method rowwidth {{string ""}} {configure_variable rowwidth $string}
+
public variable DIO ""
+ public variable DIOResult ""
public variable errorhandler "handle_error"
public variable title ""
@@ -471,16 +567,34 @@
public variable cleanup 1
public variable confirmdelete 1
+ public variable allowsort 1
+ public variable sortfields ""
+ public variable background "white"
+ public variable border 0
+ public variable bordercolor "black"
+ public variable topnav 1
+ public variable bottomnav 1
+
public variable rowfields ""
public variable rowfunctions "Details Edit Delete"
public variable rowwidth "100%"
- public variable response
-
- private variable allfunctions \
- "Search List Add Edit Delete Details Main Save DoDelete Cancel"
+ private variable response
+ private variable document ""
private variable allfields ""
private variable FieldTextMap
+ private variable allfunctions {
+ Search
+ List
+ Add
+ Edit
+ Delete
+ Details
+ Main
+ Save
+ DoDelete
+ Cancel
+ }
} ; ## ::itcl::class DIODisplay
@@ -564,9 +678,9 @@
} ; ## ::itcl::class DIODisplayField
-catch { ::itcl::delete class ::DIODisplayFieldyesno }
+catch { ::itcl::delete class ::DIODisplayField_boolean }
-::itcl::class ::DIODisplayFieldboolean {
+::itcl::class ::DIODisplayField_boolean {
inherit ::DIODisplayField
constructor {args} {eval configure $args} {
@@ -581,12 +695,21 @@
puts "<TR>"
puts "<TD ALIGN=RIGHT><B>$text</B>:</TD>"
puts "<TD>"
- if {[boolean_value]} {
- $form default_value $name $true
+ if {$readonly} {
+ if {[boolean_value]} {
+ puts $true
+ } else {
+ puts $false
+ }
} else {
- $form default_value $name $false
+ if {[boolean_value]} {
+ $form default_value $name $true
+ } else {
+ $form default_value $name $false
+ }
+ eval $form radiobuttons $name \
+ -values [list "$true $false"] $formargs
}
- eval $form radiobuttons $name -values [list "$true $false"] $formargs
puts "</TD>"
puts "</TR>"
}
@@ -621,4 +744,4 @@
set value $false
}
}
-}
+} ; ## ::itcl::class ::DIODisplayField_boolean
---------------------------------------------------------------------
To unsubscribe, e-mail: rivet-dev-unsubscribe@tcl.apache.org
For additional commands, e-mail: rivet-dev-help@tcl.apache.org