source: tol/tolbase/lib/byswidget/bodbc/bodbc.tcl @ 6d7779d

1941-docker-image-to-testdevel
Last change on this file since 6d7779d was 6d7779d, checked in by Jorge Pérez <josp.jorge@…>, 18 years ago

initial import

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

  • Property mode set to 100644
File size: 29.8 KB
Line 
1#/////////////////////////////////////////////////////////////////////////////
2# FILE    : bodbc.tcl
3# PURPOSE : This file contains a namespace called BODBC with its
4#           functions. It's for creating a ODBC connect options dialog and
5#           managing its events.
6#/////////////////////////////////////////////////////////////////////////////
7
8# INCLUDES
9package require byscommon
10package require bcombobox
11package require bfunctions
12
13
14#/////////////////////////////////////////////////////////////////////////////
15snit::type bodbcmodule {
16#
17# PURPOSE: BODBCModule widget class definition
18#
19#/////////////////////////////////////////////////////////////////////////////
20#/////////////////////////////////////////////////////////////////////////////
21#
22# options:
23#   module        -> Name of modules to connect
24#   stdtolcon     -> Connection TOL : true
25#   stdtclcon     -> Connection TCL : true
26#   procconnect   -> Function connect : {}
27#   prodisconnect -> Function disconnect : {}
28#   iniconfig     -> Header in .ini file : $modele
29#   savealias     -> Save "alias" in .ini : true
30#   saveuser      -> Save "user" in .ini : true
31#   savepwd       -> Save "pwd" in .ini : false
32#   alias         -> Alias by defect
33#   user          -> User by defect
34#   pwd           -> Password by defect
35#   connection    -> name of connection TCL
36#   autoconnect   -> connects automatically with Data Base
37#   
38#/////////////////////////////////////////////////////////////////////////////
39  delegate option * to hull
40
41  option -stdtolcon     true
42  option -stdtclcon     true
43  option -procconnect   {}
44  option -prodisconnect {}
45  option -iniconfig     {}
46  option -savealias     true
47  option -saveuser      true
48  option -savepwd       false
49  option -alias         {}
50  option -user          {}
51  option -pwd           {}
52  option -gestor        {}
53  option -name          {}
54  option -connection    {}
55  option -autoconnect   false
56
57  #///////////////////////////////////////////////////////////////////////////
58  constructor { args } {
59  #
60  # PURPOSE: beditselector constructor
61  #
62  # PARAMETERS:
63  #   args -> options
64  #
65  #///////////////////////////////////////////////////////////////////////////
66    # Apply all options passed at creation time.
67    $self configurelist $args
68    # load parameters .ini
69    $self _LoadParamIni
70  }
71
72  #///////////////////////////////////////////////////////////////////////////
73  method _LoadParamIni {} {
74  #
75  # PURPOSE: To recover values of the configuration file
76  #
77  #///////////////////////////////////////////////////////////////////////////
78    # load parameters from ini
79    if {[string length [$self cget -iniconfig]]} {
80Tolcon_Trace "_LoadParamIni ..."
81      set key [$self cget -iniconfig]
82      if {[$self cget -savealias]} {
83        $self configure -alias  \
84          [::iniFile::Read $key DataSource [$self cget -alias] ]
85      }
86      if {[$self cget -saveuser]} {
87        $self configure -user \
88          [::iniFile::Read $key Userid     [$self cget -user]]
89      }
90      if {[$self cget -savepwd]} {
91        $self configure -pwd \
92          [::iniFile::Read $key Password   [$self cget -pwd]]
93      }     
94    }
95  }
96
97
98  #///////////////////////////////////////////////////////////////////////////
99  method get { var} {
100  #
101  # PURPOSE: Obtain the value of a variable
102  #
103  # PARAMETERS: var -> name of variable
104  #
105  # RETURN: value of variable
106  #
107  #///////////////////////////////////////////////////////////////////////////
108    return [subst $[subst "$var"]]
109  }
110   
111  #///////////////////////////////////////////////////////////////////////////
112  method set { var value } {
113  #
114  # PURPOSE: Establish the value of a variable
115  #
116  # PARAMETERS: var -> name of variable
117  #             value -> values of variable
118  #
119  #///////////////////////////////////////////////////////////////////////////
120    set [subst $var] $value
121  }
122}
123
124
125#/////////////////////////////////////////////////////////////////////////////
126snit::type bodbc {
127#
128# PURPOSE: BODBC type class definition
129#
130#/////////////////////////////////////////////////////////////////////////////
131#/////////////////////////////////////////////////////////////////////////////
132# variables:
133#   lstConnection -> list of list. Name module  and  variable connection
134#   widget        -> array of name of widget
135#   counter       -> variable accountant
136#
137#/////////////////////////////////////////////////////////////////////////////
138  delegate option * to hull
139
140  variable lstConnection     {}
141  variable widget
142  variable counter 0
143 
144  #///////////////////////////////////////////////////////////////////////////
145  method showdialog { { namMod {} } } {
146  #
147  # PURPUSE: Show the dialog
148  #
149  # PARAMETERS:
150  #   namMod -> if it is specified I name of module, it asks for its information.
151  #             ESLE
152  #             it shows box of selection of module
153  #
154  #///////////////////////////////////////////////////////////////////////////
155    set module {}
156    # one connection ?
157    if {[string length $namMod]} {
158      set module [$self _GetModuleToName $namMod]
159    } else {
160      switch -exact [llength $lstConnection] {
161        0 { return 0 }
162        1 { set module [lindex [lindex $lstConnection 0] 1] }
163      }
164    }
165    # create dialog
166    if {[string length $module]} {
167      $self _CreateDialog $module
168    } else  {
169      $self _CreateDialog
170    }
171  }
172
173  #///////////////////////////////////////////////////////////////////////////
174  method validateconnection { namMod } {
175  #
176  # PURPUSE: it verifies if we are connected to the data base, else, show
177  #          selection dialog   
178  #
179  # PARAMETERS:
180  #   namMod -> if it is specified I name of module, it asks for its information.
181  #
182  # RETURN: 1 -> connection OK
183  #         0 -> error
184  #       
185  #///////////////////////////////////////////////////////////////////////////
186    # get module
187    set module [$self _GetModuleToName $namMod]
188    if {![string length module]} {
189#Tolcon_Trace "ValidateConnection. El modulo $namMod no existe"
190      return 0
191    } 
192   
193    # connection TCL && TCL
194    set status [$self checkconnection $module]
195    # activate connection TOL
196    if {($status) && ([$module cget -stdtolcon])} {
197      set op [GetUniqueName Real _ActTOLTK]
198      set alias  [$module cget -alias]
199      set cmd "Real $op = DBActivate(\"$alias\");"
200      ::tol::console eval $cmd
201      ::tol::console stack release $op
202    }
203    # Auto connect
204    if {[expr !$status && [$module cget -autoconnect]]} {
205      set alias  [$module cget -alias]
206      set user   [$module cget -user]
207      set pwd    [$module cget -pwd]
208      set status [$self _Connect $module $alias $user $pwd]
209    }
210       
211    # Dialogo
212    if {!$status} {
213      set status [$self _CreateDialog $module]
214    }
215    # return
216#Tolcon_Trace "validateconnection $status"
217    return $status
218  }
219
220
221  #///////////////////////////////////////////////////////////////////////////
222  method checkconnection {module} {
223  #
224  # PURPUSE: Verifies if its are connected to the data base in TCL and TOL
225  #
226  # PARAMETERS:
227  #   module: name of connection (bodbcmodule)
228  #
229  # RETURN: 1 -> connection
230  #         0 -> no
231  #       
232  #///////////////////////////////////////////////////////////////////////////
233    # connection TCL
234    set statusTcl [$self checkconnectiontcl $module]
235    # connection TOL
236    set statusTol [$self checkconnectiontol $module]
237#Tolcon_Trace "CheckConnection [expr $statusTcl && $statusTol]"
238    return [expr $statusTcl && $statusTol]
239  }
240
241  #///////////////////////////////////////////////////////////////////////////
242  method checkconnectiontcl {module} {
243  #
244  # PURPUSE: Verifies if is are connected to the data base in TCL
245  #
246  # PARAMETERS:
247  #   module: name of connection (bodbcmodule)
248  #
249  # RETURN: 1 -> connection
250  #         0 -> no
251  #       
252  #///////////////////////////////////////////////////////////////////////////
253#Tolcon_Trace "checkconnectiontcl $module"
254    set result 1
255
256    # connection TCL ?
257    if {[$module cget -stdtclcon]} {
258      if {[string length [$module cget -connection]]} {
259        if {[catch "[$module cget -connection] statement get tables" msg]} {
260          # no connection
261          #Tolcon_Trace "No conectado a TCL"
262          set result 0
263        }
264      } else {
265        #Tolcon_Trace "No conectado a TCL"
266        set result 0
267      }
268    }
269#Tolcon_Trace "checkconnectiontcl $result"
270    return $result
271  }
272
273  #///////////////////////////////////////////////////////////////////////////
274  method checkconnectiontol {module} {
275  #
276  # PURPUSE: Verifies if is are connected to the data base in TOL
277  #
278  # PARAMETERS:
279  #   module: name of connection (bodbcmodule)
280  #
281  # RETURN: 1 -> connection
282  #         0 -> no
283  #       
284  #///////////////////////////////////////////////////////////////////////////
285#Tolcon_Trace "checkconnectiontol ... module: $module"
286    set status 1
287    # connection TOL
288    if {[$module cget -stdtolcon]} {
289      # parameters
290      set alias [$module cget -alias]
291      # connect tol ?
292      set status [GetAliasOpen $alias]
293    }
294#Tolcon_Trace "CheckConnectionTol.end = $status"
295    return $status
296  }
297
298  #///////////////////////////////////////////////////////////////////////////
299  method _ConnectDlg {} {
300  #
301  # PURPUSE: Makes connection a TOL and TCL with dialog
302  #
303  #///////////////////////////////////////////////////////////////////////////
304#Tolcon_Trace "ConnectDlg  ..."
305#    variable widget
306    # wait
307    blt::busy hold $widget(dlg)
308    update
309    # _Connect
310    set mamMod [BfoGetText $widget(module)]
311    # get module
312    set module {}
313    set lst [ltraspose $lstConnection]
314    set inx [lsearch [lindex $lst 0] $mamMod]
315    if {[expr $inx != -1]} {
316      set module [lindex [lindex $lst 1] $inx]
317    }
318#Tolcon_Trace "Nombre del module: mamMod ; Modulo a cargar: $module"
319    set alias [BfoGetText $widget(alias)]
320    set user  [BfoGetText $widget(user)]
321    set pwd   [BfoGetText $widget(pwd)]
322    set status [$self _Connect $module $alias $user $pwd]
323    # OK -> close dialog ?
324    if {$status} {
325      $widget(dlg) enddialog ok
326    }
327    # end wait
328    blt::busy release $widget(dlg)
329  }
330
331  #///////////////////////////////////////////////////////////////////////////
332  method _Connect {module alias user pwd} {
333  #
334  # PURPUSE: Makes connection a TOL and TCL
335  #
336  # PARAMETERS:
337  #   module: name of connection (bodbcmodule)
338  #   alias: name od bridge
339  #   user: name of user
340  #   pwd: password
341  #
342  #///////////////////////////////////////////////////////////////////////////
343    # parameters
344
345Tolcon_Trace "_Connect: module: $module  alias: $alias  user: $user  pwd: $pwd"
346    # connection TCL
347    set statusTcl [$self _ConnectTcl $module $alias $user $pwd]
348    # connection TOL
349    set statusTol [$self _ConnectTol $module $alias $user $pwd]
350#Tolcon_Trace "Connect: [expr $statusTcl && $statusTol]"
351    # .ini ?
352    if {[string length [$module cget -iniconfig]]} {
353#Tolcon_Trace ".ini"
354      if {[$module cget -savealias]} {
355        ::iniFile::Write [$module cget -iniconfig] DataSource $alias
356      }
357      if {[$module cget -saveuser]} {
358        ::iniFile::Write [$module cget -iniconfig] Userid     $user
359      }
360      if {[$module cget -savepwd]} {
361        ::iniFile::Write [$module cget -iniconfig] Password   $pwd
362      }
363    }
364    # update parameters
365    $module configure -alias $alias
366    $module configure -user  $user
367    $module configure -pwd   $pwd
368 
369    set gestor [SqlGetGestor [SqlGetDriver $alias]]
370    $module configure -gestor $gestor
371 
372    # execute proc ?
373    if {[string length [$module cget -procconnect]]} {
374#Tolcon_Trace "execute proc"
375      eval [$module cget -procconnect]
376    }
377
378    # OK ?
379    return [expr $statusTcl && $statusTol]
380  }
381
382  #///////////////////////////////////////////////////////////////////////////
383  method _ConnectTcl {module alias user pwd} {
384  #
385  # PURPUSE: Makes connection a TCL
386  #
387  # PARAMETERS:
388  #   module: name of connection (bodbcmodule)
389  #   alias: name od bridge
390  #   user: name of user
391  #   pwd: password
392  #
393  # RETURN:
394  # RETURN: 1 -> connection
395  #         0 -> no   
396  #
397  #///////////////////////////////////////////////////////////////////////////
398#Tolcon_Trace "_ConnectTcl: module: $module  alias: $alias  user: $user  pwd: $pwd Connect: [$module cget -stdtclcon]"
399    set status 1
400    # connection TCL
401    if {[$module cget -stdtclcon]} {
402      if {[$self checkconnectiontcl $module]} {
403        set status [$self _DisconnectTcl $module]
404      }
405      if {$status} {
406        if {[string length [$module cget -connection]]} {
407          set con [$module cget -connection]
408        } else  {
409          set con [$self _UniqueName [$module cget -name]]
410        }
411        if [catch "database $con $alias $user $pwd" msg] {
412          set status 0
413          $module configure -connection {}
414          MessageDlg ._msgerr -type ok -icon error -message $msg
415        } else  {
416          $module configure -connection $con
417        }
418      }
419#Tolcon_Trace "ConnectTcl = $status"
420    }
421    # return
422    return $status
423  }
424
425  #///////////////////////////////////////////////////////////////////////////
426  method _ConnectTol {module alias user pass} {
427  #
428  # PURPUSE: Makes connection a TOL
429  #
430  # PARAMETERS:
431  #   module: name of connection (bodbcmodule)
432  #   alias: name od bridge
433  #   user: name of user
434  #   pwd: password
435  #
436  # RETURN:
437  # RETURN: 1 -> connection
438  #         0 -> no 
439  #
440  #///////////////////////////////////////////////////////////////////////////
441#Tolcon_Trace "_ConnectTol: module: $module  alias: $alias  user: $user  pwd: $pwd Connect: [$module cget -stdtolcon]"
442    if {[$module cget -stdtolcon]} {
443      if {[$self checkconnectiontol $module]} {
444        # intento activarla
445        #
446        if {[$self _ActivateAlias $alias]} {
447          # el alias ha sido activado correctamente
448          #
449          return 1
450        } else {
451          # Algo ha ido mal, me dijeron que el alias estaba abierto
452          # pero no pude activarlo.
453          #
454          $self _CloseAlias $alias
455        }
456      }
457      return [$self _OpenAlias $alias $user $pass]
458    }
459    return 1
460  }
461
462  method _OpenAlias { alias user pass } {
463    tol::console eval [string map "%A $alias %U $user %P $pass" {
464      Real __dbopen_status__ = DBOpen("%A","%U","%P");
465    }]
466    set status [lindex [tol::info variable Real __dbopen_status__] 0]
467    tol::console stack release __dbopen_status__
468    expr {$status>0.0}
469  }
470
471  method _ActivateAlias { alias } {
472    tol::console eval [string map "%A $alias" {
473      Real __dbact_status__ = DBActivate("%A");
474    }]
475    set status [lindex [tol::info variable Real __dbact_status__] 0]
476    tol::console stack release __dbact_status__
477    expr {$status>0.0}
478  }
479
480  method _CloseAlias { alias } {
481    tol::console eval [string map "%A $alias" {
482      Real __dbclose_status__ = DBClose("%A");
483    }]
484    set status [lindex [tol::info variable Real __dbclose_status__] 0]
485    tol::console stack release __dbclose_status__
486    expr {$status>0.0}
487  }
488
489  #///////////////////////////////////////////////////////////////////////////
490  method _Disconnect { } {
491  #
492  # PURPUSE: Makes disconnect of TOL and TCL
493  #
494  # RETURN: 1 -> disconnect
495  #         0 -> error   
496  #
497  #///////////////////////////////////////////////////////////////////////////
498#Tolcon_Trace "Disconnect ..."
499    # get module
500    set lst [ltraspose $lstConnection]
501    set inx [lsearch [lindex $lst 0] [BfoGetText $widget(module)]]
502    if {[expr $inx != -1]} {
503        set module [lindex [lindex $lst 1] $inx]
504    }
505    # parameters
506    set alias [BfoGetText $widget(alias)]
507    # disconnect TOL
508    set statusTol [$self _DisconnectTol $module $alias]
509
510    # disconnect TCL
511    set statusTcl [$self _DisconnectTcl $module]
512
513    # control status
514    SetControlsStatus $self $widget(dlg) [expr !($statusTol && $statusTcl)]
515
516    # execute proc ?
517    if {[string length [$module cget -prodisconnect]]} {
518#Tolcon_Trace "Disconnect ... proc"
519      eval [$module cget -prodisconnect]
520    }
521    # return
522#Tolcon_Trace "Disconnect = [expr $statusTol && $statusTcl]"
523    return [expr $statusTol && $statusTcl]
524  }
525
526  #///////////////////////////////////////////////////////////////////////////
527  method _DisconnectTol {module alias} {
528  #
529  # PURPUSE: Makes disconnect of TOL
530  #
531  # PARAMETERS:
532  #   module: name of connection (bodbcmodule)
533  #   alias: name od bridge
534  #
535  # RETURN:
536  # RETURN: 1 -> disconnect
537  #         0 -> no 
538  #
539  #///////////////////////////////////////////////////////////////////////////
540#Tolcon_Trace "_DisconnectTol. module:  $module alias: $alias Disconnect: [$module cget -stdtolcon]"
541    set status 1
542    # disconnect TOL
543    if {[$module cget -stdtolcon]} {
544      # connecition TOL ?
545      set connect [GetAliasOpen $alias]
546      if {$connect} {
547        set op [GetUniqueName Real]
548        set cmd "Real $op =  DBClose(\"$alias\");"
549        ::tol::console eval $cmd
550        array set aryInfo [TclInfoVar Real $op]
551        #> 1 -> OK, 0 -> ERROR
552        set status [lindex $aryInfo(CONTENT) 0]
553        unset aryInfo
554        ::tol::console stack release $op
555      }
556#Tolcon_Trace "Disconnect: $status"
557    }
558    # return
559    return $status
560  }
561
562  #///////////////////////////////////////////////////////////////////////////
563  method _DisconnectTcl {module} {
564  #
565  # PURPUSE: Makes disconnect of TCL
566  #
567  # PARAMETERS:
568  #   module: name of connection (bodbcmodule)
569  #   alias: name od bridge
570  #
571  # RETURN:
572  # RETURN: 1 -> disconnect
573  #         0 -> no 
574  #
575  #///////////////////////////////////////////////////////////////////////////
576#Tolcon_Trace "DisconnectTcl .... module: $module  Disconnect: [$module cget -stdtclcon]"
577    set status 1
578    # disconnect TCL
579    if {[$module cget -stdtclcon]} {
580      set con [$module cget -connection]
581      if {[string length $con]} {
582        $con disconnect
583      }
584#Tolcon_Trace "DisconnectTcl = $status"
585    }
586    # return
587    return $status
588  }
589
590  #///////////////////////////////////////////////////////////////////////////
591  method _CreateDialog { {module {}} } {
592  #
593  # PURPOSE: Shows the connect options dialog
594  #
595  # PARAMETERS:
596  #   module: name of connection (bodbcmodule)
597  #
598  #///////////////////////////////////////////////////////////////////////////
599Tolcon_Trace "CreateDialog: module: $module"   
600    set status 0
601    # exists module ?
602    set alias    {}
603    set user     {}
604    set password {}
605    if {[string length $module]} {
606      # parameters
607      set alias    [$module cget -alias]
608      set user     [$module cget -user]
609      set password [$module cget -pwd]
610      # connection
611      set status [$self checkconnection $module]
612    }
613
614    set widget(dlg) [set dlg \
615      [Dialog ._cnnopt -title [mc "Connect options"] \
616               -parent . -default 0 -cancel 1 -modal local]]
617    if {$status} {
618      $dlg add -text [mc "Disconnect"] -image [::Bitmap::get stop] \
619               -compound right -helptext [mc "Disconnect"] \
620               -relief link -command [list $self _Disconnect]
621    } else  {
622      $dlg add -text [mc "Connect"] -image [::Bitmap::get play]\
623               -compound right -helptext [mc "Connect"] \
624               -relief link -command [list $self _ConnectDlg]
625    }
626   
627    $dlg add -text [mc "Cancel"] -image [::Bitmap::get cancel]\
628             -compound right -helptext [mc "Cancel"]\
629             -relief link -command "$dlg enddialog cancel"
630   
631    set w [$dlg getframe]
632    wm resizable $dlg 0 0
633    bind $widget(dlg) <F1> "ShowHelp Tb4Cbd"
634
635    # one module ?
636    Label $w.lbmodule -text "[mc Connection]:"
637    if {[string length $module]} {
638      set widget(module) [Entry $w.ecbmodule -editable false \
639                                             -text [$module cget -name]]
640    } else {
641      set widget(module) \
642        [ComboBox $w.ecbmodule -editable false \
643           -values [lindex [ltraspose $lstConnection] 0] \
644           -modifycmd [list $self _ModifyLstConnection]]
645# BComboBox (desactivado hasta que se realize un nuevo BComboBox)
646#        [bcombobox $w.ecbmodule -editable false \
647                   -values [lindex [ltraspose $lstConnection] 0] \
648                   -bkeypress true \
649                   -modifycmd [list $self _ModifyLstConnection]]
650    }
651    Label $w.lbalias -text "[mc Alias]:"
652    #ComboBox $w.cbalias
653    #  -editable false -textvariable $alias
654    set widget(alias) \
655      [ComboBox $w.cbalias -editable false \
656         -text $alias]
657# BComboBox (desactivado hasta que se realize un nuevo BComboBox)
658#      [bcombobox $w.cbalias -editable false \
659                 -text $alias \
660                 -bkeypress true]
661    Label $w.lbuser -text "[mc User]:"
662    set widget(user) [Entry $w.euser -text $user]
663    Label $w.lbpass -text "[mc Password]:"
664    set widget(pwd) [Entry $w.epass  -show "*" -text $password]
665    set datasources [lsort -index 0 [database datasources]]
666    #  puts "DataSources : $datasources"
667    set all_alias ""
668    foreach al $datasources {
669      lappend all_alias [lindex $al 0]
670    }
671    #  puts "ALL_alias: $all_alias"
672    SetControlsStatus $self $dlg $status
673    $w.cbalias configure -values $all_alias
674
675    grid $w.lbmodule     -row 0 -column 0 -sticky e -padx 4
676    grid $w.ecbmodule    -row 0 -column 1 -sticky w -padx 2
677    grid $w.lbalias      -row 1 -column 0 -sticky e -padx 4
678    grid $w.cbalias      -row 1 -column 1 -sticky w -padx 2
679    grid $w.lbuser       -row 2 -column 0 -sticky e -padx 4
680    grid $w.euser        -row 2 -column 1 -sticky w -padx 2
681    grid $w.lbpass       -row 3 -column 0 -sticky e -padx 4
682    grid $w.epass        -row 3 -column 1 -sticky w -padx 2
683    grid rowconfigure $w 1 -pad 6
684   
685    set result [$dlg draw]
686    # puts "draw == $result"
687   
688
689    destroy $dlg
690Tolcon_Trace "_CreateDialog = $result"
691    return $result
692  }
693
694 
695  #///////////////////////////////////////////////////////////////////////////
696  method _ModifyLstConnection {} {
697  #
698  # PURPOSE: Updates the values after a selection
699  #
700  #///////////////////////////////////////////////////////////////////////////
701    # module
702    set namMod [BfoGetText $widget(module)]
703    # load values
704    set module [$self _GetModuleToName $namMod]
705    if {[string length $module]} {
706      $widget(alias) configure -text [$module cget -alias]
707      $widget(user)  configure -text [$module cget -user]
708      $widget(pwd)   configure -text [$module cget -pwd]
709      # control state
710      SetControlsStatus $self $widget(dlg) [$self checkconnection $module]
711    } else {
712      $widget(alias) configure -text {}
713      $widget(user)  configure -text {}
714      $widget(pwd)   configure -text {}
715    }
716   
717    # set the focus in the first field not filled
718    if {!([$module cget -alias] eq "")} {
719      if {!([$module cget -user] eq "")} {
720        if {!([$module cget -pwd] eq "")} {
721          focus $widget(module)
722        } else {
723          focus $widget(pwd)
724        }         
725      } else {
726        focus $widget(user)
727      }       
728    } else {
729      focus $widget(alias)
730    }
731  }
732
733  #/////////////////////////////////////////////////////////////////////////////
734  proc SetControlsStatus {self dlg status} {
735  #
736  # PURPOSE: Sets controls state to normal/disabled according to connection
737  #          status.
738  #
739  # PARAMETERS:
740  #   self   -> instancia of bodbc
741  #   dlg    -> Instance of ODBCDialog
742  #   status -> connection status (1 connected, 0 disconnected)
743  #
744  #/////////////////////////////////////////////////////////////////////////////
745    set w [$dlg getframe]
746    if {$status} {
747      set newStat disabled
748      # button disconnect
749      $dlg itemconfigure 0 -text [mc "Disconnect"] -image [::Bitmap::get stop]\
750             -compound right -helptext [mc "Disconnect"] \
751             -relief link -command [list $self _Disconnect]
752    } else  {
753      set newStat normal
754      #button disconnect
755      $dlg itemconfigure 0 -text [mc "Connect"] -image [::Bitmap::get play]\
756             -compound right -helptext [mc "Connect"] \
757             -relief link -command [list $self _ConnectDlg]
758     
759    }
760    $w.cbalias configure -state $newStat
761    $w.euser   configure -state $newStat
762    $w.epass   configure -state $newStat
763   
764    if {[$w.epass cget -text] eq ""} {
765      focus $w.epass
766    }
767    # set the focus in the first field not filled
768    if {!([$w.cbalias cget -text] eq "")} {
769      if {!([$w.euser cget -text] eq "")} {
770        if {!([$w.epass cget -text] eq "")} {
771          focus $dlg
772        } else {
773          focus $w.epass
774        }         
775      } else {
776        focus $w.euser
777      }       
778    } else {
779      focus $w.cbalias
780    }
781   
782  }
783
784
785  #///////////////////////////////////////////////////////////////////////////
786  method connection {args} {
787  #
788  # PURPOSE: Create a new module
789  #
790  # PARAMETERS:
791  #   args: parameters of bodbcmodule
792  #
793  #///////////////////////////////////////////////////////////////////////////
794    # parameters default
795    array set opts [list \
796                      -name   {} \
797                   ]
798    array set opts $args   
799    # exists $mod ?
800    if {[string length $opts(-name)]} {
801      if {[info exists lstConnection]} {
802        set lst [lindex [ltraspose $lstConnection] 0]
803        if {[expr [lsearch $lst $opts(-name)] != -1]} {
804          puts "Error. Modulo ya asignado."
805          return {}
806        }
807      }
808    } else  {
809      puts "Error. No se ha especificado un nombre al modulo"
810      return {}
811    }
812    # create a new module
813    set namCon [$self _UniqueName $opts(-name)]
814    set namMod [eval ::bodbcmodule $namCon $args]
815    lappend lstConnection "$opts(-name) $namMod"
816    return $namMod
817  }
818
819  #///////////////////////////////////////////////////////////////////////////
820  method unregister {module} {
821  #
822  # PURPOSE: Eliminated one module register
823  #
824  # PARAMETERS:
825  #   module: name of connection (bodbcmodule)
826  #
827  #///////////////////////////////////////////////////////////////////////////
828    # eliminate register
829    set lst [ltraspose $lstConnection]
830    set inx [lsearch [lindex $lst 1] $module]
831    if {[expr $inx != -1]} {
832      set lstConnection [lreplace $lstConnection $inx $inx]
833      # disconnect
834      if {[$self checkconnectiontcl $module]} {
835        $self _DisconnectTcl $module
836      }
837      if {[$self checkconnectiontol $module]} {
838        $self _DisconnectTol $module [$module cget -alias]
839      }
840      # destroy
841      $module destroy
842    }
843  }
844 
845
846  #///////////////////////////////////////////////////////////////////////////
847  method get { var } {
848  #
849  # PURPOSE: Obtain the value of a variable
850  #
851  # PARAMETERS: var -> name of variable
852  #
853  # RETURN: value of variable
854  #
855  #///////////////////////////////////////////////////////////////////////////
856    return [subst $[subst "$var"]]
857  }
858
859  #///////////////////////////////////////////////////////////////////////////
860  method _UniqueName { namMod } {
861  #
862  # PURPOSE: Obtain a unique name
863  #
864  # RETURN: name of bodbcmodule
865  #
866  #///////////////////////////////////////////////////////////////////////////
867    set lst [lindex [ltraspose $lstConnection] 1]
868    while 1 {
869      incr counter
870      if {$counter > 2147483647} {
871        set counter 0
872      }
873      set candidate db${namMod}${counter}
874      if {[expr [lsearch $lst $candidate] == -1]} {
875        return $candidate
876      }
877    }
878  }
879
880  #///////////////////////////////////////////////////////////////////////////
881  method _GetModuleToName { namMod } {
882  #
883  # PURPOSE: From the name of I modulate, we obtain the name of connection TCL
884  #
885  # PARAMETERS:
886  #   namMod -> name of module
887  #
888  # RETURN: name of connection TCL
889  #
890  #///////////////////////////////////////////////////////////////////////////
891    set module {}
892    if {[string length $namMod]} {
893      set lst [ltraspose $lstConnection]
894      set inx [lsearch [lindex $lst 0] $namMod]
895      if {[expr $inx != -1]} {
896        set module [lindex [lindex $lst 1] $inx]
897      } else {
898        set module {}
899      }
900    }
901    return $module
902  }
903
904  #///////////////////////////////////////////////////////////////////////////
905  proc GetAliasOpen {alias} {
906  #
907  # PURPOSE: It verifies that connection TOL this open one
908  #
909  # PARAMETERS: alias -> name of bridget
910  #
911  # RETURN: 1 -> open
912  #         0 -> no
913  #
914  #///////////////////////////////////////////////////////////////////////////
915
916    set result 0
917    # command
918    set op [GetUniqueName Set]
919    set cmd "Set $op = DBGetOpened(\"\");"
920    # evaluation
921    ::tol::console eval $cmd
922    #Capturar el valor devuelto por TOL
923    array set aryInfo [TclInfoVar Set $op]
924    set lenAry [lindex $aryInfo(CONTENT) 0]
925    set inx 1
926    while {!($result) && ([expr $inx <= $lenAry])} {
927      array set infoReg [TclInfoRef $op $inx 1]
928      set aliasConn [lindex $infoReg(CONTENT) 0]
929      if {[string equal $alias $aliasConn]} {
930        set result 1
931      }
932      incr inx
933    }
934    ::tol::console stack release $op
935    # return
936#Tolcon_Trace "GetAliasOpen $result"
937    return $result
938  }
939}
940
941
942#/////////////////////////////////////////////////////////////////////////////
943proc test_bodbc {} {
944#
945# PURPOSE: Test function of a bodbc
946#
947#/////////////////////////////////////////////////////////////////////////////
948#  package require msgcat
949#  namespace import msgcat::*
950#  ::msgcat::mclocale es
951 
952  package require tclodbc
953  package require BWidget
954  package require BLT
955  Bitmap::use
956  #lappend Bitmap::path ../images
957
958  if {[winfo exists ._cnnopt]} {
959    destroy ._cnnopt
960  }
961
962  set b [bodbc b]
963
964  set telesp [$b connection -name telesp -alias ddw_ld -user dbo -pwd dbo]
965  set sadd   [$b connection -name sadd   -alias prueba -user bayes -pwd bayes]
966
967  set latam   [$b connection -name latam  -alias TfnLatam -user bayes -pwd bayes \
968     -stdtolcon 0 -stdtclcon 1 -procconnect [list puts "Conexion realizada"] \
969     -prodisconnect [list puts "Deconexión realizada"] \
970     -iniconfig     TflLatamODBC \
971     -savealias     1 \
972     -saveuser      1 \
973     -savepwd       0]
974
975  set prbini  [$b connection -name prbini  \
976     -procconnect [list puts "Conexion realizada"] \
977     -prodisconnect [list puts "Deconexión realizada"] \
978     -iniconfig     PruebaIniODBC \
979     -savealias     1 \
980     -saveuser      1 \
981     -savepwd       1]
982
983
984  $b connection -name sadd
985
986  puts "Modulos cargados: [$b get lstConnection]"
987
988  #$b showdialog
989  $b showdialog
990}
991
992
993package provide bodbc 1.0
Note: See TracBrowser for help on using the repository browser.