| 1 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | # FILE : sqltable.tcl |
|---|
| 3 | # PURPOSE : This file |
|---|
| 4 | # It's a child of BayesTable so inherites all its functionalities. |
|---|
| 5 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | #@! OBSOLETO |
|---|
| 8 | #@! Usa BLT |
|---|
| 9 | |
|---|
| 10 | package require bcombobox |
|---|
| 11 | |
|---|
| 12 | # load the helper tcl files |
|---|
| 13 | catch { |
|---|
| 14 | source [file join [file join $::toltk_library tclodbc] tclIndex] |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | namespace eval ::SQLTable { |
|---|
| 18 | variable num_SQL 0 |
|---|
| 19 | variable tmpdata |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 23 | proc ::SQLTable::TableBayesSQL { {qry ""} } { |
|---|
| 24 | # |
|---|
| 25 | # PURPOSE: |
|---|
| 26 | # |
|---|
| 27 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 28 | variable num_SQL |
|---|
| 29 | variable tmpdata |
|---|
| 30 | |
|---|
| 31 | incr num_SQL |
|---|
| 32 | # connection |
|---|
| 33 | set tmpdata(bodbc$num_SQL) [$::project::data(bodbc) connection \ |
|---|
| 34 | -name BayesSQL_$num_SQL \ |
|---|
| 35 | -stdtolcon 0 \ |
|---|
| 36 | -stdtclcon 1 \ |
|---|
| 37 | -iniconfig SQLTable \ |
|---|
| 38 | -savealias 1 \ |
|---|
| 39 | -saveuser 1] |
|---|
| 40 | Tolcon_Trace "SQLTable.TableBayesSQL : \ |
|---|
| 41 | tmpdata(bodbc$num_SQL)=$tmpdata(bodbc$num_SQL)" |
|---|
| 42 | # create |
|---|
| 43 | set tl [::project::CreateForm \ |
|---|
| 44 | -width 900 -height 600 \ |
|---|
| 45 | -title [mc "Bayes SQL ($num_SQL)"] \ |
|---|
| 46 | -iniconfig SQLTable \ |
|---|
| 47 | -closecmd [list ::SQLTable::Destroy $tmpdata(bodbc$num_SQL)] \ |
|---|
| 48 | -type SQL -helpkey "Tb4Sql" ] |
|---|
| 49 | set tlf [$tl getframe] |
|---|
| 50 | set this [::SQLTable::Create $tlf.t $qry] |
|---|
| 51 | $tl bind <Control-F4> [list $tl kill] |
|---|
| 52 | pack $tlf.t -fill both -expand yes |
|---|
| 53 | |
|---|
| 54 | # query ? |
|---|
| 55 | if {[string length $qry]} { |
|---|
| 56 | set w [::BayesTable::GetWidget $this txt] |
|---|
| 57 | $w insert end $qry |
|---|
| 58 | ::BayesText::Refresh $w |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | # Fill TreView |
|---|
| 62 | $tmpdata(bodbc$num_SQL) configure \ |
|---|
| 63 | -procconnect [list ::SQLTable::FillTreeView $this] |
|---|
| 64 | |
|---|
| 65 | #???::BayesTable::SetData $this toplevel $tl |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 69 | proc ::SQLTable::Create {path {query ""}} { |
|---|
| 70 | # |
|---|
| 71 | # PURPOSE: |
|---|
| 72 | # |
|---|
| 73 | # PARAMETERS: |
|---|
| 74 | # path -> |
|---|
| 75 | # query -> |
|---|
| 76 | # |
|---|
| 77 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 78 | variable tmpdata |
|---|
| 79 | variable num_SQL |
|---|
| 80 | |
|---|
| 81 | set this [::BayesTable::Create $path \ |
|---|
| 82 | -clientproc "::SQLTable::CreateWidget" -child SQLTable \ |
|---|
| 83 | -OnWriteIni "::SQLTable::WriteIni"] |
|---|
| 84 | ReadIni $this |
|---|
| 85 | Configure $this |
|---|
| 86 | |
|---|
| 87 | ::BayesTable::SetOption $this "db" $tmpdata(bodbc$num_SQL) |
|---|
| 88 | |
|---|
| 89 | #Inicializamos id_after a "" (para que al salir sin ejecutar no de error) |
|---|
| 90 | ::BayesTable::SetData $this id_after "" |
|---|
| 91 | #Añadimos opciones específicas de BayesSQL |
|---|
| 92 | ::BayesTable::AddOptionCmd $this "sepSQL1" "--" |
|---|
| 93 | ::BayesTable::AddOptionCmd $this "sepSQL2" "--" |
|---|
| 94 | ::BayesTable::AddOptionCmd $this "db" \ |
|---|
| 95 | "::SQLTable::OnConnect" \ |
|---|
| 96 | -where tool -icon db -hint [mc "Connect Options"] -notparamthis 1 |
|---|
| 97 | ::BayesTable::AddOptionCmd $this "open" "::SQLTable::OpenQuery"\ |
|---|
| 98 | -where tool -icon bopen -hint [mc "Open query"] |
|---|
| 99 | ::BayesTable::AddOptionCmd $this "save" "::SQLTable::SaveQuery"\ |
|---|
| 100 | -where tool -icon bsave -hint [mc "Save query"] |
|---|
| 101 | ::BayesTable::AddOptionCmd $this "exec" "::SQLTable::ExecQuery"\ |
|---|
| 102 | -where tool -icon play -hint [mc "Execute query"] |
|---|
| 103 | ::BayesTable::AddOptionCmd $this "object" "::SQLTable::ObjectInspector"\ |
|---|
| 104 | -where tool -icon objectexplorer -hint [mc "Object inspector"] |
|---|
| 105 | |
|---|
| 106 | return $this |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 110 | proc ::SQLTable::OnConnect {this} { |
|---|
| 111 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 112 | set module [::BayesTable::GetOption $this "db"] |
|---|
| 113 | Tolcon_Trace "Nombre conexion SQL: $module" |
|---|
| 114 | $::project::data(bodbc) showdialog [$module cget -name] |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 118 | proc ::SQLTable::Destroy {connect} { |
|---|
| 119 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 120 | # unregister connection ODBC |
|---|
| 121 | $::project::data(bodbc) unregister $connect |
|---|
| 122 | return 1 |
|---|
| 123 | } |
|---|
| 124 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 125 | proc ::SQLTable::OnDelete {this} { |
|---|
| 126 | # |
|---|
| 127 | # PURPOSE: |
|---|
| 128 | # |
|---|
| 129 | # PARAMETERS: |
|---|
| 130 | # this -> Instance of BayesTable |
|---|
| 131 | # |
|---|
| 132 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 133 | set w [::BayesTable::GetWidget $this txt] |
|---|
| 134 | after cancel [::BayesTable::GetData $this id_after] |
|---|
| 135 | ::BayesTable::SetOption $this txt,font [$w cget -font] |
|---|
| 136 | destroy [::BayesTable::GetData $this toplevel] |
|---|
| 137 | return 1 |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 141 | proc ::SQLTable::CreateWidget {this} { |
|---|
| 142 | # |
|---|
| 143 | # PURPOSE: |
|---|
| 144 | # |
|---|
| 145 | # PARAMETERS: |
|---|
| 146 | # this -> Instance of BayesTable |
|---|
| 147 | # |
|---|
| 148 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 149 | global toltk_options |
|---|
| 150 | set w [::BayesTable::GetClientArea $this] |
|---|
| 151 | |
|---|
| 152 | set pw [PanedWindow $w.pw1 -pad 0 -side top] |
|---|
| 153 | ::BayesTable::SetWidget $this PW $pw |
|---|
| 154 | set pan0 [$pw add -weight 1] |
|---|
| 155 | set pan1 [$pw add -weight 5] |
|---|
| 156 | |
|---|
| 157 | set pw2 [PanedWindow $pan1.pw -pad 0 -side right] |
|---|
| 158 | set pane1 [$pw2 add -weight 3] |
|---|
| 159 | set pane2 [$pw2 add -weight 1] |
|---|
| 160 | |
|---|
| 161 | #En el primer panel del pan1 creamos la tabla |
|---|
| 162 | ::BayesTable::CreateWidget $this $pane1 |
|---|
| 163 | |
|---|
| 164 | #En el segundo panel del pan1 creamos el text para editar las SQL |
|---|
| 165 | set sw [ScrolledWindow $pane2.sw -ipad 0 -borderwidth 0 -scrollbar vertical ] |
|---|
| 166 | |
|---|
| 167 | #Creamos un text con resaltado SQL |
|---|
| 168 | set w_text [::BayesText::CreateHLText $sw.t sql] |
|---|
| 169 | |
|---|
| 170 | set m [::BayesText::MenuCreate $w_text] |
|---|
| 171 | |
|---|
| 172 | $m insert 0 separator |
|---|
| 173 | $m insert 0 command -label [mc Refresh] -accelerator F5\ |
|---|
| 174 | -command [list ::BayesText::Refresh $w_text] |
|---|
| 175 | $m insert 0 command -label [mc "Execute query"] -accelerator F9\ |
|---|
| 176 | -command [list ::SQLTable::ExecQuery $this] |
|---|
| 177 | |
|---|
| 178 | bind $w_text <F5> [list ::BayesText::Refresh $w_text] |
|---|
| 179 | |
|---|
| 180 | bind $w_text <F9> [list ::SQLTable::ExecQuery $this] |
|---|
| 181 | ::BayesTable::SetWidget $this txt $sw.t |
|---|
| 182 | |
|---|
| 183 | $sw setwidget $sw.t |
|---|
| 184 | pack $sw -fill both -expand yes |
|---|
| 185 | pack $pw2 -fill both -expand yes |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | #En el pan0 creamos un TreeView para visualizar informacion de las tablas |
|---|
| 189 | # treeview y scroll |
|---|
| 190 | set swtv [ScrolledWindow $pan0.sw -ipad 0 -borderwidth 0 -scrollbar both ] |
|---|
| 191 | set tv [blt::treeview $swtv.tv -hideroot no -activeicons ""\ |
|---|
| 192 | -selectborderwidth 0\ |
|---|
| 193 | -highlightthickness 0 -font $toltk_options(fonts,Label)] |
|---|
| 194 | |
|---|
| 195 | bind $tv <Button-1> "::focus $tv" |
|---|
| 196 | ::BayesTable::SetWidget $this tv $tv |
|---|
| 197 | $tv style configure text -bg white |
|---|
| 198 | $tv entry configure root -label [mc "Not connected"] |
|---|
| 199 | $swtv setwidget $swtv.tv |
|---|
| 200 | pack $swtv -fill both -expand yes |
|---|
| 201 | pack $pw -fill both -expand yes |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 207 | proc ::SQLTable::OpenQuery {this} { |
|---|
| 208 | # |
|---|
| 209 | # PURPOSE: Loads a query from a file to the text widget of Bayes SQL |
|---|
| 210 | # |
|---|
| 211 | # PARAMETERS: |
|---|
| 212 | # this -> Instance of BayesTable |
|---|
| 213 | # |
|---|
| 214 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 215 | set txt [::BayesTable::GetWidget $this txt] |
|---|
| 216 | set typelist {{"SQL Files" {".sql"}}} |
|---|
| 217 | set path [tk_getOpenFile -title [mc "Open"] \ |
|---|
| 218 | -parent $txt \ |
|---|
| 219 | -defaultextension .sql -filetypes $typelist] |
|---|
| 220 | if {$path != ""} { |
|---|
| 221 | $txt delete 1.0 end |
|---|
| 222 | if [::BayesText::Open $txt $path] { |
|---|
| 223 | } else { |
|---|
| 224 | } |
|---|
| 225 | tk::TextSetCursor $txt 1.0 |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 230 | proc ::SQLTable::SaveQuery {this} { |
|---|
| 231 | # |
|---|
| 232 | # PURPOSE: Saves text of text widget of Bayes SQL. |
|---|
| 233 | # |
|---|
| 234 | # PARAMETERS: |
|---|
| 235 | # this -> Instance of BayesTable |
|---|
| 236 | # |
|---|
| 237 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 238 | set txt [::BayesTable::GetWidget $this txt] |
|---|
| 239 | |
|---|
| 240 | set typelist {{"SQL Files" {".sql"}}} |
|---|
| 241 | set path [tk_getSaveFile -title [mc "Save as"] -parent $txt \ |
|---|
| 242 | -defaultextension .sql -filetypes $typelist] |
|---|
| 243 | if {![string equal $path ""] } { |
|---|
| 244 | set ok [::BayesText::Save $txt $path] |
|---|
| 245 | } else { |
|---|
| 246 | set ok 0 |
|---|
| 247 | } |
|---|
| 248 | return $ok |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 252 | proc ::SQLTable::ExecQuery {this} { |
|---|
| 253 | # |
|---|
| 254 | # PURPOSE: Executes a query of BayesSQL |
|---|
| 255 | # |
|---|
| 256 | # PARAMETERS: |
|---|
| 257 | # this -> Instance of BayesTable |
|---|
| 258 | # |
|---|
| 259 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 260 | set t [::BayesTable::GetTkTable $this] |
|---|
| 261 | |
|---|
| 262 | set w [::BayesTable::GetWidget $this txt] |
|---|
| 263 | set txt [::BayesText::GetSelectionOrAll $w] |
|---|
| 264 | # unselected table |
|---|
| 265 | $t selection clear all |
|---|
| 266 | |
|---|
| 267 | set dbConnect [::BayesTable::GetOption $this db] |
|---|
| 268 | set status [$::project::data(bodbc) checkconnectiontcl $dbConnect] |
|---|
| 269 | if {!$status} { |
|---|
| 270 | tk_messageBox -type ok -icon warning -title [mc BayesSQL] -parent $t \ |
|---|
| 271 | -message [mc "You must set connection first"] |
|---|
| 272 | ::SQLTable::OnConnect $this |
|---|
| 273 | return |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | [$dbConnect cget -connection] statement stmt $txt |
|---|
| 277 | stmt execute |
|---|
| 278 | |
|---|
| 279 | set rowCount [stmt rowcount] |
|---|
| 280 | puts "rowCount=$rowCount" |
|---|
| 281 | if {$rowCount == -1} { |
|---|
| 282 | ::BayesTable::WriteInfo $this "Consulta ejecutada correctamente" |
|---|
| 283 | } else { |
|---|
| 284 | ::BayesTable::WriteInfo $this "Completado proceso. $rowCount filas afectadas" |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | # Borrar mensaje.Cancelamos el comando anterior (si lo hubiese) |
|---|
| 288 | after cancel [::BayesTable::GetData $this id_after] |
|---|
| 289 | set id_after [after 5000 [list ::BayesTable::WriteInfo $this ""]] |
|---|
| 290 | ::BayesTable::SetData $this id_after $id_after |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | $t configure -state normal |
|---|
| 294 | ::BayesTable::UnPackTkTable $this |
|---|
| 295 | tclodbc::TkTableInit $t stmt |
|---|
| 296 | # Si la instruccion sql es select se llena la tabla |
|---|
| 297 | if {$rowCount == -1} { |
|---|
| 298 | tclodbc::TkTableRead $t stmt |
|---|
| 299 | } |
|---|
| 300 | # TODO: Se puede hacer q se muestre un mensaje mas visible que la |
|---|
| 301 | # barra de estado para insert,delete, update |
|---|
| 302 | ::BayesTable::AdjustTable $this |
|---|
| 303 | ::BayesTable::PackTkTable $this |
|---|
| 304 | $t configure -state disabled |
|---|
| 305 | ::BayesTable::UpdateInfoRows $this |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 309 | proc ::SQLTable::ObjectInspector {this} { |
|---|
| 310 | # |
|---|
| 311 | # PURPOSE: |
|---|
| 312 | # |
|---|
| 313 | # PARAMETERS: |
|---|
| 314 | # |
|---|
| 315 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 316 | set pw [::BayesTable::GetWidget $this PW] |
|---|
| 317 | |
|---|
| 318 | set dbConnect [::BayesTable::GetOption $this db] |
|---|
| 319 | set status [$::project::data(bodbc) checkconnectiontcl $dbConnect] |
|---|
| 320 | |
|---|
| 321 | if {$status} { |
|---|
| 322 | ::PanedWindow::MoveSash $pw 1 x middle |
|---|
| 323 | } else { |
|---|
| 324 | ::PanedWindow::MoveSash $pw 1 x min |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 329 | proc ::SQLTable::ConnectOptions_ {this} { |
|---|
| 330 | # |
|---|
| 331 | # PURPOSE: |
|---|
| 332 | # |
|---|
| 333 | # PARAMETERS: |
|---|
| 334 | # this -> Instance of BayesTable |
|---|
| 335 | # |
|---|
| 336 | # TODO: ESTE PROCEDIMIENTO NO SE DEBE USAR, UTILIZAR EL BODBC |
|---|
| 337 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 338 | variable tmpdata |
|---|
| 339 | |
|---|
| 340 | set table [::BayesTable::GetTkTable $this] |
|---|
| 341 | set dlg [Dialog ._cnnopt -title [mc "Connect options"] \ |
|---|
| 342 | -parent $table -default 0 -cancel 1 -modal local] |
|---|
| 343 | |
|---|
| 344 | $dlg add -image [::Bitmap::get accept] -helptext [mc "Accept"] \ |
|---|
| 345 | -relief link -command [list ::SQLTable::OnConnectOk $this] |
|---|
| 346 | $dlg add -image [::Bitmap::get cancelx] -helptext "Cancelar" -relief link\ |
|---|
| 347 | -command "$dlg enddialog cancel" |
|---|
| 348 | |
|---|
| 349 | set w [$dlg getframe] |
|---|
| 350 | wm resizable $dlg 0 0 |
|---|
| 351 | catch { |
|---|
| 352 | array unset tmpdata |
|---|
| 353 | } |
|---|
| 354 | set tmpdata(alias) [::BayesTable::GetOption $this alias] |
|---|
| 355 | set tmpdata(user) [::BayesTable::GetOption $this user] |
|---|
| 356 | set tmpdata(password) [::BayesTable::GetOption $this password] |
|---|
| 357 | set tmpdata(dlg) $dlg |
|---|
| 358 | Label $w.lbalias -text "[mc Alias]:" |
|---|
| 359 | |
|---|
| 360 | #bcombobox $w.cbalias -editable false \ |
|---|
| 361 | # -textvariable ::SQLTable::tmpdata(alias) \ |
|---|
| 362 | # -bkeypress true |
|---|
| 363 | ComboBox $w.cbalias -editable false -textvariable ::SQLTable::tmpdata(alias) |
|---|
| 364 | set tmpdata(cbalias) $w.cbalias |
|---|
| 365 | Label $w.lbuser -text "[mc User]:" |
|---|
| 366 | Entry $w.euser -textvariable ::SQLTable::tmpdata(user) |
|---|
| 367 | Label $w.lbpass -text "[mc Password]:" |
|---|
| 368 | Entry $w.epass -show "*" -textvariable ::SQLTable::tmpdata(password) |
|---|
| 369 | |
|---|
| 370 | set tmpdata(datasources) [lsort -index 0 [database datasources]] |
|---|
| 371 | #Tolcon_Trace "DataSources : $tmpdata(datasources)" |
|---|
| 372 | set all_alias "" |
|---|
| 373 | foreach alias $tmpdata(datasources) { |
|---|
| 374 | lappend all_alias [lindex $alias 0] |
|---|
| 375 | } |
|---|
| 376 | $w.cbalias configure -values $all_alias |
|---|
| 377 | grid $w.lbalias -row 0 -column 0 -sticky e -padx 4 |
|---|
| 378 | grid $w.cbalias -row 0 -column 1 -sticky w -padx 2 |
|---|
| 379 | grid $w.lbuser -row 1 -column 0 -sticky e -padx 4 |
|---|
| 380 | grid $w.euser -row 1 -column 1 -sticky w -padx 2 |
|---|
| 381 | grid $w.lbpass -row 2 -column 0 -sticky e -padx 4 |
|---|
| 382 | grid $w.epass -row 2 -column 1 -sticky w -padx 2 |
|---|
| 383 | grid rowconfigure $w 1 -pad 6 |
|---|
| 384 | |
|---|
| 385 | set result [$dlg draw] |
|---|
| 386 | #Tolcon_Trace "draw == $result" |
|---|
| 387 | if { [string equal $result "ok"] } { |
|---|
| 388 | ::BayesTable::SetOption $this alias $tmpdata(alias) |
|---|
| 389 | ::BayesTable::SetOption $this user $tmpdata(user) |
|---|
| 390 | ::BayesTable::SetOption $this password $tmpdata(password) |
|---|
| 391 | array unset tmpdata |
|---|
| 392 | } |
|---|
| 393 | destroy $dlg |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 397 | proc ::SQLTable::OnConnectOk {this} { |
|---|
| 398 | # |
|---|
| 399 | # PURPOSE: |
|---|
| 400 | # |
|---|
| 401 | # PARAMETERS: |
|---|
| 402 | # this -> Instance of BayesTable |
|---|
| 403 | # |
|---|
| 404 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 405 | variable tmpdata |
|---|
| 406 | |
|---|
| 407 | blt::busy hold $tmpdata(dlg) |
|---|
| 408 | update |
|---|
| 409 | if [catch "database dbtest $tmpdata(alias) $tmpdata(user) $tmpdata(password)" msg] { |
|---|
| 410 | MessageDlg ._msgerr -type ok -icon error -message $msg |
|---|
| 411 | } else { |
|---|
| 412 | dbtest disconnect |
|---|
| 413 | set idx [$tmpdata(cbalias) getvalue] |
|---|
| 414 | #Tolcon_Trace "idx= $idx, [lindex $tmpdata(datasources) $idx]" |
|---|
| 415 | set issql [string equal "SQL Server" [lindex [lindex $tmpdata(datasources) $idx] 1]] |
|---|
| 416 | database connect db $tmpdata(alias) $tmpdata(user) $tmpdata(password) |
|---|
| 417 | ::BayesTable::SetOption $this "db" db |
|---|
| 418 | $tmpdata(dlg) enddialog ok |
|---|
| 419 | #Llenar treeview |
|---|
| 420 | FillTreeView $this |
|---|
| 421 | } |
|---|
| 422 | blt::busy release $tmpdata(dlg) |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 426 | proc ::SQLTable::FillTreeView {this} { |
|---|
| 427 | # |
|---|
| 428 | # PURPOSE: |
|---|
| 429 | # |
|---|
| 430 | # PARAMETERS: |
|---|
| 431 | # this -> Instance of BayesTable |
|---|
| 432 | # |
|---|
| 433 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 434 | variable tmpdata |
|---|
| 435 | set tv [::BayesTable::GetWidget $this tv] |
|---|
| 436 | set dbConnect [::BayesTable::GetOption $this db] |
|---|
| 437 | |
|---|
| 438 | set img [::Bitmap::get bd] |
|---|
| 439 | $tv delete root |
|---|
| 440 | $tv entry configure root -label [$dbConnect cget -alias] -icons [list $img $img] |
|---|
| 441 | |
|---|
| 442 | set idxUsrTbl [$tv insert -at root end "User_tables" -label [mc "User Tables"]] |
|---|
| 443 | set idxSysTbl [$tv insert -at root end "System_tables" -label [mc "System Tables"]] |
|---|
| 444 | set idxView [$tv insert -at root end "Views" -label [mc "Views"]] |
|---|
| 445 | set idxSynonym [$tv insert -at root end "Synonyms" -label [mc "Synonyms"]] |
|---|
| 446 | set idxSequence [$tv insert -at root end "Sequence" -label [mc "Sequence"]] |
|---|
| 447 | #set idxOther [$tv insert -at root end "Other" -label [mc "Other"]] |
|---|
| 448 | |
|---|
| 449 | set imgTbl [::Bitmap::get table] |
|---|
| 450 | set imgView [::Bitmap::get view] |
|---|
| 451 | set lstTables [[$dbConnect cget -connection] tables] |
|---|
| 452 | set nTable 0 |
|---|
| 453 | foreach tb_info $lstTables { |
|---|
| 454 | set tb "[lindex $tb_info 1].[lindex $tb_info 2]" |
|---|
| 455 | switch [lindex $tb_info 3] { |
|---|
| 456 | "TABLE" { |
|---|
| 457 | set idx [$tv insert -at $idxUsrTbl end $nTable] |
|---|
| 458 | $tv entry configure $idx -label $tb -icons [list $imgTbl $imgTbl]\ |
|---|
| 459 | -opencommand "::SQLTable::FillTable $this %#" -button yes |
|---|
| 460 | } |
|---|
| 461 | "SYSTEM TABLE" { |
|---|
| 462 | set idx [$tv insert -at $idxSysTbl end $nTable] |
|---|
| 463 | $tv entry configure $idx -label $tb -icons [list $imgTbl $imgTbl]\ |
|---|
| 464 | -opencommand "::SQLTable::FillTable $this %#" -button yes |
|---|
| 465 | } |
|---|
| 466 | "VIEW" { |
|---|
| 467 | set idx [$tv insert -at $idxView end $nTable] |
|---|
| 468 | $tv entry configure $idx -label $tb -icons [list $imgView $imgView]\ |
|---|
| 469 | -opencommand "::SQLTable::FillTable $this %#" -button yes |
|---|
| 470 | } |
|---|
| 471 | "SYNONYM" { |
|---|
| 472 | set idx [$tv insert -at $idxSynonym end $nTable] |
|---|
| 473 | $tv entry configure $idx -label $tb -icons [list $imgTbl $imgTbl]\ |
|---|
| 474 | -opencommand "::SQLTable::FillTable $this %#" -button yes |
|---|
| 475 | } |
|---|
| 476 | "SEQUENCE" { |
|---|
| 477 | set idx [$tv insert -at $idxSequence end $nTable] |
|---|
| 478 | $tv entry configure $idx -label $tb -icons [list $imgTbl $imgTbl]\ |
|---|
| 479 | -opencommand "::SQLTable::FillTable $this %#" -button yes |
|---|
| 480 | } |
|---|
| 481 | default { |
|---|
| 482 | # set idx [$tv insert -at $idxOther end $nTable] |
|---|
| 483 | # $tv entry configure $idx -label $tb -icons [list $imgTbl $imgTbl]\ |
|---|
| 484 | # -opencommand "::SQLTable::FillTable $this %#" -button yes |
|---|
| 485 | } |
|---|
| 486 | } |
|---|
| 487 | incr nTable |
|---|
| 488 | } |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 492 | proc ::SQLTable::GetColumnTxt {colinfo} { |
|---|
| 493 | # |
|---|
| 494 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 495 | |
|---|
| 496 | # TODO: Repasar la parte que une nombres y valores en array y llevarlo a un |
|---|
| 497 | # procedimiento comun (que devuelva lista [array get $inf]) |
|---|
| 498 | # Hacerlo tb para indices y tablas |
|---|
| 499 | set lst_names [list TABLE_QUALIFIER\ |
|---|
| 500 | TABLE_OWNER \ |
|---|
| 501 | TABLE_NAME\ |
|---|
| 502 | COLUMN_NAME\ |
|---|
| 503 | DATA_TYPE\ |
|---|
| 504 | TYPE_NAME\ |
|---|
| 505 | PRECISION \ |
|---|
| 506 | LENGTH\ |
|---|
| 507 | SCALE\ |
|---|
| 508 | RADIX\ |
|---|
| 509 | NULLABLE\ |
|---|
| 510 | REMARKS] |
|---|
| 511 | #En colinfo hay mas informacion q estos 12 campos (al menos para SQLServer) |
|---|
| 512 | #puts "ln=[llength $lst_names], lv=[llength $colinfo]" |
|---|
| 513 | for {set i 0} {$i < [llength $lst_names]} {incr i} { |
|---|
| 514 | set inf([lindex $lst_names $i]) [lindex $colinfo $i] |
|---|
| 515 | } |
|---|
| 516 | #parray inf |
|---|
| 517 | |
|---|
| 518 | switch -- $inf(DATA_TYPE) { |
|---|
| 519 | 2 - 3 - 4 - 5 - 6 - 7 - 8 - {-5} - {-6} { |
|---|
| 520 | # numerical datatypes |
|---|
| 521 | set type_name $inf(TYPE_NAME) |
|---|
| 522 | } |
|---|
| 523 | 1 - 12 - {-1} { |
|---|
| 524 | # string types |
|---|
| 525 | set type_name "$inf(TYPE_NAME) ($inf(LENGTH))" |
|---|
| 526 | } |
|---|
| 527 | default { |
|---|
| 528 | # logical, date and time types |
|---|
| 529 | set type_name $inf(TYPE_NAME) |
|---|
| 530 | } |
|---|
| 531 | } |
|---|
| 532 | if $inf(NULLABLE) { |
|---|
| 533 | set null "Null" |
|---|
| 534 | } else { |
|---|
| 535 | set null "Not Null" |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | return "$inf(COLUMN_NAME) ($type_name,$null)" |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 542 | proc ::SQLTable::FillTable {this index} { |
|---|
| 543 | # |
|---|
| 544 | # PURPOSE: Script called each time a node of a table in treeview is opened. |
|---|
| 545 | # |
|---|
| 546 | # PARAMETERS: |
|---|
| 547 | # this : Instance of BayesTable |
|---|
| 548 | # index : The id of the node opened |
|---|
| 549 | # |
|---|
| 550 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 551 | variable tmpdata |
|---|
| 552 | set tv [::BayesTable::GetWidget $this tv] |
|---|
| 553 | set dbConnect [::BayesTable::GetOption $this db] |
|---|
| 554 | |
|---|
| 555 | set path [$tv get -full $index] |
|---|
| 556 | puts "FillTable. index=$index, tv=$tv" |
|---|
| 557 | set imgCol [::Bitmap::get column] |
|---|
| 558 | set imgIdx [::Bitmap::get index] |
|---|
| 559 | |
|---|
| 560 | if ![llength [$tv entry children $index]] { |
|---|
| 561 | set tbl [$tv entry cget $index -label] |
|---|
| 562 | set idx $index |
|---|
| 563 | set nTable [lindex $path end] |
|---|
| 564 | set tblName [lindex [split $tbl .] end] |
|---|
| 565 | if ![catch {set lstColumns [[$dbConnect cget -connection] columns $tblName]}] { |
|---|
| 566 | set idxColumns [$tv insert -at $idx end c$nTable] |
|---|
| 567 | $tv entry configure $idxColumns -label [mc "Columns"] |
|---|
| 568 | set nCol 0 |
|---|
| 569 | foreach col_info $lstColumns { |
|---|
| 570 | |
|---|
| 571 | set idxCol [$tv insert -at $idxColumns end c_${nTable}_$nCol] |
|---|
| 572 | set col_txt [GetColumnTxt $col_info] |
|---|
| 573 | $tv entry configure $idxCol -label $col_txt -icons [list $imgCol $imgCol] |
|---|
| 574 | incr nCol |
|---|
| 575 | } |
|---|
| 576 | } |
|---|
| 577 | if ![catch {set lstIndexes [[$dbConnection cget -connection] indexes $tblName]}] { |
|---|
| 578 | set idxIndexes [$tv insert -at $idx end i$nTable] |
|---|
| 579 | $tv entry configure $idxIndexes -label [mc "Indexes"] |
|---|
| 580 | set nIdx 0 |
|---|
| 581 | foreach idx_info $lstIndexes { |
|---|
| 582 | set idxIdx [$tv insert -at $idxIndexes end i_${nTable}_$nIdx] |
|---|
| 583 | set idx_txt [lindex $idx_info 4] |
|---|
| 584 | $tv entry configure $idxIdx -label $idx_txt -icons [list $imgIdx $imgIdx] |
|---|
| 585 | incr nIdx |
|---|
| 586 | } |
|---|
| 587 | } |
|---|
| 588 | } |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | |
|---|
| 592 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 593 | # INIFILE |
|---|
| 594 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 595 | |
|---|
| 596 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 597 | proc ::SQLTable::ReadIni {this} { |
|---|
| 598 | # |
|---|
| 599 | # PURPOSE: Reads specific options from .ini file. First of all, reads options |
|---|
| 600 | # from BayesTable. |
|---|
| 601 | # |
|---|
| 602 | # PARAMETERS: |
|---|
| 603 | # this -> Instance of BayesTable |
|---|
| 604 | # |
|---|
| 605 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 606 | global toltk_options |
|---|
| 607 | # General options (parent) |
|---|
| 608 | ::BayesTable::ReadIni $this |
|---|
| 609 | # Specific options |
|---|
| 610 | set rini ::iniFile::Read |
|---|
| 611 | |
|---|
| 612 | ::BayesTable::SetOption $this txt,font \ |
|---|
| 613 | [$rini SQLTable txt,font $toltk_options(fonts,FixSmall)] |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 618 | proc ::SQLTable::WriteIni {this} { |
|---|
| 619 | # |
|---|
| 620 | # PURPOSE: Writes specific options to .ini file. This procedure is called by |
|---|
| 621 | # parent BayesGraph. |
|---|
| 622 | # |
|---|
| 623 | # PARAMETERS: |
|---|
| 624 | # this -> Instance of BayesTable |
|---|
| 625 | # |
|---|
| 626 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 627 | set wini ::iniFile::Write |
|---|
| 628 | |
|---|
| 629 | $wini SQLTable txt,font [::BayesTable::GetOption $this txt,font] |
|---|
| 630 | } |
|---|
| 631 | |
|---|
| 632 | |
|---|
| 633 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 634 | proc ::SQLTable::Configure {this} { |
|---|
| 635 | # |
|---|
| 636 | # PURPOSE: Configures some options of SQL Table. First of all, configures |
|---|
| 637 | # options from BayesTable. |
|---|
| 638 | # |
|---|
| 639 | # PARAMETERS: |
|---|
| 640 | # this -> Instance of BayesTable |
|---|
| 641 | # |
|---|
| 642 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 643 | # General options (parent) |
|---|
| 644 | ::BayesTable::Configure $this |
|---|
| 645 | # Specific options |
|---|
| 646 | set w [::BayesTable::GetWidget $this txt] |
|---|
| 647 | $w configure -font [::BayesTable::GetOption $this txt,font] |
|---|
| 648 | } |
|---|