dbtng.inc

  1. 8.0.x includes/dbtng.inc
  2. 6.x includes/dbtng.inc
  3. 7.x includes/dbtng.inc
  4. 5.x includes/dbtng.inc
  5. master includes/dbtng.inc

Wrappers to abstract database operations from Drupal version.

Functions

Namesort ascending Description
_drush_replace_query_placeholders Replace named placeholders in a WHERE snippet.
drush_db_select A db_select() that works for any version of Drupal.
drush_db_result A db_result() that works consistently for any version of Drupal.
drush_db_fetch_object A db_fetch_object() that works for any version of Drupal.
drush_db_delete A db_delete() that works for any version of Drupal.

File

includes/dbtng.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Wrappers to abstract database operations from Drupal version.
  5. */
  6. /**
  7. * @defgroup dbfunctions Database convenience functions.
  8. * @{
  9. */
  10. /**
  11. * Replace named placeholders in a WHERE snippet.
  12. *
  13. * Helper function to allow the usage of Drupal 7+ WHERE snippets
  14. * with named placeholders in code for Drupal 6.
  15. *
  16. * @param $where
  17. * String with a WHERE snippet using named placeholders.
  18. * @param $args
  19. * Array of placeholder values.
  20. * @return
  21. * String. $where filled with literals from $args.
  22. */
  23. function _drush_replace_query_placeholders($where, $args) {
  24. foreach ($args as $key => $data) {
  25. if (is_array($data)) {
  26. $new_keys = array();
  27. // $data can't have keys that are a prefix of other keys to
  28. // prevent a corrupted result in the below calls to str_replace().
  29. // To avoid this we will use a zero padded indexed array of the values of $data.
  30. $pad_length = strlen((string)count(array_values($data)));
  31. foreach (array_values($data) as $i => $value) {
  32. if (!is_numeric($value)) {
  33. $value = "'".$value."'";
  34. }
  35. $new_keys[$key . '_' . str_pad($i, $pad_length, '0', STR_PAD_LEFT)] = $value;
  36. }
  37. $where = preg_replace('#' . $key . '\b#', implode(', ', array_keys($new_keys)), $where);
  38. unset($args[$key]);
  39. $args += $new_keys;
  40. }
  41. else if (!is_numeric($data)) {
  42. $args[$key] = "'".$data."'";
  43. }
  44. }
  45. foreach ($args as $key => $data) {
  46. $where = str_replace($key, $data, $where);
  47. }
  48. return $where;
  49. }
  50. /**
  51. * A db_select() that works for any version of Drupal.
  52. *
  53. * @param $table
  54. * String. The table to operate on.
  55. * @param $fields
  56. * Array or string. Fields affected in this operation. Valid string values are '*' or a single column name.
  57. * @param $where
  58. * String. WHERE snippet for the operation. It uses named placeholders. see @_drush_replace_query_placeholders()
  59. * @param $args
  60. * Array. Arguments for the WHERE snippet.
  61. * @param $start
  62. * Int. Value for OFFSET.
  63. * @param $length
  64. * Int. Value for LIMIT.
  65. * @param $order_by_field
  66. * String. Database column to order by.
  67. * @param $order_by_direction
  68. * ('ASC', 'DESC'). Ordering direction.
  69. * @return
  70. * A database resource.
  71. */
  72. function drush_db_select($table, $fields = '*', $where = NULL, $args = NULL, $start = NULL, $length = NULL, $order_by_field = NULL, $order_by_direction = 'ASC') {
  73. if (drush_drupal_major_version() >= 7) {
  74. if (!is_array($fields)) {
  75. if ($fields == '*') {
  76. $fields = array();
  77. }
  78. else {
  79. $fields = array($fields);
  80. }
  81. }
  82. $query = db_select($table, $table)
  83. ->fields($table, $fields);
  84. if (!empty($where)) {
  85. $query = $query->where($where, $args);
  86. }
  87. if (isset($order_by_field)) {
  88. $query = $query->orderBy($order_by_field, $order_by_direction);
  89. }
  90. if (isset($length)) {
  91. $query = $query->range($start, $length);
  92. }
  93. return $query->execute();
  94. }
  95. else {
  96. if (is_array($fields)) {
  97. $fields = implode(', ', $fields);
  98. }
  99. $query = "SELECT $fields FROM {{$table}}";
  100. if (!empty($where)) {
  101. $where = _drush_replace_query_placeholders($where, $args);
  102. $query .= " WHERE ".$where;
  103. }
  104. if (isset($order_by_field)) {
  105. $query .= " ORDER BY $order_by_field $order_by_direction";
  106. }
  107. if (isset($length)) {
  108. $sql = drush_sql_get_class();
  109. $db_scheme = $sql->scheme();
  110. if ($db_scheme == 'oracle')
  111. return db_query_range($query, $start, $length);
  112. else {
  113. $limit = " LIMIT $length";
  114. if (isset($start)) {
  115. $limit .= " OFFSET $start";
  116. }
  117. $query .= $limit;
  118. }
  119. }
  120. return db_query($query, $args);
  121. }
  122. }
  123. /**
  124. * A db_delete() that works for any version of Drupal.
  125. *
  126. * @param $table
  127. * String. The table to operate on.
  128. * @param $where
  129. * String. WHERE snippet for the operation. It uses named placeholders. see @_drush_replace_query_placeholders()
  130. * @param $args
  131. * Array. Arguments for the WHERE snippet.
  132. * @return
  133. * Affected rows (except on D7+mysql without a WHERE clause - returns TRUE) or FALSE.
  134. */
  135. function drush_db_delete($table, $where = NULL, $args = NULL) {
  136. if (drush_drupal_major_version() >= 7) {
  137. if (!empty($where)) {
  138. $query = db_delete($table)->where($where, $args);
  139. return $query->execute();
  140. }
  141. else {
  142. return db_truncate($table)->execute();
  143. }
  144. }
  145. else {
  146. $query = "DELETE FROM {{$table}}";
  147. if (!empty($where)) {
  148. $where = _drush_replace_query_placeholders($where, $args);
  149. $query .= ' WHERE '.$where;
  150. }
  151. if (!db_query($query, $args)) {
  152. return FALSE;
  153. }
  154. return db_affected_rows();
  155. }
  156. }
  157. /**
  158. * A db_result() that works consistently for any version of Drupal.
  159. *
  160. * @param
  161. * A Database result object.
  162. */
  163. function drush_db_result($result) {
  164. switch (drush_drupal_major_version()) {
  165. case 6:
  166. return db_result($result);
  167. case 7:
  168. default:
  169. return $result->fetchField();
  170. }
  171. }
  172. /**
  173. * A db_fetch_object() that works for any version of Drupal.
  174. *
  175. * @param
  176. * A Database result object.
  177. */
  178. function drush_db_fetch_object($result) {
  179. return drush_drupal_major_version() >= 7 ? $result->fetchObject() : db_fetch_object($result);
  180. }
  181. /**
  182. * @} End of "defgroup dbfunctions".
  183. */