5 * Wrappers to abstract database operations from Drupal version.
9 * @defgroup dbfunctions Database convenience functions.
14 * Replace named placeholders in a WHERE snippet.
16 * Helper function to allow the usage of Drupal 7+ WHERE snippets
17 * with named placeholders in code for Drupal 6.
20 * String with a WHERE snippet using named placeholders.
22 * Array of placeholder values.
24 * String. $where filled with literals from $args.
26 function _drush_replace_query_placeholders($where, $args) {
27 foreach ($args as $key => $data) {
28 if (is_array($data)) {
30 // $data can't have keys that are a prefix of other keys to
31 // prevent a corrupted result in the below calls to str_replace().
32 // To avoid this we will use a zero padded indexed array of the values of $data.
33 $pad_length = strlen((string)count(array_values($data)));
34 foreach (array_values($data) as $i => $value) {
35 if (!is_numeric($value)) {
36 $value = "'".$value."'";
38 $new_keys[$key . '_' . str_pad($i, $pad_length, '0', STR_PAD_LEFT)] = $value;
40 $where = preg_replace('#' . $key . '\b#', implode(', ', array_keys($new_keys)), $where);
44 else if (!is_numeric($data)) {
45 $args[$key] = "'".$data."'";
49 foreach ($args as $key => $data) {
50 $where = str_replace($key, $data, $where);
57 * A db_select() that works for any version of Drupal.
60 * String. The table to operate on.
62 * Array or string. Fields affected in this operation. Valid string values are '*' or a single column name.
64 * String. WHERE snippet for the operation. It uses named placeholders. see @_drush_replace_query_placeholders()
66 * Array. Arguments for the WHERE snippet.
68 * Int. Value for OFFSET.
70 * Int. Value for LIMIT.
71 * @param $order_by_field
72 * String. Database column to order by.
73 * @param $order_by_direction
74 * ('ASC', 'DESC'). Ordering direction.
76 * A database resource.
78 function drush_db_select($table, $fields = '*', $where = NULL, $args = NULL, $start = NULL, $length = NULL, $order_by_field = NULL, $order_by_direction = 'ASC') {
79 if (drush_drupal_major_version() >= 7) {
80 if (!is_array($fields)) {
85 $fields = array($fields);
88 $query = db_select($table, $table)
89 ->fields($table, $fields);
91 $query = $query->where($where, $args);
93 if (isset($order_by_field)) {
94 $query = $query->orderBy($order_by_field, $order_by_direction);
97 $query = $query->range($start, $length);
99 return $query->execute();
102 if (is_array($fields)) {
103 $fields = implode(', ', $fields);
105 $query = "SELECT $fields FROM {{$table}}";
106 if (!empty($where)) {
107 $where = _drush_replace_query_placeholders($where, $args);
108 $query .= " WHERE ".$where;
110 if (isset($order_by_field)) {
111 $query .= " ORDER BY $order_by_field $order_by_direction";
113 if (isset($length)) {
114 $sql = drush_sql_get_class();
115 $db_scheme = $sql->scheme();
116 if ($db_scheme == 'oracle')
117 return db_query_range($query, $start, $length);
119 $limit = " LIMIT $length";
121 $limit .= " OFFSET $start";
127 return db_query($query, $args);
132 * A db_delete() that works for any version of Drupal.
135 * String. The table to operate on.
137 * String. WHERE snippet for the operation. It uses named placeholders. see @_drush_replace_query_placeholders()
139 * Array. Arguments for the WHERE snippet.
141 * Affected rows (except on D7+mysql without a WHERE clause - returns TRUE) or FALSE.
143 function drush_db_delete($table, $where = NULL, $args = NULL) {
144 if (drush_drupal_major_version() >= 7) {
145 if (!empty($where)) {
146 $query = db_delete($table)->where($where, $args);
147 return $query->execute();
150 return db_truncate($table)->execute();
154 $query = "DELETE FROM {{$table}}";
155 if (!empty($where)) {
156 $where = _drush_replace_query_placeholders($where, $args);
157 $query .= ' WHERE '.$where;
159 if (!db_query($query, $args)) {
162 return db_affected_rows();
167 * A db_result() that works consistently for any version of Drupal.
170 * A Database result object.
172 function drush_db_result($result) {
173 switch (drush_drupal_major_version()) {
175 return db_result($result);
178 return $result->fetchField();
183 * A db_fetch_object() that works for any version of Drupal.
186 * A Database result object.
188 function drush_db_fetch_object($result) {
189 return drush_drupal_major_version() >= 7 ? $result->fetchObject() : db_fetch_object($result);
193 * @} End of "defgroup dbfunctions".