5
* @description Database handler for WebmasterKit
6
* @author Pietro Albini <pietro98.albini@gmail.com>
9
defined("WEBMASTERKIT") or die("Restricted access");
11
interface wkDatabaseBase
13
public function GetDSN($args);
19
public $handler; // PDO handler
20
protected $type; // Database type
21
protected $db_tools; // Database engine tools
24
public $in_transaction; // Transation is running or not
25
public $is_connect; // Is connected
26
public $num_queries; // Numer of queries
27
public $error; // Last error
30
public $engines; // Available engines
31
public $insert_id; // The last insered item's id
32
public $affected_rows; // Affected rows
36
* Initialize the class
38
* @param $auto_connect Auto connect to the database
42
public function __construct($auto_connect=true)
44
$this->handler = false;
46
$this->db_tool = false;
48
$this->in_transation = false;
49
$this->is_connect = false;
50
$this->num_queries = 0;
53
$this->engines = $this->GetEngines();
55
$this->affected_rows = 0;
57
if($auto_connect and wk::$config->database_auto_connect)
59
$args = array_merge(array(wk::$config->database_engine, wk::$config->database_username, wk::$config->database_password), wk::$config->database_options);
72
public function __destruct()
83
* Prevent to cloning the class
88
public function __clone()
90
if(wk::$config->database_prevent_cloning)
93
trigger_error("You cannot clone a database instance", E_USER_ERROR);
99
* Get the list of available engines
104
public function GetEngines()
106
$engines = PDO::getAvailableDrivers();
107
foreach($engines as &$engine)
109
if(!wk::$tools->HasLibrary('database', $engine))
119
* Connect to a database engine
121
* @param $engine The database engine
122
* @param $username Your username
123
* @param $password Your password
124
* @param * Connection params
128
public function Connect()
132
$args = func_get_args();
133
$engine = array_shift($args);
134
$username = array_shift($args);
135
$password = array_shift($args);
137
if(in_array($engine, $this->GetEngines()))
139
$db_tools = 'wkDatabase'.ucfirst($engine);
140
$this->db_tools = new $db_tools();
141
$dsn = $this->db_tools->GetDSN($args);
145
$this->handler = new PDO($dsn, $username, $password);
146
$this->handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
147
$this->type = $engine;
148
$this->is_connect = true;
150
catch (PDOException $e)
152
$this->error = $e->getMessage();
153
trigger_error('Impossibile connettersi al database', E_USER_ERROR);
159
trigger_error('Il database engine richiesto non è disponibile', E_USER_WARNING);
165
trigger_error('Non è possibile connettersi poiché si è già connessi', E_USER_WARNING);
172
* Close the connection
177
public function Close()
181
$this->handler = false;
182
$this->is_connect = false;
184
$this->db_tools = false;
189
trigger_error('Non è possibile chiudere la connessione poiché non si è connessi', E_USER_WARNING);
198
* @param $sql The query
202
public function Query($sql)
206
if(substr(trim($sql), -1) == ";")
208
$sql = substr(trim($sql), 0, -1);
210
$queries = explode($sql, ";");
211
if(count($queries) > 1)
213
trigger_error("Non sono permesse più query contemporaneamente", E_USER_ERROR);
218
$is_select = preg_match("/^select$/i", $sql);
223
$result = $this->handler->query($sql);
225
catch(PDOException $e)
227
$this->error = $e->getMessage();
228
trigger_error("Si è verificato un errore durante l'esecuzione della query", E_USER_ERROR);
234
$return = new wkDatabaseResult($this, $this->handler, $result);
245
$result = $this->handler->exec($sql);
247
catch(PDOException $e)
249
$this->error = $e->getMessage();
250
trigger_error("Si è verificato un errore durante l'esecuzione della query", E_USER_ERROR);
256
$this->affected_rows = $affected;
257
$is_insert = preg_match("/^insert$/i", $sql);
260
$this->insert_id = $this->handler->lastInsertId();