source: tol/tolbase/lib/byswidget/bfunctions/bftoltcl.tcl @ 01994af

1941-docker-image-to-testdevel
Last change on this file since 01994af was b9d4214, checked in by Jaime García <jgarcia@…>, 18 years ago

PostgreSQL types added.

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@131 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 15.9 KB
RevLine 
[6d7779d]1#/////////////////////////////////////////////////////////////////////////////
2# FILE    : bftoltcl.tcl -> BfT
3# PURPOSE : Bayes Function TolTcl. Functions for easy Tol-Tcl comunication
4#/////////////////////////////////////////////////////////////////////////////
5
6variable TclData
7
8#///////////////////////////////////////////////////////////////////////////
9  proc TolDateFormat2TclDateFormat {format} {
10# PURPUSE: Translates TolDateFormat to TclDateFomat
11#
12# PARAMETERS:
13#   format : TOL date format
14# RETURN: Tcl date format
15#
16#///////////////////////////////////////////////////////////////////////////
17  regsub  %u $format "" format
18  regsub  %c $format "" format
19  regsub  %W $format "%A" format
20  regsub  %N $format "%B" format
21  regsub  %w $format "%a" format
22  regsub  %n $format "%b" format
23  regsub  %h $format "%H" format
24  regsub  %i $format "%M" format
25  regsub  %s $format "%S" format
26  return $format
27}
28
29#/////////////////////////////////////////////////////////////////////////////
30  proc GetUniqueName {gra {seed _TOLTK}} {
31#
32# PURPOSE: Gets a unique Name for a given grammar. Optionally user can give
33#          the initial part of the name
34#
35# PARAMETERS:
36#   gra : TOL Grammar
37#   seed: initial part of the name
38#
39#/////////////////////////////////////////////////////////////////////////////
40  set i 1
41  set end 0
42  set name $seed
43  while {[string equal $end 0]} {
44    if [catch {::tol::info variable $gra $name}] {
45      set end 1
46    } else {
47      set name ${seed}_$i
48      incr i
49    }
50  }
51  return $name
52}
53
54#/////////////////////////////////////////////////////////////////////////////
55  proc GetGlobalUniqueName {{seed _TOLTK}} {
56#
57# PURPOSE: Gets a unique Name independent of grammar. Optionally user can give
58#          the initial part of the name
59#
60# PARAMETERS:
61#   seed: initial part of the name
62#
63#/////////////////////////////////////////////////////////////////////////////
64  set i 1
65  set end 0
66  set name $seed
67  # list of grammars
68  set lstGra [::tol::info grammars]
69
70  while {[string equal $end 0]} {
71    set end 1
72    foreach gra $lstGra {
73      set inf [TclInfoVar $gra $name]
74      if {[string length $inf]} {
75        set end 0
76      }
77    }
78    if { !$end } {
79      set name ${seed}_$i
80    }
81    incr i
82  }
83  return $name
84}
85
86#/////////////////////////////////////////////////////////////////////////////
87proc TclInfoVar {gra obj} {
88#
89# PURPOSE: Get the value of variable TOL
90#
91# PARAMETERS:
92#   gra: grammar of variable TOL
93#   obj: name of variable TOL
94#
95# RETURN: output [::tol::info var obg] information in "array get"
96#
97#/////////////////////////////////////////////////////////////////////////////
98  # eval
[f752ef9]99  if {![catch {set lst [::tol::info variable [list $gra $obj]]}]} {
[6d7779d]100    set lstNam [list CONTENT PATH DESC INDEXES ISFILE\
101                     HASSUBSET SUBTYPE STRUCT]
102    for {set i 0} {$i < [llength $lstNam]} {incr i} {
[f752ef9]103      set inf([lindex $lstNam $i]) [lindex $lst [expr $i+2]]
[6d7779d]104    }
105    #parray inf
106    return [array get inf]
107  }
108  # error
109  return {}
110}
111
112
113#/////////////////////////////////////////////////////////////////////////////
114proc TclInfoRef {obj args} {
115#
116# PURPOSE: Get the value os Set TOL
117#
118# PARAMETERS:
119#   obj:  reference of TCL
120#   args: indix of set
121#
122# RETURN: output [::tol::info var ref] information in "array get"
123#
124#/////////////////////////////////////////////////////////////////////////////
125  # eval
[5930b07]126  set cmd "::tol::info variable {$obj $args}"
[bb8cc0a]127  #puts "cmd: $cmd"
[6d7779d]128  if {![catch {set lst [eval $cmd]}]} {
129    set lstNam [list GRAMMAR NAME CONTENT PATH DESC ISFILE\
130                     HASSUBSET SUBTYPE STRUCT]
131    for {set i 0} {$i < [llength $lstNam]} {incr i} {
[bb8cc0a]132      set inf([lindex $lstNam $i]) [lindex $lst [expr $i]]
[6d7779d]133    }
134    #parray inf
135    return [array get inf]
136  }
137  return {}
138}
139
140
141#/////////////////////////////////////////////////////////////////////////////
142proc TclInfoFun {gra fun} {
143#
144# PURPOSE: Get the value of Set TOL
145#
146# PARAMETERS:
147#   gra: grammars of functions TOL
148#   fun: name of function
149#
150# RETURN: output [::tol::info functions gre fun] information in "array get"
151#
152#/////////////////////////////////////////////////////////////////////////////
153  # eval
154  set cmd "::tol::info functions $gra $fun"
155  #puts "cmd: $cmd"
156  if {![catch {set lst [eval $cmd]}]} {
157    set lstNam [list CONTENT PATH DESC]
158    for {set i 0} {$i < [llength $lstNam]} {incr i} {
159      set inf([lindex $lstNam $i]) [lindex $lst $i]
160    }
161    #parray inf
162    return [array get inf]
163  }
164  return {}
165}
166
167#/////////////////////////////////////////////////////////////////////////////
168proc TclInfoInc {fil} {
169#
170# PURPOSE: Get the value of file included in TOL
171#
172# PARAMETERS:
173#   fil: name of file included en TOL
174#
175# RETURN: output [::tol::info included fil] information in "array get"
176#
177#/////////////////////////////////////////////////////////////////////////////
178# eval
179  set cmd "::tol::info included $fil"
180  #puts "cmd: $cmd"
181  if {![catch {set lst [eval $cmd]}]} {
182    set lstNam [list CONTENT PATH DESC HASSUBSET SUBTYPE STRUCT]
183    for {set i 0} {$i < [llength $lstNam]} {incr i} {
184      set inf([lindex $lstNam $i]) [lindex $lst $i]
185    }
186    #parray inf
187    return [array get inf]
188  }
189  return {}
190}
191
192#/////////////////////////////////////////////////////////////////////////////
193proc TclRefToNameTol { ref } {
194#
195# PURPOSE: Obtain the name of an object TOL, of the form, name of the joint
196#          father, if it has, and indices of the set, if it has
197#
198# PARAMETERS:
199#   ref: reference to object TOL
200#
201# RETURN: name in format: namaVariable (variable without father)
202#                         namVarFat[x][y][z] (variable with father)
203#/////////////////////////////////////////////////////////////////////////////
204  Tolcon_Trace "TclRefToNameTol: ref-> $ref"
205  set ref [lindex $ref 0]
206  # the object belongs to a set?
207  if {[expr [llength $ref] == 1]} {
208    set namTol $ref
209  } else  {
210    array set infRef [TclInfoRef [lrange $ref 0 1]]
211    set namTol $infRef(NAME)
212    #Tolcon_Trace "TclRefToNameTol: namTol -> $namTol"
213    # name ?
214    if {[string length $namTol]} {
215      # ckeck parameters
216      if {[expr [llength $ref] > 2]} {
217        set lstInx [lrange $ref 2 end]
218        foreach ind $lstInx {
219          set namTol $namTol\[$ind\]
220        }
221      }
222    } else  {
223      set namTol {}
224    }
225  }
226  Tolcon_Trace "TclRefToNameTol: Nombre de la variable a usar -> $namTol"
227  return $namTol
228}
229
230
231#/////////////////////////////////////////////////////////////////////////////
232proc TclGetVar {gra obj {property CONTENT}} {
233#
234# PURPOSE: Get a property of a variable TOL (default: returns value)
235#
236# PARAMETERS:
237#   gra: grammar of variable TOL
238#   obj: name of variable TOL
239#   pro: property to return
240#
241# RETURN: the property value requested of the TOL object
242#
243#/////////////////////////////////////////////////////////////////////////////
244  array set tmparray [TclInfoVar $gra $obj]
245  set property [string toupper $property]
246  if { [lsearch [array names tmparray] $property] != -1 } {
247    return $tmparray($property)
248  }
249}
250
251
252#/////////////////////////////////////////////////////////////////////////////
253proc TclGetRef {obj args {property CONTENT}} {
254#
255# PURPOSE: Get a property of a TOL reference (default: returns value)
256#
257# PARAMETERS:
258#   obj: name of TOL object
259#   args: list with reference indexes
260#   property: property to return
261#
262# RETURN: the property value requested of the TOL object
263#
264#/////////////////////////////////////////////////////////////////////////////
265  array set tmparray [eval TclInfoRef $obj $args]
266  set property [string toupper $property]
267  if { [lsearch [array names tmparray] $property] != -1 } {
268    return $tmparray($property)
269  }
270}
271
272
273#/////////////////////////////////////////////////////////////////////////////
274proc TclGetFromSet { type set args } { 
275#
276# PURPOSE: returns the value of a reference in a set that is a real date or
277#          serie element
278# ARGUMETS:
279#  type: grammar of the element beeing accesed in the set
280#  set:  name of the tol set
281#  args: names and indices for indexing the set
282#
283#/////////////////////////////////////////////////////////////////////////////
284  switch -regexp type {
285    [Rr]eal { set type Real }
286    [Tt]ext -
287    [Tt]exto { set type Text }
288    [Dd]ate -
289    [Ff]echa { set type Date }
290  }
291
292  set name [GetGlobalUniqueName _TclGetFromSet]
293  set str "$type $name = $set"
294  foreach arg $args {
295    if { ! [regexp "^\[0-9\]*$" $arg] } {
296      set str [concat $str "\[\"$arg\"\]"]
297    } else {
298      set str [concat $str "\[$arg\]"]
299    }
300  }
301
302  ::tol::console eval $str
303  set result [lindex [::tol::info var $type $name] 0]
304  ::tol::console stack release $name
305  return $result
306}
307
308
309#/////////////////////////////////////////////////////////////////////////////
310proc TclLst2TolSet {lst args} {
311#
312# PURPOSE: returns a TOL code that when is evaluated creates a set equivalent
313#          to a tcl list.
314# ARGUMETS:
315#   lst  -> List to convert to TOL set (code)
316#   args  -> Function's arguments as pairs "-key value"
317#    -level  : Number of sublist levels to transform in TOL Set
318#    -grammar: Gramatica de los elementos del ultimo nivel
319#    -struct : Name of struct
320#
321#/////////////////////////////////////////////////////////////////////////////
322  array set opt [list \
323    -level   1 \
324    -grammar Text \
325    -struct  ""
326  ]
327  array set opt $args
328 
329  # name of struct ?
330  if {[string length $opt(-struct)]} {
331    set struct $opt(-struct)
332  } else {
333    switch -- $opt(-grammar) {
334      Real     { set struct SetOfReal }
335      Date     { set struct SetOfDate }
336      default  { set struct SetOfText }
337    }
338  } 
339  if {![llength $lst]} {
340    return "Copy(Empty)"
341  }
342  if [expr $opt(-level) <= 1] {
343    switch -- $opt(-grammar) {
344      Real     { set code "${struct}([join $lst ,])" }
345      Date     { set code "${struct}([join $lst {,}])" }
346      default  { set code "${struct}(\"[join $lst \",\"]\")" }
347    }
348  } else {
349    set sSet ""
350    foreach ite $lst {
351      lappend sSet [TclLst2TolSet $ite \
352        -level [expr $opt(-level)-1] \
353        -grammar $opt(-grammar)      \
354        -struct  $opt(-struct) ]
355    }
356    set code "SetOfSet([join $sSet ,])"
357  }
358  return $code
359}
360
361#/////////////////////////////////////////////////////////////////////////////
362proc TolSet2TclLst {settol} {
363#
364# PURPOSE: returns a TCL code that when is evaluated creates a set equivalent
365#          to a tol set.
366# ARGUMETS:
367#   settol  -> set tol
368#
369#///////////////////////////////////////////////////////////////////////////// 
370  set name [GetGlobalUniqueName __TolSet2TclLst__]
371  set cmd  "Text $name = TolSet2TclLst($settol);"
372  ::tol::console eval $cmd
373  set result [TclGetVar Text $name]
374  ::tol::console stack release $name
375  if {[string length $result]} {
376    eval set result $result
377  }
378  return $result
379}
380
381#/////////////////////////////////////////////////////////////////////////////
382proc TclGetIdFromName { name cjto {refs ""}} {
383#
384# PURPOSE: returns the access index equivalent to name in the set cjto
385# ARGUMETS:
386#  name: a text indexing the set that should be converted to index
387#  cjto: the TOL set
388#  refs: names and indices indexing the set
389#
390#/////////////////////////////////////////////////////////////////////////////
391  if { $refs ne "" } {
392    if { [TclGetRef $cjto $refs GRAMMAR] != "Set" } { return }
393    set n [lindex [TclGetRef $cjto $refs] 0]
394  } else {
395    set n [lindex [TclGetVar Set $cjto] 0]
396  }
397 
398  for {set i 1} { $i <= $n } { incr i } {
399    if { [TclGetRef $cjto [linsert $refs end $i] NAME] == $name } {
400      return $i
401    }
402  }
403}
404
405#/////////////////////////////////////////////////////////////////////////////
406proc TclInfoExists {gra obj} {
407#
408# PURPOSE: Get the exits of variable TOL
409#
410# PARAMETERS:
411#   gra: grammar of variable TOL
412#   obj: name of variable TOL
413#
414# RETURN: 1 -> Exists
415#         0 -> No
416#
417#/////////////////////////////////////////////////////////////////////////////
418  # eval
419  if {![catch {set lst [::tol::info variable $gra]}]} {
420    return [expr [lsearch $lst $obj] >= 0 ? 1 : 0]
421  }
422  # error
423  return 0
424}
425
426
427#/////////////////////////////////////////////////////////////////////////////
428proc TclSetVar {gra obj value} {
429#
430# PURPOSE: Get the exits of variable TOL
431#
432# PARAMETERS:
433#   gra: grammar of variable TOL
434#   obj: name of variable TOL
435#   value: value of variable
436#
437# RETURN: 1 -> Exists
438#         0 -> No
439#
440#/////////////////////////////////////////////////////////////////////////////
441  # exists ?
442  if {[TclInfoExists $gra $obj]} {
443    set cmd "$gra $obj := $value ;"
444  } else {
445    set cmd "$gra $obj = $value ;"
446  }
447  # eval
448  ::tol::console eval $cmd
449}
450
451
452#/////////////////////////////////////////////////////////////////////////////
453proc TclTolEval {cmd} {
454#
455# PURPOSE: Execute sentence TOL
456#
457# PARAMETERS:
458#   cmd: sentenc
459#
460#/////////////////////////////////////////////////////////////////////////////
461  # trace
462  if {([info exists ::project::data(EvalTra)]) && \
463      ($::project::data(EvalTra))} {
464    ::TolConsole::HciWriter "$cmd \n"
465  } 
466  ::tol::console eval $cmd
467}
468
469
470
471#///////////////////////////////////////////////////////////////////////////
472proc TolValidateData {P typFie} {
473#
474# PURPOSE: Verifies that a given value observes the rules of a data type
475#
476# PARAMETERS:
477#   P      : value to verify
478#   typFie : data type
479#
480# RETURN: 1 -> OK, 0 -> NO
481#
482#///////////////////////////////////////////////////////////////////////////
483  set ok 1
484  # si no hay ningun caracter no comprobamos
485  if {![string length $P]} {
486    return $ok
487  }
488  # ralizamos las comprobacines
489  set sepDec {.}
490  set typeField [string tolower [lindex $typFie 0]]
491  set lonField  [string tolower [lindex $typFie 1]]
492 
493  switch $typeField {   
494    "real" {
495      #Datos numéricos de punto flotante entre –3.40E + 38 y 3.40E + 38.
496      # El tamaño de almacenamiento es 4 bytes. En SQL Server, el sinónimo
497      # de real es float(24).
498      set ok [expr {([string match {[-+]} $P] || [string is double $P]) && \
499                     $P>=[expr -3.40 * pow(10,38)] && \
500                     $P<=[expr  3.40 * pow(10,38)]}]
501    }
502   
503    "text" {
504      # Todo tipo de cadenas de caracteres
505     
506      set ok 1
507    }
508   
509    "date" {
510      # Todo tipo de cadenas de caracteres
511     
512      set ok 1
513    }
514   
515    default {puts "TolValidateData:\n\
516                   WARNING: Tipo no reconocido $typeField"}
517  }
518}
519
520  proc TolGetFieldValues { tableName field getConnection} {
521   
522    set txtSql \
523    "select distinct $field \n \
524     from $tableName \n \
525     order by $field"
526    # se ejecuta la query
527    if { [catch { set lstInfoValues [$getConnection $txtSql] } result ] } {         
528        ShowMessage E "$result" "." ;# si ha habido error, se muestra
529    } else { ;# devolvemos el valor
530      # tratamos la lista de valores para no tener { } en valores con espacios
531      set lstInfoVal {}
532      foreach it $lstInfoValues {
533        lappend lstInfoVal [lindex $it 0]
534      }
535      return $lstInfoVal
536      #return $lstInfoValues
537    }   
538  }
539
540#///////////////////////////////////////////////////////////////////////////
541  proc TolGetDelimeterField {type} {
542# PURPUSE: Comprobamos el tipo de un campo e indicamos si sus valores deben
543#          ir precedidos por algun caracter especial. Por ejemplo los textos
544#          van entre comillas, los numeros no cesesitan comillas
545#          TODO: Igual hay que usar las las funciones de TOL
546#
547# RETURN: El texto a concatenar a al valor del campo
548#
549#///////////////////////////////////////////////////////////////////////////
550
551  set delimeter {}
552  set typeField [string tolower [lindex $type 0]]
553  switch $typeField {
554    "real" {
555      # no necesita ningun delimitador
556    }
557    # datetime y smalldatetime
558    "date" {
559      set delimeter {' '}
560      set gestor [string trim [TclGetVar Text DBA] {"}]
[b9d4214]561
[6d7779d]562      switch $gestor {
563        "SqlServer" {
564          set delimeter {{convert(datetime,'} {',121)}}       
565        }
[b9d4214]566        "Oracle" - "Pos" {
[6d7779d]567          set delimeter {{to_date('} {','yyyy/mm/dd')}}       
568        }
569      }
570     }
571    # Cadenas de caracteres
572    "text" {
573      set delimeter {' '}
574    }
575   
576   
577    default {puts "TolGetDelimeterField:\n\
578                   WARNING: Tipo no reconocido $typeField"}
579  }
580  #puts "TolGetDelimeterField: $delimeter"
581  return $delimeter
582}
Note: See TracBrowser for help on using the repository browser.