Change to QueryType

This commit is contained in:
Xephi 2015-12-30 09:59:28 +01:00
parent da1adb632e
commit f7010f1d3b
2 changed files with 54 additions and 63 deletions

View File

@ -1,15 +1,10 @@
package fr.xephi.authme.datasource.queries;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.datasource.DataSource;
public class Query {
@ -21,10 +16,7 @@ public class Query {
private List<String> into = new ArrayList<String>();
private List<String> values = new ArrayList<String>();
private List<String> updateSet = new ArrayList<String>();
private boolean isSelect = false;
private boolean isDelete = false;
private boolean isUpdate = false;
private boolean isInsert = false;
private QueryType type;
private String buildQuery = "";
/**
@ -44,10 +36,7 @@ public class Query {
public Query select(String selector)
{
this.selector = selector;
isSelect = true;
isDelete = false;
isUpdate = false;
isInsert = false;
type = QueryType.SELECT;
return this;
}
@ -57,10 +46,7 @@ public class Query {
*/
public Query update()
{
isSelect = false;
isDelete = false;
isUpdate = true;
isInsert = false;
type = QueryType.UPDATE;
return this;
}
@ -70,10 +56,7 @@ public class Query {
*/
public Query delete()
{
isSelect = false;
isDelete = true;
isUpdate = false;
isInsert = false;
type = QueryType.DELETE;
return this;
}
@ -84,10 +67,7 @@ public class Query {
*/
public Query insert()
{
isSelect = false;
isDelete = false;
isUpdate = false;
isInsert = true;
type = QueryType.INSERT;
return this;
}
@ -149,15 +129,16 @@ public class Query {
public Query build(){
StringBuilder str = new StringBuilder();
if (isSelect)
switch (type) {
case SELECT:
{
str.append("SELECT ").append(selector).append(" FROM ").append(from);
}
else if (isDelete)
case DELETE:
{
str.append("DELETE FROM ").append(from);
}
else if (isUpdate)
case UPDATE:
{
str.append("UPDATE ").append(from).append(" SET ");
Iterator<String> iter = updateSet.iterator();
@ -169,7 +150,7 @@ public class Query {
str.append(", ");
}
}
else if (isInsert)
case INSERT:
{
str.append("INSERT INTO ").append(from).append(" ('");
Iterator<String> iter = into.iterator();
@ -194,6 +175,7 @@ public class Query {
str.append("')");
}
}
}
if (!where.isEmpty())
{
str.append(" WHERE");

View File

@ -0,0 +1,9 @@
package fr.xephi.authme.datasource.queries;
public enum QueryType {
DELETE,
INSERT,
SELECT,
UPDATE;
}