1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * $Id: check_database.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
19 class check_database {
25 string config_file = "config/config.txt.steam";
32 #define NEED_ARGS(s) if (!arg) { write("--"+(string)s+" needs an argument passed\n");return;}
34 int get_variable(string name)
37 res = db()->big_query("select value from variables where "+
39 if (objectp(res) && res->num_rows())
40 return (int) res->fetch_row()[0];
45 string read_config(string filename, string param)
47 string config = Stdio.read_file(filename);
49 sscanf(config, "%*s<"+param+">%s</"+param+">%*s", acc_str);
51 write(sprintf("reading config file %s accessing %s\n",
59 handle = Sql.Sql(db_connect);
65 int iObjectsId = get_variable("#objects");
68 // check for existing differing tables
69 if (sizeof(db()->list_tables("i_"+(string)iObjectsId)))
71 if (sizeof(db()->list_tables("i_objects")))
74 // we have both objects and i_oid_alias
78 res = db()->big_query("select v from i_objects "+
79 "where k=\"rootroom\"");
80 string id_objects = res->fetch_row()[0];
81 res = db()->big_query("select v from i_"+
82 (string)iObjectsId+" where k="+
84 string id_ioid = res->fetch_row()[0];
86 if ( ((int) id_objects[1..]) < ((int) id_ioid[1..]))
96 write(sprintf("Object ID of module objects is %d status %d\n",
98 // i_objects found without alternatives, thus naming is 1, but are
99 // there other strange modules?
104 db()->big_query("select var, value from variables where var "+
108 while (vec = res->fetch_row())
111 if (sizeof(db()->list_tables("i_"+value)))
112 return 6; // at least one module with aliases
121 void main(int argc, array argv)
123 for (int i=1;i <sizeof(argv); i++) {
125 if (sscanf(argv[i], "--%s=%s", cmd, arg) ==2 ||
126 sscanf(argv[i], "--%s", cmd))
131 db_connect = read_config(config_file,"database");
134 write("no tables found -> unable to repair!\n");
137 write("no table i_objects -> rename i_oid tables.\n");
140 write("i_objects exists and there is no alternative "+
141 "table -> everything seems to be ok!\n");
144 write("huh? differing tables exist, but couldn't "+
148 write("differing tables found i_objects seems to be "+
149 "the original -> deleting i_oid files.\n");
150 write(sprintf("naming %d\n",naming));
153 write("differing tables found i_oid seems to be "+
154 "the original -> moving values.\n");
156 write("i_objects exists and has no alternative, but "+
157 "there are other broken modules.\n");
163 NEED_ARGS("database");
167 NEED_ARGS("with-config");
170 db_connect = read_config(config_file,"database");
184 db_connect = read_config(config_file, "database");
188 Sql.sql_result modules =
189 db()->big_query("select var, value from variables where var "+
194 array temp = ({ ({ "#modules", "0" }) });
195 while (vec = modules->fetch_row())
202 if (search(db()->list_tables("i_"+value), "i_"+value)!=-1)
204 if (search(db()->list_tables("i_"+var[1..]), "i_"+var[1..])!=-1)
209 write(sprintf("rename i_%s to i_%s\n", value, var[1..]));
212 db()->big_query(sprintf("alter table i_%s rename to i_%s",
217 write(sprintf("keep i_%s\n", var[1..]));
220 if (naming ==1) // heuristics is from table i_objects
222 write(sprintf("conflict keep i_%s and "+
227 db()->big_query(sprintf("drop table i_%s", value));
232 write(sprintf("conflict move values from i_%s to"+
233 " i_%s\n", value, var[1..]));
236 db()->big_query(sprintf("drop table i_%s", var[1..]));
237 db()->big_query(sprintf("alter table i_%s rename "+
238 "to i_%s", value, var[1..]));