-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.php
124 lines (117 loc) · 3.96 KB
/
database.php
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
<?php
require_once("config.php");
require_once("logger.php");
function dbConnect()
{
$mysqli = new mysqli(DBADDR, DBUSER, DBPASS, DBNAME);
if ($mysqli->connect_errno)
{
LogMessage("(".$mysqli->connect_errno.") ".$mysqli->connect_error, "mysql_connect.log");
exit;
}
$mysqli->set_charset("utf8");
return $mysqli;
}
function dbClose($mysqli)
{
if($mysqli->errno)
LogMessage("(".$mysqli->errno.") ".$mysqli->error, "mysql.log", true);
$mysqli->close();
}
function dbUpdate($query,$parametersType = null,$parameters = null, $returnType = DatabaseReturns::RETURN_BOOLEAN)
{
$res = false;
$dbConn = dbConnect();
if($st = $dbConn->prepare($query))
{
if($parametersType!=null)
call_user_func_array(array($st, 'bind_param'), array_merge(array($parametersType), makeValuesReferenced($parameters)));
$res = $st->execute();
switch($returnType)
{
case DatabaseReturns::RETURN_BOOLEAN:
//è il valore salvato di default in $res
break;
case DatabaseReturns::RETURN_AFFECTED_ROWS:
$res = $dbConn->affected_rows;
break;
case DatabaseReturns::RETURN_INSERT_ID:
$res = $dbConn->insert_id;
break;
}
$st->close();
}
dbClose($dbConn);
return $res;
}
function dbSelect($query,$parametersType = null,$parameters = null,$oneRow = false)
{
$res = false;
$dbConn = dbConnect();
if($st = $dbConn->prepare($query))
{
if($parametersType!=null)
call_user_func_array(array($st, 'bind_param'), array_merge(array($parametersType), makeValuesReferenced($parameters)));
if($st->execute())
{
if($oneRow)
$res = null;
else
$res = array();
$result = $st->get_result();
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
if($oneRow)
{
$res = $row;
break;
}
else
array_push($res,$row);
}
}
$st->close();
}
dbClose($dbConn);
return $res;
}
function makeValuesReferenced(&$arr)
{
$refs = array();
foreach($arr as $key => $value)
$refs[$key] = &$arr[$key];
return $refs;
}
abstract class DatabaseReturns extends BasicEnum
{
const RETURN_BOOLEAN = 1;
const RETURN_AFFECTED_ROWS = 2;
const RETURN_INSERT_ID = 3;
}
abstract class BasicEnum {
private static $constCacheArray = NULL;
public static function getConstants() {
if (self::$constCacheArray == NULL) {
self::$constCacheArray = array();
}
$calledClass = get_called_class();
if (!array_key_exists($calledClass, self::$constCacheArray)) {
$reflect = new ReflectionClass($calledClass);
self::$constCacheArray[$calledClass] = $reflect->getConstants();
}
return self::$constCacheArray[$calledClass];
}
public static function isValidName($name, $strict = false) {
$constants = self::getConstants();
if ($strict) {
return array_key_exists($name, $constants);
}
$keys = array_map('strtolower', array_keys($constants));
return in_array(strtolower($name), $keys);
}
public static function isValidValue($value, $strict = true) {
$values = array_values(self::getConstants());
return in_array($value, $values, $strict);
}
}
?>