]>
jfr.im git - irc/Ozafy/borknet_p10_irc_services.git/blob - core/modules/s/DBControl.java
3 # BorkNet Services Core
7 # Copyright (C) 2004 Ozafy - ozafy@borknet.org - http://www.borknet.org
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 import java
.security
.*;
28 import borknet_services
.core
.*;
31 * The database communication class of the Q IRC C.
32 * @author Ozafy - ozafy@borknet.org - http://www.borknet.org
34 public class DBControl
39 private String dbname
= "services.db";
41 private CoreDBControl dbc
;
45 private HashMap
<String
,String
> channels
= new HashMap
<String
,String
>();
46 private HashMap
<String
,String
> userMsg
= new HashMap
<String
,String
>();
47 private HashMap
<String
,Integer
> userPoints
= new HashMap
<String
,Integer
>();
48 private int kills
= 1;
51 * Constructs a Database connection.
52 * @param server Database server
53 * @param user Database user
54 * @param pass Database password
56 * @param debug Are we debugging?
59 public DBControl(Core C
, S Bot
)
65 this.dbc
= C
.get_dbc();
67 C
.printDebug("[>---<] >> *** Checking SQL db...");
69 C
.printDebug( "[>---<] >> *** Done." );
74 C
.printDebug("Database error!");
76 C
.die("SQL error, trying to die gracefully.");
80 private void testDriver ( )
84 Class
.forName ( "org.sqlite.JDBC" );
85 C
.printDebug( "[>---<] >> *** SQL Driver Found" );
87 catch ( java
.lang
.ClassNotFoundException e
)
89 C
.printDebug("SQL JDBC Driver not found!");
90 C
.die("SQL error, trying to die gracefully.");
94 private void createTables()
96 Connection con
= null;
99 con
= DriverManager
.getConnection("jdbc:sqlite:"+dbname
);
100 Statement statement
= con
.createStatement();
101 statement
.setQueryTimeout(30);
102 statement
.executeUpdate("CREATE TABLE IF NOT EXISTS s_channels (name string, flags string)");
104 catch ( Exception e
)
106 C
.printDebug("Database error!");
108 C
.die("SQL error, trying to die gracefully.");
121 C
.printDebug("Database error!");
123 C
.die("SQL error, trying to die gracefully.");
128 private void executeUpdate(String query
, String
[] params
)
130 Connection con
= null;
133 con
= DriverManager
.getConnection("jdbc:sqlite:"+dbname
);
134 PreparedStatement pstmt
= con
.prepareStatement(query
);
135 for(int i
=0;i
<params
.length
;i
++)
137 pstmt
.setString((i
+1),params
[i
]);
139 pstmt
.executeUpdate();
141 catch ( Exception e
)
143 C
.printDebug("Database error!");
145 C
.die("SQL error, trying to die gracefully.");
158 C
.printDebug("Database error!");
160 C
.die("SQL error, trying to die gracefully.");
165 public boolean chanExists(String chan
)
167 return dbc
.chanExists(chan
);
170 public boolean SchanExists(String chan
)
172 return channels
.containsKey(chan
.toLowerCase());
175 public List
<String
> getChanTable()
177 List
<String
> keys
= new ArrayList
<String
>(channels
.keySet());
183 Connection con
= null;
186 con
= DriverManager
.getConnection("jdbc:sqlite:"+dbname
);
187 Statement statement
= con
.createStatement();
188 statement
.setQueryTimeout(30);
189 ResultSet rs
= statement
.executeQuery("SELECT name,flags FROM s_channels");
190 ArrayList
<String
> a
= new ArrayList
<String
>();
193 channels
.put(rs
.getString("name").toLowerCase(),rs
.getString("flags"));
196 catch ( Exception e
)
198 C
.printDebug("Database error!");
200 C
.die("SQL error, trying to die gracefully.");
213 C
.printDebug("Database error!");
215 C
.die("SQL error, trying to die gracefully.");
220 public String
getChanFlags(String channel
)
222 return channels
.get(channel
.toLowerCase());
225 public int getPoints(String user
)
227 return userPoints
.get(user
);
235 public boolean isService(String numeric
)
237 return dbc
.isService(numeric
);
240 public boolean repeat(String user
, String msg
)
244 String m
= userMsg
.get(user
);
245 return m
.equals(msg
);
253 public void delChan(String chan
)
255 executeUpdate("DELETE FROM s_channels WHERE name = ?", new String
[] {chan
});
256 channels
.remove(chan
.toLowerCase());
259 public void delPoints(int points
)
261 List
<String
> users
= new ArrayList
<String
>(userPoints
.keySet());
262 for(String user
: users
)
264 int p
= userPoints
.get(user
);
268 userPoints
.remove(user
);
269 userMsg
.remove(user
);
273 userPoints
.put(user
,p
);
278 public void addPoints(String user
, int points
)
280 Integer p
= userPoints
.get(user
);
281 if(p
instanceof Integer
)
283 userPoints
.put(user
,(points
+p
));
287 userPoints
.put(user
,points
);
291 public void addChan(String chan
, String flags
)
293 executeUpdate("INSERT INTO s_channels VALUES (?,?)", new String
[] {chan
, flags
});
294 channels
.put(chan
.toLowerCase(),flags
);
297 public boolean setChanFlags(String chan
, String flags
)
301 executeUpdate("UPDATE s_channels SET flags = ? WHERE name = ?", new String
[] {flags
, chan
});
302 channels
.put(chan
.toLowerCase(),flags
);
305 catch ( Exception e
)
311 public void setMsg(String user
, String msg
)
313 userMsg
.put(user
,msg
);
316 public int getChanUsers(String channel
)
318 return dbc
.getChanUsers(channel
);
323 List
<String
> channelkeys
= new ArrayList
<String
>(channels
.keySet());
324 for(String channel
: channelkeys
)
326 if(getChanUsers(channel
) < 1)
333 public User
getUser(String numer
)
335 return dbc
.getUser(numer
);