hal/src/zutil/db/DBQueue.java

216 lines
5.9 KiB
Java
Raw Normal View History

2011-07-13 17:53:17 +00:00
/*******************************************************************************
* Copyright (c) 2011 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
2008-11-14 16:38:36 +00:00
package zutil.db;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
2010-04-15 20:52:34 +00:00
import java.sql.Statement;
2008-11-14 16:38:36 +00:00
import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
2009-05-17 18:46:05 +00:00
import zutil.converters.Converter;
2010-10-27 18:02:44 +00:00
import zutil.io.MultiPrintStream;
2008-11-14 16:38:36 +00:00
/**
* This class creates a queue that stors the
* data in a mysql table.
* The table should look like this:
* CREATE TABLE `queue` (
* `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
* `data` BINARY NOT NULL
* );
* @author Ziver
*
*/
2010-04-15 20:52:34 +00:00
public class DBQueue<E> implements Queue<E>{
// GO TO KNOW = SELECT LAST_INSERT_ID() as pos_id
2010-04-15 20:52:34 +00:00
private DBConnection db;
2008-11-14 16:38:36 +00:00
private String table;
/**
2010-04-15 20:52:34 +00:00
* Initiates the queue.<br>
* <b>WARNING!!<b> this will erase all rows in the table
*
* @param db is the connection to the DB
* @param table is the name of the table
2008-11-14 16:38:36 +00:00
*/
2010-04-15 20:52:34 +00:00
public DBQueue(DBConnection db, String table){
2008-11-14 16:38:36 +00:00
this.db = db;
this.table = table;
}
public boolean add(Object arg0){
try {
2010-04-15 20:52:34 +00:00
PreparedStatement sql = db.getPreparedStatement("INSERT INTO "+table+" (data) VALUES(?)");
2008-11-14 16:38:36 +00:00
sql.setObject(1, arg0);
2010-04-15 20:52:34 +00:00
DBConnection.exec(sql);
2008-11-14 16:38:36 +00:00
} catch (Exception e) {
e.printStackTrace(MultiPrintStream.out);
return false;
}
return true;
}
public E element() {
return peek();
}
public boolean offer(Object arg0) {
2010-04-15 20:52:34 +00:00
return add(arg0);
2008-11-14 16:38:36 +00:00
}
@SuppressWarnings("unchecked")
public synchronized E peek() {
try {
2010-04-15 20:52:34 +00:00
return db.exec("SELECT * FROM "+table+" LIMIT 1", new SQLResultHandler<E>(){
2010-10-07 11:27:23 +00:00
public E handleQueryResult(Statement stmt, ResultSet rs) throws SQLException{
2010-04-15 20:52:34 +00:00
if (rs.next())
try {
return (E) Converter.toObject(rs.getBytes("data"));
} catch (Exception e) {
e.printStackTrace(MultiPrintStream.out);
}
return null;
}
});
2008-11-14 16:38:36 +00:00
} catch (Exception e) {
e.printStackTrace(MultiPrintStream.out);
}
return null;
}
@SuppressWarnings("unchecked")
public synchronized E poll() {
try {
2010-04-15 20:52:34 +00:00
return db.exec("SELECT * FROM "+table+" LIMIT 1", new SQLResultHandler<E>(){
2010-10-07 11:27:23 +00:00
public E handleQueryResult(Statement stmt, ResultSet rs) {
2010-04-15 20:52:34 +00:00
try{
if (rs.next()) {
db.exec("DELETE FROM "+table+" WHERE id="+rs.getInt("id")+" LIMIT 1");
return (E) Converter.toObject(rs.getBytes("data"));
}
}catch(Exception e){
e.printStackTrace(MultiPrintStream.out);
}
return null;
}
});
2008-11-14 16:38:36 +00:00
} catch (Exception e) {
e.printStackTrace(MultiPrintStream.out);
}
return null;
}
public E remove() {
return poll();
}
2009-08-22 18:18:54 +00:00
public boolean addAll(Collection<? extends E> arg0) {
2008-11-14 16:38:36 +00:00
// TODO Auto-generated method stub
return false;
}
public void clear() {
try {
2010-04-15 20:52:34 +00:00
db.exec("TRUNCATE TABLE `"+table+"`");
2008-11-14 16:38:36 +00:00
} catch (SQLException e) {
e.printStackTrace(MultiPrintStream.out);
}
}
public boolean contains(Object arg0) {
try {
2010-04-15 20:52:34 +00:00
return db.exec("SELECT data FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1", new SQLResultHandler<Boolean>(){
2010-10-07 11:27:23 +00:00
public Boolean handleQueryResult(Statement stmt, ResultSet rs) throws SQLException{
2010-04-15 20:52:34 +00:00
return rs.next();
}
});
2008-11-14 16:38:36 +00:00
} catch (Exception e) {
e.printStackTrace(MultiPrintStream.out);
}
return false;
}
2009-08-22 18:18:54 +00:00
public boolean containsAll(Collection<?> arg0) {
2008-11-14 16:38:36 +00:00
// TODO Auto-generated method stub
return false;
}
public boolean isEmpty() {
return (peek() != null);
}
2009-08-22 18:18:54 +00:00
public Iterator<E> iterator() {
// TODO: Auto-generated method stub
2008-11-14 16:38:36 +00:00
return null;
}
public synchronized boolean remove(Object arg0) {
try {
2010-04-15 20:52:34 +00:00
db.exec("DELETE FROM "+table+" WHERE data='"+Converter.toBytes(arg0)+"' LIMIT 1");
2008-11-14 16:38:36 +00:00
return true;
} catch (Exception e) {
e.printStackTrace(MultiPrintStream.out);
}
return false;
}
2009-08-22 18:18:54 +00:00
public synchronized boolean removeAll(Collection<?> arg0) {
2008-11-14 16:38:36 +00:00
// TODO Auto-generated method stub
return false;
}
2009-08-22 18:18:54 +00:00
public boolean retainAll(Collection<?> arg0) {
2008-11-14 16:38:36 +00:00
// TODO Auto-generated method stub
return false;
}
public int size() {
try {
2010-04-15 20:52:34 +00:00
return db.exec("SELECT count(*) FROM "+table, new SQLResultHandler<Integer>(){
2010-10-07 11:27:23 +00:00
public Integer handleQueryResult(Statement stmt, ResultSet rs) throws SQLException{
2010-04-15 20:52:34 +00:00
if (rs.next())
return rs.getInt(1);
return 0;
}
});
2008-11-14 16:38:36 +00:00
} catch (Exception e) {
e.printStackTrace(MultiPrintStream.out);
}
return 0;
}
public E[] toArray() {
// TODO Auto-generated method stub
return null;
}
@SuppressWarnings("unchecked")
public E[] toArray(Object[] arg0) {
// TODO Auto-generated method stub
return null;
}
}