This repository was archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathSQL.php
More file actions
175 lines (145 loc) · 4.46 KB
/
Copy pathSQL.php
File metadata and controls
175 lines (145 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace includes;
// class SQL, interface with mysqli, it's a singleton, non-static method can be call staticly
// Build for PHP 5.3
class SQL
{
// singleton
private static $instance;
// warper obj
private static $pdo_obj;
//=====================
// singleton
//=====================
// singleton pattern, code from php.net
// fucking parameters ... I don't find a way to use $args and call construtor with it ...
public static function singleton()
{
if (!isset(self::$instance)) {
$className = __CLASS__;
require CONFIG_PATH . 'dbconnect.php';
self::$instance = new $className($mysql_serveur, $mysql_user, $mysql_pass, $mysql_database);
}
return self::$instance;
}
public static function existsDatabase(string $name) : bool
{
$instance = self::singletonWithoutDb();
$res = $instance->query('SHOW DATABASES');
foreach ($res->fetch_all() as $database) {
if ($name === $database['Database']) {
$instance->select_db($name);
return true;
}
}
return in_array($name, $res->fetch_all(), true);
}
public static function singletonWithoutDb()
{
require CONFIG_PATH . 'dbconnect.php';
return new self($mysql_serveur, $mysql_user, $mysql_pass, '');
}
public function initialized()
{
return isset(self::$instance);
}
private function __construct(string $server, string $user, string $host, string $database)
{
self::$pdo_obj = new Database($server, $user, $host, $database);
}
public function __clone() { error_handler('Clone is not allowed.', E_USER_ERROR); }
public function __wakeup() { error_handler('Unserializing is not allowed.', E_USER_ERROR); }
/**
* for call staticly dynamic fx (doesn't use instance vars and doesn't use singleton ;-) )
*
* @deprecated
*/
public static function __callStatic($name, $args)
{
self::singleton();
if (method_exists(self::$instance, $name)) {
return call_user_func_array([self::$instance, $name], $args);
}
if (method_exists(self::$pdo_obj, $name)) {
return call_user_func_array([self::$pdo_obj, $name], $args);
}
throw new \Exception(sprintf('The required method "%s" does not exist for %s', $name, get_class(self::$instance)));
}
//=====================
// warper
//=====================
// isset on the warped obj
public function __isset($name)
{
return isset(self::$pdo_obj->$name);
}
// get on the warped obj
public function __get($name)
{
return self::$pdo_obj->$name;
}
// isset on the warped obj
public function __set($name, $value)
{
self::$pdo_obj->$name = $value;
}
// unset on the warped obj
public function __unset($name)
{
unset(self::$pdo_obj->$name);
}
// call on the warped obj
public function __call($name, $args)
{
return call_user_func_array(array(self::$pdo_obj, $name), $args);
}
// call on the warped obj
public static function getVar($name)
{
return self::$pdo_obj->$name;
}
/**
* Retourne l'objet DB
*
* @return \includes\Database
*/
public function getPdoObj()
{
return self::$pdo_obj;
}
}
class Database extends \mysqli
{
public function __construct($host, $username, $passwd, $dbname)
{
/* activate reporting */
$driver = new \mysqli_driver();
// @TODO: mettre ALL quand on voudra travailler dessus;
$driver->report_mode = MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX;
parent::__construct($host, $username, $passwd, $dbname);
$this->query('SET NAMES \'utf8\';');
$this->query("SET @@SESSION.sql_mode='';");
}
public function query($query, $resultmode = MYSQLI_STORE_RESULT) : Database_MySQLi_Result
{
unset($resultmode);
$this->real_query($query);
return new Database_MySQLi_Result($this);
}
public function quote($escapestr)
{
return $this->escape_string($escapestr);
}
}
class Database_MySQLi_Result extends \mysqli_result
{
public function fetch_all($result_type = null)
{
unset($result_type);
$rows = array();
while($row = $this->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}