Completed
Push — master ( d508c6...ead087 )
by smiley
02:37
created

PostgreSQLDriver::__prepared()   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 4
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
class PostgreSQLDriver extends DBDriverAbstract{
16
17
	protected $dialect = 'pgsql';
18
	protected $quotes = ['"', '"'];
19
20
	/**
21
	 * Holds the database resource object
22
	 *
23
	 * @var resource
24
	 */
25
	protected $db;
26
27
	/**
28
	 * Establishes a database connection and returns the connection object
29
	 *
30
	 * @return \chillerlan\Database\Drivers\DBDriverInterface
31
	 * @throws \chillerlan\Database\Drivers\DBDriverException
32
	 */
33
	public function connect():DBDriverInterface{
34
35
		if(gettype($this->db) === 'resource'){
36
			return $this;
37
		}
38
39
		// i am an ugly duckling. fix me please.
40
41
		$options = [
42
			'--client_encoding='.$this->options->pgsql_charset,
43
		];
44
45
		$conn_str = [
46
			'host=\''.$this->options->host.'\'',
47
			'port=\''.(int)$this->options->port.'\'',
48
			'dbname=\''.$this->options->database.'\'',
49
			'user=\''.$this->options->username.'\'',
50
			'password=\''.$this->options->password.'\'',
51
			'options=\''.implode(' ', $options).'\'',
52
		];
53
54
		try{
55
			$this->db = pg_connect(implode(' ', $conn_str));
56
57
			return $this;
58
		}
59
		catch(\Exception $e){
60
			throw new DBDriverException('db error: [PostgreSQLDriver]: '.$e->getMessage());
61
		}
62
63
	}
64
65
	/**
66
	 * Closes a database connection
67
	 *
68
	 * @return bool
69
	 */
70
	public function disconnect():bool{
71
		return pg_close($this->db);
72
	}
73
74
	/**
75
	 * Returns info about the used php client
76
	 *
77
	 * @return string php's database client string
78
	 */
79
	public function getClientInfo():string{
80
		$ver = pg_version($this->db);
81
82
		return 'PostgreSQL '.$ver['client'].' ('.$ver['client_encoding'].')';
83
	}
84
85
	/**
86
	 * Returns info about the database server
87
	 *
88
	 * @return string database's serverinfo string
89
	 */
90
	public function getServerInfo():string{
91
		$ver = pg_version($this->db);
92
93
		return 'PostgreSQL '.$ver['server'].' ('.$ver['server_encoding'].', date style: '.$ver['DateStyle'].', time zone: '.$ver['TimeZone'].')';
94
	}
95
96
	/**
97
	 * @param $data
98
	 *
99
	 * @return string
100
	 */
101
	public function escape($data){
102
		return pg_escape_string($this->db, $data);
103
	}
104
105
	/**
106
	 * @param             $result
107
	 * @param string|null $index
108
	 * @param bool        $assoc
109
	 *
110
	 * @return bool|\chillerlan\Database\DBResult
111
	 */
112
	protected function __getResult($result, string $index = null, bool $assoc = true){
113
114
		if(is_bool($result)){
115
			return $result; // @codeCoverageIgnore
116
		}
117
118
		$r = $this->getResult($assoc ? 'pg_fetch_assoc' : 'pg_fetch_row', [$result], $index, $assoc);
119
120
		pg_free_result($result);
121
122
		return $r;
123
	}
124
125
	/**
126
	 * @param string      $sql
127
	 * @param string|null $index
128
	 * @param bool        $assoc
129
	 *
130
	 * @return bool|\chillerlan\Database\DBResult
131
	 */
132
	protected function __raw(string $sql, string $index = null, bool $assoc = true){
133
		return $this->__getResult(pg_query($sql), $index, $assoc);
134
	}
135
136
	/**
137
	 * @param string      $sql
138
	 * @param array       $values
139
	 * @param string|null $index
140
	 * @param bool        $assoc
141
	 *
142
	 * @return bool|\chillerlan\Database\DBResult
143
	 */
144
	protected function __prepared(string $sql, array $values = [], string $index = null, bool $assoc = true){
145
		pg_prepare($this->db, '', $this->replaceParams($sql));
146
147
		return $this->__getResult(pg_execute($this->db, '', $values), $index, $assoc);
148
	}
149
150
	/**
151
	 * @param string $sql
152
	 * @param array  $values
153
	 *
154
	 * @return bool
155
	 */
156 View Code Duplication
	protected function __multi(string $sql, array $values){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
		pg_prepare($this->db, '', $this->replaceParams($sql));
158
159
		foreach($values as $row){
160
			pg_execute($this->db, '', $row);
161
		}
162
163
		return true;
164
	}
165
166
	/**
167
	 * @param string $sql
168
	 * @param array  $data
169
	 * @param        $callback
170
	 *
171
	 * @return bool
172
	 */
173 View Code Duplication
	protected function __multi_callback(string $sql, array $data, $callback){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
		pg_prepare($this->db, '', $this->replaceParams($sql));
175
176
		foreach($data as $row){
177
			pg_execute($this->db, '', call_user_func_array($callback, [$row]));
178
		}
179
180
		return true;
181
	}
182
183
	protected function replaceParams(string $sql):string{
184
		$i = 0;
185
186
		return preg_replace_callback('/(\?)/', function() use (&$i){
187
			return '$'.++$i;
188
		}, $sql);
189
	}
190
191
}
192