aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpgtcl/pgtcl.c
blob: 2cbec47395d40b6ee78b134b947e891b65e37615 (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
/*-------------------------------------------------------------------------
 *
 * pgtcl.c--
 *    
 *    libpgtcl is a tcl package for front-ends to interface with pglite
 *   It's the tcl equivalent of the old libpq C interface.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtcl.c,v 1.5 1996/11/11 12:14:38 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */

#include <stdlib.h>

#include "postgres.h"
#include "tcl.h"
#include "libpgtcl.h"
#include "pgtclCmds.h"
#include "pgtclId.h"

/*
 * Pgtcl_Init 
 *    initialization package for the PGLITE Tcl package
 *
 */

/*
 * Tidy up forgotten postgres connection at Tcl_Exit
 */
static void
Pgtcl_AtExit (ClientData cData)
{
  Pg_clientData *cd = (Pg_clientData *)cData;
  Tcl_HashEntry		*hent;
  Tcl_HashSearch	hsearch;
  Pg_ConnectionId	*connid;
  PGconn		*conn;

  while((hent = Tcl_FirstHashEntry(&(cd->dbh_hash), &hsearch)) != NULL) {
      connid = (Pg_ConnectionId *)Tcl_GetHashValue(hent);
      conn = connid->conn;
      PgDelConnectionId(cd, connid->id);
      PQfinish(conn);
  }

  Tcl_DeleteHashTable(&(cd->dbh_hash));
  Tcl_DeleteHashTable(&(cd->res_hash));

  Tcl_DeleteExitHandler(Pgtcl_AtExit, cData);
}

/*
 * Tidy up forgotten postgres connections on Interpreter deletion
 */
static void
Pgtcl_Shutdown (ClientData cData, Tcl_Interp *interp)
{
  Pgtcl_AtExit(cData);
}

int
Pgtcl_Init (Tcl_Interp *interp)
{
  Pg_clientData	*cd;

  /* Create and initialize the client data area */
  cd = (Pg_clientData *)ckalloc(sizeof(Pg_clientData));
  Tcl_InitHashTable(&(cd->dbh_hash), TCL_STRING_KEYS);
  Tcl_InitHashTable(&(cd->res_hash), TCL_STRING_KEYS);
  cd->dbh_count = 0L;
  cd->res_count = 0L;

  /* Arrange for tidy up when interpreter is deleted or Tcl exits */
  Tcl_CallWhenDeleted(interp, Pgtcl_Shutdown, (ClientData)cd);
  Tcl_CreateExitHandler(Pgtcl_AtExit, (ClientData)cd);

  /* register all pgtcl commands */
  Tcl_CreateCommand(interp,
		    "pg_conndefaults",
		    Pg_conndefaults,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);

  Tcl_CreateCommand(interp,
		    "pg_connect",
		    Pg_connect,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);

  Tcl_CreateCommand(interp,
		    "pg_disconnect",
		    Pg_disconnect,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);
  
  Tcl_CreateCommand(interp,
		    "pg_exec",
		    Pg_exec,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);
  
  Tcl_CreateCommand(interp,
		    "pg_select",
		    Pg_select,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);
  
  Tcl_CreateCommand(interp,
		    "pg_result",
		    Pg_result,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);
  
  Tcl_CreateCommand(interp,
		    "pg_lo_open",
		    Pg_lo_open,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);
  
  Tcl_CreateCommand(interp,
		    "pg_lo_close",
		    Pg_lo_close,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);

  Tcl_CreateCommand(interp,
		    "pg_lo_read",
		    Pg_lo_read,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);

  Tcl_CreateCommand(interp,
		    "pg_lo_write",
		    Pg_lo_write,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);

  Tcl_CreateCommand(interp,
		    "pg_lo_lseek",
		    Pg_lo_lseek,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);

  Tcl_CreateCommand(interp,
		    "pg_lo_creat",
		    Pg_lo_creat,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);

  Tcl_CreateCommand(interp,
		    "pg_lo_tell",
		    Pg_lo_tell,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);

  Tcl_CreateCommand(interp,
		    "pg_lo_unlink",
		    Pg_lo_unlink,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);

  Tcl_CreateCommand(interp,
		    "pg_lo_import",
		    Pg_lo_import,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);
  
  Tcl_CreateCommand(interp,
		    "pg_lo_export",
		    Pg_lo_export,
		    (ClientData)cd, (Tcl_CmdDeleteProc*)NULL);
  
  Tcl_PkgProvide(interp, "Pgtcl", "1.0");

  return TCL_OK;
}


int
Pgtcl_SafeInit (Tcl_Interp *interp)
{
    return Pgtcl_Init(interp);
}