aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pgaccess/lib/mainlib.tcl
blob: b4379a4f83128bc6d1012f2f7aa3011d7897c9c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
namespace eval Mainlib {

proc {cmd_Delete} {} {
global PgAcVar CurrentDB
if {$CurrentDB==""} return;
set objtodelete [get_dwlb_Selection]
if {$objtodelete==""} return;
set delmsg [format [intlmsg "You are going to delete\n\n %s \n\nProceed?"] $objtodelete]
if {[tk_messageBox -title [intlmsg "FINAL WARNING"] -parent .pgaw:Main -message $delmsg -type yesno -default no]=="no"} { return }
switch $PgAcVar(activetab) {
	Tables {
		sql_exec noquiet "drop table \"$objtodelete\""
		sql_exec quiet "delete from pga_layout where tablename='$objtodelete'"
		cmd_Tables
	}
	Schema {
		sql_exec quiet "delete from pga_schema where schemaname='$objtodelete'"
		cmd_Schema
	}
	Views {
		sql_exec noquiet "drop view \"$objtodelete\""
		sql_exec quiet "delete from pga_layout where tablename='$objtodelete'"
		cmd_Views
	}
	Queries {
		sql_exec quiet "delete from pga_queries where queryname='$objtodelete'"
		sql_exec quiet "delete from pga_layout where tablename='$objtodelete'"
		cmd_Queries
	}
	Scripts {
		sql_exec quiet "delete from pga_scripts where scriptname='$objtodelete'"
		cmd_Scripts
	}
	Forms {
		sql_exec quiet "delete from pga_forms where formname='$objtodelete'"
		cmd_Forms
	}
	Sequences {
		sql_exec quiet "drop sequence \"$objtodelete\""
		cmd_Sequences
	}
	Functions {
		delete_function $objtodelete
		cmd_Functions
	}
	Reports {
		sql_exec noquiet "delete from pga_reports where reportname='$objtodelete'"
		cmd_Reports
	}
	Users {
		sql_exec noquiet "drop user \"$objtodelete\""
		cmd_Users
	}
}
}

proc {cmd_Design} {} {
global PgAcVar CurrentDB
if {$CurrentDB==""} return;
if {[.pgaw:Main.lb curselection]==""} return;
set objname [.pgaw:Main.lb get [.pgaw:Main.lb curselection]]
set tablename $objname
switch $PgAcVar(activetab) {
	Tables  {
		Tables::design $objname
	}
	Schema  {
		Schema::open $objname
	}
	Queries {
		Queries::design $objname
	}
	Views {
		Views::design $objname
	}
	Scripts {
		Scripts::design $objname
	}
	Forms {
		Forms::design $objname
	}
	Functions {
		Functions::design $objname
	}
	Reports {
		Reports::design $objname
	}
	Users {
		Users::design $objname
	}
}
}

proc {cmd_Forms} {} {
global CurrentDB
	setCursor CLOCK
	.pgaw:Main.lb delete 0 end
	catch {
		wpg_select $CurrentDB "select formname from pga_forms order by formname" rec {
			.pgaw:Main.lb insert end $rec(formname)
		}
	}
	setCursor DEFAULT
}


proc {cmd_Functions} {} {
global CurrentDB
	set maxim 16384
	setCursor CLOCK
	catch {
		wpg_select $CurrentDB "select oid from pg_database where datname='template1'" rec {
			set maxim $rec(oid)
		}
	}
	.pgaw:Main.lb delete 0 end
	catch {
		wpg_select $CurrentDB "select proname from pg_proc where oid>$maxim order by proname" rec {
			.pgaw:Main.lb insert end $rec(proname)
		}	
	}
	setCursor DEFAULT
}


proc {cmd_Import_Export} {how} {
global PgAcVar CurrentDB
	if {$CurrentDB==""} return;
	Window show .pgaw:ImportExport
	set PgAcVar(impexp,tablename) {}
	set PgAcVar(impexp,filename) {}
	set PgAcVar(impexp,delimiter) {}
	if {$PgAcVar(activetab)=="Tables"} {
		set tn [get_dwlb_Selection]
		set PgAcVar(impexp,tablename) $tn
		if {$tn!=""} {set PgAcVar(impexp,filename) "$tn.txt"}
	}
	.pgaw:ImportExport.expbtn configure -text [intlmsg $how]
}


proc {cmd_New} {} {
global PgAcVar CurrentDB
if {$CurrentDB==""} return;
switch $PgAcVar(activetab) {
	Tables {
		Tables::new
	}
	Schema {
		Schema::new
	}
	Queries {
		Queries::new
	}
	Users {
		Users::new
	}
	Views {
		Views::new
	}
	Sequences {
		Sequences::new
	}
	Reports {
		Reports::new
	}
	Forms {
		Forms::new
	}
	Scripts {
		Scripts::new
	}
	Functions {
		Functions::new
	}
}
}


proc {cmd_Open} {} {
global PgAcVar CurrentDB
	if {$CurrentDB==""} return;
	set objname [get_dwlb_Selection]
	if {$objname==""} return;
	switch $PgAcVar(activetab) {
		Tables		{ Tables::open $objname }
		Schema		{ Schema::open $objname }
		Forms		{ Forms::open $objname }
		Scripts		{ Scripts::open $objname }
		Queries		{ Queries::open $objname }
		Views		{ Views::open $objname }
		Sequences	{ Sequences::open $objname }
		Functions	{ Functions::design $objname }
		Reports		{ Reports::open $objname }
	}
}



proc {cmd_Queries} {} {
global CurrentDB
	.pgaw:Main.lb delete 0 end
	catch {
		wpg_select $CurrentDB "select queryname from pga_queries order by queryname" rec {
			.pgaw:Main.lb insert end $rec(queryname)
		}
	}
}


proc {cmd_Rename} {} {
global PgAcVar CurrentDB
	if {$CurrentDB==""} return;
	if {$PgAcVar(activetab)=="Views"} return;
	if {$PgAcVar(activetab)=="Sequences"} return;
	if {$PgAcVar(activetab)=="Functions"} return;
	if {$PgAcVar(activetab)=="Users"} return;
	set temp [get_dwlb_Selection]
	if {$temp==""} {
		tk_messageBox -title [intlmsg Warning] -parent .pgaw:Main -message [intlmsg "Please select an object first!"]
		return;
	}
	set PgAcVar(Old_Object_Name) $temp
	Window show .pgaw:RenameObject
}


proc {cmd_Reports} {} {
global CurrentDB
	setCursor CLOCK
	catch {
		wpg_select $CurrentDB "select reportname from pga_reports order by reportname" rec {
		.pgaw:Main.lb insert end "$rec(reportname)"
		}
	}
	setCursor DEFAULT
}

proc {cmd_Users} {} {
global CurrentDB
	setCursor CLOCK
	.pgaw:Main.lb delete 0 end
	catch {
		wpg_select $CurrentDB "select * from pg_user order by usename" rec {
			.pgaw:Main.lb insert end $rec(usename)
		}
	}
	setCursor DEFAULT
}


proc {cmd_Scripts} {} {
global CurrentDB
	setCursor CLOCK
	.pgaw:Main.lb delete 0 end
	catch {
		wpg_select $CurrentDB "select scriptname from pga_scripts order by scriptname" rec {
		.pgaw:Main.lb insert end $rec(scriptname)
		}
	}
	setCursor DEFAULT
}


proc {cmd_Sequences} {} {
global CurrentDB

setCursor CLOCK
.pgaw:Main.lb delete 0 end
catch {
	wpg_select $CurrentDB "select relname from pg_class where (relname not like 'pg_%') and (relkind='S') order by relname" rec {
		.pgaw:Main.lb insert end $rec(relname)
	}
}
setCursor DEFAULT
}

proc {cmd_Tables} {} {
global CurrentDB
	setCursor CLOCK
	.pgaw:Main.lb delete 0 end
	foreach tbl [Database::getTablesList] {.pgaw:Main.lb insert end $tbl}
	setCursor DEFAULT
}

proc {cmd_Schema} {} {
global CurrentDB
.pgaw:Main.lb delete 0 end
catch {
	wpg_select $CurrentDB "select schemaname from pga_schema order by schemaname" rec {
		.pgaw:Main.lb insert end $rec(schemaname)
	}
}
}

proc {cmd_Views} {} {
global CurrentDB
setCursor CLOCK
.pgaw:Main.lb delete 0 end
catch {
	wpg_select $CurrentDB "select c.relname,count(c.relname) from pg_class C, pg_rewrite R where (relname !~ '^pg_') and (r.ev_class = C.oid) and (r.ev_type = '1') group by relname" rec {
		if {$rec(count)!=0} {
			set itsaview($rec(relname)) 1
		}
	}
	wpg_select $CurrentDB "select relname from pg_class where (relname !~ '^pg_') and (relkind='r') and (relhasrules) order by relname" rec {
		if {[info exists itsaview($rec(relname))]} {
			.pgaw:Main.lb insert end $rec(relname)
		}
	}
}
setCursor DEFAULT
}

proc {delete_function} {objname} {
global CurrentDB
	wpg_select $CurrentDB "select proargtypes,pronargs from pg_proc where proname='$objname'" rec {
		set PgAcVar(function,parameters) $rec(proargtypes)
		set nrpar $rec(pronargs)
	}
	set lispar {}
	for {set i 0} {$i<$nrpar} {incr i} {
		lappend lispar [Database::getPgType [lindex $PgAcVar(function,parameters) $i]]
	}
	set lispar [join $lispar ,]
	sql_exec noquiet "drop function $objname ($lispar)"
}


proc {draw_tabs} {} {
global PgAcVar
	set ypos 85
	foreach tab $PgAcVar(tablist) {
		label .pgaw:Main.tab$tab -borderwidth 1  -anchor w -relief raised -text [intlmsg $tab]
		place .pgaw:Main.tab$tab -x 10 -y $ypos -height 25 -width 82 -anchor nw -bordermode ignore
		lower .pgaw:Main.tab$tab
		bind .pgaw:Main.tab$tab <Button-1> "Mainlib::tab_click $tab"
		incr ypos 25
	}
	set PgAcVar(activetab) ""
}


proc {get_dwlb_Selection} {} {
	set temp [.pgaw:Main.lb curselection]
	if {$temp==""} return "";
	return [.pgaw:Main.lb get $temp]
}




proc {sqlw_display} {msg} {
	if {![winfo exists .pgaw:SQLWindow]} {return}
	.pgaw:SQLWindow.f.t insert end "$msg\n\n"
	.pgaw:SQLWindow.f.t see end
	set nrlines [lindex [split [.pgaw:SQLWindow.f.t index end] .] 0]
	if {$nrlines>50} {
		.pgaw:SQLWindow.f.t delete 1.0 3.0
	}
}


proc {open_database} {} {
global PgAcVar CurrentDB
setCursor CLOCK
if {$PgAcVar(opendb,username)!=""} {
	if {$PgAcVar(opendb,host)!=""} {
		set connres [catch {set newdbc [pg_connect -conninfo "host=$PgAcVar(opendb,host) port=$PgAcVar(opendb,pgport) dbname=$PgAcVar(opendb,dbname) user=$PgAcVar(opendb,username) password=$PgAcVar(opendb,password)"]} msg]
	} else {
		set connres [catch {set newdbc [pg_connect -conninfo "dbname=$PgAcVar(opendb,dbname) user=$PgAcVar(opendb,username) password=$PgAcVar(opendb,password)"]} msg]
	}
} else {
	set connres [catch {set newdbc [pg_connect $PgAcVar(opendb,dbname) -host $PgAcVar(opendb,host) -port $PgAcVar(opendb,pgport)]} msg]
}
if {$connres} {
	setCursor DEFAULT
	showError [format [intlmsg "Error trying to connect to database '%s' on host %s \n\nPostgreSQL error message:%s"] $PgAcVar(opendb,dbname) $PgAcVar(opendb,host) $msg"]
	return $msg
} else {
	catch {pg_disconnect $CurrentDB}
	set CurrentDB $newdbc
	set PgAcVar(currentdb,host) $PgAcVar(opendb,host)
	set PgAcVar(currentdb,pgport) $PgAcVar(opendb,pgport)
	set PgAcVar(currentdb,dbname) $PgAcVar(opendb,dbname)
	set PgAcVar(currentdb,username) $PgAcVar(opendb,username)
	set PgAcVar(currentdb,password) $PgAcVar(opendb,password)
	set PgAcVar(statusline,dbname) $PgAcVar(currentdb,dbname)
	set PgAcVar(pref,lastdb) $PgAcVar(currentdb,dbname)
	set PgAcVar(pref,lasthost) $PgAcVar(currentdb,host)
	set PgAcVar(pref,lastport) $PgAcVar(currentdb,pgport)
	set PgAcVar(pref,lastusername) $PgAcVar(currentdb,username)
	Preferences::save
	catch {setCursor DEFAULT ; Window hide .pgaw:OpenDB}
	tab_click Tables
	# Check for pga_ tables
	foreach {table structure} {pga_queries {queryname varchar(64),querytype char(1),querycommand text,querytables text,querylinks text,queryresults text,querycomments text} pga_forms {formname varchar(64),formsource text} pga_scripts {scriptname varchar(64),scriptsource text} pga_reports {reportname varchar(64),reportsource text,reportbody text,reportprocs text,reportoptions text} pga_schema {schemaname varchar(64),schematables text,schemalinks text}} {
		set pgres [wpg_exec $CurrentDB "select relname from pg_class where relname='$table'"]
		if {$PgAcVar(pgsql,status)!="PGRES_TUPLES_OK"} {
			showError "[intlmsg {FATAL ERROR searching for PgAccess system tables}] : $PgAcVar(pgsql,errmsg)\nStatus:$PgAcVar(pgsql,status)"
			catch {pg_disconnect $CurrentDB}
			exit
		} elseif {[pg_result $pgres -numTuples]==0} {
			pg_result $pgres -clear
			sql_exec quiet "create table $table ($structure)"
			sql_exec quiet "grant ALL on $table to PUBLIC"
		} else {
			foreach fieldspec [split $structure ,] {
				set field [lindex [split $fieldspec] 0]
				set pgres [wpg_exec $CurrentDB "select \"$field\" from \"$table\""]
				if {$PgAcVar(pgsql,status)!="PGRES_TUPLES_OK"} {
					if {![regexp "attribute '$field' not found" $PgAcVar(pgsql,errmsg)]} {
						showError "[intlmsg {FATAL ERROR upgrading PgAccess table}] $table: $PgAcVar(pgsql,errmsg)\nStatus:$PgAcVar(pgsql,status)"
						catch {pg_disconnect $CurrentDB}
						exit
					} else {
						pg_result $pgres -clear
						sql_exec quiet "alter table \"$table\" add column $fieldspec "
					}
				}
			}
		}
		catch {pg_result $pgres -clear}
	}
	
	# searching for autoexec script
	wpg_select $CurrentDB "select * from pga_scripts where scriptname ~* '^autoexec$'" recd {
		eval $recd(scriptsource)
	}
	return ""
}
}


proc {tab_click} {tabname} {
global PgAcVar CurrentDB
	set w .pgaw:Main.tab$tabname
	if {$CurrentDB==""} return;
	set curtab $tabname
	#if {$PgAcVar(activetab)==$curtab} return;
	.pgaw:Main.btndesign configure -state disabled
	if {$PgAcVar(activetab)!=""} {
		place .pgaw:Main.tab$PgAcVar(activetab) -x 10
		.pgaw:Main.tab$PgAcVar(activetab) configure -font $PgAcVar(pref,font_normal)
	}
	$w configure -font $PgAcVar(pref,font_bold)
	place $w -x 7
	place .pgaw:Main.lmask -x 80 -y [expr 86+25*[lsearch -exact $PgAcVar(tablist) $curtab]]
	set PgAcVar(activetab) $curtab
	# Tabs where button Design is enabled
	if {[lsearch {Tables Schema Scripts Queries Functions Views Reports Forms Users} $PgAcVar(activetab)]!=-1} {
		.pgaw:Main.btndesign configure -state normal
	}
	.pgaw:Main.lb delete 0 end
	cmd_$curtab
}



}


proc vTclWindow.pgaw:Main {base} {
global PgAcVar
	if {$base == ""} {
		set base .pgaw:Main
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel \
		-background #efefef -cursor left_ptr
	wm focusmodel $base passive
	wm geometry $base 332x390+96+172
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm deiconify $base
	wm title $base "PostgreSQL access"
	bind $base <Key-F1> "Help::load index"
	label $base.labframe \
		-relief raised 
	listbox $base.lb \
		-background #fefefe \
		-selectbackground #c3c3c3 \
		-foreground black -highlightthickness 0 -selectborderwidth 0 \
		-yscrollcommand {.pgaw:Main.sb set} 
	bind $base.lb <Double-Button-1> {
		Mainlib::cmd_Open
	}
	button $base.btnnew \
		-borderwidth 1 -command Mainlib::cmd_New -text [intlmsg New]
	button $base.btnopen \
		-borderwidth 1 -command Mainlib::cmd_Open -text [intlmsg Open]
	button $base.btndesign \
		-borderwidth 1 -command Mainlib::cmd_Design -text [intlmsg Design]
	label $base.lmask \
		-borderwidth 0 \
		-text {  } 
	frame $base.fm \
        -borderwidth 1 -height 75 -relief raised -width 125 
	menubutton $base.fm.mndb \
		-borderwidth 1 -font $PgAcVar(pref,font_normal) \
		-menu .pgaw:Main.fm.mndb.01 -padx 4 -pady 3 -text [intlmsg Database]
	menu $base.fm.mndb.01 \
		-borderwidth 1 -font $PgAcVar(pref,font_normal) \
		-tearoff 0 
	$base.fm.mndb.01 add command \
		-command {
Window show .pgaw:OpenDB
set PgAcVar(opendb,host) $PgAcVar(currentdb,host)
set PgAcVar(opendb,pgport) $PgAcVar(currentdb,pgport)
focus .pgaw:OpenDB.f1.e3
wm transient .pgaw:OpenDB .pgaw:Main
.pgaw:OpenDB.f1.e3 selection range 0 end} \
		-label [intlmsg Open] -font $PgAcVar(pref,font_normal)
	$base.fm.mndb.01 add command \
		-command {.pgaw:Main.lb delete 0 end
set CurrentDB {}
set PgAcVar(currentdb,dbname) {}
set PgAcVar(statusline,dbname) {}} \
		-label [intlmsg Close]
	$base.fm.mndb.01 add command \
		-command Database::vacuum -label [intlmsg Vacuum]
	$base.fm.mndb.01 add separator
	$base.fm.mndb.01 add command \
		-command {Mainlib::cmd_Import_Export Import} -label [intlmsg {Import table}]
	$base.fm.mndb.01 add command \
		-command {Mainlib::cmd_Import_Export Export} -label [intlmsg {Export table}]
	$base.fm.mndb.01 add separator
	$base.fm.mndb.01 add command \
		-command Preferences::configure -label [intlmsg Preferences]
	$base.fm.mndb.01 add command \
		-command "Window show .pgaw:SQLWindow" -label [intlmsg "SQL window"]
	$base.fm.mndb.01 add separator
	$base.fm.mndb.01 add command \
		-command {
set PgAcVar(activetab) {}
Preferences::save
catch {pg_disconnect $CurrentDB}
exit} -label [intlmsg Exit]
	label $base.lshost \
		-relief groove -text localhost -textvariable PgAcVar(currentdb,host) 
	label $base.lsdbname \
		-anchor w \
		-relief groove -textvariable PgAcVar(statusline,dbname) 
	scrollbar $base.sb \
		-borderwidth 1 -command {.pgaw:Main.lb yview} -orient vert 
	menubutton $base.fm.mnob \
		-borderwidth 1 \
		-menu .pgaw:Main.fm.mnob.m -font $PgAcVar(pref,font_normal) -text [intlmsg Object]
	menu $base.fm.mnob.m \
		-borderwidth 1 -font $PgAcVar(pref,font_normal) \
		-tearoff 0 
	$base.fm.mnob.m add command \
		-command Mainlib::cmd_New -font $PgAcVar(pref,font_normal) -label [intlmsg New] 
	$base.fm.mnob.m add command \
		-command Mainlib::cmd_Delete -label [intlmsg Delete] 
	$base.fm.mnob.m add command \
		-command Mainlib::cmd_Rename -label [intlmsg Rename] 
	menubutton $base.fm.mnhelp \
		-borderwidth 1 \
		-menu .pgaw:Main.fm.mnhelp.m -font $PgAcVar(pref,font_normal) -text [intlmsg Help]
	menu $base.fm.mnhelp.m \
		-borderwidth 1 -font $PgAcVar(pref,font_normal) \
		-tearoff 0 
	$base.fm.mnhelp.m add command \
		-label [intlmsg Contents] -command {Help::load index}
	$base.fm.mnhelp.m add command \
		-label PostgreSQL  -command {Help::load postgresql}
	$base.fm.mnhelp.m add separator
	$base.fm.mnhelp.m add command \
		-command {Window show .pgaw:About} -label [intlmsg About]
	place $base.labframe \
		-x 80 -y 30 -width 246 -height 325 -anchor nw -bordermode ignore 
	place $base.lb \
		-x 90 -y 75 -width 210 -height 272 -anchor nw -bordermode ignore 
	place $base.btnnew \
		-x 89 -y 40 -width 75 -height 25 -anchor nw -bordermode ignore 
	place $base.btnopen \
		-x 166 -y 40 -width 75 -height 25 -anchor nw -bordermode ignore 
	place $base.btndesign \
		-x 243 -y 40 -width 76 -height 25 -anchor nw -bordermode ignore 
	place $base.lmask \
		-x 1550 -y 4500 -width 10 -height 23 -anchor nw -bordermode ignore 
	place $base.lshost \
		-x 3 -y 370 -width 91 -height 20 -anchor nw -bordermode ignore 
	place $base.lsdbname \
		-x 95 -y 370 -width 233 -height 20 -anchor nw -bordermode ignore 
	place $base.sb \
		-x 301 -y 74 -width 18 -height 274 -anchor nw -bordermode ignore 
	place $base.fm \
        -x 1 -y 0 -width 331 -height 25 -anchor nw -bordermode ignore 
	pack $base.fm.mndb \
        -in .pgaw:Main.fm -anchor center -expand 0 -fill none -side left 
	pack $base.fm.mnob \
        -in .pgaw:Main.fm -anchor center -expand 0 -fill none -side left 
	pack $base.fm.mnhelp \
        -in .pgaw:Main.fm -anchor center -expand 0 -fill none -side right 
}

proc vTclWindow.pgaw:ImportExport {base} {
	if {$base == ""} {
		set base .pgaw:ImportExport
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 287x151+259+304
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm title $base [intlmsg "Import-Export table"]
	label $base.l1  -borderwidth 0 -text [intlmsg {Table name}]
	entry $base.e1  -background #fefefe -borderwidth 1 -textvariable PgAcVar(impexp,tablename) 
	label $base.l2  -borderwidth 0 -text [intlmsg {File name}]
	entry $base.e2  -background #fefefe -borderwidth 1 -textvariable PgAcVar(impexp,filename) 
	label $base.l3  -borderwidth 0 -text [intlmsg {Field delimiter}]
	entry $base.e3  -background #fefefe -borderwidth 1 -textvariable PgAcVar(impexp,delimiter) 
	button $base.expbtn  -borderwidth 1  -command {if {$PgAcVar(impexp,tablename)==""} {
	showError [intlmsg "You have to supply a table name!"]
} elseif {$PgAcVar(impexp,filename)==""} {
	showError [intlmsg "You have to supply a external file name!"]
} else {
	if {$PgAcVar(impexp,delimiter)==""} {
		set sup ""
	} else {
		set sup " USING DELIMITERS '$PgAcVar(impexp,delimiter)'"
	}
	if {[.pgaw:ImportExport.expbtn cget -text]=="Import"} {
		set oper "FROM"
	} else {
		set oper "TO"
	}
		if {$PgAcVar(impexp,withoids)} {
				set sup2 " WITH OIDS "
		} else {
				set sup2 ""
		}
	set sqlcmd "COPY \"$PgAcVar(impexp,tablename)\" $sup2 $oper '$PgAcVar(impexp,filename)'$sup"
	setCursor CLOCK
	if {[sql_exec noquiet $sqlcmd]} {
		tk_messageBox -title [intlmsg Information] -parent .pgaw:ImportExport -message [intlmsg "Operation completed!"]
		Window destroy .pgaw:ImportExport
	}
	setCursor DEFAULT
}}  -text Export 
	button $base.cancelbtn  -borderwidth 1 -command {Window destroy .pgaw:ImportExport} -text [intlmsg Cancel]
	checkbutton $base.oicb  -borderwidth 1  -text [intlmsg {with OIDs}] -variable PgAcVar(impexp,withoids) 
	place $base.l1  -x 15 -y 15 -anchor nw -bordermode ignore 
	place $base.e1  -x 115 -y 10 -height 22 -anchor nw -bordermode ignore 
	place $base.l2  -x 15 -y 45 -anchor nw -bordermode ignore 
	place $base.e2  -x 115 -y 40 -height 22 -anchor nw -bordermode ignore 
	place $base.l3  -x 15 -y 75 -height 18 -anchor nw -bordermode ignore 
	place $base.e3  -x 115 -y 74 -width 33 -height 22 -anchor nw -bordermode ignore 
	place $base.expbtn  -x 60 -y 110 -height 25 -width 75 -anchor nw -bordermode ignore 
	place $base.cancelbtn  -x 155 -y 110 -height 25 -width 75 -anchor nw -bordermode ignore 
	place $base.oicb  -x 170 -y 75 -anchor nw -bordermode ignore
}



proc vTclWindow.pgaw:RenameObject {base} {
	if {$base == ""} {
		set base .pgaw:RenameObject
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 272x105+294+262
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm title $base [intlmsg "Rename"]
	label $base.l1  -borderwidth 0 -text [intlmsg {New name}]
	entry $base.e1  -background #fefefe -borderwidth 1 -textvariable PgAcVar(New_Object_Name) 
	button $base.b1  -borderwidth 1  -command {
			if {$PgAcVar(New_Object_Name)==""} {
				showError [intlmsg "You must give object a new name!"]
			} elseif {$PgAcVar(activetab)=="Tables"} {
				set retval [sql_exec noquiet "alter table \"$PgAcVar(Old_Object_Name)\" rename to \"$PgAcVar(New_Object_Name)\""]
				if {$retval} {
					sql_exec quiet "update pga_layout set tablename='$PgAcVar(New_Object_Name)' where tablename='$PgAcVar(Old_Object_Name)'"
					Mainlib::cmd_Tables
					Window destroy .pgaw:RenameObject
				}
			} elseif {$PgAcVar(activetab)=="Queries"} {
				set pgres [wpg_exec $CurrentDB "select * from pga_queries where queryname='$PgAcVar(New_Object_Name)'"]
				if {$PgAcVar(pgsql,status)!="PGRES_TUPLES_OK"} {
					showError "[intlmsg {Error retrieving from}] pga_queries\n$PgAcVar(pgsql,errmsg)\n$PgAcVar(pgsql,status)"
				} elseif {[pg_result $pgres -numTuples]>0} {
					showError [format [intlmsg "Query '%s' already exists!"] $PgAcVar(New_Object_Name)]
				} else {
					sql_exec noquiet "update pga_queries set queryname='$PgAcVar(New_Object_Name)' where queryname='$PgAcVar(Old_Object_Name)'"
					sql_exec noquiet "update pga_layout set tablename='$PgAcVar(New_Object_Name)' where tablename='$PgAcVar(Old_Object_Name)'"
					Mainlib::cmd_Queries
					Window destroy .pgaw:RenameObject
				}
				catch {pg_result $pgres -clear}
			} elseif {$PgAcVar(activetab)=="Forms"} {
				set pgres [wpg_exec $CurrentDB "select * from pga_forms where formname='$PgAcVar(New_Object_Name)'"]
				if {$PgAcVar(pgsql,status)!="PGRES_TUPLES_OK"} {
					showError "[intlmsg {Error retrieving from}] pga_forms\n$PgAcVar(pgsql,errmsg)\n$PgAcVar(pgsql,status)"
				} elseif {[pg_result $pgres -numTuples]>0} {
					showError [format [intlmsg "Form '%s' already exists!"] $PgAcVar(New_Object_Name)]
				} else {
					sql_exec noquiet "update pga_forms set formname='$PgAcVar(New_Object_Name)' where formname='$PgAcVar(Old_Object_Name)'"
					Mainlib::cmd_Forms
					Window destroy .pgaw:RenameObject
				}
				catch {pg_result $pgres -clear}
			} elseif {$PgAcVar(activetab)=="Scripts"} {
				set pgres [wpg_exec $CurrentDB "select * from pga_scripts where scriptname='$PgAcVar(New_Object_Name)'"]
				if {$PgAcVar(pgsql,status)!="PGRES_TUPLES_OK"} {
					showError "[intlmsg {Error retrieving from}] pga_scripts\n$PgAcVar(pgsql,errmsg)\n$PgAcVar(pgsql,status)"
				} elseif {[pg_result $pgres -numTuples]>0} {
					showError [format [intlmsg "Script '%s' already exists!"] $PgAcVar(New_Object_Name)]
				} else {
					sql_exec noquiet "update pga_scripts set scriptname='$PgAcVar(New_Object_Name)' where scriptname='$PgAcVar(Old_Object_Name)'"
					Mainlib::cmd_Scripts
					Window destroy .pgaw:RenameObject
				}
				catch {pg_result $pgres -clear}
			} elseif {$PgAcVar(activetab)=="Schema"} {
				set pgres [wpg_exec $CurrentDB "select * from pga_schema where schemaname='$PgAcVar(New_Object_Name)'"]
				if {$PgAcVar(pgsql,status)!="PGRES_TUPLES_OK"} {
					showError "[intlmsg {Error retrieving from}] pga_schema\n$PgAcVar(pgsql,errmsg)\n$PgAcVar(pgsql,status)"
				} elseif {[pg_result $pgres -numTuples]>0} {
					showError [format [intlmsg "Schema '%s' already exists!"] $PgAcVar(New_Object_Name)]
				} else {
					sql_exec noquiet "update pga_schema set schemaname='$PgAcVar(New_Object_Name)' where schemaname='$PgAcVar(Old_Object_Name)'"
					Mainlib::cmd_Schema
					Window destroy .pgaw:RenameObject
				}
				catch {pg_result $pgres -clear}
			}
	   } -text [intlmsg Rename]
	button $base.b2  -borderwidth 1 -command {Window destroy .pgaw:RenameObject} -text [intlmsg Cancel]
	place $base.l1  -x 15 -y 28 -anchor nw -bordermode ignore 
	place $base.e1  -x 100 -y 25 -anchor nw -bordermode ignore 
	place $base.b1  -x 55 -y 65 -width 80 -anchor nw -bordermode ignore 
	place $base.b2  -x 155 -y 65 -width 80 -anchor nw -bordermode ignore
}


proc vTclWindow.pgaw:GetParameter {base} {
	if {$base == ""} {
		set base .pgaw:GetParameter
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	set sw [winfo screenwidth .]
	set sh [winfo screenheight .]
	set x [expr ($sw - 297)/2]
	set y [expr ($sh - 98)/2]
	wm geometry $base 297x98+$x+$y
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm deiconify $base
	wm title $base [intlmsg "Input parameter"]
	label $base.l1 \
		-anchor nw -borderwidth 1 \
		-justify left -relief sunken -textvariable PgAcVar(getqueryparam,msg) -wraplength 200 
	entry $base.e1 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-textvariable PgAcVar(getqueryparam,var) 
	bind $base.e1 <Key-KP_Enter> {
		set PgAcVar(getqueryparam,result) 1
destroy .pgaw:GetParameter
	}
	bind $base.e1 <Key-Return> {
		set PgAcVar(getqueryparam,result) 1
destroy .pgaw:GetParameter
	}
	button $base.bok \
		-borderwidth 1 -command {set PgAcVar(getqueryparam,result) 1
destroy .pgaw:GetParameter} -text Ok 
	button $base.bcanc \
		-borderwidth 1 -command {set PgAcVar(getqueryparam,result) 0
destroy .pgaw:GetParameter} -text [intlmsg Cancel]
	place $base.l1 \
		-x 10 -y 5 -width 201 -height 53 -anchor nw -bordermode ignore 
	place $base.e1 \
		-x 10 -y 65 -width 200 -height 24 -anchor nw -bordermode ignore 
	place $base.bok \
		-x 225 -y 5 -width 61 -height 26 -anchor nw -bordermode ignore 
	place $base.bcanc \
		-x 225 -y 35 -width 61 -height 26 -anchor nw -bordermode ignore 
}


proc vTclWindow.pgaw:SQLWindow {base} {
	if {$base == ""} {
		set base .pgaw:SQLWindow
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 551x408+192+169
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm deiconify $base
	wm title $base [intlmsg "SQL window"]
	frame $base.f \
		-borderwidth 1 -height 392 -relief raised -width 396 
	scrollbar $base.f.01 \
		-borderwidth 1 -command {.pgaw:SQLWindow.f.t xview} -orient horiz \
		-width 10 
	scrollbar $base.f.02 \
		-borderwidth 1 -command {.pgaw:SQLWindow.f.t yview} -orient vert -width 10 
	text $base.f.t \
		-borderwidth 1 \
		-height 200 -width 200 -wrap word \
		-xscrollcommand {.pgaw:SQLWindow.f.01 set} \
		-yscrollcommand {.pgaw:SQLWindow.f.02 set} 
	button $base.b1 \
		-borderwidth 1 -command {.pgaw:SQLWindow.f.t delete 1.0 end} -text [intlmsg Clean]
	button $base.b2 \
		-borderwidth 1 -command {destroy .pgaw:SQLWindow} -text [intlmsg Close] 
	grid columnconf $base 0 -weight 1
	grid columnconf $base 1 -weight 1
	grid rowconf $base 0 -weight 1
	grid $base.f \
		-in .pgaw:SQLWindow -column 0 -row 0 -columnspan 2 -rowspan 1 
	grid columnconf $base.f 0 -weight 1
	grid rowconf $base.f 0 -weight 1
	grid $base.f.01 \
		-in .pgaw:SQLWindow.f -column 0 -row 1 -columnspan 1 -rowspan 1 -sticky ew 
	grid $base.f.02 \
		-in .pgaw:SQLWindow.f -column 1 -row 0 -columnspan 1 -rowspan 1 -sticky ns 
	grid $base.f.t \
		-in .pgaw:SQLWindow.f -column 0 -row 0 -columnspan 1 -rowspan 1 \
		-sticky nesw 
	grid $base.b1 \
		-in .pgaw:SQLWindow -column 0 -row 1 -columnspan 1 -rowspan 1 
	grid $base.b2 \
		-in .pgaw:SQLWindow -column 1 -row 1 -columnspan 1 -rowspan 1 
}

proc vTclWindow.pgaw:About {base} {
	if {$base == ""} {
		set base .pgaw:About
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 471x177+168+243
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm title $base [intlmsg "About"]
	label $base.l1  -borderwidth 3 -font -Adobe-Helvetica-Bold-R-Normal-*-*-180-*-*-*-*-*  -relief ridge -text PgAccess 
	label $base.l2  -relief groove  -text [intlmsg "A Tcl/Tk interface to\nPostgreSQL\nby Constantin Teodorescu"]
	label $base.l3  -borderwidth 0 -relief sunken -text {v 0.98}
	label $base.l4  -relief groove  -text "[intlmsg {You will always get the latest version at:}]
http://www.flex.ro/pgaccess

[intlmsg {Suggestions at}] : teo@flex.ro"
	button $base.b1  -borderwidth 1 -command {Window destroy .pgaw:About} -text Ok 
	place $base.l1  -x 10 -y 10 -width 196 -height 103 -anchor nw -bordermode ignore 
	place $base.l2  -x 10 -y 115 -width 198 -height 55 -anchor nw -bordermode ignore 
	place $base.l3  -x 145 -y 80 -anchor nw -bordermode ignore 
	place $base.l4  -x 215 -y 10 -width 246 -height 103 -anchor nw -bordermode ignore 
	place $base.b1  -x 295 -y 130 -width 105 -height 28 -anchor nw -bordermode ignore
}

proc vTclWindow.pgaw:OpenDB {base} {
	if {$base == ""} {
		set base .pgaw:OpenDB
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 283x172+119+210
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm deiconify $base
	wm title $base [intlmsg "Open database"]
	frame $base.f1 \
		-borderwidth 2 -height 75 -width 125 
	label $base.f1.l1 \
		-borderwidth 0 -relief raised -text [intlmsg Host]
	entry $base.f1.e1 \
		-background #fefefe -borderwidth 1 -textvariable PgAcVar(opendb,host) -width 200 
	bind $base.f1.e1 <Key-KP_Enter> {
		focus .pgaw:OpenDB.f1.e2
	}
	bind $base.f1.e1 <Key-Return> {
		focus .pgaw:OpenDB.f1.e2
	}
	label $base.f1.l2 \
		-borderwidth 0 -relief raised -text [intlmsg Port]
	entry $base.f1.e2 \
		-background #fefefe -borderwidth 1 -textvariable PgAcVar(opendb,pgport) -width 200 
	bind $base.f1.e2 <Key-Return> {
		focus .pgaw:OpenDB.f1.e3
	}
	label $base.f1.l3 \
		-borderwidth 0 -relief raised -text [intlmsg Database]
	entry $base.f1.e3 \
		-background #fefefe -borderwidth 1 -textvariable PgAcVar(opendb,dbname) -width 200 
	bind $base.f1.e3 <Key-Return> {
		focus .pgaw:OpenDB.f1.e4
	}
	label $base.f1.l4 \
		-borderwidth 0 -relief raised -text [intlmsg Username]
	entry $base.f1.e4 \
		-background #fefefe -borderwidth 1 -textvariable PgAcVar(opendb,username) \
		-width 200 
	bind $base.f1.e4 <Key-Return> {
		focus .pgaw:OpenDB.f1.e5
	}
	label $base.f1.ls2 \
		-borderwidth 0 -relief raised -text { } 
	label $base.f1.l5 \
		-borderwidth 0 -relief raised -text [intlmsg Password]
	entry $base.f1.e5 \
		-background #fefefe -borderwidth 1 -show x -textvariable PgAcVar(opendb,password) \
		-width 200 
	bind $base.f1.e5 <Key-Return> {
		focus .pgaw:OpenDB.fb.btnopen
	}
	frame $base.fb \
		-height 75 -relief groove -width 125 
	button $base.fb.btnopen \
		-borderwidth 1 -command Mainlib::open_database -padx 9 \
		-pady 3 -text [intlmsg Open]
	button $base.fb.btncancel \
		-borderwidth 1 -command {Window hide .pgaw:OpenDB} \
		-padx 9 -pady 3 -text [intlmsg Cancel]
	place $base.f1 \
		-x 9 -y 5 -width 265 -height 126 -anchor nw -bordermode ignore 
	grid columnconf $base.f1 2 -weight 1
	grid $base.f1.l1 \
		-in .pgaw:OpenDB.f1 -column 0 -row 0 -columnspan 1 -rowspan 1 -sticky w 
	grid $base.f1.e1 \
		-in .pgaw:OpenDB.f1 -column 2 -row 0 -columnspan 1 -rowspan 1 -pady 2 
	grid $base.f1.l2 \
		-in .pgaw:OpenDB.f1 -column 0 -row 2 -columnspan 1 -rowspan 1 -sticky w 
	grid $base.f1.e2 \
		-in .pgaw:OpenDB.f1 -column 2 -row 2 -columnspan 1 -rowspan 1 -pady 2 
	grid $base.f1.l3 \
		-in .pgaw:OpenDB.f1 -column 0 -row 4 -columnspan 1 -rowspan 1 -sticky w 
	grid $base.f1.e3 \
		-in .pgaw:OpenDB.f1 -column 2 -row 4 -columnspan 1 -rowspan 1 -pady 2 
	grid $base.f1.l4 \
		-in .pgaw:OpenDB.f1 -column 0 -row 6 -columnspan 1 -rowspan 1 -sticky w 
	grid $base.f1.e4 \
		-in .pgaw:OpenDB.f1 -column 2 -row 6 -columnspan 1 -rowspan 1 -pady 2 
	grid $base.f1.ls2 \
		-in .pgaw:OpenDB.f1 -column 1 -row 0 -columnspan 1 -rowspan 1 
	grid $base.f1.l5 \
		-in .pgaw:OpenDB.f1 -column 0 -row 7 -columnspan 1 -rowspan 1 -sticky w 
	grid $base.f1.e5 \
		-in .pgaw:OpenDB.f1 -column 2 -row 7 -columnspan 1 -rowspan 1 -pady 2 
	place $base.fb \
		-x 0 -y 135 -width 283 -height 40 -anchor nw -bordermode ignore 
	grid $base.fb.btnopen \
		-in .pgaw:OpenDB.fb -column 0 -row 0 -columnspan 1 -rowspan 1 -padx 5 
	grid $base.fb.btncancel \
		-in .pgaw:OpenDB.fb -column 1 -row 0 -columnspan 1 -rowspan 1 -padx 5 
}