2 /* Copyright (C) 2012, 2013 The Meme Factory, Inc., http://www.meme.com/
3 * Copyright (C) 2006-2009, Karl O. Pinc
5 * This file is part of Gombe-MI.
7 * Gombe-MI is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Gombe-MI. If not, see <http://www.gnu.org/licenses/>.
20 * Execute sql, one statement or more per line.
22 * Karl O. Pinc <kop@meme.com>
25 * SQL statements must appear on a single line.
26 * Upon error, remining statements on the line are ignored and
27 * subsqeuent lines are executed.
29 * Shows errors, but no results.
33 require('constants.php.inc');
34 require('lib.php.inc');
35 require('lib_upload.php.inc');
41 // Delimiter separating sql statements.
42 define('DELIM', "\r\n");
49 function execute_sql(&$upload_form, &$dbconn, $form_sql) {
51 * Execute the sql and show the result.
54 * $upload_form An instance of the Upload_form class.
55 * $dbconn An instance of the Upload_connection class.
56 * $form_sql Sql code to execute if no file content is uploaded.
64 * One of the main pages of the program.
67 // Get the uploaded data.
68 $upload_object = new Uploaded_data($upload_form);
69 if ($upload_object->filename == '') {
70 $sql = explode(DELIM, $form_sql);
72 if (!$upload_object->upload()) {
75 $sql = $upload_object->data;
81 if (!$dbconn->connect()) {
86 // Go through all the data and load it.
91 for ($line = 1; $line <= $lines; $line++) {
93 $this_sql = $sql[$line - 1];
94 if ($this_sql !== '' && !$dbconn->sql($this_sql)) {
96 // Let the user try again.
97 $upload_form->error($line
98 , 'PostgreSQL will not execute your statement, it reports the error: '
99 . htmlentities($dbconn->last_error())
101 , 'The sql statement is: '
102 . htmlentities($sql[$line - 1]));
107 $upload_object->success(); // Remember uploaded file.
109 $upload_form->show('<p><em class="error">'
110 . 'Errors occurred</em> during the execution of '
112 . ' lines of sql.</p>');
115 // Everything seems to have worked.
116 // Allow the user to do it again.
118 $upload_form->show('<p><em class="success">'
119 . 'Successfully executed</em> '
121 . ' lines of sql<em class="success">!</em></p>');
131 maintain_login_session();
133 show_html_start('sqlbyline -- Execute multiple lines of sql');
137 <h1>Execute multiple lines of SQL</h1>
142 // Get the form data.
146 // What page we're in.
147 if (array_key_exists('action', $_REQUEST)) {
148 $action = $_REQUEST['action'];
150 $action = ACTION_NONE;
154 $db = get_session_var('db', TRUE);
157 $user = get_session_var('user', TRUE);
160 $password = get_session_var('password', FALSE);
163 $sql = get_session_var('sql', FALSE);
165 //error_log('p: ' . crypt($password, substr('12345678901234',0, CRYPT_SALT_LENGTH)) . ': ' . ($password === ''));
167 // Instantiate the sql form.
168 $upload_form =& new Upload_form($db, $user, $password, 'SQL to execute');
170 $upload_form->extra_input
173 => '<label for="sql_id">SQL:</label>'
175 => '<textarea name="sql" id="sql_id" tabindex="4" rows="20" cols="72">' . htmlentities($sql, ENT_QUOTES) . '</textarea>'));
178 // Process input values
182 // Dispatch on action
186 $upload_form->show();
190 // Make a database connection.
191 $dbconn =& new Upload_connection($db
196 execute_sql($upload_form, $dbconn, $sql);
200 // Finish the stock page.
204 return; // End of program.