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
|
/*-------------------------------------------------------------------------
*
* pgtclId.c--
* useful routines to convert between strings and pointers
* Needed because everything in tcl is a string, but we want pointers
* to data structures
*
* ASSUMPTION: sizeof(long) >= sizeof(void*)
*
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclId.c,v 1.3 1996/11/11 12:14:45 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <string.h>
#include <tcl.h>
#include "postgres.h"
#include "pgtclCmds.h"
#include "pgtclId.h"
/*
* Create the Id for a new connection and hash it
*/
void
PgSetConnectionId(Pg_clientData *cd, char *id, PGconn *conn)
{
Tcl_HashEntry *hent;
Pg_ConnectionId *connid;
int hnew;
connid = (Pg_ConnectionId *)ckalloc(sizeof(Pg_ConnectionId));
connid->conn = conn;
Tcl_InitHashTable(&(connid->res_hash), TCL_STRING_KEYS);
sprintf(connid->id, "pgc%ld", cd->dbh_count++);
strcpy(id, connid->id);
hent = Tcl_CreateHashEntry(&(cd->dbh_hash), connid->id, &hnew);
Tcl_SetHashValue(hent, (ClientData)connid);
}
/*
* Get back the connection from the Id
*/
PGconn *
PgGetConnectionId(Pg_clientData *cd, char *id)
{
Tcl_HashEntry *hent;
Pg_ConnectionId *connid;
hent = Tcl_FindHashEntry(&(cd->dbh_hash), id);
if(hent == NULL) {
return (PGconn *)NULL;
}
connid = (Pg_ConnectionId *)Tcl_GetHashValue(hent);
return connid->conn;
}
/*
* Remove a connection Id from the hash table and
* close all portals the user forgot.
*/
void
PgDelConnectionId(Pg_clientData *cd, char *id)
{
Tcl_HashEntry *hent;
Tcl_HashEntry *hent2;
Tcl_HashEntry *hent3;
Tcl_HashSearch hsearch;
Pg_ConnectionId *connid;
Pg_ResultId *resid;
hent = Tcl_FindHashEntry(&(cd->dbh_hash), id);
if(hent == NULL) {
return;
}
connid = (Pg_ConnectionId *)Tcl_GetHashValue(hent);
hent2 = Tcl_FirstHashEntry(&(connid->res_hash), &hsearch);
while(hent2 != NULL) {
resid = (Pg_ResultId *)Tcl_GetHashValue(hent2);
PQclear(resid->result);
hent3 = Tcl_FindHashEntry(&(cd->res_hash), resid->id);
if(hent3 != NULL) {
Tcl_DeleteHashEntry(hent3);
}
ckfree(resid);
hent2 = Tcl_NextHashEntry(&hsearch);
}
Tcl_DeleteHashTable(&(connid->res_hash));
Tcl_DeleteHashEntry(hent);
ckfree(connid);
}
/*
* Create a new result Id and hash it
*/
void
PgSetResultId(Pg_clientData *cd, char *id, char *connid_c, PGresult *res)
{
Tcl_HashEntry *hent;
Pg_ConnectionId *connid;
Pg_ResultId *resid;
int hnew;
hent = Tcl_FindHashEntry(&(cd->dbh_hash), connid_c);
if(hent == NULL) {
connid = NULL;
} else {
connid = (Pg_ConnectionId *)Tcl_GetHashValue(hent);
}
resid = (Pg_ResultId *)ckalloc(sizeof(Pg_ResultId));
resid->result = res;
resid->connection = connid;
sprintf(resid->id, "pgr%ld", cd->res_count++);
strcpy(id, resid->id);
hent = Tcl_CreateHashEntry(&(cd->res_hash), resid->id, &hnew);
Tcl_SetHashValue(hent, (ClientData)resid);
if(connid != NULL) {
hent = Tcl_CreateHashEntry(&(connid->res_hash), resid->id, &hnew);
Tcl_SetHashValue(hent, (ClientData)resid);
}
}
/*
* Get back the result pointer from the Id
*/
PGresult *
PgGetResultId(Pg_clientData *cd, char *id)
{
Tcl_HashEntry *hent;
Pg_ResultId *resid;
hent = Tcl_FindHashEntry(&(cd->res_hash), id);
if(hent == NULL) {
return (PGresult *)NULL;
}
resid = (Pg_ResultId *)Tcl_GetHashValue(hent);
return resid->result;
}
/*
* Remove a result Id from the hash tables
*/
void
PgDelResultId(Pg_clientData *cd, char *id)
{
Tcl_HashEntry *hent;
Tcl_HashEntry *hent2;
Pg_ResultId *resid;
hent = Tcl_FindHashEntry(&(cd->res_hash), id);
if(hent == NULL) {
return;
}
resid = (Pg_ResultId *)Tcl_GetHashValue(hent);
if (resid->connection != NULL) {
hent2 = Tcl_FindHashEntry(&(resid->connection->res_hash), id);
if(hent2 != NULL) {
Tcl_DeleteHashEntry(hent2);
}
}
Tcl_DeleteHashEntry(hent);
ckfree(resid);
}
/*
* Get the connection Id from the result Id
*/
void
PgGetConnByResultId(Pg_clientData *cd, char *id, char *resid_c)
{
Tcl_HashEntry *hent;
Pg_ResultId *resid;
hent = Tcl_FindHashEntry(&(cd->res_hash), id);
if(hent == NULL) {
return;
}
resid = (Pg_ResultId *)Tcl_GetHashValue(hent);
if (resid->connection != NULL) {
strcpy(id, resid->connection->id);
}
}
|