InquireDB.java
Go to the documentation of this file.
1 package p2pserver;
2 
3 import java.sql.*;
4 import java.util.ArrayList;
5 
11 public class InquireDB
12 {
13 
14  private Connection c;
15  private Statement s;
16  private boolean speak;
17 
29  public InquireDB(boolean disperr, String db, String user, String pswd)
30  {
31  super();
32  speak = disperr;
33  c = p2pserver.Connecttodbms.connect(speak,db,user,pswd);
34  s = p2pserver.Connecttodbms.createStat(speak,c);
35  }
36 
45  public boolean putFileList(ArrayList l)
46  {
47  for(int i=0;i<l.size();i++)
48  {
49  ArrayList ll = (ArrayList)l.get(i);
50  int j = -1;
51 
52  String query = "INSERT INTO "+p2pserver.PrepareDB_user.tab2()+
53  " VALUES("+
54  ll.get(++j).toString()+","+
55  "'"+ll.get(++j).toString()+"',"+
56  "'"+ll.get(++j).toString()+"',"+
57  ll.get(++j).toString()+","+
58  ll.get(++j).toString()+","+
59  ll.get(++j).toString()+","+
60  "'"+ll.get(++j).toString()+"',"+
61  ll.get(++j).toString()+","+
62  ll.get(++j).toString()+","+
63  "default"+
64  ");";
65  //if(speak) p2putility.Util.logServer(query);
66  boolean ok = p2pserver.Connecttodbms.noRSetQuery(speak,s,query);
67 
68  if(!ok) return false;
69  }
70  return true;
71  }
72 
90  public boolean delFileListBy(String ckey, String name, String path, String id,
91  String isdir, String size, String cdate, String rights,
92  String state, String idate
93  )
94  {
95  String query = "DELETE FROM "+p2pserver.PrepareDB_user.tab2();
96 
97  boolean bef =false;
98 
99  if(ckey!=null){ query +=" WHERE ckey="+ckey; bef = true;}
100 
101  if(bef&&(name!=null)) query += " AND name='"+name+"'";
102  else if(name!=null){ query += " WHERE name='"+name+"'"; bef=true;}
103 
104  if(bef&&(path!=null)) query += " AND path='"+path+"'";
105  else if(path!=null){ query += " WHERE path='"+path+"'"; bef=true;}
106 
107  if(bef&&(id!=null)) query += " AND id="+id;
108  else if(id!=null){ query += " WHERE id="+id; bef=true;}
109 
110  if(bef&&(isdir!=null)) query += " AND isdir="+isdir;
111  else if(isdir!=null){ query += " WHERE isdir="+isdir; bef=true;}
112 
113  if(bef&&(size!=null)) query += " AND size="+size;
114  else if(size!=null){ query += " WHERE size="+size; bef=true;}
115 
116  if(bef&&(cdate!=null)) query += " AND cdate='"+cdate+"'";
117  else if(cdate!=null){ query += " WHERE cdate='"+cdate+"'"; bef=true;}
118 
119  if(bef&&(rights!=null)) query += " AND rights="+rights;
120  else if(rights!=null){ query += " WHERE rights="+rights; bef=true;}
121 
122  if(bef&&(state!=null)) query += " AND state="+state;
123  else if(state!=null){ query += " WHERE state="+state; bef=true;}
124 
125  if(bef&&(idate!=null)) query += " AND idate='"+idate+"'";
126  else if(idate!=null){ query += " WHERE idate='"+idate+"'"; bef=true;}
127 
128  query += ";";
129 
130  //if(speak) p2putility.Util.logServer(query);
131  return p2pserver.Connecttodbms.noRSetQuery(speak,s,query);
132  }
133 
140  public long nextCkeyFileList() {
141  long ret = 0;
142  try {
143  ResultSet r = p2pserver.Connecttodbms.yesRSetQuery(speak, s, "SELECT MAX(ckey) AS c FROM " + p2pserver.PrepareDB_user.tab2());
144 
145  if (r.next())
146  ret = r.getLong("c");
147 
148  if(speak) p2putility.Util.logServer("Count file: "+(ret+1));
149 
151 
152  } catch (SQLException ex) {
153  if(speak) p2putility.Util.logServer("nextCkeyFileList: "+ex);
154  }
155  return (ret+1);
156  }
157 
166  public ArrayList getFileList(String where)
167  {
168  where = validate(where);
169 
170  ArrayList l = new ArrayList();
171 
172  ResultSet r = p2pserver.Connecttodbms.yesRSetQuery(speak,s,"SELECT * FROM "+p2pserver.PrepareDB_user.tab2()+where);
173 
174  try
175  {
176  int i = -1;
177  while(r.next())
178  {
179  int j = -1;
180  ArrayList ll = new ArrayList();
181  ll.add(++j, r.getInt("ckey"));
182  ll.add(++j, r.getString("name"));
183  ll.add(++j, r.getString("path"));
184  ll.add(++j, r.getInt("id"));
185  ll.add(++j, r.getBoolean("isdir"));
186  ll.add(++j, r.getInt("size"));
187  ll.add(++j, r.getDate("cdate"));
188  ll.add(++j, r.getInt("rights"));
189  ll.add(++j, r.getInt("state"));
190  ll.add(++j, r.getTimestamp("idate"));
191 
192  l.add(++i, ll);
193 
194  }
195 
196  }
197  catch(Exception x)
198  {
199  if(speak) if(speak) p2putility.Util.logServer("InquireDB getFileList ERROR!\n"+x);
200  return null;
201  }
202 
204 
205  return l;
206  }
207 
216  public boolean putUserList(ArrayList l)
217  {
218  for(int i=0;i<l.size();i++)
219  {
220  ArrayList ll = (ArrayList)l.get(i);
221  int j = -1;
222 
223  String query = "INSERT INTO "+p2pserver.PrepareDB_user.tab1()+
224  " VALUES("+
225  ll.get(++j).toString()+","+
226  "'"+ll.get(++j).toString()+"',"+
227  "'"+ll.get(++j).toString()+"',"+
228  "'"+ll.get(++j).toString()+"'"+
229  ");";
230  //if(speak) p2putility.Util.logServer(query);
231  boolean ok = p2pserver.Connecttodbms.noRSetQuery(speak,s,query);
232  if(!ok) return false;
233  }
234  return true;
235  }
236 
248  public boolean delUserListBy(String id, String ip, String username, String password)
249  {
250  String query = "DELETE FROM "+p2pserver.PrepareDB_user.tab1();
251 
252  boolean bef =false;
253 
254  if(id!=null){ query +=" WHERE id="+id; bef = true;}
255 
256  if(bef&&(ip!=null)) query += " AND ip='"+ip+"'";
257  else if(ip!=null){ query += " WHERE ip='"+ip+"'"; bef=true;}
258 
259  if(bef&&(username!=null)) query += " AND username='"+username+"'";
260  else if(username!=null){ query += " WHERE username='"+username+"'"; bef=true;}
261 
262  if(bef&&(password!=null)) query += " AND password='"+password+"'";
263  else if(password!=null){ query += " WHERE password='"+password+"'"; bef=true;}
264 
265  query += ";";
266 
267  return p2pserver.Connecttodbms.noRSetQuery(speak,s,query);
268  }
269 
276  public int nextUserID(){
277  try {
278  ResultSet r = p2pserver.Connecttodbms.yesRSetQuery(speak, s, "SELECT MAX(id) AS i FROM " + p2pserver.PrepareDB_user.tab1());
279  if(r!=null)
280  if (r.next())
281  return (r.getInt("i") + 1); //next
282 
283  }catch (Exception ex){if(speak) p2putility.Util.logServer("nextUserID error: "+ex);}
284  return 0;// (long)(java.lang.Math.random()*100); // id casuale tra 0 e 100
285  }
286 
295  public ArrayList getUserList(String where)
296  {
297  where = validate(where);
298 
299  ArrayList l = new ArrayList();
300 
301  ResultSet r = p2pserver.Connecttodbms.yesRSetQuery(speak,s,"SELECT * FROM "+p2pserver.PrepareDB_user.tab1()+where);
302 
303  try
304  {
305  int i = -1;
306  while(r.next())
307  {
308  int j = -1;
309  ArrayList ll = new ArrayList();
310  ll.add(++j, r.getInt("id"));
311  ll.add(++j, r.getString("ip"));
312  ll.add(++j, r.getString("username"));
313  ll.add(++j, r.getString("password"));
314 
315  l.add(++i, ll);
316 
317  }
318 
319  }
320  catch(Exception x)
321  {
322  if(speak) p2putility.Util.logServer("InquireDB getUserList ERROR!\n"+x);
323  return null;
324  }
325 
327 
328  return l;
329  }
330 
335  public void close()
336  {
339  }
340 
347  private String validate(String where){
348 
349  if(where==null) return "";
350 
351  if(where.length()<5) return "";
352 
353  if(!where.startsWith(" ")) return (" "+where);
354 
355  return where;
356  }
357 
363  public boolean isOpen(){
364  try
365  {
366  if(!c.isClosed() && !s.isClosed())
367  return true;
368  }
369  catch(Exception sqe){
370  if(speak) p2putility.Util.logServer("isOpen error: "+sqe);
371  return false;
372  }
373 
374  return false;
375  }
376 }
Classe che mette a disposizione funzioni per interfacciarsi al DBMS in modo rapido ed eventualmente s...
Definition: InquireDB.java:11
long nextCkeyFileList()
Seleziona il prossimo ID file.
Definition: InquireDB.java:140
String validate(String where)
Verifica se la stringa e&#39; valida altrimenti la modifica o la annulla.
Definition: InquireDB.java:347
boolean delUserListBy(String id, String ip, String username, String password)
Cancella i dati nella tabella identificata da PrepareDB_user.tab1() in base ai valori specificati in ...
Definition: InquireDB.java:248
Classe che implementa metodi vari di utilita&#39; generale.
Definition: Util.java:14
ArrayList getUserList(String where)
Restituisce la lista degli utenti presenti nel DBMS, in base alla clausola WHERE specificabile in inp...
Definition: InquireDB.java:295
boolean delFileListBy(String ckey, String name, String path, String id, String isdir, String size, String cdate, String rights, String state, String idate)
Cancella i dati nella tabella identificata da PrepareDB_user.tab2() in base ai valori specificati in ...
Definition: InquireDB.java:90
InquireDB(boolean disperr, String db, String user, String pswd)
Costruttore.
Definition: InquireDB.java:29
Classe che permette la preparazione delle tabelle accessibili dall&#39;utente specificato in configurazio...
static boolean noRSetQuery(boolean speak, Statement s, String query)
Esegue una query sullo Statement specificato.
Classe che mette a disposizione funzioni per interfacciarsi al DBMS in modo rapido ed eventualmente s...
boolean isOpen()
Verifica se la connessione al DBMS e&#39; attiva.
Definition: InquireDB.java:363
void close()
Chiude la connessione al DBMS aperta dal costruttore.
Definition: InquireDB.java:335
int nextUserID()
Seleziona il prossimo ID user.
Definition: InquireDB.java:276
boolean putFileList(ArrayList l)
Inserisce valori ricavati da un ArrayList di ArrayList nella tabella identificata da PrepareDB_user...
Definition: InquireDB.java:45
static ResultSet yesRSetQuery(boolean speak, Statement s, String query)
Esegue una query sullo Statement specificato.
static boolean closeStat(boolean speak, Statement s)
Chiude lo Statement specificato.
ArrayList getFileList(String where)
Restituisce la lista dei file presenti nel DBMS, in base alla clausola WHERE specificabile in input...
Definition: InquireDB.java:166
static Connection connect(boolean speak, String dbms, String user, String pswd)
Restituisce una connessione (Connection) al DB selezionato.
static Statement createStat(boolean speak, Connection db)
Crea uno Statement con la connessione specificata.
static boolean closeRSet(boolean speak, ResultSet r)
Chiude il ResultSet specificato.
boolean putUserList(ArrayList l)
Inserisce valori ricavati da un ArrayList di ArrayList nella tabella identificata da PrepareDB_user...
Definition: InquireDB.java:216
static void logServer(String data)
Scrive i dati in input sul file di log del Server (se questo esiste valido)
Definition: Util.java:110
static boolean disconnect(boolean speak, Connection db)
Disconnette la connessione specificata.