home *** CD-ROM | disk | FTP | other *** search
- #---------------------------------------------------------------------------
- #
- # Copyright (c) 1995 by Cadre Technologies Inc.
- #
- # This software is furnished under a license and may be used only in
- # accordance with the terms of such license and with the inclusion of
- # the above copyright notice. This software or any other copies thereof
- # may not be provided or otherwise made available to any other person.
- # No title to and ownership of the software is hereby transferred.
- #
- # The information in this software is subject to change without notice
- # and should not be construed as a commitment by Cadre Technologies Inc.
- #
- #---------------------------------------------------------------------------
- #
- # File : @(#)libsybsql.tcl 2.1
- # Original date : Tue Dec 5 10:39:19 MET 1995
- # Description : Sybase specific procedures for gensql
- #
- #---------------------------------------------------------------------------
- #
-
- #
- # FROM tcl/l_cpp/cpp_disp.tcl
- #
-
-
- #
- # FROM tcl/l_cpp/cpp_funcs.tcl
- #
-
-
- proc attrib_init::generate {init init_sect body_sect} {
- ### hack !?
- set data_struct 0
- set attrib [$init attrib]
- if {[$attrib get_obj_type] == "db_data_attrib"} {
- set tgt "data.[[$attrib column] getUniqueName]"
- set data_struct 1
- } else {
- set tgt [$attrib getName]
- }
- if {[type_is_char_scalar [$attrib ooplType]]} {
- $body_sect append \
- "$tgt\[0\] = [$init getName]; $tgt\[1\] = '\\0';\n"
- } else { if [type_is_char_array [$attrib ooplType]] {
- add_[determine_sect_type $body_sect]_inc_name "string" "h"
- $body_sect append "strcpy($tgt, [$init getName]);\n"
- } else {
- if $data_struct {
- $body_sect append "$tgt = [$init getName];\n"
- return
- }
- append_ctor_init $tgt [$init getName]
- } }
- }
-
- proc inher_key_init::generate {init init_sect body_sect} {
- set col [$init key]
- set name [$col getUniqueName]
- if {$name == $TYPE_ID_NM} {
- return
- }
- set class_nm [[$init ooplClass] getName]
-
- if {[type_is_char_scalar $col]} {
- set base_name "[$col getForeignName]\[0\]"
- } else {
- set base_name [$col getForeignName]
- }
-
- $body_sect append \
- "[assign_var data.$name $class_nm::data.$base_name $col $body_sect]\n"
- }
-
- proc assign_var {to from type_obj {sect "src"}} {
- if {[type_is_char_scalar $type_obj]} {
- return "$to\[0\] = $from; $to\[1\] = '\\0';"
- }
- if {[type_is_char_array $type_obj]} {
- add_[determine_sect_type $sect]_inc_name "string" "h"
- return "strcpy($to, $from);"
- }
- return "$to = $from;"
- }
-
- proc base_type::gen_var_decl {type name {col ""}} {
- set type [$type getType3GL]
-
- if {$type == "char"} {
- return "$type $name\[2\]"
- #return "CS_BINARY $name\[2\]"
- }
-
- if [regsub {(var)?char\[} $type "char $name\[" type] {
- regexp {\[(.*)\]$} $type dummy index
- set index [expr {$index + 1}]
- regsub {\[(.*)\]$} $type "\[$index]" type
- return $type
- #set _type {} ; regsub {^char } $type {CS_BINARY } _type ; return $_type
- }
- return "$type $name"
- }
-
-
-
- #
- # FROM tcl/libsql.tcl
- #
-
- #
- # Extra column_selectors
- #
- global column_selector
- set column_selector(ALL_NONSERIAL) {!([$col getTypeStd] == "identity" && [$col get_obj_type] == "column")}
- set column_selector(NONKEYS_NONSERIAL) {[$col getColumnType] != "key" && !([$col getTypeStd] == "identity" && [$col get_obj_type] == "column")}
-
-
- if 1 {
-
- proc sqlpostfix_needed { col } {
- if {[$col getColumnType] == "key"} {
- return "NOT NULL"
- }
-
- if { [$col get_obj_type] == "column" } {
- if {[$col getColumnType] == "key"} {
- return "NOT NULL"
- }
- if {[$col getTypeStd] == "identity"} {
- return "IDENTITY"
- }
- if {[$col isNullable]} {
- return "NULL"
- }
- return "NOT NULL"
- }
-
- # it's an imported column
- #
- set link [$col getImport]
- if {[$link getDelType] == "optional" && [$col getColumnType] == "field"} {
- return "NULL"
- }
-
- #if {[$col getPropertyValue "nullable"] == 1} { return "NULL" }
- return "NULL"
- }
-
- } else { # 0
-
- proc sqlpostfix_needed {col} {
-
- if {[$col get_obj_type] == "column"} {
- if {[$col getTypeStd] == "identity"} {
- return "IDENTITY"
- }
- return [$col getSqlPostfix]
- }
- set link [$col getImport]
- if {[$link getDetailType] == "optional" &&
- [$col getColumnType] == "field"} {
- return "";
- }
- return [$col getSqlPostfix]
- }
-
- } # 0
-
-
- # Return the Sybase dependent string for one assignment
- #
- proc assign_elem {col {prefix1 ""} {postfix1 ""} {prefix2 ""} \
- {postfix2 ""} {sect "src"}} {
- set name [$col getUniqueName]
- if [type_is_char_scalar $col] {
- return "$prefix1$name$postfix1\[0\] = $prefix2$name$postfix2; $prefix1$name$postfix1\[1\] = '\\0';\n"
- }
- if [type_is_char_array $col] {
- add_[determine_sect_type $sect]_inc_name "string" "h"
- return "strcpy($prefix1$name$postfix1, $prefix2$name$postfix2);\n"
- }
- return "$prefix1$name$postfix1 = $prefix2$name$postfix2;\n"
- }
-
-
- proc get_type_3gl {object} {
- set type [$object getType3GL]
- if {$type == "char"} {
- set type "char\[1\]"
- }
- return $type
- }
-
-
- # Generate a 3gl data declaration
- #
- proc gen_data_decl_3gl {section table selector {prefix ""} \
- {newline ""} {postfix ""}} {
-
- set columns [get_col_list $table $selector]
- if { [lempty $columns] } {
- return
- }
- $section pushIndent
- set col [lvarpop columns]
-
- # first col
-
- build_type_3gl [get_type_3gl $col] result_type result_range
- set result_type $prefix$result_type
- set result_range $result_range$postfix
- expand_text $section {\~[create_3gl_var {~$name} {~$type} {~$range}
- ]} \
- name [$col getUniqueName] type $result_type range $result_range
- set newprefix $newline$prefix
-
- foreach col $columns {
-
- # next cols
-
- build_type_3gl [get_type_3gl $col] result_type result_range
- set result_type $newprefix$result_type
- set result_range $result_range$postfix
- expand_text $section {\~[create_3gl_var {~$name} {~$type} {~$range}
- ]} \
- name [$col getUniqueName] type $result_type range $result_range
- }
- $section append $newline
- $section popIndent
- }
-
- # Generate a 3gl data declaration directly (without calling create_3gl_var)
- #
- proc gen_dir_data_decl_3gl {section object selector {separator ", "} \
- {newline ""} {prefix ""} {postfix ""} \
- {prefix2 ""} {flag ""}} {
-
- gen_dir_data_decl_3glc $section [get_col_list $object $selector] \
- $separator $newline $prefix $postfix $prefix2 $flag
- }
-
- # Generate a 3gl data declaration for a link directly (without calling
- # create_3gl_var)
- #
- proc gen_dir_data_decl_3gl_link {section link {separator ", "} \
- {newline ""} {prefix ""} {postfix ""} \
- {prefix2 ""} {flag ""}} {
-
- gen_dir_data_decl_3glc $section [$link columnSet] \
- $separator $newline $prefix $postfix $prefix2 $flag
- }
-
- proc gen_dir_data_decl_3glc {section columns {separator ", "} \
- {newline ""} {prefix ""} {postfix ""} \
- {prefix2 ""} {flag ""}} {
-
- if { [lempty $columns] } {
- return
- }
- set col [lvarpop columns]
- $section pushIndent
- $section append $prefix[mk_3gl_decl $col $postfix $prefix2 $flag]
- set newpf $separator$newline$prefix
- foreach col $columns {
- $section append $newpf[mk_3gl_decl $col $postfix $prefix2 $flag]
- }
- $section popIndent
- $section append $newline
- }
-
- # Make a 3gl variable declaration for Sybase
- # {name, simpel_type} becomes "simpel_type name"
- # {name, char } becomes "char name[2]"
- # {name, char[index]} becomes "char name[index + 1]"
- #
- proc mk_3gl_decl {col {postfix ""} {prefix ""} {flag ""}} {
- set name [$col getUniqueName]
- set type [$col getType3GL]
-
- # if {$flag == "syb"}
- if {$type == "char"} {
- return "$type $prefix$name$postfix\[2\]"
- #return "CS_BINARY $prefix$name$postfix\[2\]"
- }
-
- if [regsub {(var)?char\[} $type "char $prefix$name\[" type] {
- regexp {\[(.*)\]$} $type dummy index
- set index [expr {$index + 1}]
- regsub {\[(.*)\]$} $type "$postfix\[$index]" type
- return $type
- #set _type {} ; regsub {^char } $type {CS_BINARY } _type ; return $_type
- }
- return "$type $prefix$name$postfix"
- }
-
- # Determine if the 3gl type is a character array
- #
- proc type_is_char_array {obj} {
- return [string match {*char\[*} [get_type_3gl $obj]]
- }
-
- # Determine if the 3gl type is a character scalar
- #
- proc type_is_char_scalar {obj} {
- #return [regexp {^[ ]*char[ ]*$} [$obj getType3GL]]
- return [expr {([$obj getType3GL] == "char") ? 1 : 0}]
- }
-
-
- #
- # NEWly added
- #
-
-
- if 0 {
-
- proc getTypeInfo {obj} {
- set type [get_table_type $obj]
- set match {}
- set dbtype {}
- set arg1 {}
- set arg2 {}
- regexp -- {^([^(]*)\(*([^,)]*),*([^)]*).*$} $type match dbtype arg1 arg2
- switch -- "$dbtype" {
- "VARCHAR" {set dbtype CHAR}
- "INTEGER" {set dbtype SMALLINT}
- "DEC" {set dbtype NUMERIC}
- }
- return [list $dbtype $arg1 $arg2 ""]
- }
-
- } # 0
-
-
- proc getTypeInfo {obj} {
- set cpp_type [get_type_3gl $obj]
- set dummy {}
- set index {}
- regexp -- {\[(.*)\]$} $cpp_type dummy index
- switch -glob -- "$cpp_type" {
- "*unsigned*char*" {set dbtype CHAR} # TINYINT?
- "*signed*char*" -
- "*char*" {set dbtype CHAR}
- "*short*" {set dbtype SMALLINT}
- "*long*" -
- "*int*" {set dbtype INT}
- "*float*" {set dbtype REAL}
- "*double*" {set dbtype FLOAT}
- default {set dbtype ILLEGAL}
- }
- set type [get_table_type $obj]
- set match {}
- set arg1 {}
- set arg2 {}
- regexp -- {^([^(]*)\(*([^,)]*),*([^)]*).*$} $type match syb_type arg1 arg2
- switch -glob -- "$syb_type" {
- "*CHAR" {set is_str_type 1}
- default {set is_str_type 0}
- }
- return [list $dbtype $index "" $is_str_type]
- }
-
-
- proc gen_syb_arg_listc {section columns {prefix ""} {postfix ""} \
- {separator ", "} {newline ""}} {
- if { [lempty $columns] } {
- return;
- }
- set newpf $separator$newline$prefix
- set col [lvarpop columns]
- set arg "${prefix}[$col getUniqueName]"
- set i_arg "${postfix}[$col getUniqueName]"
- set typeInfo [getTypeInfo $col]
- set dbtype [lindex $typeInfo 0]
- if {$dbtype == "CHAR"} {
- set len "strlen($arg)"
- } else {
- set len 1
- }
- set type CS_${dbtype}_TYPE
- set isStrType [lindex $typeInfo 3]
- expand_text $section {__str += sybConvert(&~$arg, ~$type, ~$isStrType,
- ~$len, ~$i_arg);
- }
- foreach col $columns {
- set arg "${prefix}[$col getUniqueName]"
- set i_arg "${postfix}[$col getUniqueName]"
- set typeInfo [getTypeInfo $col]
- set dbtype [lindex $typeInfo 0]
- if {$dbtype == "CHAR"} {
- set len "strlen($arg)"
- } else {
- set len 1
- }
- set type CS_${dbtype}_TYPE
- set isStrType [lindex $typeInfo 3]
- expand_text $section {__str += " , ";
- }
- expand_text $section {__str += sybConvert(&~$arg, ~$type, ~$isStrType,
- ~$len, ~$i_arg);
- }
- }
- }
-
- proc gen_syb_arg_list {section table selector {prefix ""} {postfix ""} \
- {separator ", "} {newline ""} {master_table ""}} {
- gen_syb_arg_listc $section [get_col_list $table $selector $master_table] \
- $prefix $postfix $separator $newline
- }
-
-
- # Generate a Sybase local assignment for the given list of columns
- #
- proc gen_syb_lcl_assignc {section columns {prefix ""} {postfix ""} \
- {separator ","} {newline ""}} {
-
- if { [lempty $columns] } {
- return;
- }
- $section pushIndent
- set newpf $separator$newline$prefix
- set col [lvarpop columns]
- expand_text $section {~$prefix~[$col getUniqueName] = ~[$col getUniqueName]~$postfix}
- foreach col $columns {
- expand_text $section {~$newpf~[$col getUniqueName] = ~[$col getUniqueName]~$postfix}
- }
- $section append $newline
- $section popIndent
- }
-
- # Generate a Sybase local assignment
- # The list of columns is determined by the value of the selector
- #
- proc gen_syb_lcl_assign {section table selector {prefix ""} {postfix ""} \
- {separator ","} {newline ""} {master_table ""}} {
-
- gen_syb_lcl_assignc $section [get_col_list $table $selector $master_table] $prefix $postfix $separator $newline
- }
-
-
- # Generate a list of column names for the given link
- # If type is "char" then "...[0]" is created
- #
- proc gen_char_col_listl {section link {prefix ""} {postfix ""} \
- {separator ", "} {newline ""}} {
-
- gen_char_col_listc $section [$link columnSet] $prefix $postfix \
- $separator $newline
- }
-
- # Generate a list of column names for the given list of columns
- # If type is "char" then "...[0]" is created
- #
- proc gen_char_col_listc {section columns {prefix ""} {postfix ""} \
- {separator ", "} {newline ""}} {
-
- if { [lempty $columns] } {
- return;
- }
- $section pushIndent
- set newpf $separator$newline$prefix
- set col [lvarpop columns]
- if [type_is_char_scalar $col] {
- expand_text $section {~$prefix~[$col getUniqueName][0]~$postfix}
- } else {
- expand_text $section {~$prefix~[$col getUniqueName]~$postfix}
- }
- foreach col $columns {
- if [type_is_char_scalar $col] {
- expand_text $section {~$newpf~[$col getUniqueName][0]~$postfix}
- } else {
- expand_text $section {~$newpf~[$col getUniqueName]~$postfix}
- }
- }
- $section append $newline
- $section popIndent
- }
-
- # Generate a list of column names for the given table
- # The list of columns is determined by the value of the selector
- # If type is "char" then "...[0]" is created
- #
- proc gen_char_col_list {section table selector {prefix ""} {postfix ""} \
- {separator ", "} {newline ""} {master_table ""}} {
-
- gen_char_col_listc $section [get_col_list $table $selector $master_table] \
- $prefix $postfix $separator $newline
- }
-
-
- proc gen_trunc_calls {section table selector {prefix ""}} {
-
- set columns [get_col_list $table $selector]
- if {[lempty $columns]} {
- return;
- }
- $section pushIndent
- set newline "\n"
- set newpf $newline
- set col [lvarpop columns]
- set name [$col getUniqueName]
- if {[regexp {char} [$col getType3GL]]} {
- expand_text $section {sybTruncate(~$prefix~$name);}
- }
- while {![lempty $columns]} {
- set col [lvarpop columns]
- set name [$col getUniqueName]
- if {[regexp {char} [$col getType3GL]]} {
- expand_text $section {
- ~${newpf}sybTruncate(~$prefix~$name);}
- }
- }
- #$section append $newline
- $section popIndent
- }
-
- proc strToCharPtr {name} {
- if {$name == "String"} {
- return "as_ptr()"
- }
- return "data()"
- }
-