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