1 | #!/usr/bin/env php |
---|
2 | <?php |
---|
3 | /** |
---|
4 | * The Strangecode Codebase - a general application development framework for PHP |
---|
5 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
6 | * Copyright 2001-2012 Strangecode, LLC |
---|
7 | * |
---|
8 | * This file is part of The Strangecode Codebase. |
---|
9 | * |
---|
10 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
11 | * modify it under the terms of the GNU General Public License as published by the |
---|
12 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
13 | * any later version. |
---|
14 | * |
---|
15 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
18 | * details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License along with |
---|
21 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | /** |
---|
25 | * sql.cli.php |
---|
26 | */ |
---|
27 | |
---|
28 | if ($_SERVER['argc'] > 1 && isset($_SERVER['argv'][1]) && '' != $_SERVER['argv'][1] && is_dir($_SERVER['argv'][1])) { |
---|
29 | // First arg is path to current site. Realpath removes trailing /s |
---|
30 | define('COMMON_BASE', realpath($_SERVER['argv'][1])); |
---|
31 | } else { |
---|
32 | die("Error: First argument must be the directory path to an existing site (ex: /home/sc/www.strangecode.com).\n"); |
---|
33 | } |
---|
34 | |
---|
35 | include_once dirname(__FILE__) . '/../_config.inc.php'; |
---|
36 | |
---|
37 | $op = null; |
---|
38 | $valid_ops = array('sortorder', 'insert', 'update', 'delete', 'search', 'filter'); |
---|
39 | |
---|
40 | // Test arguments. |
---|
41 | if (isset($_SERVER['argv'][2])) { |
---|
42 | // Second arg is db table. |
---|
43 | $db_tbl = $_SERVER['argv'][2]; |
---|
44 | } else { |
---|
45 | die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s\n", basename($_SERVER['argv'][0]), join(', ', $valid_ops))); |
---|
46 | } |
---|
47 | |
---|
48 | // Test for operation. |
---|
49 | if (isset($_SERVER['argv'][3])) { |
---|
50 | // Optional third arg is op. |
---|
51 | $op = $_SERVER['argv'][3]; |
---|
52 | // Make sure op is valid. |
---|
53 | if (!in_array($op, $valid_ops)) { |
---|
54 | die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n"); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | // Get DB tables. |
---|
59 | $qid = $db->query("SHOW TABLES"); |
---|
60 | while (list($row) = mysql_fetch_row($qid)) { |
---|
61 | $tables[] = $row; |
---|
62 | } |
---|
63 | |
---|
64 | // Make sure requested table is in database. |
---|
65 | if (!in_array($db_tbl, $tables)) { |
---|
66 | die(sprintf("%s Warning: %s does not exist in database %s. Please select one of: \n\n%s\n\n", basename($_SERVER['argv'][0]), $db_tbl, $app->getParam('db_name'), join("\n", $tables))); |
---|
67 | } |
---|
68 | |
---|
69 | // Make sure op is valid. |
---|
70 | if (isset($op) && !in_array($op, $valid_ops)) { |
---|
71 | die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n"); |
---|
72 | } |
---|
73 | |
---|
74 | // Get DB table column info. |
---|
75 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
76 | while ($row = mysql_fetch_row($qid)) { |
---|
77 | $cols[] = $row; |
---|
78 | } |
---|
79 | |
---|
80 | $sort_columns = ''; |
---|
81 | $primary_key = '__///__'; |
---|
82 | |
---|
83 | // Loop through columns |
---|
84 | if (is_array($cols) && !empty($cols)) { |
---|
85 | foreach ($cols as $col) { |
---|
86 | |
---|
87 | // Human readable. |
---|
88 | $field = $col[0]; |
---|
89 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
90 | $is_primary_key = ('PRI' == $col[3]); |
---|
91 | |
---|
92 | $sort_columns .= "\$so->setColumn('$field', '$field ASC', '$field DESC');\n"; |
---|
93 | |
---|
94 | if ($is_primary_key) { |
---|
95 | // This is the primary key. Deal with separately. |
---|
96 | $primary_key = $field; |
---|
97 | } else if ('set' == $type) { |
---|
98 | // Set types usually need to be converted to comma-delimited lists. |
---|
99 | $c[$field] = "'\" . escapedList(array_keys(\$frm['$field'])) . \"'"; |
---|
100 | } else if ('featured' == $field || 'publish' == $field || preg_match("/enum\('true'\)/", $col[1])) { |
---|
101 | // Toggle types. |
---|
102 | $c[$field] = "'\" . isset(\$frm['$field']) . \"'"; |
---|
103 | } else if ('added_by_user_id' == $field || 'modified_by_user_id' == $field) { |
---|
104 | // Expects a user_id. |
---|
105 | $c[$field] = "'\" . \$db->escapeString(\$auth->get('user_id')) . \"'"; |
---|
106 | } else if ('added_datetime' == $field || 'modified_datetime' == $field) { |
---|
107 | // DB record insertion datetime. |
---|
108 | $c[$field] = "NOW()"; |
---|
109 | } else if (preg_match('/date_|_date/', $field)) { |
---|
110 | // This is a date field. Convert to SQL date. |
---|
111 | $c[$field] = "'\" . \$db->escapeString(strToSQLDate(\$frm['$field'])) . \"'"; |
---|
112 | } else { |
---|
113 | // Default. Just insert data. |
---|
114 | $c[$field] = "'\" . \$db->escapeString(\$frm['$field']) . \"'"; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | } else { |
---|
119 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | echo isset($op) ? '' : "\n\n\n"; |
---|
124 | |
---|
125 | // Insert SQL. |
---|
126 | if (!isset($op) || 'insert' == $op) { |
---|
127 | $insert_skip_columns = array('modified_datetime', 'modified_by_user_id'); |
---|
128 | $insert_c = array(); |
---|
129 | foreach ($c as $k=>$v) { |
---|
130 | if (in_array($k, $insert_skip_columns)) { |
---|
131 | continue; |
---|
132 | } |
---|
133 | $insert_c[$k] = $v; |
---|
134 | } |
---|
135 | $db_keys = join(",\n ", array_keys($insert_c)); |
---|
136 | $db_vals = join(",\n ", $insert_c); |
---|
137 | echo <<<E_O_F |
---|
138 | // Insert record data. |
---|
139 | \$db->query(" |
---|
140 | INSERT INTO $db_tbl ( |
---|
141 | $db_keys |
---|
142 | ) VALUES ( |
---|
143 | $db_vals |
---|
144 | ) |
---|
145 | "); |
---|
146 | E_O_F; |
---|
147 | } |
---|
148 | |
---|
149 | echo isset($op) ? '' : "\n\n\n"; |
---|
150 | |
---|
151 | // Update SQL. |
---|
152 | if (!isset($op) || 'update' == $op) { |
---|
153 | $update_skip_columns = array('added_datetime', 'added_by_user_id'); |
---|
154 | $comma = ''; |
---|
155 | $key_eq_val = ''; |
---|
156 | foreach ($c as $k=>$v) { |
---|
157 | if (in_array($k, $update_skip_columns)) { |
---|
158 | continue; |
---|
159 | } |
---|
160 | $key_eq_val .= $comma . "\n $k = $v"; |
---|
161 | $comma = ','; |
---|
162 | } |
---|
163 | echo <<<E_O_F |
---|
164 | // Update record data. |
---|
165 | \$db->query(" |
---|
166 | UPDATE $db_tbl SET$key_eq_val |
---|
167 | WHERE $primary_key = '" . \$db->escapeString(\$frm['$primary_key']) . "' |
---|
168 | "); |
---|
169 | E_O_F; |
---|
170 | } |
---|
171 | |
---|
172 | echo isset($op) ? '' : "\n\n\n"; |
---|
173 | |
---|
174 | // Delete SQL. |
---|
175 | if (!isset($op) || 'delete' == $op) { |
---|
176 | $where_clause = ''; |
---|
177 | $delete_skip_columns = array(); |
---|
178 | $delim = 'WHERE'; |
---|
179 | if (!empty($primary_key)) { |
---|
180 | $where_clause = " $delim $primary_key = '\" . \$db->escapeString(\$frm['$primary_key']) . \"'\n"; |
---|
181 | $delim = 'AND'; |
---|
182 | } |
---|
183 | foreach ($c as $k=>$ignore) { |
---|
184 | if (in_array($k, $delete_skip_columns)) { |
---|
185 | continue; |
---|
186 | } |
---|
187 | $where_clause .= " $delim $k = '\" . \$db->escapeString(\$frm['$k']) . \"'\n"; |
---|
188 | $delim = 'AND'; |
---|
189 | } |
---|
190 | echo <<<E_O_F |
---|
191 | // Delete record data. |
---|
192 | \$db->query(" |
---|
193 | DELETE FROM $db_tbl |
---|
194 | $where_clause "); |
---|
195 | E_O_F; |
---|
196 | } |
---|
197 | |
---|
198 | echo isset($op) ? '' : "\n\n\n"; |
---|
199 | |
---|
200 | // SortOrder methods SQL. |
---|
201 | if (!isset($op) || 'sortorder' == $op) { |
---|
202 | echo "// Instantiate a sorting object with the default sort and order. Add SQL for each column.\n"; |
---|
203 | echo "\$so = new SortOrder('$db_tbl.$primary_key', 'DESC');\n"; |
---|
204 | echo "///\$so->asc_widget = sprintf('<i class=\"fa fa-arrow-up fa-fw\" title=\"%s\"></i>', _(\"Ascending\"));\n"; |
---|
205 | echo "///\$so->desc_widget = sprintf('<i class=\"fa fa-arrow-down fa-fw\" title=\"%s\"></i>', _(\"Descending\"));\n"; |
---|
206 | echo "\$so->setColumn('$db_tbl.$primary_key', '$db_tbl.$primary_key ASC', '$db_tbl.$primary_key DESC');\n"; |
---|
207 | foreach ($c as $k=>$v) { |
---|
208 | echo "\$so->setColumn('$db_tbl.$k', '$db_tbl.$k ASC', '$db_tbl.$k DESC');\n"; |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | echo isset($op) ? '' : "\n\n\n"; |
---|
213 | |
---|
214 | // Search SQL |
---|
215 | if (!isset($op) || 'search' == $op) { |
---|
216 | $search_skip_columns = array('added_datetime', 'added_by_user_id', 'modified_datetime', 'modified_by_user_id', 'publish', 'featured'); |
---|
217 | $search_columns = $db_tbl . '.' . join(" LIKE '%\" . \$db->escapeString(\$qry_words[\$i]) . \"%'\n OR $db_tbl.", array_diff(array_keys($c), $search_skip_columns)); |
---|
218 | echo <<<E_O_F |
---|
219 | \$where_clause .= (empty(\$where_clause) ? 'WHERE' : 'AND') . " |
---|
220 | ( |
---|
221 | $search_columns LIKE '%" . \$db->escapeString(\$qry_words[\$i]) . "%' |
---|
222 | ) |
---|
223 | "; |
---|
224 | E_O_F; |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | echo isset($op) ? '' : "\n\n\n"; |
---|