Warning: chmod(): Operation not permitted in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [0] in function chmod in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [1] in function include_once in /var/www/hopeinstoughton_www/wp-settings.php on line 560 [2] in function require_once in /var/www/hopeinstoughton_www/wp-config.php on line 85 [3] in function require_once in /var/www/hopeinstoughton_www/wp-load.php on line 50 [4] in function require_once in /var/www/hopeinstoughton_www/wp-blog-header.php on line 13 [5] in function require in /var/www/hopeinstoughton_www/index.php on line 17
| Server IP : 94.177.8.99 / Your IP : 216.73.217.165 Web Server : Apache/2.4.58 (Ubuntu) System : Linux aries 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : www-data ( 33) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/patientapps_support/modules/database/ |
Upload File : |
<?php
namespace modules\database;
// table models will use something based off this trait
require_once __DIR__ . '/model/engine_generic.php';
trait traits {
private $_database_engine = FALSE;
private $_autoinc = FALSE;
protected $requested_engine = 'auto';
private $collection = FALSE;
private $pointer = 0;
public $lastsql;
public function __construct() {
$this->collection = FALSE;
$this->lastsql = FALSE;
}
public function engine() {
if (!func_num_args()) {
return $this->requested_engine;
} else {
$old = $this->requested_engine;
$this->requested_engine = func_get_arg(0);
if ($old != $this->requested_engine); {
$this->_database_engine = FALSE;
}
return $this;
}
}
private function _database_traits_init() {
if ($this->_database_engine === FALSE) {
// no engine assigned to this database model, so assign one
$this->_database_engine = GetEngine($this->requested_engine);
}
}
public function DatabaseEngine() {
if (func_num_args()) {
$this->_database_engine = func_get_arg(0);
return $this;
}
return $this->_database_engine;
}
public function autoinc() {
if (func_num_args()) {
$this->_autoinc = func_get_arg(0);
return $this;
}
return $this->_autoinc;
}
public function query($sql) {
$this->collection = FALSE; // begin new query => clear out the old
$params = func_get_args();
$this->_database_traits_init();
if (!empty($this->_database_engine)) {
$filler = $this->_database_engine->_state(); // reset Where/Limit/OrderBy/GroupBy
$this->lastsql = call_user_func_array([$this->_database_engine, 'QuerySQL'], $params);
} else {
$this->lastsql = '';
}
return $this;
}
public function callback(Callable $callback) {
$tablename = $this->tablename();
if ($this->lastsql === FALSE && !empty($tablename) ) $this->lastsql = "select * from `{$tablename}`";
$sqlresult = $this->_database_engine->execute($this->lastsql);
if (is_object($sqlresult)) {
$this->_database_engine->callable($this, $sqlresult, $callback);
$sqlresult->free();
}
return $this;
}
// "SELECT TOP (top_value)"
public function limit() {
$this->_database_traits_init();
if (func_num_args()) {
if (!empty($this->_database_engine)) $this->_database_engine->limit(func_get_arg(0));
return $this;
}
return $this->_database_engine->limit();
}
public function tablename() {
$this->_database_traits_init();
if (func_num_args()) {
if (!empty($this->_database_engine)) $this->_database_engine->tablename(func_get_arg(0));
return $this;
}
return empty($this->_database_engine) ? '' : $this->_database_engine->tablename();
}
public function where() {
$this->_database_traits_init();
if (func_num_args()) {
call_user_func_array([$this->_database_engine, 'where'], func_get_args());
return $this;
}
return $this->_database_engine->where();
}
public function wherein($fieldname, $valuelist) {
$this->_database_traits_init();
$this->_database_engine->wherein($fieldname, $valuelist);
return $this;
}
public function orderby() {
$this->_database_traits_init();
call_user_func_array([$this->_database_engine, 'orderby'], func_get_args());
return $this;
}
public function groupby() {
$this->_database_traits_init();
call_user_func_array([$this->_database_engine, 'groupby'], func_get_args());
return $this;
}
public function get() {
$this->_database_traits_init();
if (empty($this->_database_engine)) return NULL;
$tablename = $this->tablename();
if ($this->lastsql === FALSE && !empty($tablename) ) $this->lastsql = "select * from `{$tablename}`";
$hook = $this->hook_query();
if ($hook !== FALSE && hook_exists($hook)) {
hook_execute($hook, $this);
}
$sqlresult = $this->_database_engine->execute($this->lastsql);
$this->pointer = 0;
if (is_object($sqlresult)) {
$this->collection = $this->_database_engine->AsCollection($sqlresult, $this);
$sqlresult->free();
return $this->collection;
}
return NULL;
}
// execute a standard hook to extend the query, and also to format results
public function hook_query() {
$this->_database_traits_init();
if (func_num_args()) {
if (!empty($this->_database_engine)) $this->_database_engine->hook_query(func_get_arg(0));
return $this;
}
return $this->_database_engine->hook_query();
}
public function UpdateSql_AddField($field) {
$this->_database_traits_init();
if (!empty($this->_database_engine)) $this->_database_engine->UpdateSql_AddField($field);
return $this;
}
public function first() {
if ($this->collection === FALSE) {
$this->limit(1)->get();
}
$this->pointer = 0;
if (is_bool($this->collection)) {
return new \database_record($this); //
} elseif (count($this->collection) > $this->pointer) {
return $this->collection[$this->pointer];
} else {
return new \database_record($this->collection);
}
}
// defaults used only when a new record
public function InsertOrUpdate($key, $payload, $default = NULL) {
$this->_database_traits_init();
$state = $this->State();
$tablename = $this->tablename();
// if ($tablename == 'users_source') var_dump($key);
if (is_array($key) && !count($key)) {
$data = NULL;
} else {
$data = $this->query("select * from `{$tablename}`")->where($key)->first();
}
if ($data === NULL) $data = new \database_record($this);
if ($this->autoinc()) $data->autoinc($this->autoinc());
$data->tablename($tablename);
if ($data->empty()) { // not found - create record, but key must be an associative array
$payload = $this->_array_join($key, $payload);
if (!is_null($default)) $payload = $payload + $default;
$data->LoadFromSource($payload);
$data->empty(TRUE);
} else { // update existing record
$data->LoadFromSource($payload, TRUE);
}
$data->SaveRecord($key);
$this->State($state);
return $data;
}
public function Delete($key) {
$this->_database_traits_init();
$state = $this->State();
$tablename = $this->tablename();
$this->query("delete from `{$tablename}`")->where($key)->get();
$this->State($state);
return TRUE;
}
// $keys could be "fieldname=value and fieldname2=value"
private function _array_join($keys, $payload) {
if (!is_array($keys)) throw new \Exception('keys must be an array' .var_export($keys, 1));
reset($keys);
$x = key($keys);
if (is_numeric($keys)) throw new \Exception('keys must be an associative array (fieldname => value)');
return $keys + $payload;
}
/* protected function loadDefinition($tablename) {
// load the current [internal] definition for the table
$this->_database_traits_init();
return $this->_database_engine->GetInternalTableDefinition($tablename);
} */
private function _displayEntry($entry) {
$function = $entry['function'];
if (!empty($entry['class'])) $function = $entry['class'] . '::' . $function;
echo $function . " [{$entry['file']}:{$entry['line']}]<br>\n";
}
public function DbErr($flags = 0) {
$this->_database_traits_init();
if ($flags) {
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach($bt as $bt_item) {
echo $this->_displayEntry($bt_item);
}
echo "\nLast Query: {$this->_database_engine->lastsql}";
} elseif ($this->_database_engine->errnum) {
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach($bt as $bt_item) {
echo $this->_displayEntry($bt_item);
}
echo "\nLast Query: {$this->_database_engine->lastsql}";
die("\nDatabase error: {$this->_database_engine->errmsg} [{$this->_database_engine->errnum}]\n");
}
}
public function Database_Reopen() {
$this->_database_traits_init();
$this->_database_engine->close();
$this->_database_engine->open();
}
private function State() {
$res = call_user_func_array([$this->_database_engine, '_state'], func_get_args());
return func_num_args() > 0 ? $this : $res;
}
public function debug() {
$res = call_user_func_array([$this->_database_engine, 'debug'], func_get_args());
return func_num_args() > 0 ? $this : $res;
}
}
require_once __DIR__ . '/model/database_record.php';