Completed
Push — master ( 612476...c205d6 )
by smiley
04:22
created

PostgreSQLDriver::getClientInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Class PostgreSQLDriver
4
 *
5
 * @filesource   PostgreSQLDriver.php
6
 * @created      21.02.2016
7
 * @package      chillerlan\Database\Drivers\PostgreSQL
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2016 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Drivers;
14
15
use chillerlan\Database\DBException;
16
17
/**
18
 *
19
 */
20
class PostgreSQLDriver extends DBDriverAbstract{
21
22
	/**
23
	 * Holds the database resource object
24
	 *
25
	 * @var resource
26
	 */
27
	protected $db;
28
29
	/**
30
	 * Establishes a database connection and returns the connection object
31
	 *
32
	 * @return \chillerlan\Database\Drivers\DBDriverInterface
33
	 * @throws \chillerlan\Database\DBException
34
	 */
35
	public function connect():DBDriverInterface{
36
37
		if(gettype($this->db) === 'resource'){
38
			return $this;
39
		}
40
41
		// i am an ugly duckling. fix me please.
42
43
		$options = [
44
			'--client_encoding='.$this->options->pgsql_charset,
45
		];
46
47
		$conn_str = [
48
			'host=\''.$this->options->host.'\'',
49
			'port=\''.(int)$this->options->port.'\'',
50
			'dbname=\''.$this->options->database.'\'',
51
			'user=\''.$this->options->username.'\'',
52
			'password=\''.$this->options->password.'\'',
53
			'options=\''.implode(' ', $options).'\'',
54
		];
55
56
		try{
57
			$this->db = pg_connect(implode(' ', $conn_str));
58
59
			return $this;
60
		}
61
		catch(\Exception $e){
62
			throw new DBException('db error: [PostgreSQLDriver]: '.$e->getMessage());
63
		}
64
65
	}
66
67
	/**
68
	 * Closes a database connection
69
	 *
70
	 * @return bool
71
	 */
72
	public function disconnect():bool{
73
		return pg_close($this->db);
74
	}
75
76
	/**
77
	 * Returns info about the used php client
78
	 *
79
	 * @return string php's database client string
80
	 */
81
	public function getClientInfo():string{
82
		$ver = pg_version($this->db);
83
84
		return 'PostgreSQL '.$ver['client'].' ('.$ver['client_encoding'].')';
85
	}
86
87
	/**
88
	 * Returns info about the database server
89
	 *
90
	 * @return string database's serverinfo string
91
	 */
92
	public function getServerInfo():string{
93
		$ver = pg_version($this->db);
94
95
		return 'PostgreSQL '.$ver['server'].' ('.$ver['server_encoding'].', date style: '.$ver['DateStyle'].', time zone: '.$ver['TimeZone'].')';
96
	}
97
98
	/**
99
	 * @param $data
100
	 *
101
	 * @return string
102
	 */
103
	public function escape($data){
104
		return pg_escape_string($this->db, $data);
105
	}
106
107
	/**
108
	 * @param             $result
109
	 * @param string|null $index
110
	 * @param bool        $assoc
111
	 *
112
	 * @return bool|\chillerlan\Database\DBResult
113
	 */
114
	protected function __getResult($result, string $index = null, bool $assoc = true){
115
116
		if(is_bool($result)){
117
			return $result; // @codeCoverageIgnore
118
		}
119
120
		$r = $this->getResult($assoc ? 'pg_fetch_assoc' : 'pg_fetch_row', [$result], $index, $assoc);
121
122
		pg_free_result($result);
123
124
		return $r;
125
	}
126
127
	/**
128
	 * @param string      $sql
129
	 * @param string|null $index
130
	 * @param bool        $assoc
131
	 *
132
	 * @return bool|\chillerlan\Database\DBResult
133
	 */
134
	protected function __raw(string $sql, string $index = null, bool $assoc = true){
135
		return $this->__getResult(pg_query($sql), $index, $assoc);
136
	}
137
138
	/**
139
	 * @param string      $sql
140
	 * @param array       $values
141
	 * @param string|null $index
142
	 * @param bool        $assoc
143
	 *
144
	 * @return bool|\chillerlan\Database\DBResult
145
	 */
146
	protected function __prepared(string $sql, array $values = [], string $index = null, bool $assoc = true){
147
		pg_prepare($this->db, '', $sql);
148
149
		return $this->__getResult(pg_execute($this->db, '', $values), $index, $assoc);
150
	}
151
152
	/**
153
	 * @param string $sql
154
	 * @param array  $values
155
	 *
156
	 * @return bool
157
	 */
158
	protected function __multi(string $sql, array $values){
159
		pg_prepare($this->db, '', $sql);
160
161
		foreach($values as $row){
162
			pg_execute($this->db, '', $row);
163
		}
164
165
		return true;
166
	}
167
168
	/**
169
	 * @param string $sql
170
	 * @param array  $data
171
	 * @param        $callback
172
	 *
173
	 * @return bool
174
	 */
175
	protected function __multi_callback(string $sql, array $data, $callback){
176
		pg_prepare($this->db, '', $sql);
177
178
		foreach($data as $row){
179
			pg_execute($this->db, '', call_user_func_array($callback, [$row]));
180
		}
181
182
		return true;
183
	}
184
185
}
186