-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgress.tcl
114 lines (93 loc) · 2.49 KB
/
postgress.tcl
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
# postgres.tcl --
#
# PostgresSQL connectivity module.
#
# Copyright (c) 2013 by Nagarajan Chinnasamy <[email protected]>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
namespace eval ::tdao::dbc::postgres {
}
proc ::tdao::dbc::postgres::Load {} {
package require Pgtcl
}
proc ::tdao::dbc::postgres::open {dbname args} {
if {[catch {pg_connect $dbname {*}$args} result]} {
return -code error $result
}
return $result
}
proc ::tdao::dbc::postgres::close {conn} {
catch {pg_disconnect $conn}
}
proc ::tdao::dbc::postgres::get {conn stmt fieldslist {format "dict"}} {
if {[catch {pg_exec $conn $stmt} result]} {
return -code error $result
}
set recordslist ""
switch -- $format {
dict {
set recordslist [dict values [pg_result $result -dict]]
}
list -
llist {
set recordslist [dict values [pg_result $result -$format]]
}
}
pg_result $result -clear
return $recordslist
}
proc ::tdao::dbc::postgres::insert {conn stmt {sequence_fields ""}} {
if {[catch {pg_exec $conn $stmt} result]} {
return -code error $result
}
set status [pg_result $result -status]
if {$status != "PGRES_COMMAND_OK" && $status != "PGRES_TUPLES_OK"} {
set error [pg_result $result -error]
pg_result $result -clear
return -code error $error
}
set numrows [pg_result $result -numTuples]
if {$numrows} {
set sequencedict [pg_result $result -dict]
pg_result $result -clear
return [list $numrows [dict get $sequencedict 0]]
}
pg_result $result -clear
return 1
}
proc ::tdao::dbc::postgres::update {conn stmt} {
if {[catch {pg_exec $conn $stmt} result]} {
return -code error $result
}
set changes [pg_result $result -cmdTuples]
pg_result $result -clear
return $changes
}
proc ::tdao::dbc::postgres::delete {conn stmt} {
if {[catch {pg_exec $conn $stmt} result]} {
return -code error $result
}
set changes [pg_result $result -cmdTuples]
pg_result $result -clear
return $changes
}
proc ::tdao::dbc::postgres::begin {conn {lock deferrable}} {
if {[catch {pg_exec $conn "begin $lock"} result]} {
return -code error $result
}
pg_result $result -clear
}
proc ::tdao::dbc::postgres::commit {conn} {
if {[catch {pg_exec $conn commit} result]} {
return -code error $result
}
pg_result $result -clear
}
proc ::tdao::dbc::postgres::rollback {conn} {
if {[catch {pg_exec $conn rollback} result]} {
return -code error $result
}
pg_result $result -clear
}
package provide tdao::dbc::postgres 0.1.1