1 | <?php |
||
12 | abstract class DB { |
||
13 | |||
14 | private static $link = null, $last = null, $log = [], $time = 0; |
||
15 | |||
16 | /** |
||
17 | * Connect to a database |
||
18 | */ |
||
19 | |||
20 | public static function connect(string $server, string $user, string $password, string $name) { |
||
40 | |||
41 | /** |
||
42 | * Select a database |
||
43 | */ |
||
44 | |||
45 | public static function name(string $name) { |
||
49 | |||
50 | /** |
||
51 | * Send a query |
||
52 | * |
||
53 | * @return DB\Result|false : the result object or false on failure |
||
54 | */ |
||
55 | |||
56 | public static function send(string $query) { |
||
57 | |||
58 | if (null === self::$link) return (self::$last = false); |
||
59 | |||
60 | $time = microtime(true); $result = mysqli_query(self::$link, $query); $time = (microtime(true) - $time); |
||
61 | |||
62 | self::$last = new DB\Result(self::$link, $result, $query, $time); |
||
63 | |||
64 | self::$log[] = self::$last; self::$time += $time; |
||
65 | |||
66 | # ------------------------ |
||
67 | |||
68 | return self::$last; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Send a select query |
||
73 | * |
||
74 | * @param $table a table name |
||
75 | * @param $selection a string or an array where each value is a field name |
||
76 | * @param $condition a string or an array where each key is a field name and each value is a field value |
||
77 | * @param $order a string or an array where each key is a field name and each value is a sorting direction (ASC or DESC) |
||
78 | * @param $limit a maximum number of rows to be selected |
||
79 | * |
||
80 | * @return DB\Result|false : the result object or false on failure |
||
81 | */ |
||
82 | |||
83 | public static function select(string $table, $selection, $condition = null, $order = null, int $limit = 0) { |
||
87 | |||
88 | /** |
||
89 | * Send an insert query |
||
90 | * |
||
91 | * @param $table a table name |
||
92 | * @param $set an array where each key is a field name and each value is a field value, or an array of such arrays |
||
93 | * @param $multiple tells that the set must be interpreted as a multi-dimensional array (for multi-row inserts) |
||
94 | * @param $ignore tells to ignore insert errors, such as a duplicate-key error |
||
95 | * |
||
96 | * @return DB\Result|false : the result object or false on failure |
||
97 | */ |
||
98 | |||
99 | public static function insert(string $table, array $set, bool $multiple = false, bool $ignore = false) { |
||
103 | |||
104 | /** |
||
105 | * Send an update query |
||
106 | * |
||
107 | * @param $table a table name |
||
108 | * @param $set an array where each key is a field name and each value is a field value |
||
109 | * @param $condition a string or an array where each key is a field name and each value is a field value |
||
110 | * |
||
111 | * @return DB\Result|false : the result object or false on failure |
||
112 | */ |
||
113 | |||
114 | public static function update(string $table, array $set, $condition = null) { |
||
118 | |||
119 | /** |
||
120 | * Send a delete query |
||
121 | * |
||
122 | * @param $table a table name |
||
123 | * @param $condition a string or an array where each key is a field name and each value is a field value |
||
124 | * |
||
125 | * @return DB\Result|false : the result object or false on failure |
||
126 | */ |
||
127 | |||
128 | public static function delete(string $table, $condition = null) { |
||
132 | |||
133 | /** |
||
134 | * Get the last result object |
||
135 | * |
||
136 | * @return DB\Result|false|null : the result object or false if the last query failed or null if there have been no queries sent |
||
137 | */ |
||
138 | |||
139 | public static function getLast() { |
||
143 | |||
144 | /** |
||
145 | * Get the array of all the result objects |
||
146 | */ |
||
147 | |||
148 | public static function getLog() : array { |
||
152 | |||
153 | /** |
||
154 | * Get the total time of all the queries sent |
||
155 | */ |
||
156 | |||
157 | public static function getTime() : string { |
||
161 | } |
||
162 | } |
||
163 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.