1 |
#!/usr/bin/php -q
|
2 |
<?php
|
3 |
//$Header: /hl/cvsroots/gpl01/gpl01/webprojs/fboprime/sw/standalone/dbdropall.php,v 1.1 2006/04/12 00:21:15 dashley Exp $
|
4 |
//--------------------------------------------------------------------------------
|
5 |
//dbdropall.php--Drops (Deletes) All Database Tables
|
6 |
//Copyright (C) 2006 David T. Ashley
|
7 |
//
|
8 |
//This program is free software; you can redistribute it and/or
|
9 |
//modify it under the terms of the GNU General Public License
|
10 |
//as published by the Free Software Foundation; either version 2
|
11 |
//of the License, or (at your option) any later version.
|
12 |
//
|
13 |
//This program is distributed in the hope that it will be useful,
|
14 |
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
//GNU General Public License for more details.
|
17 |
//
|
18 |
//You should have received a copy of the GNU General Public License
|
19 |
//along with this program; if not, write to the Free Software
|
20 |
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
21 |
//--------------------------------------------------------------------------------
|
22 |
//Set the include file path. This is necessary to get to the configuration
|
23 |
//information, including how to try to connect to the database.
|
24 |
set_include_path("/var/www/php_library/fboprime");
|
25 |
//
|
26 |
//Include the configuration information.
|
27 |
require_once("config.inc");
|
28 |
//
|
29 |
//--------------------------------------------------------------------------------
|
30 |
//Writes a horizontal line.
|
31 |
//
|
32 |
function hline()
|
33 |
{
|
34 |
for ($i=0; $i<80; $i++)
|
35 |
{
|
36 |
echo "-";
|
37 |
}
|
38 |
echo "\n";
|
39 |
}
|
40 |
//
|
41 |
//--------------------------------------------------------------------------------
|
42 |
//Errors out.
|
43 |
//
|
44 |
function error_out()
|
45 |
{
|
46 |
echo "FATAL ERROR\n";
|
47 |
exit(1);
|
48 |
}
|
49 |
//--------------------------------------------------------------------------------
|
50 |
//Returns a version control string. Used for randomness.
|
51 |
//
|
52 |
function vc_info()
|
53 |
{
|
54 |
return("\$Header: /hl/cvsroots/gpl01/gpl01/webprojs/fboprime/sw/standalone/dbdropall.php,v 1.1 2006/04/12 00:21:15 dashley Exp $");
|
55 |
}
|
56 |
//
|
57 |
//--------------------------------------------------------------------------------
|
58 |
//
|
59 |
function drop_all($handle, $droplist)
|
60 |
{
|
61 |
//Figure out how many tables are in the database, and get their names.
|
62 |
hline();
|
63 |
|
64 |
$result = mysql_query("show tables", $handle);
|
65 |
|
66 |
if ($result === FALSE)
|
67 |
error_out();
|
68 |
|
69 |
//Figure out the results.
|
70 |
$ntables = mysql_num_rows($result);
|
71 |
for ($i=0; $i<$ntables; $i++)
|
72 |
{
|
73 |
$temp = mysql_fetch_row($result);
|
74 |
$tables_array[$i] = $temp[0];
|
75 |
}
|
76 |
|
77 |
//Tell how many tables.
|
78 |
if ($ntables > 1)
|
79 |
echo $ntables . " tables found in database:\n";
|
80 |
else if ($ntables == 1)
|
81 |
echo $ntables . " table found in database:\n";
|
82 |
else
|
83 |
echo "No tables found in database.\n";
|
84 |
|
85 |
//Emit the table names.
|
86 |
for ($i = 0; $i < $ntables; $i++)
|
87 |
{
|
88 |
echo " " . $tables_array[$i] . "\n";
|
89 |
}
|
90 |
|
91 |
//For each table in the design, if it is in the actual database, issue
|
92 |
//the drop statement.
|
93 |
if ($ntables)
|
94 |
{
|
95 |
for ($i=0; $i<count($droplist); $i++)
|
96 |
{
|
97 |
$found = array_search($droplist[$i], $tables_array);
|
98 |
if (($found !== FALSE) && ($found !== NULL))
|
99 |
{
|
100 |
echo "Dropping table " . $droplist[$i] . " ... ";
|
101 |
|
102 |
//The table exists. Drop it.
|
103 |
//Run the query and bug out if it fails.
|
104 |
$result = mysql_query("drop table " . $droplist[$i], $handle);
|
105 |
|
106 |
if ($result === FALSE)
|
107 |
error_out();
|
108 |
|
109 |
echo "OK.\n";
|
110 |
}
|
111 |
}
|
112 |
}
|
113 |
}
|
114 |
//
|
115 |
//--------------------------------------------------------------------------------
|
116 |
//--------------------------------------------------------------------------------
|
117 |
//--------------------------------------------------------------------------------
|
118 |
//------ M A I N S C R I P T ------------------------------------------------
|
119 |
//--------------------------------------------------------------------------------
|
120 |
//--------------------------------------------------------------------------------
|
121 |
//--------------------------------------------------------------------------------
|
122 |
//
|
123 |
//
|
124 |
//This is the list of the database tables. For each one, this script will try to
|
125 |
//trash it if it exists.
|
126 |
//
|
127 |
$tables_to_drop = array
|
128 |
(
|
129 |
"usrs",
|
130 |
"usrsresv",
|
131 |
"resv",
|
132 |
"slot",
|
133 |
"rscs",
|
134 |
"sess",
|
135 |
"loge",
|
136 |
"dpar"
|
137 |
);
|
138 |
|
139 |
if ($argc == 1)
|
140 |
{
|
141 |
//The no-parameters case, as expected.
|
142 |
|
143 |
$handle = mysql_connect(CONFIG_MYSQL_SERVER, CONFIG_MYSQL_USERNAME, CONFIG_MYSQL_PASSWORD);
|
144 |
$handle_copy = $handle;
|
145 |
if ($handle === FALSE)
|
146 |
{
|
147 |
echo "ERROR: Unable to connect and/or authenticate to MySQL database.\n";
|
148 |
exit(1);
|
149 |
}
|
150 |
|
151 |
$result = mysql_select_db(CONFIG_MYSQL_DATABASE, $handle);
|
152 |
if ($result === FALSE)
|
153 |
{
|
154 |
echo "ERROR: Unable to select MySQL database \"" . CONFIG_MYSQL_DATABASE . "\".\n";
|
155 |
exit(1);
|
156 |
}
|
157 |
|
158 |
//Do the dropping.
|
159 |
drop_all($handle, $tables_to_drop);
|
160 |
hline();
|
161 |
|
162 |
//Try to close the database.
|
163 |
$result = mysql_close($handle);
|
164 |
if ($result === FALSE)
|
165 |
{
|
166 |
echo "ERROR: Unable to close MySQL connection.\n";
|
167 |
exit(1);
|
168 |
}
|
169 |
|
170 |
exit(0);
|
171 |
}
|
172 |
else
|
173 |
{
|
174 |
echo "ERROR: This command accepts no command-line parameters.\n";
|
175 |
exit(1);
|
176 |
}
|
177 |
|
178 |
//If we're here, success. Per the standard Unix way of thinking
|
179 |
//say nothing. Silence means OK.
|
180 |
exit(0);
|
181 |
//
|
182 |
//--------------------------------------------------------------------------------
|
183 |
//End of $RCSfile: dbdropall.php,v $.
|
184 |
//--------------------------------------------------------------------------------
|
185 |
?>
|