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

Functions

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

File

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