Skip to content

Commit d09fc12

Browse files
author
Michael Meskes
committed
Moved database name handling to libecpg.
1 parent 786f1a5 commit d09fc12

File tree

2 files changed

+151
-8
lines changed

2 files changed

+151
-8
lines changed

src/interfaces/ecpg/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,5 +1039,9 @@ Mon Jan 22 17:56:02 CET 2001
10391039

10401040
- Synced gram.y and preproc.y.
10411041
- Added #include "postgres.h" to pgc.l.
1042+
1043+
Tue Jan 23 08:54:14 CET 2001
1044+
1045+
- Moved database name handling to libecpg.
10421046
- Set ecpg version to 2.8.0.
10431047
- Set library version to 3.2.0.

src/interfaces/ecpg/lib/connect.c

Lines changed: 147 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,12 @@ ECPGnoticeProcessor(void *arg, const char *message)
225225
sqlca.sqlwarn[0]='W';
226226
}
227227

228+
/* this contains some quick hacks, needs to be cleaned up, but it works */
228229
bool
229-
ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd, const char *connection_name, int autocommit)
230+
ECPGconnect(int lineno, const char *name, const char *user, const char *passwd, const char *connection_name, int autocommit)
230231
{
231232
struct connection *this;
233+
char *dbname = strdup(name), *host = NULL, *tmp, *port = NULL, *realname = NULL, *options = NULL;
232234

233235
init_sqlca();
234236

@@ -238,11 +240,126 @@ ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd
238240
if (dbname == NULL && connection_name == NULL)
239241
connection_name = "DEFAULT";
240242

243+
/* get the detail information out of dbname */
244+
if (strchr(dbname, '@') != NULL)
245+
{
246+
/* old style: dbname[@server][:port] */
247+
tmp = strrchr(dbname, ':');
248+
if (tmp != NULL) /* port number given */
249+
{
250+
port = strdup(tmp + 1);
251+
*tmp = '\0';
252+
}
253+
254+
tmp = strrchr(dbname, '@');
255+
if (tmp != NULL) /* host name given */
256+
{
257+
host = strdup(tmp + 1);
258+
*tmp = '\0';
259+
}
260+
realname = strdup(dbname);
261+
}
262+
else if (strncmp(dbname, "tcp:", 4) == 0 || strncmp(dbname, "unix:", 5) == 0)
263+
{
264+
int offset = 0;
265+
266+
/*
267+
* only allow protocols tcp and unix
268+
*/
269+
if (strncmp(dbname, "tcp:", 4) == 0)
270+
offset = 4;
271+
else if (strncmp(dbname, "unix:", 5) == 0)
272+
offset = 5;
273+
274+
if (strncmp(dbname + offset, "postgresql://", strlen("postgresql://")) == 0)
275+
{
276+
/*
277+
* new style:
278+
* <tcp|unix>:postgresql://server[:port|:/unixsocket/path:][/dbname][?options]
279+
*/
280+
offset += strlen("postgresql://");
281+
282+
tmp = strrchr(dbname + offset, '?');
283+
if (tmp != NULL) /* options given */
284+
{
285+
options = strdup(tmp + 1);
286+
*tmp = '\0';
287+
}
288+
289+
tmp = strrchr(dbname + offset, '/');
290+
if (tmp != NULL) /* database name given */
291+
{
292+
realname = strdup(tmp + 1);
293+
*tmp = '\0';
294+
}
295+
296+
tmp = strrchr(dbname + offset, ':');
297+
if (tmp != NULL) /* port number or Unix socket path given */
298+
{
299+
char *tmp2;
300+
301+
*tmp = '\0';
302+
if ((tmp2 = strchr(tmp + 1, ':')) != NULL)
303+
{
304+
*tmp2 = '\0';
305+
host = strdup(tmp + 1);
306+
if (strncmp(dbname, "unix:", 5) != 0)
307+
{
308+
ECPGlog("connect: socketname %s given for TCP connection in line %d\n", host, lineno);
309+
ECPGraise(lineno, ECPG_CONNECT, realname ? realname : "<DEFAULT>");
310+
if (host)
311+
free(host);
312+
if (port)
313+
free(port);
314+
if (options)
315+
free(options);
316+
if (realname)
317+
free(realname);
318+
if (dbname)
319+
free(dbname);
320+
return false;
321+
}
322+
}
323+
else
324+
{
325+
port = strdup(tmp + 1);
326+
}
327+
}
328+
329+
if (strncmp(dbname, "unix:", 5) == 0)
330+
{
331+
if (strcmp(dbname + offset, "localhost") != 0 && strcmp(dbname + offset, "127.0.0.1") != 0)
332+
{
333+
ECPGlog("connect: non-localhost access via sockets in line %d\n", lineno);
334+
ECPGraise(lineno, ECPG_CONNECT, realname ? realname : "<DEFAULT>");
335+
if (host)
336+
free(host);
337+
if (port)
338+
free(port);
339+
if (options)
340+
free(options);
341+
if (realname)
342+
free(realname);
343+
if(dbname)
344+
free(dbname);
345+
return false;
346+
}
347+
}
348+
else
349+
{
350+
host = strdup(dbname + offset);
351+
}
352+
353+
}
354+
}
355+
else
356+
realname = strdup(dbname);
357+
241358
/* add connection to our list */
242359
if (connection_name != NULL)
243360
this->name = ecpg_strdup(connection_name, lineno);
244361
else
245-
this->name = ecpg_strdup(dbname, lineno);
362+
this->name = ecpg_strdup(realname, lineno);
246363

247364
this->cache_head = NULL;
248365

@@ -253,15 +370,37 @@ ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd
253370

254371
actual_connection = all_connections = this;
255372

256-
ECPGlog("ECPGconnect: opening database %s %s%s\n", dbname ? dbname : "<DEFAULT>", user ? "for user " : "", user ? user : "");
257-
258-
this->connection = PQsetdbLogin(NULL, NULL, NULL, NULL, dbname, user, passwd);
259-
373+
ECPGlog("ECPGconnect: opening database %s on %s port %s %s%s%s%s\n",
374+
realname ? realname : "<DEFAULT>",
375+
host ? host : "<DEFAULT>",
376+
port ? port : "<DEFAULT>",
377+
options ? "with options " : "", options ? options : "",
378+
user ? "for user " : "", user ? user : "");
379+
380+
this->connection = PQsetdbLogin(host, port, options, NULL, realname, user, passwd);
381+
382+
if (host)
383+
free(host);
384+
if (port)
385+
free(port);
386+
if (options)
387+
free(options);
388+
if (realname)
389+
free(realname);
390+
if (dbname)
391+
free(dbname);
392+
260393
if (PQstatus(this->connection) == CONNECTION_BAD)
261394
{
262395
ecpg_finish(this);
263-
ECPGlog("connect: could not open database %s %s%s in line %d\n", dbname ? dbname : "<DEFAULT>", user ? "for user " : "", user ? user : "", lineno);
264-
ECPGraise(lineno, ECPG_CONNECT, dbname ? dbname : "<DEFAULT>");
396+
ECPGlog("connect: could not open database %s on %s port %s %s%s%s%s in line %d\n",
397+
realname ? realname : "<DEFAULT>",
398+
host ? host : "<DEFAULT>",
399+
port ? port : "<DEFAULT>",
400+
options ? "with options " : "", options ? options : "",
401+
user ? "for user " : "", user ? user : "",
402+
lineno);
403+
ECPGraise(lineno, ECPG_CONNECT, realname ? realname : "<DEFAULT>");
265404
return false;
266405
}
267406

0 commit comments

Comments
 (0)