[412] | 1 | #!/usr/bin/php -q |
---|
[1] | 2 | <?php |
---|
| 3 | /** |
---|
[362] | 4 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 5 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 6 | * Copyright 2001-2012 Strangecode, LLC |
---|
[362] | 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 | /** |
---|
[1] | 25 | * sql.cli.php |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | include_once dirname(__FILE__) . '/_config.inc.php'; |
---|
| 29 | |
---|
| 30 | $op = null; |
---|
| 31 | $valid_ops = array('sortorder', 'insert', 'update', 'delete', 'search', 'filter'); |
---|
| 32 | |
---|
| 33 | // Test arguments. |
---|
| 34 | if (isset($_SERVER['argv'][2])) { |
---|
| 35 | // Second arg is db table. |
---|
| 36 | $db_tbl = $_SERVER['argv'][2]; |
---|
| 37 | } else { |
---|
[151] | 38 | die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s", basename($_SERVER['argv'][0]), join(', ', $valid_ops))); |
---|
[1] | 39 | } |
---|
| 40 | |
---|
| 41 | // Test for operation. |
---|
| 42 | if (isset($_SERVER['argv'][3])) { |
---|
| 43 | // Optional third arg is op. |
---|
| 44 | $op = $_SERVER['argv'][3]; |
---|
| 45 | // Make sure op is valid. |
---|
| 46 | if (!in_array($op, $valid_ops)) { |
---|
| 47 | die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n"); |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | // Get DB tables. |
---|
[136] | 52 | $qid = $db->query("SHOW TABLES"); |
---|
[1] | 53 | while (list($row) = mysql_fetch_row($qid)) { |
---|
| 54 | $tables[] = $row; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | // Make sure requested table is in database. |
---|
| 58 | if (!in_array($db_tbl, $tables)) { |
---|
[136] | 59 | 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))); |
---|
[1] | 60 | } |
---|
| 61 | |
---|
| 62 | // Make sure op is valid. |
---|
| 63 | if (isset($op) && !in_array($op, $valid_ops)) { |
---|
| 64 | die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n"); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | // Get DB table column info. |
---|
[136] | 68 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
[1] | 69 | while ($row = mysql_fetch_row($qid)) { |
---|
| 70 | $cols[] = $row; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | $sort_columns = ''; |
---|
[154] | 74 | $primary_key = '__///__'; |
---|
[1] | 75 | |
---|
| 76 | // Loop through columns |
---|
| 77 | if (is_array($cols) && !empty($cols)) { |
---|
| 78 | foreach ($cols as $col) { |
---|
[42] | 79 | |
---|
[1] | 80 | // Human readable. |
---|
| 81 | $field = $col[0]; |
---|
| 82 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
| 83 | $is_primary_key = ('PRI' == $col[3]); |
---|
[42] | 84 | |
---|
[1] | 85 | $sort_columns .= "\$so->setColumn('$field', '$field ASC', '$field DESC');\n"; |
---|
[42] | 86 | |
---|
[1] | 87 | if ($is_primary_key) { |
---|
| 88 | // This is the primary key. Deal with separately. |
---|
| 89 | $primary_key = $field; |
---|
| 90 | } else if ('set' == $type) { |
---|
| 91 | // Set types usually need to be converted to comma-delimited lists. |
---|
[111] | 92 | $c[$field] = "'\" . escapedList(array_keys(\$frm['$field'])) . \"'"; |
---|
[1] | 93 | } else if ('featured' == $field || 'publish' == $field || preg_match("/enum\('true'\)/", $col[1])) { |
---|
| 94 | // Toggle types. |
---|
| 95 | $c[$field] = "'\" . isset(\$frm['$field']) . \"'"; |
---|
[19] | 96 | } else if ('added_by_user_id' == $field || 'modified_by_user_id' == $field) { |
---|
[323] | 97 | // Expects a user_id. |
---|
[147] | 98 | $c[$field] = "'\" . \$db->escapeString(\$auth->get('user_id')) . \"'"; |
---|
[1] | 99 | } else if ('added_datetime' == $field || 'modified_datetime' == $field) { |
---|
| 100 | // DB record insertion datetime. |
---|
| 101 | $c[$field] = "NOW()"; |
---|
[323] | 102 | } else if (preg_match('/date_|_date/', $field)) { |
---|
| 103 | // This is a date field. Convert to SQL date. |
---|
| 104 | $c[$field] = "'\" . \$db->escapeString(strToSQLDate(\$frm['$field'])) . \"'"; |
---|
[1] | 105 | } else { |
---|
| 106 | // Default. Just insert data. |
---|
[136] | 107 | $c[$field] = "'\" . \$db->escapeString(\$frm['$field']) . \"'"; |
---|
[1] | 108 | } |
---|
| 109 | } |
---|
[42] | 110 | |
---|
[1] | 111 | } else { |
---|
| 112 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | echo isset($op) ? '' : "\n\n\n"; |
---|
| 117 | |
---|
| 118 | // Insert SQL. |
---|
| 119 | if (!isset($op) || 'insert' == $op) { |
---|
[19] | 120 | $insert_skip_columns = array('modified_datetime', 'modified_by_user_id'); |
---|
[1] | 121 | $insert_c = array(); |
---|
| 122 | foreach ($c as $k=>$v) { |
---|
| 123 | if (in_array($k, $insert_skip_columns)) { |
---|
| 124 | continue; |
---|
| 125 | } |
---|
| 126 | $insert_c[$k] = $v; |
---|
| 127 | } |
---|
| 128 | $db_keys = join(",\n ", array_keys($insert_c)); |
---|
| 129 | $db_vals = join(",\n ", $insert_c); |
---|
| 130 | echo <<<E_O_F |
---|
| 131 | // Insert record data. |
---|
[136] | 132 | \$db->query(" |
---|
[1] | 133 | INSERT INTO $db_tbl ( |
---|
| 134 | $db_keys |
---|
| 135 | ) VALUES ( |
---|
| 136 | $db_vals |
---|
| 137 | ) |
---|
| 138 | "); |
---|
| 139 | E_O_F; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | echo isset($op) ? '' : "\n\n\n"; |
---|
| 143 | |
---|
| 144 | // Update SQL. |
---|
| 145 | if (!isset($op) || 'update' == $op) { |
---|
[19] | 146 | $update_skip_columns = array('added_datetime', 'added_by_user_id'); |
---|
[1] | 147 | $comma = ''; |
---|
| 148 | $key_eq_val = ''; |
---|
| 149 | foreach ($c as $k=>$v) { |
---|
| 150 | if (in_array($k, $update_skip_columns)) { |
---|
| 151 | continue; |
---|
| 152 | } |
---|
[21] | 153 | $key_eq_val .= $comma . "\n $k = $v"; |
---|
[1] | 154 | $comma = ','; |
---|
| 155 | } |
---|
| 156 | echo <<<E_O_F |
---|
[21] | 157 | // Update record data. |
---|
[136] | 158 | \$db->query(" |
---|
[21] | 159 | UPDATE $db_tbl SET$key_eq_val |
---|
[136] | 160 | WHERE $primary_key = '" . \$db->escapeString(\$frm['$primary_key']) . "' |
---|
[21] | 161 | "); |
---|
[1] | 162 | E_O_F; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | echo isset($op) ? '' : "\n\n\n"; |
---|
| 166 | |
---|
| 167 | // Delete SQL. |
---|
| 168 | if (!isset($op) || 'delete' == $op) { |
---|
| 169 | $where_clause = ''; |
---|
| 170 | $delete_skip_columns = array(); |
---|
| 171 | $delim = 'WHERE'; |
---|
| 172 | if (!empty($primary_key)) { |
---|
[136] | 173 | $where_clause = " $delim $primary_key = '\" . \$db->escapeString(\$frm['$primary_key']) . \"'\n"; |
---|
[1] | 174 | $delim = 'AND'; |
---|
| 175 | } |
---|
| 176 | foreach ($c as $k=>$ignore) { |
---|
| 177 | if (in_array($k, $delete_skip_columns)) { |
---|
| 178 | continue; |
---|
| 179 | } |
---|
[136] | 180 | $where_clause .= " $delim $k = '\" . \$db->escapeString(\$frm['$k']) . \"'\n"; |
---|
[1] | 181 | $delim = 'AND'; |
---|
| 182 | } |
---|
| 183 | echo <<<E_O_F |
---|
| 184 | // Delete record data. |
---|
[136] | 185 | \$db->query(" |
---|
[1] | 186 | DELETE FROM $db_tbl |
---|
| 187 | $where_clause "); |
---|
| 188 | E_O_F; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | echo isset($op) ? '' : "\n\n\n"; |
---|
| 192 | |
---|
| 193 | // SortOrder methods SQL. |
---|
| 194 | if (!isset($op) || 'sortorder' == $op) { |
---|
| 195 | echo "// Instantiate a sorting object with the default sort and order. Add SQL for each column.\n"; |
---|
| 196 | echo "\$so = new SortOrder('$db_tbl.$primary_key', 'DESC');\n"; |
---|
| 197 | echo "\$so->setColumn('$db_tbl.$primary_key', '$db_tbl.$primary_key ASC', '$db_tbl.$primary_key DESC');\n"; |
---|
| 198 | foreach ($c as $k=>$v) { |
---|
| 199 | echo "\$so->setColumn('$db_tbl.$k', '$db_tbl.$k ASC', '$db_tbl.$k DESC');\n"; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | echo isset($op) ? '' : "\n\n\n"; |
---|
| 204 | |
---|
| 205 | // Search SQL |
---|
| 206 | if (!isset($op) || 'search' == $op) { |
---|
[19] | 207 | $search_skip_columns = array('added_datetime', 'added_by_user_id', 'modified_datetime', 'modified_by_user_id', 'publish', 'featured'); |
---|
[136] | 208 | $search_columns = $db_tbl . '.' . join(" LIKE '%\" . \$db->escapeString(\$qry_words[\$i]) . \"%'\n OR $db_tbl.", array_diff(array_keys($c), $search_skip_columns)); |
---|
[1] | 209 | echo <<<E_O_F |
---|
| 210 | \$where_clause .= (empty(\$where_clause) ? 'WHERE' : 'AND') . " |
---|
| 211 | ( |
---|
[136] | 212 | $search_columns LIKE '%" . \$db->escapeString(\$qry_words[\$i]) . "%' |
---|
[1] | 213 | ) |
---|
| 214 | "; |
---|
| 215 | E_O_F; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | echo isset($op) ? '' : "\n\n\n"; |
---|
| 220 | ?> |
---|