File: connection.cpp

package info (click to toggle)
pgagent 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 268 kB
  • sloc: cpp: 1,647; sql: 495; xml: 100; ansic: 52; makefile: 43
file content (554 lines) | stat: -rw-r--r-- 12,253 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////
//
// pgAgent - PostgreSQL Tools
//
// Copyright (C) 2002 - 2012, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// connection.cpp - database connection
//
//////////////////////////////////////////////////////////////////////////

#include "pgAgent.h"

#include <wx/regex.h>
#include <wx/tokenzr.h>

DBconn *DBconn::primaryConn = NULL;
wxString DBconn::basicConnectString;
static wxMutex s_PoolLock;


DBconn::DBconn(const wxString &connectString, const wxString &db)
{
	inUse = false;
	next = 0;
	prev = 0;
	majorVersion = 0;
	minorVersion = 0;
	dbname = db;
	connStr = connectString;

	if (connectString.IsEmpty())
	{
		// This is a sql call to a local database.
		// No connection string found. Use basicConnectString.
		Connect(basicConnectString  + wxT(" dbname=") + dbname);
	}
	else
	{
		Connect(connectString);
	}
}


bool DBconn::Connect(const wxString &connectString)
{
	LogMessage(wxString::Format(_("Creating DB connection: %s"), connectString.c_str()), LOG_DEBUG);
	wxCharBuffer cstrUTF = connectString.mb_str(wxConvUTF8);
	conn = PQconnectdb(cstrUTF);
	if (PQstatus(conn) != CONNECTION_OK)
	{
		lastError = wxString::FromAscii(PQerrorMessage(conn));
		PQfinish(conn);
		conn = 0;
	}
	return IsValid();
}


DBconn::~DBconn()
{
	// clear a single connection
	if (conn)
	{
		PQfinish(conn);
		conn = 0;
	}
}


wxString DBconn::qtDbString(const wxString &value)
{
	wxString result = value;

	result.Replace(wxT("\\"), wxT("\\\\"));
	result.Replace(wxT("'"), wxT("''"));
	result.Append(wxT("'"));

	if (BackendMinimumVersion(8, 1))
	{
		if (result.Contains(wxT("\\")))
			result.Prepend(wxT("E'"));
		else
			result.Prepend(wxT("'"));
	}
	else
		result.Prepend(wxT("'"));

	return result;
}


bool DBconn::BackendMinimumVersion(int major, int minor)
{
	if (!majorVersion)
	{
		wxString version = ExecuteScalar(wxT("SELECT version();")) ;
		sscanf(version.ToAscii(), "%*s %d.%d", &majorVersion, &minorVersion);
	}
	return majorVersion > major || (majorVersion == major && minorVersion >= minor);
}


DBconn *DBconn::InitConnection(const wxString &connectString)
{
	wxMutexLocker lock(s_PoolLock);

	basicConnectString = connectString;
	wxString dbname;

	connInfo cnInfo = connInfo::getConnectionInfo(connectString);
	if (cnInfo.isValid)
	{
		dbname = cnInfo.dbname;
		basicConnectString = cnInfo.getConnectionString();
		primaryConn = new DBconn(cnInfo.getConnectionString(), dbname);

		if (!primaryConn)
			LogMessage(_("Failed to create primary connection!"), LOG_ERROR);
		primaryConn->dbname = dbname;
		primaryConn->inUse = true;
	}
	else
	{
		primaryConn = NULL;
		LogMessage(wxT("Primary connection string is not valid!"), LOG_ERROR);
	}

	return primaryConn;
}


DBconn *DBconn::Get(const wxString &connStr, const wxString &db)
{
	if (db.IsEmpty() && connStr.IsEmpty())
	{
		LogMessage(_("Cannot allocate connection - no database or connection string specified!"), LOG_WARNING);
		return NULL;
	}

	wxMutexLocker lock(s_PoolLock);

	DBconn *thisConn = primaryConn, *lastConn;

	// find an existing connection
	do
	{
		if (thisConn && ((!db.IsEmpty() && db == thisConn->dbname && connStr.IsEmpty()) || (!connStr.IsEmpty() && connStr == thisConn->connStr)) && !thisConn->inUse)
		{
			LogMessage(wxString::Format(_("Allocating existing connection to database %s"), thisConn->dbname.c_str()), LOG_DEBUG);
			thisConn->inUse = true;
			return thisConn;
		}

		lastConn = thisConn;
		thisConn = thisConn->next;

	}
	while (thisConn != 0);


	// No suitable connection was found, so create a new one.
	DBconn *newConn = NULL;
	newConn = new DBconn(connStr, db);

	if (newConn->conn)
	{
		LogMessage(wxString::Format(_("Allocating new connection to database %s"), newConn->dbname.c_str()), LOG_DEBUG);
		newConn->inUse = true;
		newConn->prev = lastConn;
		lastConn->next = newConn;
	}
	else
	{
		wxString warnMsg;
		if (connStr.IsEmpty())
			warnMsg = wxString::Format(_("Failed to create new connection to database '%s':'%s'"),
			                           db.c_str(), newConn->GetLastError().c_str());
		else
			warnMsg = wxString::Format(_("Failed to create new connection for connection string '%s':%s"),
			                           connStr.c_str(), newConn->GetLastError().c_str());
		LogMessage(warnMsg, LOG_STARTUP);
		return NULL;
	}

	return newConn;
}


void DBconn::Return()
{
	wxMutexLocker lock(s_PoolLock);

	// Cleanup
	this->ExecuteVoid(wxT("RESET ALL"));
	this->lastError.Empty();

	LogMessage(wxString::Format(_("Returning connection to database %s"), dbname.c_str()), LOG_DEBUG);
	inUse = false;
}


void DBconn::ClearConnections(bool all)
{
	wxMutexLocker lock(s_PoolLock);

	if (all)
		LogMessage(_("Clearing all connections"), LOG_DEBUG);
	else
		LogMessage(_("Clearing inactive connections"), LOG_DEBUG);

	DBconn *thisConn = primaryConn, *deleteConn;
	int total = 0, free = 0, deleted = 0;

	if (thisConn)
	{

		total++;

		// Find the last connection
		while (thisConn->next != 0)
		{
			total++;

			if (!thisConn->inUse)
				free++;

			thisConn = thisConn->next;
		}
		if (!thisConn->inUse)
			free++;

		// Delete connections as required
		// If a connection is not in use, delete it, and reset the next and previous
		// pointers appropriately. If it is in use, don't touch it.
		while (thisConn->prev != 0)
		{
			if ((!thisConn->inUse) || all)
			{
				deleteConn = thisConn;
				thisConn = deleteConn->prev;
				thisConn->next = deleteConn->next;
				if (deleteConn->next)
					deleteConn->next->prev = deleteConn->prev;
				delete deleteConn;
				deleted++;
			}
			else
			{
				thisConn = thisConn->prev;
			}
		}

		if (all)
		{
			delete thisConn;
			deleted++;
		}

		wxString tmp;
		tmp.Printf(_("Connection stats: total - %d, free - %d, deleted - %d"), total, free, deleted);
		LogMessage(tmp, LOG_DEBUG);

	}
	else
		LogMessage(_("No connections found!"), LOG_DEBUG);

}


DBresult *DBconn::Execute(const wxString &query)
{
	DBresult *res = new DBresult(this, query);
	if (!res->IsValid())
	{
		// error handling here

		delete res;
		return 0;
	}
	return res;
}


wxString DBconn::ExecuteScalar(const wxString &query)
{
	int rows = -1;
	DBresult *res = Execute(query);
	wxString data;
	if (res)
	{
		data = res->GetString(0);
		rows = res->RowsAffected();
		delete res;
		return data;
	}
	return wxEmptyString;
}


int DBconn::ExecuteVoid(const wxString &query)
{
	int rows = -1;
	DBresult *res = Execute(query);
	if (res)
	{
		rows = res->RowsAffected();
		delete res;
	}
	return rows;
}


wxString DBconn::GetLastError()
{
	// Return the last error message, minus any trailing line ends
	if (lastError.substr(lastError.length() - 2, 2) == wxT("\r\n")) // DOS
		return lastError.substr(0, lastError.length() - 2);
	else if (lastError.substr(lastError.length() - 1, 1) == wxT("\n")) // Unix
		return lastError.substr(0, lastError.length() - 1);
	else if (lastError.substr(lastError.length() - 1, 1) == wxT("\r")) // Mac
		return lastError.substr(0, lastError.length() - 1);
	else
		return lastError;
}

///////////////////////////////////////////////////////7

DBresult::DBresult(DBconn *conn, const wxString &query)
{
	wxCharBuffer cstrUTF = query.mb_str(wxConvUTF8);
	result = PQexec(conn->conn, cstrUTF);
	currentRow = 0;
	maxRows = 0;

	if (result)
	{
		int rc = PQresultStatus(result);
		if (rc == PGRES_TUPLES_OK)
			maxRows = PQntuples(result);
		else if (rc != PGRES_COMMAND_OK)
		{
			conn->lastError = wxString::FromAscii(PQerrorMessage(conn->conn));
			LogMessage(wxT("Query error: ") + conn->lastError, LOG_WARNING);
			PQclear(result);
			result = 0;
		}
	}
	else
		conn->lastError = wxString::FromAscii(PQerrorMessage(conn->conn));

}


DBresult::~DBresult()
{
	if (result)
		PQclear(result);
}


wxString DBresult::GetString(int col) const
{
	wxString str;

	if (result && currentRow < maxRows && col >= 0)
	{
		str = wxString::FromAscii(PQgetvalue(result, currentRow, col));
	}
	return str;
}


wxString DBresult::GetString(const wxString &colname) const
{
	wxCharBuffer cstrUTF = colname.mb_str(wxConvUTF8);
	int col = PQfnumber(result, cstrUTF);
	if (col < 0)
	{
		// fatal: not found
		return wxT("");
	}
	return GetString(col);
}

///////////////////////////////////////////////////////7

bool connInfo::IsValidIP()
{
	if (host.IsEmpty())
		return false;

	// check for IPv4 format
	wxStringTokenizer tkip4(host, wxT("."));
	int count = 0;

	while (tkip4.HasMoreTokens())
	{
		unsigned long val = 0;
		if (!tkip4.GetNextToken().ToULong(&val))
			break;
		if (count == 0 || count == 3)
			if (val > 0 && val < 255)
				count++;
			else
				break;
		else if (val >= 0 && val < 255)
			count++;
		else
			break;
	}

	if (count == 4)
		return true;

	// check for IPv6 format
	wxStringTokenizer tkip6(host, wxT(":"));
	count = 0;

	while (tkip6.HasMoreTokens())
	{
		unsigned long val = 0;
		wxString strVal = tkip6.GetNextToken();
		if (strVal.Length() > 4 || !strVal.ToULong(&val, 16))
			return false;
		count++;
	}
	if (count <= 8)
		return true;

	// TODO:: We're not supporting mix mode (x:x:x:x:x:x:d.d.d.d)
	//        i.e. ::ffff:12.34.56.78
	return false;
}


wxString connInfo::getConnectionString()
{
	wxString connStr;

	// Check if it has valid connection info
	if (!isValid)
		return connStr;

	// User
	connStr = wxT("user=") + user;

	// Port
	if (port != 0)
	{
		wxString portStr;
		portStr.Printf(wxT("%ld"), port);
		connStr += wxT(" port=") + portStr;
	}

	// host or hostaddr
	if (!host.IsEmpty())
		if (IsValidIP())
			connStr += wxT(" hostaddr=") + host;
		else
			connStr += wxT(" host=") + host;

	// connection timeout
	if (connection_timeout != 0)
	{
		wxString val;
		val.Printf(wxT("%ld"), connection_timeout);
		connStr += wxT(" connection_timeout=") + val;
	}

	// password
	if (!password.IsEmpty())
		connStr += wxT(" password=") + password;

	if (!dbname.IsEmpty())
		connStr += wxT(" dbname=") + dbname;

	LogMessage(wxString::Format(_("Connection Information:")), LOG_DEBUG);
	LogMessage(wxString::Format(_("     user         : %s"), user.c_str()), LOG_DEBUG);
	LogMessage(wxString::Format(_("     port         : %ld"), port), LOG_DEBUG);
	LogMessage(wxString::Format(_("     host         : %s"), host.c_str()), LOG_DEBUG);
	LogMessage(wxString::Format(_("     dbname       : %s"), dbname.c_str()), LOG_DEBUG);
	LogMessage(wxString::Format(_("     password     : %s"), password.c_str()), LOG_DEBUG);
	LogMessage(wxString::Format(_("     conn timeout : %ld"), connection_timeout), LOG_DEBUG);

	return connStr;
}


connInfo connInfo::getConnectionInfo(wxString connStr)
{
	connInfo cnInfo;

	wxRegEx propertyExp;

	// Remove the white-space(s) to match the following format
	// i.e. prop=value
	bool res = propertyExp.Compile(wxT("(([ ]*[\t]*)+)="));

	propertyExp.ReplaceAll(&connStr, wxT("="));

	res = propertyExp.Compile(wxT("=(([ ]*[\t]*)+)"));
	propertyExp.ReplaceAll(&connStr, wxT("="));

	// Seperate all the prop=value patterns
	wxArrayString tokens = wxStringTokenize(connStr, wxT("\t \n\r"));

	unsigned int index = 0;
	while (index < tokens.Count())
	{
		wxString prop, value;

		wxArrayString pairs = wxStringTokenize(tokens[index++], wxT("="));

		if (pairs.GetCount() != 2)
			return cnInfo;

		prop = pairs[0];
		value = pairs[1];

		if (prop.CmpNoCase(wxT("user")) == 0)
			cnInfo.user = value;
		else if (prop.CmpNoCase(wxT("host")) == 0 || prop.CmpNoCase(wxT("hostAddr")) == 0)
			cnInfo.host = value;
		else if (prop.CmpNoCase(wxT("port")) == 0)
		{
			if (!value.ToULong(&cnInfo.port))
				// port must be an unsigned integer
				return cnInfo;
		}
		else if (prop.CmpNoCase(wxT("password")) == 0)
			cnInfo.password = value;
		else if (prop.CmpNoCase(wxT("connection_timeout")) == 0)
		{
			if (!value.ToULong(&cnInfo.connection_timeout))
				// connection timeout must be an unsigned interger
				return cnInfo;
		}
		else if (prop.CmpNoCase(wxT("dbname")) == 0)
			cnInfo.dbname = value;
		else
			// Not valid property found
			return cnInfo;
	}

	// If user, dbname & host all are blank than we will consider this an invalid connection string
	if (cnInfo.user.IsEmpty() && cnInfo.dbname.IsEmpty() && cnInfo.host.IsEmpty())
		cnInfo.isValid = false;
	else
		cnInfo.isValid = true;

	return cnInfo;
}