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