-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql.php
136 lines (133 loc) · 3.77 KB
/
sql.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
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* The xSQL function is supposed to run a SQL query and return the appropriate
* results.
* For select, it returns 2D associative arrays
* For UPDATE and DELETE it returns affectedRows
* For INSERT it returns LastInsertID, if no auto-increment found affectedRows
* is returned
*
* The query should be in format "SELECT * FROM table WHERE Title=?",$Title
*
* @param string $Query
* @throws Exception if no database interface found.
*/
class xSQL
{
public static $DB = null;
static function SQL($Query)
{
$args = func_get_args ();
if (get_class ( self::$DB ) == "PDO")
return call_user_func_array ( "self::SQL_pdo", $args );
else
if (get_class ( self::$DB ) == "mysqli")
return call_user_func_array ( "self::SQL_mysqli", $args );
else
throw new Exception ( "Unknown database interface type." );
}
function SQL_pdo($Query)
{
$args = func_get_args ();
if (count ( $args ) == 1)
{
$result = self::$DB->query ( $Query );
if ($result->rowCount ())
{
return $result->fetchAll ( PDO::FETCH_ASSOC );
}
return null;
}
else
{
if (! $stmt = self::$DB->prepare ( $Query ))
{
$Error = self::$DB->errorInfo ();
trigger_error ( "Unable to prepare statement: {$Query}, reason: {$Error[2]}" );
}
array_shift ( $args ); // remove $Query from args
$i = 0;
foreach ( $args as &$v )
$stmt->bindValue ( ++ $i, $v );
$stmt->execute ();
$type = substr ( trim ( strtoupper ( $Query ) ), 0, 6 );
if ($type == "INSERT")
{
$res = self::$DB->lastInsertId ();
if ($res == 0)
return self::$DB->rowCount ();
return $res;
}
elseif ($type == "DELETE" or $type == "UPDATE" or $type == "REPLAC")
return self::$DB->rowCount ();
elseif ($type == "SELECT")
return $stmt->fetchAll ( PDO::FETCH_ASSOC );
}
}
function SQL_mysqli( $Query)
{
$args = func_get_args ();
if (count ( $args ) == 1)
{
$result = self::$DB->query ( $Query );
if ($result->num_rows)
{
$out = array ();
while ( null != ($r = $result->fetch_array ( MYSQLI_ASSOC )) )
$out [] = $r;
return $out;
}
return null;
}
else
{
if (! $preparedStatement = self::$DB->prepare ( $Query ))
trigger_error ( "Unable to prepare statement: {$Query}, reason: {self::$DB->error}" );
array_shift ( $args ); // remove $Query from args
$a = array ();
foreach ( $args as $k => &$v )
$a [$k] = &$v;
$types = str_repeat ( "s", count ( $args ) ); // all params are
// strings, works well on
// MySQL
// and SQLite
array_unshift ( $a, $types );
call_user_func_array ( array ($preparedStatement, 'bind_param' ), $a );
$preparedStatement->execute ();
$type = substr ( trim ( strtoupper ( $Query ) ), 0, 6 );
if ($type == "INSERT")
{
$res = self::$DB->insert_id;
if ($res == 0)
return self::$DB->affected_rows;
return $res;
}
elseif ($type == "DELETE" or $type == "UPDATE" or $type == "REPLAC")
return self::$DB->affected_rows;
elseif ($type == "SELECT")
{
// fetching all results in a 2D array
$metadata = $preparedStatement->result_metadata ();
$out = array ();
$fields = array ();
if (! $metadata)
return null;
while ( null != ($field = $metadata->fetch_field ()) )
$fields [] = &$out [$field->name];
call_user_func_array ( array ($preparedStatement, "bind_result" ), $fields );
$output = array ();
$count = 0;
while ( $preparedStatement->fetch () )
{
foreach ( $out as $k => $v )
$output [$count] [$k] = $v;
$count ++;
}
$preparedStatement->free_result ();
return ($count == 0) ? null : $output;
}
else
return null;
}
}
}