aboutsummaryrefslogtreecommitdiffstats
path: root/include/dba/dba_pdo.php
blob: 7255a2b6635e60c4697199fe2d3294115bb63c7b (plain) (blame)
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
<?php /** @file */

require_once('include/dba/dba_driver.php');

class dba_pdo extends dba_driver {


	public $driver_dbtype = null;

	function connect($server,$port,$user,$pass,$db) {
		
		$this->driver_dbtype = 'mysql'; // (($dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql');
		$dns = $this->driver_dbtype
		. ':host=' . $server . (is_null($port) ? '' : ';port=' . $port)
		. ';dbname=' . $db;


		try {
			$this->db = new PDO($dns,$user,$pass);
			$this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
		}
		catch(PDOException $e) {
			if(file_exists('dbfail.out')) {
				file_put_contents('dbfail.out', datetime_convert() . "\nConnect: " . $e->getMessage() . "\n", FILE_APPEND);
			}

			return false;
		}

		$this->connected = true;
		return true;

	}

	function q($sql) {
		if((! $this->db) || (! $this->connected))
			return false;

		$this->error = '';
		$select = ((stripos($sql,'select') === 0) ? true : false);

		try {
			$result = $this->db->query($sql);
		}
		catch(PDOException $e) {
	
			$this->error = $e->getMessage();
			if($this->error) {
				db_logger('dba_mysqli: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR);
				if(file_exists('dbfail.out')) {
					file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . "\n" . $this->error . "\n", FILE_APPEND);
				}
			}
		}

		if(!($select)) {
			if($this->debug) {
				db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returns ' . (($result) ? 'true' : 'false'), LOGGER_NORMAL,(($result) ? LOG_INFO : LOG_ERR));
			}
			return $result;
		}

		if($this->debug) {
			db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returned ' . count($result) . ' results.', LOGGER_NORMAL, LOG_INFO); 
		}

		$r = array();
		if($result) {
			foreach($result as $x) {
				$r[] = $x;
			}
			if($this->debug) {
				db_logger('dba_pdo: ' . printable(print_r($r,true)), LOGGER_NORMAL, LOG_INFO);
			}
		}
		return $r;
	}

	function escape($str) {
		if($this->db && $this->connected) {
			return substr(substr(@$this->db->quote($str),1),0,-1);
		}
	}

	function close() {
		if($this->db)
			$this->db = null;
		$this->connected = false;
	}
	
	function getdriver() {
		return 'pdo';
	}

}