2022-11-24 22:41:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PgDatabase {
|
2022-11-27 20:17:38 +00:00
|
|
|
private $db = null;
|
|
|
|
|
2022-12-03 15:49:40 +00:00
|
|
|
public function __construct($software=null, $instance=null) {
|
2022-12-08 20:08:23 +00:00
|
|
|
if ($software === null && !isset($GLOBALS['ap_software'])) {
|
2022-12-03 15:49:40 +00:00
|
|
|
$url = trim($_SERVER['REQUEST_URI'], '/');
|
|
|
|
$url = substr($url, 7); // api/vX/
|
|
|
|
$ps = explode('/', $url);
|
|
|
|
$software = $ps[1];
|
|
|
|
}
|
2022-12-08 20:08:23 +00:00
|
|
|
if ($software === null && isset($GLOBALS['ap_software']))
|
|
|
|
$software = $GLOBALS['ap_software'];
|
2022-12-03 15:49:40 +00:00
|
|
|
|
|
|
|
$config = null;
|
|
|
|
if ($instance === null)
|
|
|
|
$config = instance_config($software);
|
|
|
|
else $config = instance_config($software, $instance);
|
2022-11-27 23:54:24 +00:00
|
|
|
if ($config === null) return;
|
2022-11-27 20:17:38 +00:00
|
|
|
|
|
|
|
$this->db = pg_connect(
|
|
|
|
' host='.$config['psql_host'].
|
|
|
|
' port='.$config['psql_port'].
|
|
|
|
' dbname='.$config['psql_db'].
|
|
|
|
' user='.$config['psql_username'].
|
|
|
|
' password='.$config['psql_password'].
|
2022-11-28 23:44:44 +00:00
|
|
|
'') or apiresult(['error' => 'Could not connect to postgres: '.pg_last_error()], 500);
|
2022-11-27 20:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function is_ok() {
|
|
|
|
return $this->db !== null;
|
|
|
|
}
|
|
|
|
|
2022-12-01 00:05:56 +00:00
|
|
|
public function escape($str) {
|
|
|
|
return pg_escape_string($this->db, $str);
|
|
|
|
}
|
|
|
|
|
2022-11-27 20:17:38 +00:00
|
|
|
public function query($sql) {
|
2022-12-01 00:05:56 +00:00
|
|
|
$result = pg_query($this->db, $sql);
|
2022-11-27 20:17:38 +00:00
|
|
|
if (!$result) apiresult('The postgres query has failed: '.pg_last_error());
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2022-12-11 17:01:03 +00:00
|
|
|
public function fetch($sql, $cache=null, $only_cached=false) {
|
2022-12-03 14:59:01 +00:00
|
|
|
$sql = trim($sql);
|
|
|
|
cronjob_db_create__check_cachecfg($GLOBALS['ap_software'],
|
|
|
|
$GLOBALS['ap_instance'], $sql, $cache);
|
|
|
|
$cache_key = $GLOBALS['ap_software'].$GLOBALS['ap_instance'].$sql;
|
2022-12-02 15:53:24 +00:00
|
|
|
if ($cache !== null) {
|
2022-12-03 14:59:01 +00:00
|
|
|
$cached = content_cache__get($cache_key);
|
2022-12-02 15:53:24 +00:00
|
|
|
if ($cached !== null)
|
|
|
|
return $cached;
|
|
|
|
}
|
2022-12-11 17:01:03 +00:00
|
|
|
if ($only_cached)
|
|
|
|
return 'not_cached';
|
2022-12-01 00:17:41 +00:00
|
|
|
$result = $this->query($sql);
|
2022-12-02 15:53:24 +00:00
|
|
|
$data = pg_fetch_assoc($result);
|
|
|
|
if ($cache !== null)
|
2022-12-03 14:59:01 +00:00
|
|
|
content_cache__put($cache_key, $cache, $data);
|
2022-12-02 15:53:24 +00:00
|
|
|
return $data;
|
2022-12-01 00:17:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-11 17:01:03 +00:00
|
|
|
public function fetch_all($sql, $cache=null, $only_cached=false) {
|
2022-12-03 14:59:01 +00:00
|
|
|
$sql = trim($sql);
|
|
|
|
cronjob_db_create__check_cachecfg($GLOBALS['ap_software'],
|
|
|
|
$GLOBALS['ap_instance'], $sql, $cache);
|
|
|
|
$cache_key = $GLOBALS['ap_software'].$GLOBALS['ap_instance'].$sql;
|
2022-12-02 15:53:24 +00:00
|
|
|
if ($cache !== null) {
|
2022-12-03 14:59:01 +00:00
|
|
|
$cached = content_cache__get($cache_key);
|
2022-12-02 15:53:24 +00:00
|
|
|
if ($cached !== null)
|
|
|
|
return $cached;
|
|
|
|
}
|
2022-12-11 17:01:03 +00:00
|
|
|
if ($only_cached)
|
|
|
|
return 'not_cached';
|
2022-11-27 20:17:38 +00:00
|
|
|
$data = [];
|
|
|
|
$result = $this->query($sql);
|
|
|
|
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
|
|
|
|
$data []= $line;
|
|
|
|
}
|
|
|
|
pg_free_result($result);
|
2022-12-02 15:53:24 +00:00
|
|
|
if ($cache !== null)
|
2022-12-03 14:59:01 +00:00
|
|
|
content_cache__put($cache_key, $cache, $data);
|
2022-11-27 20:17:38 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function close($result=null) {
|
|
|
|
if ($result !== null)
|
|
|
|
pg_free_result($result);
|
|
|
|
pg_close($this->db);
|
|
|
|
}
|
2022-11-24 22:41:05 +00:00
|
|
|
}
|