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