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,15 +129,16 @@ 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); str.append("SELECT ").append(selector).append(" FROM ").append(from);
} }
else if (isDelete) case DELETE:
{ {
str.append("DELETE FROM ").append(from); str.append("DELETE FROM ").append(from);
} }
else if (isUpdate) case UPDATE:
{ {
str.append("UPDATE ").append(from).append(" SET "); str.append("UPDATE ").append(from).append(" SET ");
Iterator<String> iter = updateSet.iterator(); Iterator<String> iter = updateSet.iterator();
@ -169,7 +150,7 @@ public class Query {
str.append(", "); str.append(", ");
} }
} }
else if (isInsert) case INSERT:
{ {
str.append("INSERT INTO ").append(from).append(" ('"); str.append("INSERT INTO ").append(from).append(" ('");
Iterator<String> iter = into.iterator(); Iterator<String> iter = into.iterator();
@ -194,6 +175,7 @@ public class Query {
str.append("')"); str.append("')");
} }
} }
}
if (!where.isEmpty()) if (!where.isEmpty())
{ {
str.append(" WHERE"); str.append(" WHERE");

View File

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