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; 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.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
public class Query { public class Query {
@ -21,10 +16,7 @@ public class Query {
private List<String> into = new ArrayList<String>(); private List<String> into = new ArrayList<String>();
private List<String> values = new ArrayList<String>(); private List<String> values = new ArrayList<String>();
private List<String> updateSet = new ArrayList<String>(); private List<String> updateSet = new ArrayList<String>();
private boolean isSelect = false; private QueryType type;
private boolean isDelete = false;
private boolean isUpdate = false;
private boolean isInsert = false;
private String buildQuery = ""; private String buildQuery = "";
/** /**
@ -44,10 +36,7 @@ public class Query {
public Query select(String selector) public Query select(String selector)
{ {
this.selector = selector; this.selector = selector;
isSelect = true; type = QueryType.SELECT;
isDelete = false;
isUpdate = false;
isInsert = false;
return this; return this;
} }
@ -57,10 +46,7 @@ public class Query {
*/ */
public Query update() public Query update()
{ {
isSelect = false; type = QueryType.UPDATE;
isDelete = false;
isUpdate = true;
isInsert = false;
return this; return this;
} }
@ -70,10 +56,7 @@ public class Query {
*/ */
public Query delete() public Query delete()
{ {
isSelect = false; type = QueryType.DELETE;
isDelete = true;
isUpdate = false;
isInsert = false;
return this; return this;
} }
@ -84,10 +67,7 @@ public class Query {
*/ */
public Query insert() public Query insert()
{ {
isSelect = false; type = QueryType.INSERT;
isDelete = false;
isUpdate = false;
isInsert = true;
return this; return this;
} }
@ -149,49 +129,51 @@ public class Query {
public Query build(){ public Query build(){
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
if (isSelect) switch (type) {
{ case SELECT:
str.append("SELECT ").append(selector).append(" FROM ").append(from);
}
else if (isDelete)
{
str.append("DELETE FROM ").append(from);
}
else if (isUpdate)
{
str.append("UPDATE ").append(from).append(" SET ");
Iterator<String> iter = updateSet.iterator();
while (iter.hasNext())
{ {
String s = iter.next(); str.append("SELECT ").append(selector).append(" FROM ").append(from);
str.append(s);
if (iter.hasNext())
str.append(", ");
} }
} case DELETE:
else if (isInsert)
{
str.append("INSERT INTO ").append(from).append(" ('");
Iterator<String> iter = into.iterator();
while (iter.hasNext())
{ {
String s = iter.next(); str.append("DELETE FROM ").append(from);
str.append(s);
if (iter.hasNext())
str.append("', '");
else
str.append("')");
} }
str.append(" VALUES ('"); case UPDATE:
iter = values.iterator();
while (iter.hasNext())
{ {
String s = iter.next(); str.append("UPDATE ").append(from).append(" SET ");
str.append(s); Iterator<String> iter = updateSet.iterator();
if (iter.hasNext()) while (iter.hasNext())
str.append("', '"); {
else String s = iter.next();
str.append("')"); str.append(s);
if (iter.hasNext())
str.append(", ");
}
}
case INSERT:
{
str.append("INSERT INTO ").append(from).append(" ('");
Iterator<String> iter = into.iterator();
while (iter.hasNext())
{
String s = iter.next();
str.append(s);
if (iter.hasNext())
str.append("', '");
else
str.append("')");
}
str.append(" VALUES ('");
iter = values.iterator();
while (iter.hasNext())
{
String s = iter.next();
str.append(s);
if (iter.hasNext())
str.append("', '");
else
str.append("')");
}
} }
} }
if (!where.isEmpty()) if (!where.isEmpty())

View File

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