Rev 260 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 260 | Rev 261 | ||
---|---|---|---|
Line 18... | Line 18... | ||
18 | */ |
18 | */ |
19 | 19 | ||
20 | if (!defined('IN_OIDPLUS')) die(); |
20 | if (!defined('IN_OIDPLUS')) die(); |
21 | 21 | ||
22 | class OIDplusDatabasePluginPgSql extends OIDplusDatabasePlugin { |
22 | class OIDplusDatabasePluginPgSql extends OIDplusDatabasePlugin { |
23 | private $conn; |
23 | private $conn = null; |
24 | private $already_prepared; |
24 | private $already_prepared = array(); |
- | 25 | private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior |
|
25 | 26 | ||
26 | public static function getPluginInformation(): array { |
27 | public static function getPluginInformation(): array { |
27 | $out = array(); |
28 | $out = array(); |
28 | $out['name'] = 'PostgreSQL'; |
29 | $out['name'] = 'PostgreSQL'; |
29 | $out['author'] = 'ViaThinkSoft'; |
30 | $out['author'] = 'ViaThinkSoft'; |
Line 34... | Line 35... | ||
34 | 35 | ||
35 | public static function name(): string { |
36 | public static function name(): string { |
36 | return "PgSQL"; |
37 | return "PgSQL"; |
37 | } |
38 | } |
38 | 39 | ||
39 | public function __construct() { |
- | |
40 | if (!defined('OIDPLUS_PGSQL_HOST')) define('OIDPLUS_PGSQL_HOST', 'localhost:5432'); |
- | |
41 | if (!defined('OIDPLUS_PGSQL_USERNAME')) define('OIDPLUS_PGSQL_USERNAME', 'postgres'); |
- | |
42 | if (!defined('OIDPLUS_PGSQL_PASSWORD')) define('OIDPLUS_PGSQL_PASSWORD', ''); // base64 encoded |
- | |
43 | if (!defined('OIDPLUS_PGSQL_DATABASE')) define('OIDPLUS_PGSQL_DATABASE', 'oidplus'); |
- | |
44 | } |
- | |
45 | - | ||
46 | public function query(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult { |
40 | public function doQuery(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult { |
- | 41 | $this->last_error = null; |
|
47 | if (is_null($prepared_args)) { |
42 | if (is_null($prepared_args)) { |
48 | $res = pg_query($this->conn, $sql); |
43 | $res = @pg_query($this->conn, $sql); |
49 | 44 | ||
50 | if ($res === false) { |
45 | if ($res === false) { |
- | 46 | $this->last_error = pg_last_error($this->conn); |
|
51 | throw new OIDplusSQLException($sql, $this->error()); |
47 | throw new OIDplusSQLException($sql, $this->error()); |
52 | } else { |
48 | } else { |
53 | return new OIDplusQueryResultPgSql($res); |
49 | return new OIDplusQueryResultPgSql($res); |
54 | } |
50 | } |
55 | } else { |
51 | } else { |
Line 62... | Line 58... | ||
62 | static $i = 0; |
58 | static $i = 0; |
63 | $i++; |
59 | $i++; |
64 | return '$'.$i; |
60 | return '$'.$i; |
65 | }, $sql); |
61 | }, $sql); |
66 | 62 | ||
67 | $prepare_name = 'OIDPLUS_'.sha1($sql); |
63 | $prepare_name = 'OIDplus_ps_'.sha1($sql); |
68 | if (!in_array($prepare_name, $this->already_prepared)) { |
64 | if (!in_array($prepare_name, $this->already_prepared)) { |
69 | $res = pg_prepare($this->conn, $prepare_name, $sql); |
65 | $res = @pg_prepare($this->conn, $prepare_name, $sql); |
70 | if ($res === false) { |
66 | if ($res === false) { |
71 | throw new OIDplusSQLException($sql, 'Cannot prepare statement'); |
67 | throw new OIDplusSQLException($sql, 'Cannot prepare statement'); |
72 | } |
68 | } |
73 | $this->already_prepared[] = $prepare_name; |
69 | $this->already_prepared[] = $prepare_name; |
74 | } |
70 | } |
Line 77... | Line 73... | ||
77 | if (is_bool($value)) $value = $value ? '1' : '0'; |
73 | if (is_bool($value)) $value = $value ? '1' : '0'; |
78 | } |
74 | } |
79 | 75 | ||
80 | $ps = pg_execute($this->conn, $prepare_name, $prepared_args); |
76 | $ps = pg_execute($this->conn, $prepare_name, $prepared_args); |
81 | if ($ps === false) { |
77 | if ($ps === false) { |
- | 78 | $this->last_error = pg_last_error($this->conn); |
|
82 | throw new OIDplusSQLException($sql, $this->error()); |
79 | throw new OIDplusSQLException($sql, $this->error()); |
83 | } |
80 | } |
84 | return new OIDplusQueryResultPgSql($ps); |
81 | return new OIDplusQueryResultPgSql($ps); |
85 | } |
82 | } |
86 | } |
83 | } |
Line 88... | Line 85... | ||
88 | public function insert_id(): int { |
85 | public function insert_id(): int { |
89 | return (int)$this->query('select lastval() as id')->fetch_object()->id; |
86 | return (int)$this->query('select lastval() as id')->fetch_object()->id; |
90 | } |
87 | } |
91 | 88 | ||
92 | public function error(): string { |
89 | public function error(): string { |
93 | $err = pg_last_error($this->conn); |
90 | $err = $this->last_error; |
94 | if (!$err) $err = ''; |
91 | if ($err == null) $err = ''; |
95 | return $err; |
92 | return $err; |
96 | } |
93 | } |
97 | 94 | ||
98 | protected function doConnect(): void { |
95 | protected function doConnect(): void { |
99 | if (!function_exists('pg_connect')) throw new OIDplusConfigInitializationException('PHP extension "PostgreSQL" not installed'); |
96 | if (!function_exists('pg_connect')) throw new OIDplusConfigInitializationException('PHP extension "PostgreSQL" not installed'); |
100 | 97 | ||
101 | // Try connecting to the database |
98 | // Try connecting to the database |
102 | ob_start(); |
99 | ob_start(); |
103 | try { |
- | |
104 | $err = ''; |
100 | $err = ''; |
- | 101 | try { |
|
105 | list($hostname, $port) = explode(':', OIDPLUS_PGSQL_HOST.':5432'); |
102 | $host = OIDplus::baseConfig()->getValue('PGSQL_HOST', 'localhost:5432'); |
106 | $username = OIDPLUS_PGSQL_USERNAME; |
103 | $username = OIDplus::baseConfig()->getValue('PGSQL_USERNAME', 'postgres'); |
107 | $password = base64_decode(OIDPLUS_PGSQL_PASSWORD); |
104 | $password = OIDplus::baseConfig()->getValue('PGSQL_PASSWORD', ''); |
108 | $dbname = OIDPLUS_PGSQL_DATABASE; |
105 | $database = OIDplus::baseConfig()->getValue('PGSQL_DATABASE', 'oidplus'); |
- | 106 | list($hostname, $port) = explode(':', "$host:5432"); |
|
109 | $this->conn = pg_connect("host=$hostname user=$username password=$password port=$port dbname=$dbname"); |
107 | $this->conn = pg_connect("host=$hostname user=$username password=$password port=$port dbname=$database"); |
110 | } finally { |
108 | } finally { |
111 | # TODO: this does not seem to work?! (at least not for CLI) |
109 | # TODO: this does not seem to work?! (at least not for CLI) |
112 | $err = ob_get_contents(); |
110 | $err = ob_get_contents(); |
113 | ob_end_clean(); |
111 | ob_end_clean(); |
114 | } |
112 | } |