Permalink
Browse files

Fixed SQL spam; ModuleFactoid now imports data from old format

  • Loading branch information...
1 parent 15085c0 commit 47783a4cdb26a63e51684db96b510984eb5e84f7 @Shockah Shockah committed May 12, 2012
Showing with 35 additions and 8 deletions.
  1. +27 −5 modulesrc/ModuleFactoid.java
  2. +8 −0 src/pl/shockah/Config.java
  3. +0 −3 src/pl/shockah/shocky/sql/SQL.java
@@ -19,7 +19,6 @@
public class ModuleFactoid extends Module {
protected Command cmdR, cmdF, cmdFCMD, cmdManage;
private ArrayList<CmdFactoid> fcmds = new ArrayList<CmdFactoid>();
- private Config config;
private HashMap<String,Function> functions = new HashMap<String,Function>();
private static Pattern functionPattern = Pattern.compile("([a-zA-Z_][a-zA-Z0-9_]*)\\(.*?\\)");
@@ -33,8 +32,33 @@ public void onEnable() {
SQL.raw("CREATE TABLE IF NOT EXISTS "+SQL.getTable("factoid")+" (channel TEXT NOT NULL,factoid TEXT,author TEXT,rawtext TEXT,stamp INT(10) UNSIGNED NOT NULL,locked INT(1) UNSIGNED NOT NULL DEFAULT 0,forgotten INT(1) UNSIGNED NOT NULL DEFAULT 0)");
- config = new Config();
- config.load(new File("data","factoid.cfg"));
+ if (new File("data","factoid.cfg").exists()) {
+ Config config = new Config();
+ config.load(new File("data","factoid.cfg"));
+
+ ArrayList<String> cfgs = new ArrayList<String>();
+ cfgs.add(null);
+ cfgs.addAll(config.getKeysSubconfigs());
+
+ for (String subc : cfgs) {
+ Config cfg = subc == null ? config : config.getConfig(subc);
+ ArrayList<String> factoids = new ArrayList<String>();
+ for (String s : cfg.getKeys()) if (s.startsWith("r_")) factoids.add(s);
+ for (String s : factoids) {
+ s = s.substring(2);
+ QueryInsert q = new QueryInsert(SQL.getTable("factoid"));
+ q.add("channel",subc == null ? "" : subc);
+ q.add("factoid",s);
+ q.add("author",cfg.getString("b_"+s));
+ q.add("rawtext",cfg.getString("r_"+s));
+ q.add("stamp",0);
+ SQL.insert(q);
+ if (cfg.exists("l_"+s)) q.add("locked",1);
+ }
+ }
+
+ new File("data","factoid.cfg").delete();
+ }
ArrayList<String> lines = FileLine.read(new File("data","factoidCmd.cfg"));
for (int i = 0; i < lines.size(); i += 2) fcmds.add(new CmdFactoid(lines.get(i),lines.get(i+1)));
@@ -97,8 +121,6 @@ public void onDisable() {
Command.removeCommands(cmdR,cmdF,cmdFCMD,cmdManage);
}
public void onDataSave() {
- config.save(new File("data","factoid.cfg"));
-
ArrayList<String> lines = new ArrayList<String>();
for (CmdFactoid fcmd : fcmds) {
StringBuilder sb = new StringBuilder();
@@ -6,6 +6,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
+import java.util.Set;
import java.util.regex.Pattern;
import pl.shockah.FileLine;
@@ -93,6 +94,13 @@ public boolean getBoolean(String key) {
public float getFloat(String key) {return Float.parseFloat(getString(key));}
public double getDouble(String key) {return Double.parseDouble(getString(key));}
+ public Set<String> getKeysSubconfigs() {
+ return mapSubconfigs.keySet();
+ }
+ public Set<String> getKeys() {
+ return mapValues.keySet();
+ }
+
public void load(File file) {
load(FileLine.read(file));
}
@@ -12,23 +12,20 @@
public static String update(QueryUpdate query) {return queryRaw(query.getSQLQuery());}
public static String queryRaw(String query) {
- System.out.println(query);
HTTPQuery q = new HTTPQuery(Data.config.getString("main-sqlurl")+"?"+HTTPQuery.parseArgs("type","raw","q",query,"eval",getEval()),"GET");
q.connect(true,false);
String s = q.readWhole();
q.close();
return s;
}
public static String queryInsertId(String query) {
- System.out.println(query);
HTTPQuery q = new HTTPQuery(Data.config.getString("main-sqlurl")+"?"+HTTPQuery.parseArgs("type","insertid","q",query,"eval",getEval()),"GET");
q.connect(true,false);
String s = q.readWhole();
q.close();
return s;
}
public static JSONObject queryJSON(String query) {
- System.out.println(query);
HTTPQuery q = new HTTPQuery(Data.config.getString("main-sqlurl")+"?"+HTTPQuery.parseArgs("type","json","q",query,"eval",getEval()),"GET");
q.connect(true,false);
JSONObject j = JSONObject.deserialize(q.readWhole());

0 comments on commit 47783a4

Please sign in to comment.