Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / vendor / drush / drush / includes / dbtng.inc
1 <?php
2
3 /**
4  * @file
5  * Wrappers to abstract database operations from Drupal version.
6  */
7
8 /**
9  * @defgroup dbfunctions Database convenience functions.
10  * @{
11  */
12
13 /**
14  * Replace named placeholders in a WHERE snippet.
15  *
16  * Helper function to allow the usage of Drupal 7+ WHERE snippets
17  * with named placeholders in code for Drupal 6.
18  *
19  * @param $where
20  *   String with a WHERE snippet using named placeholders.
21  * @param $args
22  *   Array of placeholder values.
23  * @return
24  *   String. $where filled with literals from $args.
25  */
26 function _drush_replace_query_placeholders($where, $args) {
27   foreach ($args as $key => $data) {
28     if (is_array($data)) {
29       $new_keys = array();
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."'";
37         }
38         $new_keys[$key . '_' . str_pad($i, $pad_length, '0', STR_PAD_LEFT)] = $value;
39       }
40       $where = preg_replace('#' . $key . '\b#', implode(', ', array_keys($new_keys)), $where);
41       unset($args[$key]);
42       $args += $new_keys;
43     }
44     else if (!is_numeric($data)) {
45       $args[$key] = "'".$data."'";
46     }
47   }
48
49   foreach ($args as $key => $data) {
50     $where = str_replace($key, $data, $where);
51   }
52
53   return $where;
54 }
55
56 /**
57  * A db_select() that works for any version of Drupal.
58  *
59  * @param $table
60  *   String. The table to operate on.
61  * @param $fields
62  *   Array or string. Fields affected in this operation. Valid string values are '*' or a single column name.
63  * @param $where
64  *   String. WHERE snippet for the operation. It uses named placeholders. see @_drush_replace_query_placeholders()
65  * @param $args
66  *   Array. Arguments for the WHERE snippet.
67  * @param $start
68  *   Int. Value for OFFSET.
69  * @param $length
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.
75  * @return
76  *   A database resource.
77  */
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)) {
81       if ($fields == '*') {
82         $fields = array();
83       }
84       else {
85         $fields = array($fields);
86       }
87     }
88     $query = db_select($table, $table)
89       ->fields($table, $fields);
90     if (!empty($where)) {
91       $query = $query->where($where, $args);
92     }
93     if (isset($order_by_field)) {
94       $query = $query->orderBy($order_by_field, $order_by_direction);
95     }
96     if (isset($length)) {
97       $query = $query->range($start, $length);
98     }
99     return $query->execute();
100   }
101   else {
102     if (is_array($fields)) {
103       $fields = implode(', ', $fields);
104     }
105     $query = "SELECT $fields FROM {{$table}}";
106     if (!empty($where)) {
107       $where = _drush_replace_query_placeholders($where, $args);
108       $query .= " WHERE ".$where;
109     }
110     if (isset($order_by_field)) {
111       $query .= " ORDER BY $order_by_field $order_by_direction";
112     }
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);
118       else {
119               $limit = " LIMIT $length";
120               if (isset($start)) {
121           $limit .= " OFFSET $start";
122               }
123               $query .= $limit;
124       }
125     }
126
127     return db_query($query, $args);
128   }
129 }
130
131 /**
132  * A db_delete() that works for any version of Drupal.
133  *
134  * @param $table
135  *   String. The table to operate on.
136  * @param $where
137  *   String. WHERE snippet for the operation. It uses named placeholders. see @_drush_replace_query_placeholders()
138  * @param $args
139  *   Array. Arguments for the WHERE snippet.
140  * @return
141  *   Affected rows (except on D7+mysql without a WHERE clause - returns TRUE) or FALSE.
142  */
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();
148     }
149     else {
150       return db_truncate($table)->execute();
151     }
152   }
153   else {
154     $query = "DELETE FROM {{$table}}";
155     if (!empty($where)) {
156       $where = _drush_replace_query_placeholders($where, $args);
157       $query .= ' WHERE '.$where;
158     }
159     if (!db_query($query, $args)) {
160       return FALSE;
161     }
162     return db_affected_rows();
163   }
164 }
165
166 /**
167  * A db_result() that works consistently for any version of Drupal.
168  *
169  * @param
170  *   A Database result object.
171  */
172 function drush_db_result($result) {
173   switch (drush_drupal_major_version()) {
174     case 6:
175       return db_result($result);
176     case 7:
177     default:
178       return $result->fetchField();
179   }
180 }
181
182 /**
183  * A db_fetch_object() that works for any version of Drupal.
184  *
185  * @param
186  *   A Database result object.
187  */
188 function drush_db_fetch_object($result) {
189   return drush_drupal_major_version() >= 7 ? $result->fetchObject() : db_fetch_object($result);
190 }
191
192 /**
193  * @} End of "defgroup dbfunctions".
194  */