Index: openacs-4/packages/xotcl-core/tcl/05-db-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xotcl-core/tcl/05-db-procs.tcl,v diff -u -r1.24 -r1.25 --- openacs-4/packages/xotcl-core/tcl/05-db-procs.tcl 25 May 2007 12:59:41 -0000 1.24 +++ openacs-4/packages/xotcl-core/tcl/05-db-procs.tcl 8 Jun 2007 12:01:00 -0000 1.25 @@ -6,7 +6,7 @@ @cvs-id $Id$ } -namespace eval ::xo::db { +namespace eval ::xo::db::sql { Object call # during load, we do not have "package_plsql_args" available yet, so we do it by hand @@ -190,11 +190,19 @@ } } DbPackage create_all_functions +} + +namespace eval ::xo::db { + # we create for the previously created namespace ::xo::db::sql + # a few methods via the object ::xo::db::sql ::xotcl::Object create sql + if {[db_driverkey ""] eq "postgresql"} { - proc map_sql_datatype {type} {return $type} + sql proc map_datatype {type} {return $type} + sql proc datatype_constraint {type table att} {return ""} + sql proc select { -vars:required -from:required @@ -221,12 +229,22 @@ } } else { ;# Oracle - proc map_sql_datatype {type} { + sql proc map_datatype {type} { switch $type { text {set type varchar2(4000)} + boolean {set type char(1)} } return $type } + sql proc datatype_constraint {type table att} { + set constraint "" + switch $type { + boolean { + set cname [::xo::db::mk_sql_constraint_name $table $att $ck] + set constraint "constraint $cname check ($att in ('t','f'))"} + } + return $constraint + } sql proc select { -vars:required @@ -272,6 +290,10 @@ set since [clock format [clock scan "-$interval"] -format "%Y-%m-%d %T"] return "$var > TO_TIMESTAMP('$since','YYYY-MM-DD HH24:MI:SS')" } +} + + +namespace eval ::xo::db { ::xotcl::Object create require require set postgresql_table_exists {select 1 from pg_tables where tablename = '$name'} @@ -296,19 +318,28 @@ } } + if {[db_driverkey ""] eq "oracle"} { + proc mk_sql_constraint_name {table att suffix} { + set name ${table}_${att}_$suffix + if {[string length $name]>30} { + set sl [string length $suffix] + set name [string range ${table}_${att} 0 [expr {28 - $sl}]]_$suffix + } + return [string toupper $name] + } + } else { + proc mk_sql_constraint_name {table att suffix} { + set name ${table}_${att}_$suffix + return $name + } + } + require proc index {-table -col {-using ""} {-unique false}} { set colpart $col regsub -all ", *" $colpart _ colpart set suffix [expr {$unique ? "un_idx" : "idx"}] set uniquepart [expr {$unique ? "UNIQUE" : ""}] - set name ${table}_${colpart}_$suffix - if {[string length $name]>30} { - if {[db_driverkey ""] eq "oracle"} { - set sl [string length $suffix] - set name [string range ${table}_${colpart} 0 [expr {28 - $sl}]]_$suffix - } - } - if {[db_driverkey ""] eq "oracle"} {set name [string toupper $name]} + set name [::xo::db::mk_sql_constraint_name $table $colpart $suffix] if {![db_0or1row [my qn ""] [subst [my set [db_driverkey ""]_index_exists]]]} { set using [expr {$using ne "" ? "using $using" : ""}] db_dml [my qn create-index-$name] \ Index: openacs-4/packages/xotcl-core/tcl/generic-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xotcl-core/tcl/generic-procs.tcl,v diff -u -r1.69 -r1.70 --- openacs-4/packages/xotcl-core/tcl/generic-procs.tcl 25 May 2007 11:49:13 -0000 1.69 +++ openacs-4/packages/xotcl-core/tcl/generic-procs.tcl 8 Jun 2007 12:01:00 -0000 1.70 @@ -170,7 +170,7 @@ if {![info exists folder_id]} { my instvar folder_id } - ::xo::db::content_folder ${operation}_content_type \ + ::xo::db::sql::content_folder ${operation}_content_type \ -folder_id $folder_id \ -content_type $object_type \ -include_subtypes t @@ -184,13 +184,18 @@ set parameters [list] foreach att [$o children] { $att instvar attribute_name datatype pretty_name sqltype default + set column_spec [::xo::db::sql map_datatype $sqltype] + if {[info exists default]} {append column_spec " default '$default'" } + append column_spec " " \ + [::xo::db::sql datatype_constraint $sqltype [my table_name] $attribute_name] + if {![attribute::exists_p $object_type $attribute_name]} { - ::xo::db::content_type create_attribute \ + ::xo::db::sql::content_type create_attribute \ -content_type $object_type \ -attribute_name $attribute_name \ -datatype $datatype \ -pretty_name $pretty_name \ - -column_spec [::xo::db::map_sql_datatype $sqltype] + -column_spec [string trim $column_spec] } if {![info exists default]} { set default "" @@ -217,7 +222,7 @@ } db_transaction { - ::xo::db::content_type create_type \ + ::xo::db::sql::content_type create_type \ -content_type $object_type \ -supertype $supertype \ -pretty_name $pretty_name \ @@ -241,13 +246,10 @@ my instvar object_type table_name db_transaction { my folder_type unregister - ::xo::db::content_type drop_type \ + ::xo::db::sql::content_type drop_type \ -content_type $object_type \ -drop_children_p t \ -drop_table_p t -# ::xo::db::CONTENT_TYPE DROP_TYPE { -# {content_type $object_type} {drop_children_p t} {drop_table_p t} -# } } } @@ -460,8 +462,7 @@ Delete a content item from the content repository. @param item_id id of the item to be deleted } { - #::xo::db::content_item delete -item_id $item_id - ::xo::db::content_item del -item_id $item_id + ::xo::db::sql::content_item del -item_id $item_id } CrClass instproc object_types { @@ -751,7 +752,7 @@ values (:[join $__atts ,:])" my update_content_length $storage_type $revision_id if {$live_p} { - ::xo::db::content_item set_live_revision \ + ::xo::db::sql::content_item set_live_revision \ -revision_id $revision_id \ -publish_status [my set publish_status] } else { @@ -793,10 +794,9 @@ @param revision_id @param publish_status one of 'live', 'ready' or 'production' } { - ::xo::db::content_item set_live_revision \ + ::xo::db::sql::content_item set_live_revision \ -revision_id $revision_id \ -publish_status $publish_status - #::xo::db::CONTENT_ITEM SET_LIVE_REVISION {revision_id publish_status} } CrItem ad_instproc save_new {-package_id -creation_user_id {-live_p:boolean true}} { @@ -841,8 +841,7 @@ "[my set __autoname_prefix]$revision_id" : $revision_id}] if {$title eq ""} {set title $name} } - #set item_id [::xo::db::CONTENT_ITEM NEW [[self class] set content_item__new_args]] - set item_id [eval ::xo::db::content_item new [[self class] set content_item__new_args]] + set item_id [eval ::xo::db::sql::content_item new [[self class] set content_item__new_args]] if {$storage_type eq "file"} { set text [cr_create_content_file $item_id $revision_id $import_file] } @@ -852,11 +851,9 @@ values (:[join $__atts ,:])" my update_content_length $storage_type $revision_id if {$live_p} { - ::xo::db::content_item set_live_revision \ + ::xo::db::sql::content_item set_live_revision \ -revision_id $revision_id \ -publish_status [my set publish_status] - #set publish_status [my set publish_status] - #::xo::db::CONTENT_ITEM SET_LIVE_REVISION {revision_id publish_status} } } my set revision_id $revision_id