Nouvelle version ORM Java
This commit is contained in:
parent
8463042cc7
commit
159cbe52af
43
src/fr/pandacube/java/util/db2/SQLContact.java
Normal file
43
src/fr/pandacube/java/util/db2/SQLContact.java
Normal file
@ -0,0 +1,43 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLContact extends SQLElement {
|
||||
|
||||
public SQLContact() { super(); }
|
||||
public SQLContact(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_contact"; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static final SQLField<Integer> time = new SQLField<>("time", SQLType.INT, false);
|
||||
public static final SQLField<String> playerId = new SQLField<>("playerId", SQLType.CHAR(36), true);
|
||||
public static final SQLField<String> userName = new SQLField<>("userName", SQLType.VARCHAR(50), true);
|
||||
public static final SQLField<String> userMail = new SQLField<>("userMail", SQLType.VARCHAR(50), true);
|
||||
public static final SQLField<String> titre = new SQLField<>("titre", SQLType.VARCHAR(100), false);
|
||||
public static final SQLField<String> texte = new SQLField<>("texte", SQLType.TEXT, false);
|
||||
public static final SQLField<Boolean> hidden = new SQLField<>("hidden", SQLType.BOOLEAN, false, (Boolean)false);
|
||||
|
||||
|
||||
|
||||
public UUID getPlayerId() {
|
||||
String id = (String)get(playerId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setPlayerId(UUID pName) {
|
||||
set(playerId, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
20
src/fr/pandacube/java/util/db2/SQLForumCategorie.java
Normal file
20
src/fr/pandacube/java/util/db2/SQLForumCategorie.java
Normal file
@ -0,0 +1,20 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLForumCategorie extends SQLElement {
|
||||
|
||||
public SQLForumCategorie() { super(); }
|
||||
public SQLForumCategorie(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_forum_categorie"; }
|
||||
|
||||
|
||||
public static final SQLField<String> nom = new SQLField<>("nom", SQLType.VARCHAR(100), false);
|
||||
public static final SQLField<Integer> ordre = new SQLField<>("ordre", SQLType.INT, false);
|
||||
|
||||
|
||||
}
|
31
src/fr/pandacube/java/util/db2/SQLForumForum.java
Normal file
31
src/fr/pandacube/java/util/db2/SQLForumForum.java
Normal file
@ -0,0 +1,31 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLForumForum extends SQLElement {
|
||||
|
||||
|
||||
public SQLForumForum() { super(); }
|
||||
public SQLForumForum(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_forum_forum"; }
|
||||
|
||||
|
||||
public static final SQLField<Integer> catId = new SQLField<>("catId", SQLType.INT, false);
|
||||
public static final SQLField<String> nom = new SQLField<>("nom", SQLType.VARCHAR(100), false);
|
||||
public static final SQLField<String> description = new SQLField<>("description", SQLType.TEXT, false);
|
||||
public static final SQLField<Integer> ordre = new SQLField<>("ordre", SQLType.INT, false);
|
||||
public static final SQLField<Integer> authView = new SQLField<>("authView", SQLType.INT, false);
|
||||
public static final SQLField<Integer> authPost = new SQLField<>("authPost", SQLType.INT, false);
|
||||
public static final SQLField<Integer> authThread = new SQLField<>("authThread", SQLType.INT, false);
|
||||
public static final SQLField<Integer> authAnchored = new SQLField<>("authAnchored", SQLType.INT, false);
|
||||
public static final SQLField<Integer> authModo = new SQLField<>("authModo", SQLType.INT, false);
|
||||
public static final SQLField<Integer> nbThreads = new SQLField<>("nbThreads", SQLType.INT, false);
|
||||
public static final SQLField<Integer> nbMessages = new SQLField<>("nbMessages", SQLType.INT, false);
|
||||
|
||||
|
||||
|
||||
}
|
38
src/fr/pandacube/java/util/db2/SQLForumPost.java
Normal file
38
src/fr/pandacube/java/util/db2/SQLForumPost.java
Normal file
@ -0,0 +1,38 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLForumPost extends SQLElement {
|
||||
|
||||
|
||||
public SQLForumPost() { super(); }
|
||||
public SQLForumPost(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_forum_post"; }
|
||||
|
||||
|
||||
public static final SQLField<String> createur = new SQLField<>("createur", SQLType.CHAR(36), false);
|
||||
public static final SQLField<String> texte = new SQLField<>("texte", SQLType.TEXT, false);
|
||||
public static final SQLField<Integer> time = new SQLField<>("time", SQLType.INT, false);
|
||||
public static final SQLField<Integer> threadId = new SQLField<>("threadId", SQLType.INT, false);
|
||||
public static final SQLField<Boolean> moderated = new SQLField<>("moderated", SQLType.BOOLEAN, false);
|
||||
|
||||
|
||||
|
||||
public UUID getCreateurId() {
|
||||
String id = (String)get(createur);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setCreateurId(UUID pName) {
|
||||
set(createur, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
|
||||
|
||||
}
|
43
src/fr/pandacube/java/util/db2/SQLForumThread.java
Normal file
43
src/fr/pandacube/java/util/db2/SQLForumThread.java
Normal file
@ -0,0 +1,43 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLForumThread extends SQLElement {
|
||||
|
||||
|
||||
public SQLForumThread() { super(); }
|
||||
public SQLForumThread(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_forum_thread"; }
|
||||
|
||||
|
||||
public static final SQLField<Integer> forumId = new SQLField<>("forumId", SQLType.INT, false);
|
||||
public static final SQLField<String> titre = new SQLField<>("titre", SQLType.VARCHAR(60), false);
|
||||
public static final SQLField<String> createur = new SQLField<>("createur", SQLType.CHAR(36), false);
|
||||
public static final SQLField<Integer> vu = new SQLField<>("vu", SQLType.INT, false);
|
||||
public static final SQLField<Long> time = new SQLField<>("time", SQLType.BIGINT, false);
|
||||
public static final SQLField<Boolean> anchored = new SQLField<>("anchored", SQLType.BOOLEAN, false);
|
||||
public static final SQLField<Boolean> locked = new SQLField<>("locked", SQLType.BOOLEAN, false);
|
||||
public static final SQLField<Integer> nbMessages = new SQLField<>("nbMessages", SQLType.INT, false);
|
||||
|
||||
|
||||
|
||||
public UUID getCreateurId() {
|
||||
String id = (String)get(createur);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setCreateurId(UUID pName) {
|
||||
set(createur, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
48
src/fr/pandacube/java/util/db2/SQLLoginHistory.java
Normal file
48
src/fr/pandacube/java/util/db2/SQLLoginHistory.java
Normal file
@ -0,0 +1,48 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLLoginHistory extends SQLElement {
|
||||
|
||||
|
||||
public SQLLoginHistory() { super(); }
|
||||
public SQLLoginHistory(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_login_history"; }
|
||||
|
||||
|
||||
public static final SQLField<Long> time = new SQLField<>("time", SQLType.BIGINT, false);
|
||||
public static final SQLField<String> playerId = new SQLField<>("playerId", SQLType.CHAR(36), false);
|
||||
public static final SQLField<String> ip = new SQLField<>("ip", SQLType.VARCHAR(128), true);
|
||||
public static final SQLField<ActionType> actionType = new SQLField<>("actionType", SQLType.ENUM(ActionType.class), false);
|
||||
public static final SQLField<Integer> nbOnline = new SQLField<>("nbOnline", SQLType.INT, false);
|
||||
public static final SQLField<String> playerName = new SQLField<>("playerName", SQLType.VARCHAR(16), true);
|
||||
public static final SQLField<Integer> minecraftVersion = new SQLField<>("minecraftVersion", SQLType.INT, false, 0);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public UUID getPlayerId() {
|
||||
String id = (String)get(playerId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setPlayerId(UUID pName) {
|
||||
set(playerId, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public enum ActionType {
|
||||
LOGIN, LOGOUT
|
||||
}
|
||||
}
|
19
src/fr/pandacube/java/util/db2/SQLMPGroup.java
Normal file
19
src/fr/pandacube/java/util/db2/SQLMPGroup.java
Normal file
@ -0,0 +1,19 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLMPGroup extends SQLElement {
|
||||
|
||||
|
||||
public SQLMPGroup() { super(); }
|
||||
public SQLMPGroup(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_mp_group"; }
|
||||
|
||||
|
||||
public static final SQLField<String> groupName = new SQLField<>("groupName", SQLType.VARCHAR(16), false);
|
||||
|
||||
}
|
37
src/fr/pandacube/java/util/db2/SQLMPGroupUser.java
Normal file
37
src/fr/pandacube/java/util/db2/SQLMPGroupUser.java
Normal file
@ -0,0 +1,37 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLMPGroupUser extends SQLElement {
|
||||
|
||||
|
||||
public SQLMPGroupUser() { super(); }
|
||||
public SQLMPGroupUser(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_mp_group_user"; }
|
||||
|
||||
|
||||
public static final SQLField<Integer> groupId = new SQLField<>("groupId", SQLType.INT, false);
|
||||
public static final SQLField<String> playerId = new SQLField<>("playerId", SQLType.CHAR(36), false);
|
||||
|
||||
// TODO ajouter un champ qui dit si le joueur est admin du groupe
|
||||
|
||||
|
||||
|
||||
public UUID getPlayerId() {
|
||||
String id = get(playerId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setPlayerId(UUID id) {
|
||||
set(playerId, (id == null) ? null : id.toString());
|
||||
}
|
||||
|
||||
|
||||
}
|
56
src/fr/pandacube/java/util/db2/SQLMPMessage.java
Normal file
56
src/fr/pandacube/java/util/db2/SQLMPMessage.java
Normal file
@ -0,0 +1,56 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLMPMessage extends SQLElement {
|
||||
|
||||
|
||||
public SQLMPMessage() { super(); }
|
||||
public SQLMPMessage(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_mp_message"; }
|
||||
|
||||
|
||||
public static final SQLField<Long> time = new SQLField<>("time", SQLType.BIGINT, false);
|
||||
public static final SQLField<Integer> securityKey = new SQLField<>("securityKey", SQLType.INT, false);
|
||||
public static final SQLField<String> viewerId = new SQLField<>("viewerId", SQLType.CHAR(36), false);
|
||||
public static final SQLField<String> sourceId = new SQLField<>("sourceId", SQLType.CHAR(36), true);
|
||||
public static final SQLField<String> destId = new SQLField<>("destId", SQLType.CHAR(36), true);
|
||||
public static final SQLField<Integer> destGroup = new SQLField<>("destGroup", SQLType.INT, true);
|
||||
public static final SQLField<String> message = new SQLField<>("message", SQLType.VARCHAR(512), false);
|
||||
public static final SQLField<Boolean> wasRead = new SQLField<>("wasRead", SQLType.BOOLEAN, false);
|
||||
public static final SQLField<Boolean> deleted = new SQLField<>("deleted", SQLType.BOOLEAN, false);
|
||||
public static final SQLField<Boolean> serverSync = new SQLField<>("serverSync", SQLType.BOOLEAN, false);
|
||||
|
||||
|
||||
public UUID getViewerId() {
|
||||
String id = get(viewerId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
public void setViewerId(UUID id) {
|
||||
set(viewerId, (id == null) ? null : id.toString());
|
||||
}
|
||||
|
||||
|
||||
public UUID getSourceId() {
|
||||
String id = get(sourceId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
public void setSourceId(UUID id) {
|
||||
set(sourceId, (id == null) ? null : id.toString());
|
||||
}
|
||||
|
||||
|
||||
public UUID getDestId() {
|
||||
String id = get(destId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
public void setDestId(UUID id) {
|
||||
set(destId, (id == null) ? null : id.toString());
|
||||
}
|
||||
}
|
59
src/fr/pandacube/java/util/db2/SQLModoHistory.java
Normal file
59
src/fr/pandacube/java/util/db2/SQLModoHistory.java
Normal file
@ -0,0 +1,59 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLModoHistory extends SQLElement {
|
||||
|
||||
|
||||
public SQLModoHistory() { super(); }
|
||||
public SQLModoHistory(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_modo_history"; }
|
||||
|
||||
|
||||
public static final SQLField<String> modoId = new SQLField<>("modoId", SQLType.CHAR(36), true);
|
||||
public static final SQLField<ActionType> actionType = new SQLField<>("actionType", SQLType.ENUM(ActionType.class), false);
|
||||
public static final SQLField<Long> time = new SQLField<>("time", SQLType.BIGINT, false);
|
||||
public static final SQLField<String> playerId = new SQLField<>("playerId", SQLType.CHAR(36), false);
|
||||
public static final SQLField<Long> value = new SQLField<>("value", SQLType.BIGINT, true);
|
||||
public static final SQLField<String> message = new SQLField<>("message", SQLType.VARCHAR(512), false);
|
||||
|
||||
|
||||
|
||||
|
||||
public UUID getModoId() {
|
||||
String id = (String)get(modoId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setModoId(UUID pName) {
|
||||
set(modoId, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public UUID getPlayerId() {
|
||||
String id = (String)get(playerId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setPlayerId(UUID pName) {
|
||||
set(playerId, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public enum ActionType{
|
||||
BAN, UNBAN, MUTE, UNMUTE, REPORT, KICK
|
||||
}
|
||||
|
||||
}
|
71
src/fr/pandacube/java/util/db2/SQLOnlineshopHistory.java
Normal file
71
src/fr/pandacube/java/util/db2/SQLOnlineshopHistory.java
Normal file
@ -0,0 +1,71 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLOnlineshopHistory extends SQLElement {
|
||||
|
||||
|
||||
public SQLOnlineshopHistory() { super(); }
|
||||
public SQLOnlineshopHistory(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_onlineshop_history"; }
|
||||
|
||||
|
||||
public static final SQLField<Long> time = new SQLField<>("time", SQLType.BIGINT, false);
|
||||
public static final SQLField<String> transactionId = new SQLField<>("transactionId", SQLType.VARCHAR(255), true);
|
||||
public static final SQLField<SourceType> sourceType = new SQLField<>("sourceType", SQLType.ENUM(SourceType.class), false);
|
||||
public static final SQLField<String> sourcePlayerId = new SQLField<>("sourcePlayerId", SQLType.CHAR(36), true);
|
||||
public static final SQLField<Double> sourceQuantity = new SQLField<>("sourceQuantity", SQLType.DOUBLE, false);
|
||||
public static final SQLField<String> sourceName = new SQLField<>("sourceName", SQLType.VARCHAR(64), false);
|
||||
public static final SQLField<DestType> destType = new SQLField<>("destType", SQLType.ENUM(DestType.class), false);
|
||||
public static final SQLField<String> destPlayerId = new SQLField<>("destPlayerId", SQLType.CHAR(36), false);
|
||||
public static final SQLField<Double> destQuantity = new SQLField<>("destQuantity", SQLType.DOUBLE, false);
|
||||
public static final SQLField<String> destName = new SQLField<>("destName", SQLType.VARCHAR(64), false);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public UUID getSourcePlayerId() {
|
||||
String id = (String)get(sourcePlayerId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setSourcePlayerId(UUID pName) {
|
||||
set(sourcePlayerId, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public UUID getDestPlayerId() {
|
||||
String id = (String)get(destPlayerId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setDestPlayerId(UUID pName) {
|
||||
set(destPlayerId, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static enum SourceType {
|
||||
REAL_MONEY, BAMBOU
|
||||
}
|
||||
|
||||
public static enum DestType {
|
||||
BAMBOU, GRADE
|
||||
}
|
||||
|
||||
|
||||
}
|
81
src/fr/pandacube/java/util/db2/SQLPlayer.java
Normal file
81
src/fr/pandacube/java/util/db2/SQLPlayer.java
Normal file
@ -0,0 +1,81 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
|
||||
public class SQLPlayer extends SQLElement {
|
||||
|
||||
public SQLPlayer() { super(); }
|
||||
public SQLPlayer(int id) { super(id); }
|
||||
|
||||
|
||||
/*
|
||||
* Nom de la table
|
||||
*/
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_player"; }
|
||||
|
||||
/*
|
||||
* Champs de la table
|
||||
*/
|
||||
public static final SQLField<String> playerId = new SQLField<>("playerId", SQLType.CHAR(36), false);
|
||||
public static final SQLField<String> token = new SQLField<>("token", SQLType.CHAR(36), true);
|
||||
public static final SQLField<String> mailCheck = new SQLField<>("mailCheck", SQLType.VARCHAR(255), true);
|
||||
public static final SQLField<String> password = new SQLField<>("password", SQLType.VARCHAR(255), true);
|
||||
public static final SQLField<String> mail = new SQLField<>("mail", SQLType.VARCHAR(255), true);
|
||||
public static final SQLField<String> playerDisplayName = new SQLField<>("playerDisplayName", SQLType.VARCHAR(255), false);
|
||||
public static final SQLField<Long> firstTimeInGame = new SQLField<>("firstTimeInGame", SQLType.BIGINT, false);
|
||||
public static final SQLField<Long> timeWebRegister = new SQLField<>("timeWebRegister", SQLType.BIGINT, true);
|
||||
public static final SQLField<Long> lastTimeInGame = new SQLField<>("lastTimeInGame", SQLType.BIGINT, true);
|
||||
public static final SQLField<Long> lastWebActivity = new SQLField<>("lastWebActivity", SQLType.BIGINT, false);
|
||||
public static final SQLField<String> onlineInServer = new SQLField<>("onlineInServer", SQLType.VARCHAR(32), true);
|
||||
public static final SQLField<String> skinURL = new SQLField<>("skinURL", SQLType.VARCHAR(255), true);
|
||||
public static final SQLField<Boolean> isVanish = new SQLField<>("isVanish", SQLType.BOOLEAN, false, (Boolean)false);
|
||||
public static final SQLField<Date> birthday = new SQLField<>("birthday", SQLType.DATE, true);
|
||||
public static final SQLField<Integer> lastYearCelebBday = new SQLField<>("lastYearCelebratedBirthday", SQLType.INT, false, 0);
|
||||
public static final SQLField<Long> banTimeout = new SQLField<>("banTimeout", SQLType.BIGINT, true);
|
||||
public static final SQLField<Long> muteTimeout = new SQLField<>("muteTimeout", SQLType.BIGINT, true);
|
||||
public static final SQLField<Boolean> isWhitelisted = new SQLField<>("isWhitelisted", SQLType.BOOLEAN, false, (Boolean)false);
|
||||
public static final SQLField<Long> bambou = new SQLField<>("bambou", SQLType.BIGINT, false, 0L);
|
||||
public static final SQLField<String> grade = new SQLField<>("grade", SQLType.VARCHAR(36), false, "default");
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Getteurs spécifique (encapsulation)
|
||||
*/
|
||||
|
||||
public UUID getPlayerId() {
|
||||
String id = (String)get(playerId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
public UUID getToken() {
|
||||
String id = (String)get(token);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Setteurs spécifique (encapsulation)
|
||||
*/
|
||||
|
||||
|
||||
|
||||
public void setPlayerId(UUID pName) {
|
||||
set(playerId, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
public void setToken(UUID t) {
|
||||
set(token, (t == null) ? (String)null : t.toString());
|
||||
}
|
||||
|
||||
}
|
48
src/fr/pandacube/java/util/db2/SQLPlayerIgnore.java
Normal file
48
src/fr/pandacube/java/util/db2/SQLPlayerIgnore.java
Normal file
@ -0,0 +1,48 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLPlayerIgnore extends SQLElement {
|
||||
|
||||
|
||||
public SQLPlayerIgnore() { super(); }
|
||||
public SQLPlayerIgnore(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_player_ignore"; }
|
||||
|
||||
|
||||
public static final SQLField<String> ignorer = new SQLField<>("ignorer", SQLType.CHAR(36), false);
|
||||
public static final SQLField<String> ignored = new SQLField<>("ignored", SQLType.CHAR(36), false);
|
||||
|
||||
|
||||
|
||||
public UUID getIgnorerId() {
|
||||
String id = (String)get(ignorer);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setIgnorerId(UUID pName) {
|
||||
set(ignorer, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public UUID getIgnoredId() {
|
||||
String id = (String)get(ignored);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setIgnoredId(UUID pName) {
|
||||
set(ignored, (pName == null) ? (String)null : pName.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
23
src/fr/pandacube/java/util/db2/SQLShopStock.java
Normal file
23
src/fr/pandacube/java/util/db2/SQLShopStock.java
Normal file
@ -0,0 +1,23 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLShopStock extends SQLElement {
|
||||
|
||||
|
||||
public SQLShopStock() { super(); }
|
||||
public SQLShopStock(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_shop_stock"; }
|
||||
|
||||
|
||||
public static final SQLField<String> material = new SQLField<>("material", SQLType.VARCHAR(50), false);
|
||||
public static final SQLField<Integer> damage = new SQLField<>("damage", SQLType.INT, false, 0);
|
||||
public static final SQLField<Double> quantity = new SQLField<>("quantity", SQLType.DOUBLE, false);
|
||||
public static final SQLField<String> server = new SQLField<>("server", SQLType.VARCHAR(50), false);
|
||||
|
||||
|
||||
}
|
49
src/fr/pandacube/java/util/db2/SQLStaffTicket.java
Normal file
49
src/fr/pandacube/java/util/db2/SQLStaffTicket.java
Normal file
@ -0,0 +1,49 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLStaffTicket extends SQLElement {
|
||||
|
||||
|
||||
public SQLStaffTicket() { super(); }
|
||||
public SQLStaffTicket(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_staff_ticket"; }
|
||||
|
||||
|
||||
public static final SQLField<String> playerId = new SQLField<>("playerId", SQLType.CHAR(36), false);
|
||||
public static final SQLField<String> message = new SQLField<>("message", SQLType.VARCHAR(1024), false);
|
||||
public static final SQLField<Long> creationTime = new SQLField<>("creationTime", SQLType.BIGINT, false);
|
||||
public static final SQLField<String> staffPlayerId = new SQLField<>("staffPlayerId", SQLType.CHAR(36), true);
|
||||
|
||||
|
||||
|
||||
|
||||
public UUID getPlayerId() {
|
||||
String id = get(playerId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setPlayerId(UUID id) {
|
||||
set(playerId, (id == null) ? null : id.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public UUID getstaffPlayerId() {
|
||||
String id = get(staffPlayerId);
|
||||
return (id == null) ? null : UUID.fromString(id);
|
||||
}
|
||||
|
||||
|
||||
public void setstaffPlayerId(UUID id) {
|
||||
set(staffPlayerId, (id == null) ? null : id.toString());
|
||||
}
|
||||
|
||||
}
|
24
src/fr/pandacube/java/util/db2/SQLStaticPages.java
Normal file
24
src/fr/pandacube/java/util/db2/SQLStaticPages.java
Normal file
@ -0,0 +1,24 @@
|
||||
package fr.pandacube.java.util.db2;
|
||||
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLElement;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLField;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLType;
|
||||
|
||||
public class SQLStaticPages extends SQLElement {
|
||||
|
||||
|
||||
public SQLStaticPages() { super(); }
|
||||
public SQLStaticPages(int id) { super(id); }
|
||||
|
||||
@Override
|
||||
protected String tableName() { return "pandacube_static_pages"; }
|
||||
|
||||
|
||||
public static final SQLField<String> permalink = new SQLField<>("permalink", SQLType.VARCHAR(128), false);
|
||||
public static final SQLField<String> titreHead = new SQLField<>("titreHead", SQLType.VARCHAR(128), false);
|
||||
public static final SQLField<String> titreH2 = new SQLField<>("titreH2", SQLType.VARCHAR(255), false);
|
||||
public static final SQLField<String> texte = new SQLField<>("texte", SQLType.TEXT, false);
|
||||
public static final SQLField<String> permissions = new SQLField<>("permissions", SQLType.VARCHAR(255), true);
|
||||
|
||||
|
||||
}
|
54
src/fr/pandacube/java/util/db2/sql_tools/DBConnection.java
Normal file
54
src/fr/pandacube/java/util/db2/sql_tools/DBConnection.java
Normal file
@ -0,0 +1,54 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class DBConnection {
|
||||
Connection conn;
|
||||
String url;
|
||||
String login;
|
||||
String pass;
|
||||
|
||||
public DBConnection(String host, int port, String dbname, String l, String p) throws ClassNotFoundException, SQLException {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
url = "jdbc:mysql://"+host+":"+port+"/"+dbname;
|
||||
login = l;
|
||||
pass = p;
|
||||
conn = DriverManager.getConnection(url, login, pass);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void reconnectIfNecessary() throws SQLException
|
||||
{
|
||||
try
|
||||
{
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.close();
|
||||
}
|
||||
catch(SQLException e)
|
||||
{
|
||||
close();
|
||||
conn = DriverManager.getConnection(url, login, pass);
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getNativeConnection() throws SQLException
|
||||
{
|
||||
if (!conn.isValid(1))
|
||||
reconnectIfNecessary();
|
||||
return conn;
|
||||
}
|
||||
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (Exception e) { }
|
||||
|
||||
}
|
||||
|
||||
}
|
297
src/fr/pandacube/java/util/db2/sql_tools/ORM.java
Normal file
297
src/fr/pandacube/java/util/db2/sql_tools/ORM.java
Normal file
@ -0,0 +1,297 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import fr.pandacube.java.PandacubeUtil;
|
||||
import fr.pandacube.java.util.db2.SQLPlayer;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLWhereChain.SQLBoolOp;
|
||||
import fr.pandacube.java.util.db2.sql_tools.SQLWhereComp.SQLComparator;
|
||||
import javafx.util.Pair;
|
||||
|
||||
/**
|
||||
* <b>ORM = Object-Relational Mapping</b>
|
||||
* @author Marc Baloup
|
||||
*
|
||||
*/
|
||||
public final class ORM {
|
||||
|
||||
private static List<Class<? extends SQLElement>> tables = new ArrayList<>();
|
||||
|
||||
private static DBConnection connection;
|
||||
|
||||
public static DBConnection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
||||
public synchronized static void init(DBConnection conn) {
|
||||
|
||||
connection = conn;
|
||||
|
||||
/*
|
||||
* Les tables à initialiser
|
||||
*
|
||||
* utile des les initialiser ici, car on peut tout de suite déceler les bugs ou erreurs dans la déclaration des SQLFields
|
||||
*/
|
||||
|
||||
initTable(SQLPlayer.class);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* package */ static <T extends SQLElement> void initTable(Class<T> elemClass) {
|
||||
if (tables.contains(elemClass))
|
||||
return;
|
||||
try {
|
||||
T instance = elemClass.newInstance();
|
||||
String tableName = instance.tableName();
|
||||
if (!tableExist(tableName))
|
||||
createTable(instance);
|
||||
tables.add(elemClass);
|
||||
} catch (Exception e) {
|
||||
PandacubeUtil.getMasterLogger().log(Level.SEVERE, "Can't init table " + elemClass.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private static <T extends SQLElement> void createTable(T elem) throws SQLException {
|
||||
|
||||
String sql = "CREATE TABLE IF NOT EXISTS "+elem.tableName()+" (";
|
||||
List<Object> params = new ArrayList<>();
|
||||
|
||||
Collection<SQLField<?>> tableFields = elem.getFields().values();
|
||||
boolean first = true;
|
||||
for (SQLField<?> f : tableFields) {
|
||||
Pair<String, List<Object>> statementPart = f.forSQLPreparedStatement();
|
||||
params.addAll(statementPart.getValue());
|
||||
|
||||
if (!first) sql += ", ";
|
||||
first = false;
|
||||
sql += statementPart.getKey();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
sql += ", PRIMARY KEY id(id))";
|
||||
PreparedStatement ps = connection.getNativeConnection().prepareStatement(sql);
|
||||
int i = 1;
|
||||
for (Object val : params) {
|
||||
ps.setObject(i++, val);
|
||||
}
|
||||
try {
|
||||
System.out.println(ps.toString());
|
||||
ps.executeUpdate();
|
||||
} finally {
|
||||
ps.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static boolean tableExist(String tableName) throws SQLException {
|
||||
ResultSet set = null;
|
||||
boolean exist = false;
|
||||
try {
|
||||
set = connection.getNativeConnection().getMetaData().getTables(null, null, tableName, null);
|
||||
exist = set.next();
|
||||
} finally {
|
||||
if (set != null)
|
||||
set.close();
|
||||
}
|
||||
return exist;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends SQLElement> SQLField<Integer> getSQLIdField(Class<T> elemClass) {
|
||||
return (SQLField<Integer>) SQLElement.fieldsCache.get(elemClass).get("id");
|
||||
}
|
||||
|
||||
|
||||
public static <T extends SQLElement> List<T> getByIds(Class<T> elemClass, Collection<Integer> ids) throws Exception {
|
||||
return getByIds(elemClass, ids.toArray(new Integer[ids.size()]));
|
||||
}
|
||||
|
||||
public static <T extends SQLElement> List<T> getByIds(Class<T> elemClass, Integer... ids) throws Exception {
|
||||
SQLField<Integer> idField = getSQLIdField(elemClass);
|
||||
SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR);
|
||||
for (Integer id : ids)
|
||||
if (id != null)
|
||||
where.add(new SQLWhereComp(idField, SQLComparator.EQ, id));
|
||||
return getAll(elemClass, where, new SQLOrderBy().addField(idField), 1, null);
|
||||
}
|
||||
|
||||
public static <T extends SQLElement> T getById(Class<T> elemClass, int id) throws Exception {
|
||||
return getFirst(elemClass, new SQLWhereComp(getSQLIdField(elemClass), SQLComparator.EQ, id), null);
|
||||
}
|
||||
|
||||
public static <T extends SQLElement> T getFirst(Class<T> elemClass, SQLWhere where, SQLOrderBy orderBy) throws Exception {
|
||||
SQLElementList<T> elts = getAll(elemClass, where, orderBy, 1, null);
|
||||
return (elts.size() == 0)? null : elts.get(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static <T extends SQLElement> SQLElementList<T> getAll(Class<T> elemClass) throws Exception {
|
||||
return getAll(elemClass, null, null, null, null);
|
||||
}
|
||||
|
||||
public static <T extends SQLElement> SQLElementList<T> getAll(Class<T> elemClass, SQLWhere where, SQLOrderBy orderBy, Integer limit, Integer offset) throws Exception {
|
||||
initTable(elemClass);
|
||||
String sql = "SELECT * FROM "+elemClass.newInstance().tableName();
|
||||
List<Object> params = new ArrayList<>();
|
||||
|
||||
if (where != null) {
|
||||
Pair<String, List<Object>> ret = where.toSQL();
|
||||
sql += " WHERE "+ret.getKey();
|
||||
params.addAll(ret.getValue());
|
||||
}
|
||||
if (orderBy != null)
|
||||
sql += " ORDER BY "+orderBy.toSQL();
|
||||
if (limit != null)
|
||||
sql += " LIMIT "+limit;
|
||||
if (offset != null)
|
||||
sql += " OFFSET "+offset;
|
||||
sql += ";";
|
||||
|
||||
SQLElementList<T> elmts = new SQLElementList<T>();
|
||||
|
||||
PreparedStatement ps = connection.getNativeConnection().prepareStatement(sql);
|
||||
|
||||
try {
|
||||
|
||||
int i = 1;
|
||||
for (Object val : params) {
|
||||
ps.setObject(i++, val);
|
||||
}
|
||||
|
||||
System.out.println(ps.toString());
|
||||
|
||||
ResultSet set = ps.executeQuery();
|
||||
|
||||
try {
|
||||
while (set.next())
|
||||
elmts.add(getElementInstance(set, elemClass));
|
||||
} finally {
|
||||
set.close();
|
||||
}
|
||||
} finally {
|
||||
ps.close();
|
||||
}
|
||||
|
||||
return elmts;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static <T extends SQLElement> T getElementInstance(ResultSet set, Class<T> elemClass) throws Exception {
|
||||
try {
|
||||
T instance = elemClass.getConstructor(int.class).newInstance(set.getInt("id"));
|
||||
|
||||
int fieldCount = set.getMetaData().getColumnCount();
|
||||
|
||||
for (int c = 1; c<= fieldCount; c++) {
|
||||
String fieldName = set.getMetaData().getColumnLabel(c);
|
||||
if (!instance.getFields().containsKey(fieldName))
|
||||
continue; // ignore when field is present in database but not handled by SQLElement instance
|
||||
@SuppressWarnings("unchecked")
|
||||
SQLField<Object> sqlField = (SQLField<Object>) instance.getFields().get(fieldName);
|
||||
instance.set(sqlField, set.getObject(c), false);
|
||||
}
|
||||
|
||||
return instance;
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
||||
throw new Exception("Can't instanciate " + elemClass.getName(), e);
|
||||
} catch (SQLException e) {
|
||||
throw new Exception("Error reading ResultSet for creating instance of " + elemClass.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private ORM() { } // rend la classe non instanciable
|
||||
|
||||
/*
|
||||
public static void main(String[] args) throws Throwable {
|
||||
ORM.init(new DBConnection("localhost", 3306, "pandacube", "pandacube", "pandacube"));
|
||||
|
||||
List<SQLPlayer> players = ORM.getAll(SQLPlayer.class,
|
||||
new SQLWhereChain(SQLBoolOp.AND)
|
||||
.add(new SQLWhereNull(SQLPlayer.banTimeout, true))
|
||||
.add(new SQLWhereChain(SQLBoolOp.OR)
|
||||
.add(new SQLWhereComp(SQLPlayer.bambou, SQLComparator.EQ, 0L))
|
||||
.add(new SQLWhereComp(SQLPlayer.grade, SQLComparator.EQ, "default"))
|
||||
),
|
||||
new SQLOrderBy().addField(SQLPlayer.playerDisplayName), null, null);
|
||||
|
||||
for(SQLPlayer p : players) {
|
||||
System.out.println(p.get(SQLPlayer.playerDisplayName));
|
||||
}
|
||||
|
||||
|
||||
// TODO LIST
|
||||
|
||||
* - Gérer mise à jour relative d'un champ (incrément / décrément)
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
392
src/fr/pandacube/java/util/db2/sql_tools/SQLElement.java
Normal file
392
src/fr/pandacube/java/util/db2/sql_tools/SQLElement.java
Normal file
@ -0,0 +1,392 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import fr.pandacube.java.PandacubeUtil;
|
||||
|
||||
public abstract class SQLElement {
|
||||
/** cache for fields for each subclass of SQLElement */
|
||||
/* package */ static final Map<Class<? extends SQLElement>, SQLFieldMap> fieldsCache = new HashMap<>();
|
||||
|
||||
|
||||
|
||||
|
||||
DBConnection db = ORM.getConnection();
|
||||
|
||||
private boolean stored = false;
|
||||
private int id;
|
||||
|
||||
private final String tableName;
|
||||
private final SQLFieldMap fields;
|
||||
|
||||
private final Map<SQLField<?>, Object> values;
|
||||
private final Set<String> modifiedSinceLastSave;
|
||||
|
||||
|
||||
public SQLElement() {
|
||||
tableName = tableName();
|
||||
|
||||
|
||||
if (fieldsCache.get(getClass()) == null) {
|
||||
fields = new SQLFieldMap(getClass());
|
||||
|
||||
// le champ id commun à toutes les tables
|
||||
fields.addField(new SQLField<>("id", SQLType.INT, false, true, 0));
|
||||
|
||||
generateFields(fields);
|
||||
fieldsCache.put(getClass(), fields);
|
||||
}
|
||||
else {
|
||||
fields = fieldsCache.get(getClass());
|
||||
}
|
||||
|
||||
values = new LinkedHashMap<>(fields.size());
|
||||
modifiedSinceLastSave = new HashSet<>(fields.size());
|
||||
|
||||
initDefaultValues();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected SQLElement(int id) {
|
||||
this();
|
||||
@SuppressWarnings("unchecked")
|
||||
SQLField<Integer> idField = (SQLField<Integer>)fields.get("id");
|
||||
set(idField, id, false);
|
||||
this.id = id;
|
||||
stored = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The name of the table in the database.
|
||||
*/
|
||||
protected abstract String tableName();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void initDefaultValues() {
|
||||
// remplissage des données par défaut (si peut être null ou si valeur par défaut existe)
|
||||
for (@SuppressWarnings("rawtypes") SQLField f : fields.values()) {
|
||||
if (f.defaultValue != null) {
|
||||
set(f, f.defaultValue);
|
||||
} else if (f.canBeNull || (f.autoIncrement && !stored)) {
|
||||
set(f, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void generateFields(SQLFieldMap listToFill) {
|
||||
|
||||
java.lang.reflect.Field[] declaredFields = getClass().getDeclaredFields();
|
||||
for (java.lang.reflect.Field field : declaredFields) {
|
||||
if (!java.lang.reflect.Modifier.isStatic(field.getModifiers()))
|
||||
continue;
|
||||
|
||||
try {
|
||||
Object val = field.get(null);
|
||||
if (val == null || !(val instanceof SQLField))
|
||||
continue;
|
||||
|
||||
listToFill.addField((SQLField<?>)val);
|
||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||
PandacubeUtil.getMasterLogger().log(Level.SEVERE, "Can't get value of static field "+field.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* package */ Map<String, SQLField<?>> getFields() {
|
||||
return Collections.unmodifiableMap(fields);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public Map<SQLField<?>, Object> getValues() {
|
||||
return Collections.unmodifiableMap(values);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public <T> void set(SQLField<T> field, T value) {
|
||||
set(field, value, true);
|
||||
}
|
||||
|
||||
|
||||
/* package */ <T> void set(SQLField<T> sqlField, T value, boolean setModified) {
|
||||
if (sqlField == null)
|
||||
throw new IllegalArgumentException("sqlField can't be null");
|
||||
if (!fields.containsValue(sqlField))
|
||||
throw new IllegalArgumentException(sqlField.name + " is not a SQLField of " + getClass().getName());
|
||||
|
||||
boolean modify = false;
|
||||
if (value == null) {
|
||||
if (sqlField.canBeNull || (sqlField.autoIncrement && !stored))
|
||||
modify = true;
|
||||
else
|
||||
throw new IllegalArgumentException("SQLField '" + sqlField.name + "' of " + getClass().getName() + " is a NOT NULL field");
|
||||
} else {
|
||||
if (sqlField.type.isAssignableFrom(value))
|
||||
modify = true;
|
||||
else
|
||||
throw new IllegalArgumentException("SQLField '" + sqlField.name + "' of " + getClass().getName() + " type is '" + sqlField.type.toString() + "' and can't accept values of type " + value.getClass().getName());
|
||||
}
|
||||
|
||||
if (modify) {
|
||||
if (!values.containsKey(sqlField)) {
|
||||
values.put(sqlField, value);
|
||||
if (setModified)
|
||||
modifiedSinceLastSave.add(sqlField.name);
|
||||
}
|
||||
else {
|
||||
Object oldVal = values.get(sqlField);
|
||||
if (!Objects.equals(oldVal, value)) {
|
||||
values.put(sqlField, value);
|
||||
if (setModified)
|
||||
modifiedSinceLastSave.add(sqlField.name);
|
||||
}
|
||||
// sinon, rien n'est modifié
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public <T> T get(SQLField<T> field) {
|
||||
if (field == null)
|
||||
throw new IllegalArgumentException("field can't be null");
|
||||
if (values.containsKey(field)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
T val = (T) values.get(field);
|
||||
return val;
|
||||
}
|
||||
throw new IllegalArgumentException("The field '" + field.name + "' in this instance of " + getClass().getName() + " does not exist or is not set");
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean isValidForSave() {
|
||||
return values.keySet().containsAll(fields.keySet());
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Map<SQLField<?>, Object> getOnlyModifiedValues() {
|
||||
Map<SQLField<?>, Object> modifiedValues = new LinkedHashMap<>();
|
||||
values.forEach((k, v) -> {
|
||||
if (modifiedSinceLastSave.contains(k))
|
||||
modifiedValues.put(k, v);
|
||||
});
|
||||
return modifiedValues;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean isModified(SQLField<?> field) {
|
||||
return modifiedSinceLastSave.contains(field.name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void save() throws SQLException {
|
||||
if (!isValidForSave())
|
||||
throw new IllegalStateException("this instance of " + getClass().getName() + " has at least one undefined value and can't be saved.");
|
||||
|
||||
ORM.initTable(getClass());
|
||||
|
||||
Connection conn = db.getNativeConnection();
|
||||
|
||||
|
||||
if (stored)
|
||||
{ // mettre à jour les valeurs dans la base
|
||||
|
||||
// restaurer l'ID au cas il aurait été changé à la main dans values
|
||||
@SuppressWarnings("unchecked")
|
||||
SQLField<Integer> idField = (SQLField<Integer>) fields.get("id");
|
||||
values.put(idField, id);
|
||||
modifiedSinceLastSave.remove("id");
|
||||
Map<SQLField<?>, Object> modifiedValues = getOnlyModifiedValues();
|
||||
|
||||
|
||||
|
||||
String sql = "";
|
||||
List<Object> psValues = new ArrayList<>();
|
||||
|
||||
for(Map.Entry<SQLField<?>, Object> entry : modifiedValues.entrySet()) {
|
||||
sql += entry.getKey() + " = ? ,";
|
||||
psValues.add(entry.getValue());
|
||||
}
|
||||
|
||||
if (sql.length() > 0)
|
||||
sql = sql.substring(0, sql.length()-1);
|
||||
|
||||
PreparedStatement ps = conn.prepareStatement("UPDATE "+tableName+" SET "+sql+" WHERE id="+id);
|
||||
|
||||
try {
|
||||
|
||||
int i = 1;
|
||||
for (Object val : psValues) {
|
||||
ps.setObject(i++, val);
|
||||
}
|
||||
|
||||
ps.executeUpdate();
|
||||
} finally {
|
||||
ps.close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // ajouter dans la base
|
||||
|
||||
// restaurer l'ID au cas il aurait été changé à la main dans values
|
||||
values.put(fields.get("id"), null);
|
||||
|
||||
|
||||
String concat_vals = "";
|
||||
String concat_fields = "";
|
||||
List<Object> psValues = new ArrayList<>();
|
||||
|
||||
boolean first = true;
|
||||
for(Map.Entry<SQLField<?>, Object> entry : values.entrySet()) {
|
||||
if (!first) {
|
||||
concat_vals += ",";
|
||||
concat_fields += ",";
|
||||
}
|
||||
first = false;
|
||||
concat_vals += " ? ";
|
||||
concat_fields += entry.getKey();
|
||||
psValues.add(entry.getValue());
|
||||
}
|
||||
|
||||
|
||||
PreparedStatement ps = conn.prepareStatement("INSERT INTO "+tableName+" ("+concat_fields+") VALUES ("+concat_vals+")", Statement.RETURN_GENERATED_KEYS);
|
||||
try {
|
||||
|
||||
int i = 1;
|
||||
for (Object val : psValues) {
|
||||
ps.setObject(i++, val);
|
||||
}
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
ResultSet rs = ps.getGeneratedKeys();
|
||||
try {
|
||||
if(rs.next())
|
||||
{
|
||||
id = rs.getInt(1);
|
||||
}
|
||||
|
||||
stored = true;
|
||||
} finally {
|
||||
rs.close();
|
||||
}
|
||||
} finally {
|
||||
ps.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
modifiedSinceLastSave.clear();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public boolean isStored() { return stored; }
|
||||
|
||||
public Integer getId() {
|
||||
return (stored) ? id : null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public SQLField<Integer> getFieldId() {
|
||||
return (SQLField<Integer>) getFields().get("id");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void delete() {
|
||||
|
||||
try {
|
||||
if (stored)
|
||||
{ // supprimer la ligne de la base
|
||||
PreparedStatement st = db.getNativeConnection().prepareStatement("DELETE FROM "+tableName+" WHERE id="+id);
|
||||
try {
|
||||
st.executeUpdate();
|
||||
markAsNotStored();
|
||||
} finally {
|
||||
st.close();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Méthode appelée quand l'élément courant est retirée de la base de données via une requête externe
|
||||
*/
|
||||
/* package */ void markAsNotStored() {
|
||||
stored = false;
|
||||
id = 0;
|
||||
modifiedSinceLastSave.clear();
|
||||
values.forEach((k, v) -> modifiedSinceLastSave.add(k.name));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected static class SQLFieldMap extends LinkedHashMap<String, SQLField<?>> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Class<? extends SQLElement> sqlElemClass;
|
||||
|
||||
private SQLFieldMap(Class<? extends SQLElement> elemClass) {
|
||||
sqlElemClass = elemClass;
|
||||
}
|
||||
|
||||
private void addField(SQLField<?> f) {
|
||||
if (f == null) return;
|
||||
if (containsKey(f.name))
|
||||
throw new IllegalArgumentException("SQLField "+f.name+" already exist in "+sqlElemClass.getName());
|
||||
f.setSQLElementType(sqlElemClass);
|
||||
put(f.name, f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
165
src/fr/pandacube/java/util/db2/sql_tools/SQLElementList.java
Normal file
165
src/fr/pandacube/java/util/db2/sql_tools/SQLElementList.java
Normal file
@ -0,0 +1,165 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <E>
|
||||
*/
|
||||
public class SQLElementList<E extends SQLElement> extends ArrayList<E> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Map<SQLField<?>, Object> modifiedValues = new LinkedHashMap<>();
|
||||
|
||||
@Override
|
||||
public synchronized boolean add(E e) {
|
||||
if (e == null || !e.isStored()) return false;
|
||||
return super.add(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Défini une valeur à un champ qui sera appliquée dans la base de données à tous les
|
||||
* entrées présente dans cette liste lors de l'appel à {@link #saveCommon()}.
|
||||
* Les valeurs stockés dans chaque élément de cette liste ne seront affectés que lors de
|
||||
* l'appel à {@link #saveCommon()}
|
||||
* @param <T>
|
||||
* @param field le champs à modifier
|
||||
* @param value la valeur à lui appliquer
|
||||
*/
|
||||
public synchronized <T> void setCommon(SQLField<T> field, T value) {
|
||||
if (isEmpty()) throw new IllegalStateException("This SQLElementList is empty");
|
||||
if (field != null && field.name == "id")
|
||||
throw new IllegalArgumentException("Can't modify id field in a SQLElementList");
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<E> elemClass = (Class<E>) get(0).getClass();
|
||||
try {
|
||||
E emptyElement = elemClass.newInstance();
|
||||
emptyElement.set(field, value, false);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Illegal field or value or can't instanciante an empty instance of " + elemClass.getName() + ". (the instance is only created to test validity of field and value)", e);
|
||||
}
|
||||
|
||||
// ici, la valeur est bonne
|
||||
modifiedValues.put(field, value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Applique toutes les valeurs défini avec {@link #setCommon(SQLField, Object)} à toutes
|
||||
* les entrées dans la base de données correspondants aux entrées de cette liste. Les nouvelles
|
||||
* valeurs sont aussi mises à jour dans les objets contenus dans cette liste, si la valeur n'a pas été modifiée individuellement avec {@link SQLElement#set(SQLField, Object)}.<br/>
|
||||
* Les objets de cette liste qui n'ont pas leur données en base de données sont ignorées.
|
||||
* @throws SQLException
|
||||
*/
|
||||
public synchronized void saveCommon() throws SQLException {
|
||||
List<E> storedEl = getStoredEl();
|
||||
if (storedEl.isEmpty()) return;
|
||||
|
||||
String sqlSet = "";
|
||||
List<Object> psValues = new ArrayList<>();
|
||||
|
||||
for(Map.Entry<SQLField<?>, Object> entry : modifiedValues.entrySet()) {
|
||||
sqlSet += entry.getKey().name + " = ? ,";
|
||||
psValues.add(entry.getValue());
|
||||
}
|
||||
|
||||
if (sqlSet.length() > 0)
|
||||
sqlSet = sqlSet.substring(0, sqlSet.length()-1);
|
||||
|
||||
String sqlWhere = "";
|
||||
boolean first = true;
|
||||
for (E el : storedEl) {
|
||||
if (!first) sqlWhere += " OR ";
|
||||
first = false;
|
||||
sqlWhere += "id = "+el.getId();
|
||||
}
|
||||
|
||||
PreparedStatement ps = ORM.getConnection().getNativeConnection().prepareStatement("UPDATE "+storedEl.get(0).tableName()+" SET "+sqlSet+" WHERE "+sqlWhere);
|
||||
try {
|
||||
|
||||
int i = 1;
|
||||
for (Object val : psValues) {
|
||||
ps.setObject(i++, val);
|
||||
}
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
applyNewValuesToElements(storedEl);
|
||||
} finally {
|
||||
ps.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void applyNewValuesToElements(List<E> storedEl) {
|
||||
// applique les valeurs dans chaques objets de la liste
|
||||
for (E el : storedEl) {
|
||||
for (@SuppressWarnings("rawtypes") SQLField entry : modifiedValues.keySet()) {
|
||||
if (!el.isModified(entry))
|
||||
el.set(entry, modifiedValues.get(entry), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private List<E> getStoredEl() {
|
||||
List<E> listStored = new ArrayList<>();
|
||||
forEach(el -> {
|
||||
if (el.isStored()) listStored.add(el);
|
||||
});
|
||||
return listStored;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public synchronized void removeFromDB() {
|
||||
List<E> storedEl = getStoredEl();
|
||||
if (storedEl.isEmpty()) return;
|
||||
|
||||
try {
|
||||
|
||||
String sqlWhere = "";
|
||||
boolean first = true;
|
||||
for (E el : storedEl) {
|
||||
if (!first) sqlWhere += " OR ";
|
||||
first = false;
|
||||
sqlWhere += "id = "+el.getId();
|
||||
}
|
||||
|
||||
PreparedStatement st = ORM.getConnection().getNativeConnection().prepareStatement("DELETE FROM "+storedEl.get(0).tableName()+" WHERE "+sqlWhere);
|
||||
try {
|
||||
st.executeUpdate();
|
||||
|
||||
for (E el : storedEl) {
|
||||
el.markAsNotStored();
|
||||
}
|
||||
|
||||
} finally {
|
||||
st.close();
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
86
src/fr/pandacube/java/util/db2/sql_tools/SQLField.java
Normal file
86
src/fr/pandacube/java/util/db2/sql_tools/SQLField.java
Normal file
@ -0,0 +1,86 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
public class SQLField<T> {
|
||||
|
||||
private Class<? extends SQLElement> sqlElemClass;
|
||||
public final String name;
|
||||
public final SQLType<T> type;
|
||||
public final boolean canBeNull;
|
||||
public final boolean autoIncrement;
|
||||
/* package */ final T defaultValue;
|
||||
|
||||
public SQLField(String n, SQLType<T> t, boolean nul, boolean autoIncr, T deflt) {
|
||||
name = n;
|
||||
type = t;
|
||||
canBeNull = nul;
|
||||
autoIncrement = autoIncr;
|
||||
defaultValue = deflt;
|
||||
}
|
||||
|
||||
public SQLField(String n, SQLType<T> t, boolean nul) {
|
||||
this(n, t, nul, false, null);
|
||||
}
|
||||
|
||||
public SQLField(String n, SQLType<T> t, boolean nul, boolean autoIncr) {
|
||||
this(n, t, nul, autoIncr, null);
|
||||
}
|
||||
|
||||
public SQLField(String n, SQLType<T> t, boolean nul, T deflt) {
|
||||
this(n, t, nul, false, deflt);
|
||||
}
|
||||
|
||||
/* package */ Pair<String, List<Object>> forSQLPreparedStatement() {
|
||||
List<Object> params = new ArrayList<>(1);
|
||||
if (defaultValue != null && !autoIncrement)
|
||||
params.add(defaultValue);
|
||||
return new Pair<>(name
|
||||
+ " "+ type.toString()
|
||||
+ (canBeNull ? " NULL" : " NOT NULL")
|
||||
+ (autoIncrement ? " AUTO_INCREMENT" : "")
|
||||
+ ((defaultValue == null || autoIncrement) ? "" : " DEFAULT ?"),
|
||||
params);
|
||||
}
|
||||
|
||||
/* package */ void setSQLElementType(Class<? extends SQLElement> elemClass) {
|
||||
sqlElemClass = elemClass;
|
||||
}
|
||||
|
||||
public Class<? extends SQLElement> getSQLElementType() {
|
||||
return sqlElemClass;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <b>Don't use this {@link #toString()} method in a SQL query, because
|
||||
* the default value is not escaped correctly</b>
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return forSQLPreparedStatement().getKey().replaceFirst("\\?", (defaultValue != null && !autoIncrement) ? defaultValue.toString() : "");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) return false;
|
||||
if (!(obj instanceof SQLField)) return false;
|
||||
SQLField<?> f = (SQLField<?>) obj;
|
||||
if (!f.name.equals(name)) return false;
|
||||
if (!f.sqlElemClass.equals(sqlElemClass)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode() + sqlElemClass.hashCode();
|
||||
}
|
||||
|
||||
|
||||
}
|
54
src/fr/pandacube/java/util/db2/sql_tools/SQLOrderBy.java
Normal file
54
src/fr/pandacube/java/util/db2/sql_tools/SQLOrderBy.java
Normal file
@ -0,0 +1,54 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SQLOrderBy {
|
||||
|
||||
private List<OBField> orderByFields = new ArrayList<>();
|
||||
|
||||
public SQLOrderBy() {}
|
||||
|
||||
public SQLOrderBy addField(SQLField<?> field,Direction d) {
|
||||
orderByFields.add(new OBField(field, d));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SQLOrderBy addField(SQLField<?> field) {
|
||||
return addField(field, Direction.ASC);
|
||||
}
|
||||
|
||||
|
||||
/* package */ String toSQL() {
|
||||
String ret = "";
|
||||
boolean first = true;
|
||||
for (OBField f : orderByFields) {
|
||||
if (!first) ret += ", ";
|
||||
first = false;
|
||||
ret += f.field.name + " " + f.direction.name();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toSQL();
|
||||
}
|
||||
|
||||
|
||||
private class OBField {
|
||||
public final SQLField<?> field;
|
||||
public final Direction direction;
|
||||
|
||||
public OBField(SQLField<?> f, Direction d) {
|
||||
field = f;
|
||||
direction = d;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private enum Direction {
|
||||
ASC, DESC;
|
||||
}
|
||||
}
|
83
src/fr/pandacube/java/util/db2/sql_tools/SQLType.java
Normal file
83
src/fr/pandacube/java/util/db2/sql_tools/SQLType.java
Normal file
@ -0,0 +1,83 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
public class SQLType<T> {
|
||||
|
||||
private final String sqlType;
|
||||
private final String sqlTypeParam;
|
||||
private final Class<?> javaTypes;
|
||||
|
||||
public SQLType(String sqlT, String sqlP, Class<T> javaT) {
|
||||
sqlType = sqlT;
|
||||
sqlTypeParam = sqlP;
|
||||
javaTypes = javaT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return sqlType + sqlTypeParam;
|
||||
}
|
||||
|
||||
public boolean isAssignableFrom(Object val) {
|
||||
if (javaTypes.isInstance(val))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static final SQLType<Boolean> BOOLEAN = new SQLType<>("BOOLEAN", "", Boolean.class);
|
||||
|
||||
public static final SQLType<Byte> TINYINT = new SQLType<>("TINYINT", "", Byte.class);
|
||||
public static final SQLType<Byte> BYTE = TINYINT;
|
||||
|
||||
public static final SQLType<Short> SMALLINT = new SQLType<>("SMALLINT", "", Short.class);
|
||||
public static final SQLType<Short> SHORT = SMALLINT;
|
||||
|
||||
public static final SQLType<Integer> INT = new SQLType<>("INT", "", Integer.class);
|
||||
public static final SQLType<Integer> INTEGER = INT;
|
||||
|
||||
public static final SQLType<Long> BIGINT = new SQLType<>("BIGINT", "", Long.class);
|
||||
public static final SQLType<Long> LONG = BIGINT;
|
||||
|
||||
public static final SQLType<Date> DATE = new SQLType<>("DATE", "", Date.class);
|
||||
|
||||
public static final SQLType<Float> FLOAT = new SQLType<>("FLOAT", "", Float.class);
|
||||
|
||||
public static final SQLType<Double> DOUBLE = new SQLType<>("DOUBLE", "", Double.class);
|
||||
|
||||
public static final SQLType<String> CHAR(int charCount) {
|
||||
if (charCount <= 0) throw new IllegalArgumentException("charCount must be positive.");
|
||||
return new SQLType<>("CHAR", "("+charCount+")", String.class);
|
||||
}
|
||||
|
||||
public static final SQLType<String> VARCHAR(int charCount) {
|
||||
if (charCount <= 0) throw new IllegalArgumentException("charCount must be positive.");
|
||||
return new SQLType<>("VARCHAR", "("+charCount+")", String.class);
|
||||
}
|
||||
|
||||
public static final SQLType<String> TEXT = new SQLType<>("TEXT", "", String.class);
|
||||
public static final SQLType<String> STRING = TEXT;
|
||||
|
||||
public static final <T extends Enum<T>> SQLType<T> ENUM(Class<T> enumType) {
|
||||
if (enumType == null) throw new IllegalArgumentException("enumType can't be null.");
|
||||
String enumStr = "'";
|
||||
boolean first = true;
|
||||
for (T el : enumType.getEnumConstants()) {
|
||||
if (!first)
|
||||
enumStr += "', '";
|
||||
first = false;
|
||||
enumStr += el.name();
|
||||
|
||||
}
|
||||
enumStr += "'";
|
||||
|
||||
return new SQLType<>("VARCHAR", "("+enumStr+")", enumType);
|
||||
}
|
||||
|
||||
|
||||
}
|
21
src/fr/pandacube/java/util/db2/sql_tools/SQLWhere.java
Normal file
21
src/fr/pandacube/java/util/db2/sql_tools/SQLWhere.java
Normal file
@ -0,0 +1,21 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
public abstract class SQLWhere {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public abstract Pair<String, List<Object>> toSQL();
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toSQL().getKey();
|
||||
}
|
||||
|
||||
}
|
60
src/fr/pandacube/java/util/db2/sql_tools/SQLWhereChain.java
Normal file
60
src/fr/pandacube/java/util/db2/sql_tools/SQLWhereChain.java
Normal file
@ -0,0 +1,60 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
public class SQLWhereChain extends SQLWhere {
|
||||
|
||||
private SQLBoolOp operator;
|
||||
private List<SQLWhere> conditions = new ArrayList<>();
|
||||
|
||||
public SQLWhereChain(SQLBoolOp op) {
|
||||
if (op == null)
|
||||
throw new IllegalArgumentException("op can't be null");
|
||||
operator = op;
|
||||
}
|
||||
|
||||
|
||||
public SQLWhereChain add(SQLWhere sqlWhere) {
|
||||
if (sqlWhere == null)
|
||||
throw new IllegalArgumentException("sqlWhere can't be null");
|
||||
conditions.add(sqlWhere);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Pair<String, List<Object>> toSQL() {
|
||||
String sql = "";
|
||||
List<Object> params = new ArrayList<>();
|
||||
boolean first = true;
|
||||
|
||||
for (SQLWhere w : conditions) {
|
||||
if (!first) sql += " " + operator.sql + " ";
|
||||
first = false;
|
||||
|
||||
Pair<String, List<Object>> ret = w.toSQL();
|
||||
sql += "(" + ret.getKey() + ")";
|
||||
params.addAll(ret.getValue());
|
||||
}
|
||||
|
||||
return new Pair<>(sql, params);
|
||||
}
|
||||
|
||||
|
||||
enum SQLBoolOp {
|
||||
/** Equivalent to SQL "<code>AND</code>" */
|
||||
AND("AND"),
|
||||
/** Equivalent to SQL "<code>OR</code>" */
|
||||
OR("OR");
|
||||
public final String sql;
|
||||
|
||||
private SQLBoolOp(String s) {
|
||||
sql = s;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
62
src/fr/pandacube/java/util/db2/sql_tools/SQLWhereComp.java
Normal file
62
src/fr/pandacube/java/util/db2/sql_tools/SQLWhereComp.java
Normal file
@ -0,0 +1,62 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
public class SQLWhereComp extends SQLWhere {
|
||||
|
||||
private SQLField<?> left;
|
||||
private SQLComparator comp;
|
||||
private Object right;
|
||||
|
||||
/**
|
||||
* Compare a field with a value
|
||||
* @param l the field at left of the comparison operator. Can't be null
|
||||
* @param c the comparison operator, can't be null
|
||||
* @param r the value at right of the comparison operator. Can't be null
|
||||
*/
|
||||
public <T> SQLWhereComp(SQLField<T> l, SQLComparator c, T r) {
|
||||
if (l == null || r == null || c == null)
|
||||
throw new IllegalArgumentException("All arguments for SQLWhereComp constructor can't be null");
|
||||
left = l;
|
||||
comp = c;
|
||||
right = r;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Pair<String, List<Object>> toSQL() {
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(right);
|
||||
return new Pair<>(left.name + " " + comp.sql + " ? ", params);
|
||||
}
|
||||
|
||||
|
||||
enum SQLComparator {
|
||||
/** Equivalent to SQL "<code>=</code>" */
|
||||
EQ("="),
|
||||
/** Equivalent to SQL "<code>></code>" */
|
||||
GT(">"),
|
||||
/** Equivalent to SQL "<code>>=</code>" */
|
||||
GEQ(">="),
|
||||
/** Equivalent to SQL "<code><</code>" */
|
||||
LT("<"),
|
||||
/** Equivalent to SQL "<code><=</code>" */
|
||||
LEQ("<="),
|
||||
/** Equivalent to SQL "<code>!=</code>" */
|
||||
NEQ("!=");
|
||||
|
||||
public final String sql;
|
||||
|
||||
private SQLComparator(String s) {
|
||||
sql = s;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
35
src/fr/pandacube/java/util/db2/sql_tools/SQLWhereLike.java
Normal file
35
src/fr/pandacube/java/util/db2/sql_tools/SQLWhereLike.java
Normal file
@ -0,0 +1,35 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
public class SQLWhereLike extends SQLWhere {
|
||||
|
||||
|
||||
private SQLField<String> field;
|
||||
private String likeExpr;
|
||||
|
||||
/**
|
||||
* Compare a field with a value
|
||||
* @param l the field at left of the comparison operator. Can't be null
|
||||
* @param c the comparison operator, can't be null
|
||||
* @param r the value at right of the comparison operator. Can't be null
|
||||
*/
|
||||
public SQLWhereLike(SQLField<String> f, String like) {
|
||||
if (f == null || like == null)
|
||||
throw new IllegalArgumentException("All arguments for SQLWhereLike constructor can't be null");
|
||||
field = f;
|
||||
likeExpr = like;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Pair<String, List<Object>> toSQL() {
|
||||
ArrayList<Object> params = new ArrayList<>();
|
||||
params.add(likeExpr);
|
||||
return new Pair<>(field.name + " LIKE ? ", params);
|
||||
}
|
||||
|
||||
}
|
37
src/fr/pandacube/java/util/db2/sql_tools/SQLWhereNull.java
Normal file
37
src/fr/pandacube/java/util/db2/sql_tools/SQLWhereNull.java
Normal file
@ -0,0 +1,37 @@
|
||||
package fr.pandacube.java.util.db2.sql_tools;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import fr.pandacube.java.PandacubeUtil;
|
||||
import javafx.util.Pair;
|
||||
|
||||
public class SQLWhereNull extends SQLWhere {
|
||||
|
||||
private SQLField<?> fild;
|
||||
private boolean nulll;
|
||||
|
||||
/**
|
||||
* Init a IS NULL / IS NOT NULL expression for a SQL WHERE condition.
|
||||
* @param field the field to check null / not null state
|
||||
* @param isNull true if we want to ckeck if "IS NULL", or false to check if "IS NOT NULL"
|
||||
*/
|
||||
public SQLWhereNull(SQLField<?> field, boolean isNull) {
|
||||
if (field == null)
|
||||
throw new IllegalArgumentException("field can't be null");
|
||||
if (!field.canBeNull)
|
||||
PandacubeUtil.getMasterLogger().log(Level.WARNING, "Useless : Trying to check IS [NOT] NULL on the field "+field.getSQLElementType().getName()+"#"+field.name+" which is declared in the ORM as 'can't be null'");
|
||||
fild = field;
|
||||
nulll = isNull;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Pair<String, List<Object>> toSQL() {
|
||||
return new Pair<>(fild.name + " IS" + ((nulll)?" NULL":" NOT NULL"), new ArrayList<>());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user