webmaster@1
|
1 <?php |
webmaster@1
|
2 // $Id: database.inc,v 1.92.2.1 2008/02/08 22:44:59 goba Exp $ |
webmaster@1
|
3 |
webmaster@1
|
4 /** |
webmaster@1
|
5 * @file |
webmaster@1
|
6 * Wrapper for database interface code. |
webmaster@1
|
7 */ |
webmaster@1
|
8 |
webmaster@1
|
9 /** |
webmaster@1
|
10 * A hash value to check when outputting database errors, md5('DB_ERROR'). |
webmaster@1
|
11 * |
webmaster@1
|
12 * @see drupal_error_handler() |
webmaster@1
|
13 */ |
webmaster@1
|
14 define('DB_ERROR', 'a515ac9c2796ca0e23adbe92c68fc9fc'); |
webmaster@1
|
15 |
webmaster@1
|
16 /** |
webmaster@1
|
17 * @defgroup database Database abstraction layer |
webmaster@1
|
18 * @{ |
webmaster@1
|
19 * Allow the use of different database servers using the same code base. |
webmaster@1
|
20 * |
webmaster@1
|
21 * Drupal provides a slim database abstraction layer to provide developers with |
webmaster@1
|
22 * the ability to support multiple database servers easily. The intent of this |
webmaster@1
|
23 * layer is to preserve the syntax and power of SQL as much as possible, while |
webmaster@1
|
24 * letting Drupal control the pieces of queries that need to be written |
webmaster@1
|
25 * differently for different servers and provide basic security checks. |
webmaster@1
|
26 * |
webmaster@1
|
27 * Most Drupal database queries are performed by a call to db_query() or |
webmaster@1
|
28 * db_query_range(). Module authors should also consider using pager_query() for |
webmaster@1
|
29 * queries that return results that need to be presented on multiple pages, and |
webmaster@1
|
30 * tablesort_sql() for generating appropriate queries for sortable tables. |
webmaster@1
|
31 * |
webmaster@1
|
32 * For example, one might wish to return a list of the most recent 10 nodes |
webmaster@1
|
33 * authored by a given user. Instead of directly issuing the SQL query |
webmaster@1
|
34 * @code |
webmaster@1
|
35 * SELECT n.title, n.body, n.created FROM node n WHERE n.uid = $uid LIMIT 0, 10; |
webmaster@1
|
36 * @endcode |
webmaster@1
|
37 * one would instead call the Drupal functions: |
webmaster@1
|
38 * @code |
webmaster@1
|
39 * $result = db_query_range('SELECT n.title, n.body, n.created |
webmaster@1
|
40 * FROM {node} n WHERE n.uid = %d', $uid, 0, 10); |
webmaster@1
|
41 * while ($node = db_fetch_object($result)) { |
webmaster@1
|
42 * // Perform operations on $node->body, etc. here. |
webmaster@1
|
43 * } |
webmaster@1
|
44 * @endcode |
webmaster@1
|
45 * Curly braces are used around "node" to provide table prefixing via |
webmaster@1
|
46 * db_prefix_tables(). The explicit use of a user ID is pulled out into an |
webmaster@1
|
47 * argument passed to db_query() so that SQL injection attacks from user input |
webmaster@1
|
48 * can be caught and nullified. The LIMIT syntax varies between database servers, |
webmaster@1
|
49 * so that is abstracted into db_query_range() arguments. Finally, note the |
webmaster@1
|
50 * common pattern of iterating over the result set using db_fetch_object(). |
webmaster@1
|
51 */ |
webmaster@1
|
52 |
webmaster@1
|
53 /** |
webmaster@1
|
54 * Perform an SQL query and return success or failure. |
webmaster@1
|
55 * |
webmaster@1
|
56 * @param $sql |
webmaster@1
|
57 * A string containing a complete SQL query. %-substitution |
webmaster@1
|
58 * parameters are not supported. |
webmaster@1
|
59 * @return |
webmaster@1
|
60 * An array containing the keys: |
webmaster@1
|
61 * success: a boolean indicating whether the query succeeded |
webmaster@1
|
62 * query: the SQL query executed, passed through check_plain() |
webmaster@1
|
63 */ |
webmaster@1
|
64 function update_sql($sql) { |
webmaster@1
|
65 $result = db_query($sql, true); |
webmaster@1
|
66 return array('success' => $result !== FALSE, 'query' => check_plain($sql)); |
webmaster@1
|
67 } |
webmaster@1
|
68 |
webmaster@1
|
69 /** |
webmaster@1
|
70 * Append a database prefix to all tables in a query. |
webmaster@1
|
71 * |
webmaster@1
|
72 * Queries sent to Drupal should wrap all table names in curly brackets. This |
webmaster@1
|
73 * function searches for this syntax and adds Drupal's table prefix to all |
webmaster@1
|
74 * tables, allowing Drupal to coexist with other systems in the same database if |
webmaster@1
|
75 * necessary. |
webmaster@1
|
76 * |
webmaster@1
|
77 * @param $sql |
webmaster@1
|
78 * A string containing a partial or entire SQL query. |
webmaster@1
|
79 * @return |
webmaster@1
|
80 * The properly-prefixed string. |
webmaster@1
|
81 */ |
webmaster@1
|
82 function db_prefix_tables($sql) { |
webmaster@1
|
83 global $db_prefix; |
webmaster@1
|
84 |
webmaster@1
|
85 if (is_array($db_prefix)) { |
webmaster@1
|
86 if (array_key_exists('default', $db_prefix)) { |
webmaster@1
|
87 $tmp = $db_prefix; |
webmaster@1
|
88 unset($tmp['default']); |
webmaster@1
|
89 foreach ($tmp as $key => $val) { |
webmaster@1
|
90 $sql = strtr($sql, array('{'. $key .'}' => $val . $key)); |
webmaster@1
|
91 } |
webmaster@1
|
92 return strtr($sql, array('{' => $db_prefix['default'], '}' => '')); |
webmaster@1
|
93 } |
webmaster@1
|
94 else { |
webmaster@1
|
95 foreach ($db_prefix as $key => $val) { |
webmaster@1
|
96 $sql = strtr($sql, array('{'. $key .'}' => $val . $key)); |
webmaster@1
|
97 } |
webmaster@1
|
98 return strtr($sql, array('{' => '', '}' => '')); |
webmaster@1
|
99 } |
webmaster@1
|
100 } |
webmaster@1
|
101 else { |
webmaster@1
|
102 return strtr($sql, array('{' => $db_prefix, '}' => '')); |
webmaster@1
|
103 } |
webmaster@1
|
104 } |
webmaster@1
|
105 |
webmaster@1
|
106 /** |
webmaster@1
|
107 * Activate a database for future queries. |
webmaster@1
|
108 * |
webmaster@1
|
109 * If it is necessary to use external databases in a project, this function can |
webmaster@1
|
110 * be used to change where database queries are sent. If the database has not |
webmaster@1
|
111 * yet been used, it is initialized using the URL specified for that name in |
webmaster@1
|
112 * Drupal's configuration file. If this name is not defined, a duplicate of the |
webmaster@1
|
113 * default connection is made instead. |
webmaster@1
|
114 * |
webmaster@1
|
115 * Be sure to change the connection back to the default when done with custom |
webmaster@1
|
116 * code. |
webmaster@1
|
117 * |
webmaster@1
|
118 * @param $name |
webmaster@1
|
119 * The name assigned to the newly active database connection. If omitted, the |
webmaster@1
|
120 * default connection will be made active. |
webmaster@1
|
121 * |
webmaster@1
|
122 * @return the name of the previously active database or FALSE if non was found. |
webmaster@1
|
123 */ |
webmaster@1
|
124 function db_set_active($name = 'default') { |
webmaster@1
|
125 global $db_url, $db_type, $active_db; |
webmaster@1
|
126 static $db_conns, $active_name = FALSE; |
webmaster@1
|
127 |
webmaster@1
|
128 if (empty($db_url)) { |
webmaster@1
|
129 include_once 'includes/install.inc'; |
webmaster@1
|
130 install_goto('install.php'); |
webmaster@1
|
131 } |
webmaster@1
|
132 |
webmaster@1
|
133 if (!isset($db_conns[$name])) { |
webmaster@1
|
134 // Initiate a new connection, using the named DB URL specified. |
webmaster@1
|
135 if (is_array($db_url)) { |
webmaster@1
|
136 $connect_url = array_key_exists($name, $db_url) ? $db_url[$name] : $db_url['default']; |
webmaster@1
|
137 } |
webmaster@1
|
138 else { |
webmaster@1
|
139 $connect_url = $db_url; |
webmaster@1
|
140 } |
webmaster@1
|
141 |
webmaster@1
|
142 $db_type = substr($connect_url, 0, strpos($connect_url, '://')); |
webmaster@1
|
143 $handler = "./includes/database.$db_type.inc"; |
webmaster@1
|
144 |
webmaster@1
|
145 if (is_file($handler)) { |
webmaster@1
|
146 include_once $handler; |
webmaster@1
|
147 } |
webmaster@1
|
148 else { |
webmaster@1
|
149 _db_error_page("The database type '". $db_type ."' is unsupported. Please use either 'mysql' or 'mysqli' for MySQL, or 'pgsql' for PostgreSQL databases."); |
webmaster@1
|
150 } |
webmaster@1
|
151 |
webmaster@1
|
152 $db_conns[$name] = db_connect($connect_url); |
webmaster@1
|
153 } |
webmaster@1
|
154 |
webmaster@1
|
155 $previous_name = $active_name; |
webmaster@1
|
156 // Set the active connection. |
webmaster@1
|
157 $active_name = $name; |
webmaster@1
|
158 $active_db = $db_conns[$name]; |
webmaster@1
|
159 |
webmaster@1
|
160 return $previous_name; |
webmaster@1
|
161 } |
webmaster@1
|
162 |
webmaster@1
|
163 /** |
webmaster@1
|
164 * Helper function to show fatal database errors. |
webmaster@1
|
165 * |
webmaster@1
|
166 * Prints a themed maintenance page with the 'Site off-line' text, |
webmaster@1
|
167 * adding the provided error message in the case of 'display_errors' |
webmaster@1
|
168 * set to on. Ends the page request; no return. |
webmaster@1
|
169 * |
webmaster@1
|
170 * @param $error |
webmaster@1
|
171 * The error message to be appended if 'display_errors' is on. |
webmaster@1
|
172 */ |
webmaster@1
|
173 function _db_error_page($error = '') { |
webmaster@1
|
174 global $db_type; |
webmaster@1
|
175 drupal_maintenance_theme(); |
webmaster@1
|
176 drupal_set_header('HTTP/1.1 503 Service Unavailable'); |
webmaster@1
|
177 drupal_set_title('Site off-line'); |
webmaster@1
|
178 |
webmaster@1
|
179 $message = '<p>The site is currently not available due to technical problems. Please try again later. Thank you for your understanding.</p>'; |
webmaster@1
|
180 $message .= '<hr /><p><small>If you are the maintainer of this site, please check your database settings in the <code>settings.php</code> file and ensure that your hosting provider\'s database server is running. For more help, see the <a href="http://drupal.org/node/258">handbook</a>, or contact your hosting provider.</small></p>'; |
webmaster@1
|
181 |
webmaster@1
|
182 if ($error && ini_get('display_errors')) { |
webmaster@1
|
183 $message .= '<p><small>The '. theme('placeholder', $db_type) .' error was: '. theme('placeholder', $error) .'.</small></p>'; |
webmaster@1
|
184 } |
webmaster@1
|
185 |
webmaster@1
|
186 print theme('maintenance_page', $message); |
webmaster@1
|
187 exit; |
webmaster@1
|
188 } |
webmaster@1
|
189 |
webmaster@1
|
190 /** |
webmaster@1
|
191 * Returns a boolean depending on the availability of the database. |
webmaster@1
|
192 */ |
webmaster@1
|
193 function db_is_active() { |
webmaster@1
|
194 global $active_db; |
webmaster@1
|
195 return !empty($active_db); |
webmaster@1
|
196 } |
webmaster@1
|
197 |
webmaster@1
|
198 /** |
webmaster@1
|
199 * Helper function for db_query(). |
webmaster@1
|
200 */ |
webmaster@1
|
201 function _db_query_callback($match, $init = FALSE) { |
webmaster@1
|
202 static $args = NULL; |
webmaster@1
|
203 if ($init) { |
webmaster@1
|
204 $args = $match; |
webmaster@1
|
205 return; |
webmaster@1
|
206 } |
webmaster@1
|
207 |
webmaster@1
|
208 switch ($match[1]) { |
webmaster@1
|
209 case '%d': // We must use type casting to int to convert FALSE/NULL/(TRUE?) |
webmaster@1
|
210 return (int) array_shift($args); // We don't need db_escape_string as numbers are db-safe |
webmaster@1
|
211 case '%s': |
webmaster@1
|
212 return db_escape_string(array_shift($args)); |
webmaster@1
|
213 case '%%': |
webmaster@1
|
214 return '%'; |
webmaster@1
|
215 case '%f': |
webmaster@1
|
216 return (float) array_shift($args); |
webmaster@1
|
217 case '%b': // binary data |
webmaster@1
|
218 return db_encode_blob(array_shift($args)); |
webmaster@1
|
219 } |
webmaster@1
|
220 } |
webmaster@1
|
221 |
webmaster@1
|
222 /** |
webmaster@1
|
223 * Generate placeholders for an array of query arguments of a single type. |
webmaster@1
|
224 * |
webmaster@1
|
225 * Given a Schema API field type, return correct %-placeholders to |
webmaster@1
|
226 * embed in a query |
webmaster@1
|
227 * |
webmaster@1
|
228 * @param $arguments |
webmaster@1
|
229 * An array with at least one element. |
webmaster@1
|
230 * @param $type |
webmaster@1
|
231 * The Schema API type of a field (e.g. 'int', 'text', or 'varchar'). |
webmaster@1
|
232 */ |
webmaster@1
|
233 function db_placeholders($arguments, $type = 'int') { |
webmaster@1
|
234 $placeholder = db_type_placeholder($type); |
webmaster@1
|
235 return implode(',', array_fill(0, count($arguments), $placeholder)); |
webmaster@1
|
236 } |
webmaster@1
|
237 |
webmaster@1
|
238 /** |
webmaster@1
|
239 * Indicates the place holders that should be replaced in _db_query_callback(). |
webmaster@1
|
240 */ |
webmaster@1
|
241 define('DB_QUERY_REGEXP', '/(%d|%s|%%|%f|%b)/'); |
webmaster@1
|
242 |
webmaster@1
|
243 /** |
webmaster@1
|
244 * Helper function for db_rewrite_sql. |
webmaster@1
|
245 * |
webmaster@1
|
246 * Collects JOIN and WHERE statements via hook_db_rewrite_sql() |
webmaster@1
|
247 * Decides whether to select primary_key or DISTINCT(primary_key) |
webmaster@1
|
248 * |
webmaster@1
|
249 * @param $query |
webmaster@1
|
250 * Query to be rewritten. |
webmaster@1
|
251 * @param $primary_table |
webmaster@1
|
252 * Name or alias of the table which has the primary key field for this query. |
webmaster@1
|
253 * Typical table names would be: {blocks}, {comments}, {forum}, {node}, |
webmaster@1
|
254 * {menu}, {term_data} or {vocabulary}. However, in most cases the usual |
webmaster@1
|
255 * table alias (b, c, f, n, m, t or v) is used instead of the table name. |
webmaster@1
|
256 * @param $primary_field |
webmaster@1
|
257 * Name of the primary field. |
webmaster@1
|
258 * @param $args |
webmaster@1
|
259 * Array of additional arguments. |
webmaster@1
|
260 * @return |
webmaster@1
|
261 * An array: join statements, where statements, field or DISTINCT(field). |
webmaster@1
|
262 */ |
webmaster@1
|
263 function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_field = 'nid', $args = array()) { |
webmaster@1
|
264 $where = array(); |
webmaster@1
|
265 $join = array(); |
webmaster@1
|
266 $distinct = FALSE; |
webmaster@1
|
267 foreach (module_implements('db_rewrite_sql') as $module) { |
webmaster@1
|
268 $result = module_invoke($module, 'db_rewrite_sql', $query, $primary_table, $primary_field, $args); |
webmaster@1
|
269 if (isset($result) && is_array($result)) { |
webmaster@1
|
270 if (isset($result['where'])) { |
webmaster@1
|
271 $where[] = $result['where']; |
webmaster@1
|
272 } |
webmaster@1
|
273 if (isset($result['join'])) { |
webmaster@1
|
274 $join[] = $result['join']; |
webmaster@1
|
275 } |
webmaster@1
|
276 if (isset($result['distinct']) && $result['distinct']) { |
webmaster@1
|
277 $distinct = TRUE; |
webmaster@1
|
278 } |
webmaster@1
|
279 } |
webmaster@1
|
280 elseif (isset($result)) { |
webmaster@1
|
281 $where[] = $result; |
webmaster@1
|
282 } |
webmaster@1
|
283 } |
webmaster@1
|
284 |
webmaster@1
|
285 $where = empty($where) ? '' : '('. implode(') AND (', $where) .')'; |
webmaster@1
|
286 $join = empty($join) ? '' : implode(' ', $join); |
webmaster@1
|
287 |
webmaster@1
|
288 return array($join, $where, $distinct); |
webmaster@1
|
289 } |
webmaster@1
|
290 |
webmaster@1
|
291 /** |
webmaster@1
|
292 * Rewrites node, taxonomy and comment queries. Use it for listing queries. Do not |
webmaster@1
|
293 * use FROM table1, table2 syntax, use JOIN instead. |
webmaster@1
|
294 * |
webmaster@1
|
295 * @param $query |
webmaster@1
|
296 * Query to be rewritten. |
webmaster@1
|
297 * @param $primary_table |
webmaster@1
|
298 * Name or alias of the table which has the primary key field for this query. |
webmaster@1
|
299 * Typical table names would be: {blocks}, {comments}, {forum}, {node}, |
webmaster@1
|
300 * {menu}, {term_data} or {vocabulary}. However, it is more common to use the |
webmaster@1
|
301 * the usual table aliases: b, c, f, n, m, t or v. |
webmaster@1
|
302 * @param $primary_field |
webmaster@1
|
303 * Name of the primary field. |
webmaster@1
|
304 * @param $args |
webmaster@1
|
305 * An array of arguments, passed to the implementations of hook_db_rewrite_sql. |
webmaster@1
|
306 * @return |
webmaster@1
|
307 * The original query with JOIN and WHERE statements inserted from |
webmaster@1
|
308 * hook_db_rewrite_sql implementations. nid is rewritten if needed. |
webmaster@1
|
309 */ |
webmaster@1
|
310 function db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $args = array()) { |
webmaster@1
|
311 list($join, $where, $distinct) = _db_rewrite_sql($query, $primary_table, $primary_field, $args); |
webmaster@1
|
312 |
webmaster@1
|
313 if ($distinct) { |
webmaster@1
|
314 $query = db_distinct_field($primary_table, $primary_field, $query); |
webmaster@1
|
315 } |
webmaster@1
|
316 |
webmaster@1
|
317 if (!empty($where) || !empty($join)) { |
webmaster@1
|
318 $pattern = '{ |
webmaster@1
|
319 # Beginning of the string |
webmaster@1
|
320 ^ |
webmaster@1
|
321 ((?P<anonymous_view> |
webmaster@1
|
322 # Everything within this set of parentheses is named "anonymous view" |
webmaster@1
|
323 (?: |
webmaster@1
|
324 [^()]++ # anything not parentheses |
webmaster@1
|
325 | |
webmaster@1
|
326 \( (?P>anonymous_view) \) # an open parenthesis, more "anonymous view" and finally a close parenthesis. |
webmaster@1
|
327 )* |
webmaster@1
|
328 )[^()]+WHERE) |
webmaster@1
|
329 }x'; |
webmaster@1
|
330 preg_match($pattern, $query, $matches); |
webmaster@1
|
331 if (!$where) { |
webmaster@1
|
332 $where = '1 = 1'; |
webmaster@1
|
333 } |
webmaster@1
|
334 if ($matches) { |
webmaster@1
|
335 $n = strlen($matches[1]); |
webmaster@1
|
336 $second_part = substr($query, $n); |
webmaster@1
|
337 $first_part = substr($matches[1], 0, $n - 5) ." $join WHERE $where AND ( "; |
webmaster@1
|
338 // PHP 4 does not support strrpos for strings. We emulate it. |
webmaster@1
|
339 $haystack_reverse = strrev($second_part); |
webmaster@1
|
340 } |
webmaster@1
|
341 else { |
webmaster@1
|
342 $haystack_reverse = strrev($query); |
webmaster@1
|
343 } |
webmaster@1
|
344 // No need to use strrev on the needle, we supply GROUP, ORDER, LIMIT |
webmaster@1
|
345 // reversed. |
webmaster@1
|
346 foreach (array('PUORG', 'REDRO', 'TIMIL') as $needle_reverse) { |
webmaster@1
|
347 $pos = strpos($haystack_reverse, $needle_reverse); |
webmaster@1
|
348 if ($pos !== FALSE) { |
webmaster@1
|
349 // All needles are five characters long. |
webmaster@1
|
350 $pos += 5; |
webmaster@1
|
351 break; |
webmaster@1
|
352 } |
webmaster@1
|
353 } |
webmaster@1
|
354 if ($matches) { |
webmaster@1
|
355 if ($pos === FALSE) { |
webmaster@1
|
356 $query = $first_part . $second_part .')'; |
webmaster@1
|
357 } |
webmaster@1
|
358 else { |
webmaster@1
|
359 $query = $first_part . substr($second_part, 0, -$pos) .')'. substr($second_part, -$pos); |
webmaster@1
|
360 } |
webmaster@1
|
361 } |
webmaster@1
|
362 elseif ($pos === FALSE) { |
webmaster@1
|
363 $query .= " $join WHERE $where"; |
webmaster@1
|
364 } |
webmaster@1
|
365 else { |
webmaster@1
|
366 $query = substr($query, 0, -$pos) . " $join WHERE $where " . substr($query, -$pos); |
webmaster@1
|
367 } |
webmaster@1
|
368 } |
webmaster@1
|
369 |
webmaster@1
|
370 return $query; |
webmaster@1
|
371 } |
webmaster@1
|
372 |
webmaster@1
|
373 /** |
webmaster@1
|
374 * Restrict a dynamic table, column or constraint name to safe characters. |
webmaster@1
|
375 * |
webmaster@1
|
376 * Only keeps alphanumeric and underscores. |
webmaster@1
|
377 */ |
webmaster@1
|
378 function db_escape_table($string) { |
webmaster@1
|
379 return preg_replace('/[^A-Za-z0-9_]+/', '', $string); |
webmaster@1
|
380 } |
webmaster@1
|
381 |
webmaster@1
|
382 /** |
webmaster@1
|
383 * @} End of "defgroup database". |
webmaster@1
|
384 */ |
webmaster@1
|
385 |
webmaster@1
|
386 /** |
webmaster@1
|
387 * @defgroup schemaapi Schema API |
webmaster@1
|
388 * @{ |
webmaster@1
|
389 * |
webmaster@1
|
390 * A Drupal schema definition is an array structure representing one or |
webmaster@1
|
391 * more tables and their related keys and indexes. A schema is defined by |
webmaster@1
|
392 * hook_schema(), which usually lives in a modulename.install file. |
webmaster@1
|
393 * |
webmaster@1
|
394 * By implementing hook_schema() and specifying the tables your module |
webmaster@1
|
395 * declares, you can easily create and drop these tables on all |
webmaster@1
|
396 * supported database engines. You don't have to deal with the |
webmaster@1
|
397 * different SQL dialects for table creation and alteration of the |
webmaster@1
|
398 * supported database engines. |
webmaster@1
|
399 * |
webmaster@1
|
400 * hook_schema() should return an array with a key for each table that |
webmaster@1
|
401 * the module defines. |
webmaster@1
|
402 * |
webmaster@1
|
403 * The following keys are defined: |
webmaster@1
|
404 * |
webmaster@1
|
405 * - 'description': A string describing this table and its purpose. |
webmaster@1
|
406 * References to other tables should be enclosed in |
webmaster@1
|
407 * curly-brackets. For example, the node_revisions table |
webmaster@1
|
408 * description field might contain "Stores per-revision title and |
webmaster@1
|
409 * body data for each {node}." |
webmaster@1
|
410 * - 'fields': An associative array ('fieldname' => specification) |
webmaster@1
|
411 * that describes the table's database columns. The specification |
webmaster@1
|
412 * is also an array. The following specification parameters are defined: |
webmaster@1
|
413 * |
webmaster@1
|
414 * - 'description': A string describing this field and its purpose. |
webmaster@1
|
415 * References to other tables should be enclosed in |
webmaster@1
|
416 * curly-brackets. For example, the node table vid field |
webmaster@1
|
417 * description might contain "Always holds the largest (most |
webmaster@1
|
418 * recent) {node_revisions}.vid value for this nid." |
webmaster@1
|
419 * - 'type': The generic datatype: 'varchar', 'int', 'serial' |
webmaster@1
|
420 * 'float', 'numeric', 'text', 'blob' or 'datetime'. Most types |
webmaster@1
|
421 * just map to the according database engine specific |
webmaster@1
|
422 * datatypes. Use 'serial' for auto incrementing fields. This |
webmaster@1
|
423 * will expand to 'int auto_increment' on mysql. |
webmaster@1
|
424 * - 'size': The data size: 'tiny', 'small', 'medium', 'normal', |
webmaster@1
|
425 * 'big'. This is a hint about the largest value the field will |
webmaster@1
|
426 * store and determines which of the database engine specific |
webmaster@1
|
427 * datatypes will be used (e.g. on MySQL, TINYINT vs. INT vs. BIGINT). |
webmaster@1
|
428 * 'normal', the default, selects the base type (e.g. on MySQL, |
webmaster@1
|
429 * INT, VARCHAR, BLOB, etc.). |
webmaster@1
|
430 * |
webmaster@1
|
431 * Not all sizes are available for all data types. See |
webmaster@1
|
432 * db_type_map() for possible combinations. |
webmaster@1
|
433 * - 'not null': If true, no NULL values will be allowed in this |
webmaster@1
|
434 * database column. Defaults to false. |
webmaster@1
|
435 * - 'default': The field's default value. The PHP type of the |
webmaster@1
|
436 * value matters: '', '0', and 0 are all different. If you |
webmaster@1
|
437 * specify '0' as the default value for a type 'int' field it |
webmaster@1
|
438 * will not work because '0' is a string containing the |
webmaster@1
|
439 * character "zero", not an integer. |
webmaster@1
|
440 * - 'length': The maximal length of a type 'varchar' or 'text' |
webmaster@1
|
441 * field. Ignored for other field types. |
webmaster@1
|
442 * - 'unsigned': A boolean indicating whether a type 'int', 'float' |
webmaster@1
|
443 * and 'numeric' only is signed or unsigned. Defaults to |
webmaster@1
|
444 * FALSE. Ignored for other field types. |
webmaster@1
|
445 * - 'precision', 'scale': For type 'numeric' fields, indicates |
webmaster@1
|
446 * the precision (total number of significant digits) and scale |
webmaster@1
|
447 * (decimal digits right of the decimal point). Both values are |
webmaster@1
|
448 * mandatory. Ignored for other field types. |
webmaster@1
|
449 * |
webmaster@1
|
450 * All parameters apart from 'type' are optional except that type |
webmaster@1
|
451 * 'numeric' columns must specify 'precision' and 'scale'. |
webmaster@1
|
452 * |
webmaster@1
|
453 * - 'primary key': An array of one or more key column specifiers (see below) |
webmaster@1
|
454 * that form the primary key. |
webmaster@1
|
455 * - 'unique key': An associative array of unique keys ('keyname' => |
webmaster@1
|
456 * specification). Each specification is an array of one or more |
webmaster@1
|
457 * key column specifiers (see below) that form a unique key on the table. |
webmaster@1
|
458 * - 'indexes': An associative array of indexes ('indexame' => |
webmaster@1
|
459 * specification). Each specification is an array of one or more |
webmaster@1
|
460 * key column specifiers (see below) that form an index on the |
webmaster@1
|
461 * table. |
webmaster@1
|
462 * |
webmaster@1
|
463 * A key column specifier is either a string naming a column or an |
webmaster@1
|
464 * array of two elements, column name and length, specifying a prefix |
webmaster@1
|
465 * of the named column. |
webmaster@1
|
466 * |
webmaster@1
|
467 * As an example, here is a SUBSET of the schema definition for |
webmaster@1
|
468 * Drupal's 'node' table. It show four fields (nid, vid, type, and |
webmaster@1
|
469 * title), the primary key on field 'nid', a unique key named 'vid' on |
webmaster@1
|
470 * field 'vid', and two indexes, one named 'nid' on field 'nid' and |
webmaster@1
|
471 * one named 'node_title_type' on the field 'title' and the first four |
webmaster@1
|
472 * bytes of the field 'type': |
webmaster@1
|
473 * |
webmaster@1
|
474 * @code |
webmaster@1
|
475 * $schema['node'] = array( |
webmaster@1
|
476 * 'fields' => array( |
webmaster@1
|
477 * 'nid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), |
webmaster@1
|
478 * 'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), |
webmaster@1
|
479 * 'type' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''), |
webmaster@1
|
480 * 'title' => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''), |
webmaster@1
|
481 * ), |
webmaster@1
|
482 * 'primary key' => array('nid'), |
webmaster@1
|
483 * 'unique keys' => array( |
webmaster@1
|
484 * 'vid' => array('vid') |
webmaster@1
|
485 * ), |
webmaster@1
|
486 * 'indexes' => array( |
webmaster@1
|
487 * 'nid' => array('nid'), |
webmaster@1
|
488 * 'node_title_type' => array('title', array('type', 4)), |
webmaster@1
|
489 * ), |
webmaster@1
|
490 * ); |
webmaster@1
|
491 * @endcode |
webmaster@1
|
492 * |
webmaster@1
|
493 * @see drupal_install_schema() |
webmaster@1
|
494 */ |
webmaster@1
|
495 |
webmaster@1
|
496 /** |
webmaster@1
|
497 * Create a new table from a Drupal table definition. |
webmaster@1
|
498 * |
webmaster@1
|
499 * @param $ret |
webmaster@1
|
500 * Array to which query results will be added. |
webmaster@1
|
501 * @param $name |
webmaster@1
|
502 * The name of the table to create. |
webmaster@1
|
503 * @param $table |
webmaster@1
|
504 * A Schema API table definition array. |
webmaster@1
|
505 */ |
webmaster@1
|
506 function db_create_table(&$ret, $name, $table) { |
webmaster@1
|
507 $statements = db_create_table_sql($name, $table); |
webmaster@1
|
508 foreach ($statements as $statement) { |
webmaster@1
|
509 $ret[] = update_sql($statement); |
webmaster@1
|
510 } |
webmaster@1
|
511 } |
webmaster@1
|
512 |
webmaster@1
|
513 /** |
webmaster@1
|
514 * Return an array of field names from an array of key/index column specifiers. |
webmaster@1
|
515 * |
webmaster@1
|
516 * This is usually an identity function but if a key/index uses a column prefix |
webmaster@1
|
517 * specification, this function extracts just the name. |
webmaster@1
|
518 * |
webmaster@1
|
519 * @param $fields |
webmaster@1
|
520 * An array of key/index column specifiers. |
webmaster@1
|
521 * @return |
webmaster@1
|
522 * An array of field names. |
webmaster@1
|
523 */ |
webmaster@1
|
524 function db_field_names($fields) { |
webmaster@1
|
525 $ret = array(); |
webmaster@1
|
526 foreach ($fields as $field) { |
webmaster@1
|
527 if (is_array($field)) { |
webmaster@1
|
528 $ret[] = $field[0]; |
webmaster@1
|
529 } |
webmaster@1
|
530 else { |
webmaster@1
|
531 $ret[] = $field; |
webmaster@1
|
532 } |
webmaster@1
|
533 } |
webmaster@1
|
534 return $ret; |
webmaster@1
|
535 } |
webmaster@1
|
536 |
webmaster@1
|
537 /** |
webmaster@1
|
538 * Given a Schema API field type, return the correct %-placeholder. |
webmaster@1
|
539 * |
webmaster@1
|
540 * Embed the placeholder in a query to be passed to db_query and and pass as an |
webmaster@1
|
541 * argument to db_query a value of the specified type. |
webmaster@1
|
542 * |
webmaster@1
|
543 * @param $type |
webmaster@1
|
544 * The Schema API type of a field. |
webmaster@1
|
545 * @return |
webmaster@1
|
546 * The placeholder string to embed in a query for that type. |
webmaster@1
|
547 */ |
webmaster@1
|
548 function db_type_placeholder($type) { |
webmaster@1
|
549 switch ($type) { |
webmaster@1
|
550 case 'varchar': |
webmaster@1
|
551 case 'char': |
webmaster@1
|
552 case 'text': |
webmaster@1
|
553 case 'datetime': |
webmaster@1
|
554 return '\'%s\''; |
webmaster@1
|
555 |
webmaster@1
|
556 case 'numeric': |
webmaster@1
|
557 // For 'numeric' values, we use '%s', not '\'%s\'' as with |
webmaster@1
|
558 // string types, because numeric values should not be enclosed |
webmaster@1
|
559 // in quotes in queries (though they can be, at least on mysql |
webmaster@1
|
560 // and pgsql). Numerics should only have [0-9.+-] and |
webmaster@1
|
561 // presumably no db's "escape string" function will mess with |
webmaster@1
|
562 // those characters. |
webmaster@1
|
563 return '%s'; |
webmaster@1
|
564 |
webmaster@1
|
565 case 'serial': |
webmaster@1
|
566 case 'int': |
webmaster@1
|
567 return '%d'; |
webmaster@1
|
568 |
webmaster@1
|
569 case 'float': |
webmaster@1
|
570 return '%f'; |
webmaster@1
|
571 |
webmaster@1
|
572 case 'blob': |
webmaster@1
|
573 return '%b'; |
webmaster@1
|
574 } |
webmaster@1
|
575 |
webmaster@1
|
576 // There is no safe value to return here, so return something that |
webmaster@1
|
577 // will cause the query to fail. |
webmaster@1
|
578 return 'unsupported type '. $type .'for db_type_placeholder'; |
webmaster@1
|
579 } |
webmaster@1
|
580 |
webmaster@1
|
581 /** |
webmaster@1
|
582 * @} End of "defgroup schemaapi". |
webmaster@1
|
583 */ |