├── lib └── mysql-connector-java-8.0.21.jar ├── .gitattributes ├── src ├── main │ └── java │ │ └── com │ │ └── code │ │ └── advancedsql │ │ ├── query │ │ ├── action │ │ │ ├── Add.java │ │ │ ├── Regular.java │ │ │ ├── Modify.java │ │ │ ├── IAction.java │ │ │ ├── Drop.java │ │ │ ├── Action.java │ │ │ └── IActionColumns.java │ │ ├── Drop.java │ │ ├── join │ │ │ ├── FullJoin.java │ │ │ ├── LeftJoin.java │ │ │ ├── InnerJoin.java │ │ │ ├── RightJoin.java │ │ │ ├── Join.java │ │ │ └── IJoin.java │ │ ├── Truncate.java │ │ ├── Delete.java │ │ ├── column │ │ │ ├── IColumn.java │ │ │ ├── Base.java │ │ │ ├── LongBinary.java │ │ │ ├── Integer.java │ │ │ ├── BigInteger.java │ │ │ ├── TinyInteger.java │ │ │ ├── SmallInteger.java │ │ │ ├── Date.java │ │ │ ├── MediumInteger.java │ │ │ ├── Text.java │ │ │ ├── Boolean.java │ │ │ ├── DateTime.java │ │ │ ├── LongText.java │ │ │ ├── Timestamp.java │ │ │ ├── MediumText.java │ │ │ ├── Binary.java │ │ │ ├── Float.java │ │ │ ├── Double.java │ │ │ ├── Decimal.java │ │ │ ├── Column.java │ │ │ ├── String.java │ │ │ ├── Enum.java │ │ │ └── Numeric.java │ │ ├── ExecuteStatement.java │ │ ├── Specifiable.java │ │ ├── ISpecifiable.java │ │ ├── Insert.java │ │ ├── Update.java │ │ ├── IQuery.java │ │ ├── Query.java │ │ ├── ExecuteUpdate.java │ │ ├── Alter.java │ │ ├── ExecuteQuery.java │ │ ├── Select.java │ │ └── Create.java │ │ ├── SQL.java │ │ ├── table │ │ ├── Table.java │ │ └── ITable.java │ │ ├── ISQL.java │ │ └── MySQL.java └── test │ └── java │ └── com │ └── code │ └── advancedsql │ └── MySQLTest.java ├── pom.xml ├── .gitignore ├── LICENSE └── README.md /lib/mysql-connector-java-8.0.21.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DenzelCode/AdvancedSQL/HEAD/lib/mysql-connector-java-8.0.21.jar -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # 2 | # https://help.github.com/articles/dealing-with-line-endings/ 3 | # 4 | # These are explicitly windows files and should use crlf 5 | *.bat text eol=crlf 6 | 7 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/action/Add.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.action; 2 | 3 | public class Add extends Action { 4 | 5 | @Override 6 | public String getPrefix() { 7 | return "ADD "; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/action/Regular.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.action; 2 | 3 | public class Regular extends Action { 4 | 5 | @Override 6 | public String getPrefix() { 7 | return ""; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/action/Modify.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.action; 2 | 3 | public class Modify extends Action { 4 | 5 | @Override 6 | public String getPrefix() { 7 | return "MODIFY "; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/action/IAction.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.action; 2 | 3 | import java.util.List; 4 | 5 | public interface IAction { 6 | /** 7 | * @return Action columns. 8 | */ 9 | List getColumns(); 10 | 11 | /** 12 | * @return Action prefix. 13 | */ 14 | java.lang.String getPrefix(); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/Drop.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.table.ITable; 4 | 5 | public class Drop extends ExecuteStatement { 6 | 7 | public Drop(ITable table) { 8 | super(table); 9 | } 10 | 11 | @Override 12 | public String toQuery() { 13 | return "DROP TABLE " + this.table; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/join/FullJoin.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.join; 2 | 3 | import com.code.advancedsql.query.Select; 4 | 5 | public class FullJoin extends Join { 6 | 7 | public FullJoin(Select query, String table) { 8 | super(query, table); 9 | } 10 | 11 | @Override 12 | public String getPrefix() { 13 | return "FULL JOIN"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/join/LeftJoin.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.join; 2 | 3 | import com.code.advancedsql.query.Select; 4 | 5 | public class LeftJoin extends Join { 6 | 7 | public LeftJoin(Select query, String table) { 8 | super(query, table); 9 | } 10 | 11 | @Override 12 | public String getPrefix() { 13 | return "LEFT JOIN"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/join/InnerJoin.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.join; 2 | 3 | import com.code.advancedsql.query.Select; 4 | 5 | public class InnerJoin extends Join { 6 | 7 | public InnerJoin(Select query, String table) { 8 | super(query, table); 9 | } 10 | 11 | @Override 12 | public String getPrefix() { 13 | return "INNER JOIN"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/join/RightJoin.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.join; 2 | 3 | import com.code.advancedsql.query.Select; 4 | 5 | public class RightJoin extends Join { 6 | 7 | public RightJoin(Select query, String table) { 8 | super(query, table); 9 | } 10 | 11 | @Override 12 | public String getPrefix() { 13 | return "RIGHT JOIN"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/Truncate.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.table.ITable; 4 | 5 | public class Truncate extends ExecuteStatement { 6 | 7 | public Truncate(ITable table) { 8 | super(table); 9 | } 10 | 11 | @Override 12 | public String toQuery() { 13 | return "TRUNCATE TABLE " + this.table; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/Delete.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.table.ITable; 4 | 5 | public class Delete extends ExecuteUpdate { 6 | 7 | public Delete(ITable table) { 8 | super(table); 9 | } 10 | 11 | @Override 12 | public String toQuery() { 13 | return "DELETE FROM " + this.table + (this.where != null ? " WHERE " + this.where : "") + 14 | (this.limit > 0 ? " LIMIT " + this.limit : "").trim(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/action/Drop.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.action; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Drop implements IAction { 7 | 8 | private List columns = new ArrayList<>(); 9 | 10 | @Override 11 | public List getColumns() { 12 | return this.columns; 13 | } 14 | 15 | public Drop column(String name) { 16 | this.columns.add(name); 17 | 18 | return this; 19 | } 20 | 21 | @Override 22 | public String getPrefix() { 23 | return "DROP "; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/IColumn.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | public interface IColumn { 4 | 5 | /** 6 | * @return Column name. 7 | */ 8 | java.lang.String setName(); 9 | 10 | /** 11 | * Allow null values on a column. 12 | * @return Column object. 13 | */ 14 | IColumn nullable(); 15 | 16 | /** 17 | * Allow SQL code on default. 18 | * @return Column object. 19 | */ 20 | IColumn nativeDefault(); 21 | 22 | /** 23 | * @return String representation of the column. 24 | */ 25 | java.lang.String toString(); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Base.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class Base extends Column { 6 | 7 | protected java.lang.Integer length; 8 | 9 | protected java.lang.String content; 10 | 11 | public Base(IAction action, java.lang.String name, java.lang.String content) { 12 | super(action, name, true); 13 | 14 | this.name = name; 15 | 16 | this.content = content; 17 | } 18 | 19 | @Override 20 | public java.lang.String toString() { 21 | return this.action.getPrefix() + name + " " + content; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/ExecuteStatement.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.table.ITable; 4 | 5 | import java.sql.PreparedStatement; 6 | import java.sql.SQLException; 7 | 8 | public abstract class ExecuteStatement extends Query { 9 | 10 | public ExecuteStatement(ITable table) { 11 | super(table); 12 | } 13 | 14 | @Override 15 | public PreparedStatement executePrepare() throws SQLException { 16 | this.execute(); 17 | 18 | return prepare; 19 | } 20 | 21 | /** 22 | * Execute query. 23 | * @return A boolean. 24 | * @throws SQLException Exception when something goes wrong. 25 | */ 26 | public Boolean execute() throws SQLException { 27 | return this.executeStatement(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/LongBinary.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | import java.io.InputStream; 6 | 7 | public class LongBinary extends Column { 8 | 9 | protected InputStream defaultValue; 10 | 11 | public LongBinary(IAction action, java.lang.String name) { 12 | super(action, name, false); 13 | } 14 | 15 | public LongBinary defaultValue(InputStream value) { 16 | this.defaultValue = value; 17 | 18 | return this; 19 | } 20 | 21 | @Override 22 | public java.lang.String toString() { 23 | return this.action.getPrefix() + name + " LONGBLOB" + (nullable ? " " : " NOT NULL ") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/Specifiable.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.table.ITable; 4 | 5 | import java.util.Arrays; 6 | 7 | public abstract class Specifiable extends Query implements ISpecifiable { 8 | 9 | 10 | public Specifiable(ITable table) { 11 | super(table); 12 | } 13 | 14 | public T limit(int limit) { 15 | this.limit = limit; 16 | 17 | return (T)this; 18 | } 19 | 20 | public int getLimit() { 21 | return this.limit; 22 | } 23 | 24 | @Override 25 | public T where(String where) { 26 | this.where = where; 27 | 28 | return (T)this; 29 | } 30 | 31 | public T where(String where, Object... execute) { 32 | this.execute.addAll(Arrays.asList(execute)); 33 | 34 | return this.where(where); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Integer.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class Integer extends Numeric { 6 | 7 | public Integer(IAction action, java.lang.String name) { 8 | super(action, name, false, false, 11); 9 | } 10 | 11 | public Integer(IAction action, java.lang.String name, java.lang.Integer length) { 12 | super(action, name, false, false, length); 13 | } 14 | 15 | @Override 16 | public java.lang.String toString() { 17 | return this.action.getPrefix() + name + " INT" + (length > 0 ? "(" + length + ")" : "") + (nullable ? " NULL" : " NOT NULL") + (primary ? " PRIMARY KEY" : "") + (increment ? " AUTO_INCREMENT" : "") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/BigInteger.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class BigInteger extends Numeric { 6 | 7 | public BigInteger(IAction action, java.lang.String name) { 8 | super(action, name, false, false, 0); 9 | } 10 | 11 | public BigInteger(IAction action, java.lang.String name, java.lang.Integer length) { 12 | super(action, name, false, false, length); 13 | } 14 | 15 | @Override 16 | public java.lang.String toString() { 17 | return this.action.getPrefix() + name + " BIGINT" + (length > 0 ? "(" + length + ")" : "") + (nullable ? " NULL" : " NOT NULL") + (primary ? " PRIMARY KEY" : "") + (increment ? " AUTO_INCREMENT" : "") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/TinyInteger.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class TinyInteger extends Numeric { 6 | 7 | public TinyInteger(IAction action, java.lang.String name) { 8 | super(action, name, false, false, 0); 9 | } 10 | 11 | public TinyInteger(IAction action, java.lang.String name, java.lang.Integer length) { 12 | super(action, name, false, false, length); 13 | } 14 | 15 | @Override 16 | public java.lang.String toString() { 17 | return this.action.getPrefix() + name + " TINYINT" + (nullable ? " NULL" : " NOT NULL") + (length > 0 ? "(" + length + ")" : "") + (primary ? " PRIMARY KEY" : "") + (increment ? " AUTO_INCREMENT" : "") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/SmallInteger.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class SmallInteger extends Numeric { 6 | 7 | public SmallInteger(IAction action, java.lang.String name) { 8 | super(action, name, false, false, 0); 9 | } 10 | 11 | public SmallInteger(IAction action, java.lang.String name, java.lang.Integer length) { 12 | super(action, name, false, false, length); 13 | } 14 | 15 | @Override 16 | public java.lang.String toString() { 17 | return this.action.getPrefix() + name + " SMALLINT" + (nullable ? " NULL" : " NOT NULL") + (length > 0 ? "(" + length + ")" : "") + (primary ? " PRIMARY KEY" : "") + (increment ? " AUTO_INCREMENT" : "") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Date.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class Date extends Column { 6 | 7 | protected java.lang.String defaultValue; 8 | 9 | public Date(IAction action, java.lang.String name) { 10 | super(action, name, false); 11 | } 12 | 13 | /** 14 | * Set default value. 15 | * @param value Default value. 16 | * @return Column object. 17 | */ 18 | public Date defaultValue(java.lang.String value) { 19 | this.defaultValue = value; 20 | 21 | return this; 22 | } 23 | 24 | @Override 25 | public java.lang.String toString() { 26 | return this.action.getPrefix() + name + " DATE" + (nullable ? " " : " NOT NULL ") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/MediumInteger.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class MediumInteger extends Numeric { 6 | 7 | public MediumInteger(IAction action, java.lang.String name) { 8 | super(action, name, false, false, 0); 9 | } 10 | 11 | public MediumInteger(IAction action, java.lang.String name, java.lang.Integer length) { 12 | super(action, name, false, false, length); 13 | } 14 | 15 | @Override 16 | public java.lang.String toString() { 17 | return this.action.getPrefix() + name + " MEDIUMINT" + (nullable ? " NULL" : " NOT NULL") + (length > 0 ? "(" + length + ")" : "") + (primary ? " PRIMARY KEY" : "") + (increment ? " AUTO_INCREMENT" : "") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Text.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class Text extends Column { 6 | 7 | protected java.lang.String defaultValue; 8 | 9 | public Text(IAction action, java.lang.String name) { 10 | super(action, name, false); 11 | } 12 | 13 | /** 14 | * Set default value. 15 | * @param value Default value. 16 | * @return Column object. 17 | */ 18 | public Text defaultValue(java.lang.String value) { 19 | this.defaultValue = value; 20 | 21 | return this; 22 | } 23 | 24 | @Override 25 | public java.lang.String toString() { 26 | return this.action.getPrefix() + name + " TEXT" + (nullable ? " " : " NOT NULL ") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Boolean.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class Boolean extends Column { 6 | 7 | protected java.lang.String defaultValue; 8 | 9 | public Boolean(IAction action, java.lang.String name) { 10 | super(action, name, false); 11 | } 12 | 13 | /** 14 | * Set default value. 15 | * @param value Default value. 16 | * @return Column object. 17 | */ 18 | public Boolean defaultValue(java.lang.String value) { 19 | this.defaultValue = value; 20 | 21 | return this; 22 | } 23 | 24 | @Override 25 | public java.lang.String toString() { 26 | return this.action.getPrefix() + name + " BOOLEAN" + (nullable ? " " : " NOT NULL ") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/SQL.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql; 2 | 3 | import com.code.advancedsql.query.IQuery; 4 | import com.code.advancedsql.table.Table; 5 | 6 | import java.sql.Connection; 7 | import java.sql.PreparedStatement; 8 | import java.sql.SQLException; 9 | 10 | public abstract class SQL implements ISQL { 11 | 12 | protected Connection connection = null; 13 | 14 | protected boolean connected = false; 15 | 16 | @Override 17 | public Connection getConnection() { 18 | return connection; 19 | } 20 | 21 | @Override 22 | public boolean isConnected() { 23 | return connected; 24 | } 25 | 26 | @Override 27 | public Table table(String name) { 28 | return new Table(this, name); 29 | } 30 | 31 | @Override 32 | public PreparedStatement prepare(IQuery query) throws SQLException { 33 | return this.connection.prepareStatement(query.toQuery()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/DateTime.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class DateTime extends Column { 6 | 7 | protected java.lang.String defaultValue; 8 | 9 | public DateTime(IAction action, java.lang.String name) { 10 | super(action, name, false); 11 | } 12 | 13 | /** 14 | * Set default value. 15 | * @param value Default value. 16 | * @return Column object. 17 | */ 18 | public DateTime defaultValue(java.lang.String value) { 19 | this.defaultValue = value; 20 | 21 | return this; 22 | } 23 | 24 | @Override 25 | public java.lang.String toString() { 26 | return this.action.getPrefix() + name + " DATETIME" + (nullable ? " " : " NOT NULL ") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/LongText.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class LongText extends Column { 6 | 7 | protected java.lang.String defaultValue; 8 | 9 | public LongText(IAction action, java.lang.String name) { 10 | super(action, name, false); 11 | } 12 | 13 | /** 14 | * Set default value. 15 | * @param value Default value. 16 | * @return Column object. 17 | */ 18 | public LongText defaultValue(java.lang.String value) { 19 | this.defaultValue = value; 20 | 21 | return this; 22 | } 23 | 24 | @Override 25 | public java.lang.String toString() { 26 | return this.action.getPrefix() + name + " LONGTEXT" + (nullable ? " " : " NOT NULL ") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Timestamp.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class Timestamp extends Column { 6 | 7 | protected java.lang.String defaultValue; 8 | 9 | public Timestamp(IAction action, java.lang.String name) { 10 | super(action, name, false); 11 | } 12 | 13 | /** 14 | * Set default value. 15 | * @param value Default value. 16 | * @return Column object. 17 | */ 18 | public Timestamp defaultValue(java.lang.String value) { 19 | this.defaultValue = value; 20 | 21 | return this; 22 | } 23 | 24 | @Override 25 | public java.lang.String toString() { 26 | return this.action.getPrefix() + name + " TIMESTAMP" + (nullable ? " " : " NOT NULL ") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/MediumText.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class MediumText extends Column { 6 | 7 | protected java.lang.String defaultValue; 8 | 9 | public MediumText(IAction action, java.lang.String name) { 10 | super(action, name, false); 11 | } 12 | 13 | /** 14 | * Set default value. 15 | * @param value Default value. 16 | * @return Column object. 17 | */ 18 | public MediumText defaultValue(java.lang.String value) { 19 | this.defaultValue = value; 20 | 21 | return this; 22 | } 23 | 24 | @Override 25 | public java.lang.String toString() { 26 | return this.action.getPrefix() + name + " MEDIUMTEXT" + (nullable ? " " : " NOT NULL ") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Binary.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | import java.io.InputStream; 6 | 7 | public class Binary extends Column { 8 | 9 | protected InputStream defaultValue; 10 | 11 | public Binary(IAction action, java.lang.String name) { 12 | super(action, name, false); 13 | } 14 | 15 | /** 16 | * Set default value. 17 | * @param value Default value. 18 | * @return Column object. 19 | */ 20 | public Binary defaultValue(InputStream value) { 21 | this.defaultValue = value; 22 | 23 | return this; 24 | } 25 | 26 | @Override 27 | public java.lang.String toString() { 28 | return this.action.getPrefix() + name + " BLOB" + (nullable ? " " : " NOT NULL ") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Float.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class Float extends Numeric { 6 | 7 | protected java.lang.Integer precision; 8 | 9 | public Float(IAction action, java.lang.String name) { 10 | this(action, name, 11, 6); 11 | } 12 | 13 | public Float(IAction action, java.lang.String name, java.lang.Integer length, java.lang.Integer precision) { 14 | super(action, name, false, false, length); 15 | 16 | this.precision = precision; 17 | } 18 | 19 | @Override 20 | public java.lang.String toString() { 21 | return this.action.getPrefix() + name + " FLOAT(" + length + ", " + precision + ")" + (nullable ? " " : " NOT NULL ") + (primary ? " PRIMARY KEY" : "") + (increment ? " AUTO_INCREMENT" : "") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Double.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class Double extends Numeric { 6 | 7 | protected java.lang.Integer precision; 8 | 9 | public Double(IAction action, java.lang.String name) { 10 | this(action, name, 11, 8); 11 | } 12 | 13 | public Double(IAction action, java.lang.String name, java.lang.Integer length, java.lang.Integer precision) { 14 | super(action, name, false, false, length); 15 | 16 | this.precision = precision; 17 | } 18 | 19 | @Override 20 | public java.lang.String toString() { 21 | return this.action.getPrefix() + name + " DOUBLE(" + length + ", " + precision + ")" + (nullable ? " " : " NOT NULL ") + (primary ? " PRIMARY KEY" : "") + (increment ? " AUTO_INCREMENT" : "")+ (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Decimal.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class Decimal extends Numeric { 6 | 7 | protected java.lang.Integer precision; 8 | 9 | public Decimal(IAction action, java.lang.String name) { 10 | this(action, name, 5, 2); 11 | } 12 | 13 | public Decimal(IAction action, java.lang.String name, java.lang.Integer length, java.lang.Integer precision) { 14 | super(action, name, false, false, length); 15 | 16 | this.precision = precision; 17 | } 18 | 19 | @Override 20 | public java.lang.String toString() { 21 | return this.action.getPrefix() + name + " DECIMAL(" + length + ", " + precision + ")" + (nullable ? " " : " NOT NULL ") + (primary ? " PRIMARY KEY" : "") + (increment ? " AUTO_INCREMENT" : "") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/ISpecifiable.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | public interface ISpecifiable { 4 | 5 | /** 6 | * Specify what values will be affected for example: 7 | * query.where("first_name = ? AND last_name = ?", "Denzel", "Code"); 8 | * @param where Where statement. 9 | * @param execute Values of the statement. 10 | * @return Query object. 11 | */ 12 | IQuery where(String where, Object... execute); 13 | 14 | /** 15 | * Specify what values will be affected for example: 16 | * query.where("first_name = 'Denzel' AND last_name = 'Code'"); 17 | * @param where Where statement. 18 | * @return Query object. 19 | */ 20 | IQuery where(String where); 21 | 22 | /** 23 | * Assign the limit of rows to be affected. 24 | * @param limit Limit amount. 25 | * @return Query object. 26 | */ 27 | IQuery limit(int limit); 28 | 29 | /** 30 | * @return Limit of rows to be affected. 31 | */ 32 | int getLimit(); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/Insert.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.table.ITable; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | public class Insert extends ExecuteUpdate { 10 | 11 | public Insert(ITable table) { 12 | super(table); 13 | } 14 | 15 | public Insert(ITable table, Map fields) { 16 | super(table, fields); 17 | } 18 | 19 | @Override 20 | public String toQuery() { 21 | StringBuilder query = new StringBuilder("INSERT INTO " + this.table + " ("); 22 | 23 | for (int i = 0; i < this.fields.size(); i++) query.append(i != this.fields.size() - 1 ? this.fields.get(i) + ", " : this.fields.get(i)); 24 | 25 | query.append(") VALUES ("); 26 | 27 | for (int i = 0; i < this.fields.size(); i++) query.append(i != this.fields.size() - 1 ? "?, " : "?"); 28 | 29 | query.append(")"); 30 | 31 | return query.toString().trim(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/Update.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.table.ITable; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | public class Update extends ExecuteUpdate { 10 | 11 | public Update(ITable table) { 12 | super(table); 13 | } 14 | 15 | public Update(ITable table, Map fields) { 16 | super(table, fields); 17 | } 18 | 19 | @Override 20 | public String toQuery() { 21 | StringBuilder query = new StringBuilder("UPDATE " + this.table + " "); 22 | 23 | for (int i = 0; i < this.fields.size(); i++) query.append(this.fields.size() == 1 ? "SET " + this.fields.get(i) + " = ? " : (i == 0 ? "SET " + this.fields.get(i) + " = ?, " : (i != this.fields.size() - 1 ? this.fields.get(i) + " = ?, " : this.fields.get(i) + " = ?"))); 24 | 25 | query.append(this.where != null ? " WHERE " + this.where : ""); 26 | 27 | query.append(this.limit > 0 ? " LIMIT " + this.limit : ""); 28 | 29 | return query.toString().trim(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Column.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | abstract class Column implements IColumn { 6 | protected java.lang.String name; 7 | 8 | protected java.lang.Boolean nullable; 9 | 10 | protected java.lang.Boolean nativeDefault; 11 | 12 | protected IAction action; 13 | 14 | public Column(IAction action, java.lang.String name, boolean nullable) { 15 | this.name = name; 16 | 17 | this.nullable = nullable; 18 | 19 | this.nativeDefault = false; 20 | 21 | this.action = action; 22 | } 23 | 24 | @Override 25 | public java.lang.String setName() { 26 | return this.name; 27 | } 28 | 29 | @Override 30 | public IColumn nullable() { 31 | this.nullable = true; 32 | 33 | return this; 34 | } 35 | 36 | @Override 37 | public IColumn nativeDefault() { 38 | this.nativeDefault = true; 39 | 40 | return this; 41 | } 42 | 43 | public java.lang.String toString() { 44 | return null; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/String.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class String extends Column { 6 | 7 | protected java.lang.Integer length; 8 | 9 | protected java.lang.String defaultValue; 10 | 11 | public String(IAction action, java.lang.String name, java.lang.Integer length, java.lang.Boolean nullable) { 12 | super(action, name, nullable); 13 | 14 | this.name = name; 15 | 16 | this.length = length; 17 | } 18 | 19 | /** 20 | * Set default value. 21 | * @param value Default value. 22 | * @return Column object. 23 | */ 24 | public String defaultValue(java.lang.String value) { 25 | this.defaultValue = value; 26 | 27 | return this; 28 | } 29 | 30 | @Override 31 | public java.lang.String toString() { 32 | return this.action.getPrefix() + name + " VARCHAR(" + length + ")" + (nullable ? " NULL" : " NOT NULL") + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Enum.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public class Enum extends Column { 6 | 7 | protected Object[] list; 8 | 9 | protected Object defaultValue; 10 | 11 | public Enum(IAction action, java.lang.String name, Object[] list) { 12 | super(action, name, false); 13 | 14 | this.name = name; 15 | 16 | this.list = list; 17 | } 18 | 19 | public Enum defaultValue(Object value) { 20 | this.defaultValue = value; 21 | 22 | return this; 23 | } 24 | 25 | @Override 26 | public java.lang.String toString() { 27 | StringBuilder string = new StringBuilder(); 28 | 29 | for (int i = 0; i < list.length; i++) { 30 | if (list.length == 1 || i == list.length - 1) 31 | string.append("'").append(list[i]).append("'"); 32 | else 33 | string.append("'").append(list[i]).append("', "); 34 | } 35 | 36 | return this.action.getPrefix() + name + " ENUM(" + string.toString() + ")" + (defaultValue != null ? " DEFAULT " + (!nativeDefault ? "'" + defaultValue + "'" : defaultValue) : ""); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/column/Numeric.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.column; 2 | 3 | import com.code.advancedsql.query.action.IAction; 4 | 5 | public abstract class Numeric extends Column { 6 | 7 | protected java.lang.Boolean unsigned; 8 | 9 | protected java.lang.Integer length; 10 | 11 | protected java.lang.Boolean primary = false; 12 | 13 | protected java.lang.Boolean increment = false; 14 | 15 | protected java.lang.Integer defaultValue; 16 | 17 | public Numeric(IAction action, java.lang.String name, java.lang.Boolean nullable, java.lang.Boolean unsigned, java.lang.Integer length) { 18 | super(action, name, nullable); 19 | 20 | this.unsigned = unsigned; 21 | 22 | this.length = length; 23 | } 24 | 25 | /** 26 | * Enable UNSIGNED. 27 | * @return Column object. 28 | */ 29 | public IColumn unsigned() { 30 | this.unsigned = true; 31 | 32 | return this; 33 | } 34 | 35 | /** 36 | * Enable PRIMARY KEY. 37 | * @return Column object. 38 | */ 39 | public Numeric primary() { 40 | this.primary = true; 41 | 42 | return this; 43 | } 44 | 45 | /** 46 | * Enable AUTO INCREMENT. 47 | * @return Column object. 48 | */ 49 | public Numeric increment() { 50 | this.increment = true; 51 | 52 | return this; 53 | } 54 | 55 | /** 56 | * Set default value. 57 | * @param value Default value. 58 | * @return Column object. 59 | */ 60 | public Numeric defaultValue(java.lang.Integer value) { 61 | this.defaultValue = value; 62 | 63 | return this; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/IQuery.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import java.sql.PreparedStatement; 4 | import java.sql.ResultSet; 5 | import java.sql.SQLException; 6 | 7 | public interface IQuery { 8 | 9 | /** 10 | * Execute query and return a boolean. 11 | * Recommended for: CREATE, ALTER, DROP, TRUNCATE. 12 | * @return boolean. 13 | * @throws SQLException Exception when something goes wrong. 14 | */ 15 | Boolean executeStatement() throws SQLException; 16 | 17 | /** 18 | * Execute query and return an integer. 19 | * Recommended for: INSERT, UPDATE, DELETE. 20 | * @return integer. 21 | * @throws SQLException Exception when something goes wrong. 22 | */ 23 | int executeUpdate() throws SQLException; 24 | 25 | /** 26 | * Execute query and return a ResultSet. 27 | * Recommended for: SELECT, SHOW. 28 | * @return ResultSet. 29 | * @throws SQLException Exception when something goes wrong. 30 | */ 31 | ResultSet executeQuery() throws SQLException; 32 | 33 | /** 34 | * Execute query and return a PreparedStatement. 35 | * Recommended for: General. 36 | * @return PreparedStatement. 37 | * @throws SQLException Exception when something goes wrong. 38 | */ 39 | PreparedStatement executePrepare() throws SQLException; 40 | 41 | /** 42 | * Get PreparedStatement of the query. 43 | * This just works if you already executed the query. 44 | * @return PreparedStatement. 45 | */ 46 | PreparedStatement getPrepare(); 47 | 48 | /** 49 | * @return String representation of the Query. 50 | */ 51 | String toQuery(); 52 | 53 | /** 54 | * @return String representation of the Query. 55 | */ 56 | String toString(); 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/Query.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.ISQL; 4 | import com.code.advancedsql.table.ITable; 5 | 6 | import java.sql.PreparedStatement; 7 | import java.sql.ResultSet; 8 | import java.sql.SQLException; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | public abstract class Query implements IQuery { 13 | 14 | protected ISQL sql; 15 | 16 | protected String table; 17 | 18 | protected List execute = new ArrayList<>(); 19 | 20 | protected PreparedStatement prepare = null; 21 | 22 | protected String where = null; 23 | 24 | protected int limit = 0; 25 | 26 | public Query(ITable table) { 27 | this.sql = table.getSQL(); 28 | 29 | this.table = table.getName(); 30 | } 31 | 32 | public Boolean executeStatement() throws SQLException { 33 | prepare = this.sql.prepare(this); 34 | 35 | ISQL.setStatementParameters(this.prepare, this.execute); 36 | 37 | boolean result = prepare.execute(); 38 | 39 | prepare.close(); 40 | 41 | return result; 42 | } 43 | 44 | public ResultSet executeQuery() throws SQLException { 45 | prepare = this.sql.prepare(this); 46 | 47 | ISQL.setStatementParameters(this.prepare, this.execute); 48 | 49 | return prepare.executeQuery(); 50 | } 51 | 52 | public int executeUpdate() throws SQLException { 53 | prepare = this.sql.prepare(this); 54 | 55 | ISQL.setStatementParameters(this.prepare, this.execute); 56 | 57 | int result = prepare.executeUpdate(); 58 | 59 | prepare.close(); 60 | 61 | return result; 62 | } 63 | 64 | public abstract String toQuery(); 65 | 66 | public PreparedStatement getPrepare() { 67 | return prepare; 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return this.toQuery(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/ExecuteUpdate.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.table.ITable; 4 | 5 | import java.sql.PreparedStatement; 6 | import java.sql.SQLException; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | public abstract class ExecuteUpdate extends Specifiable { 12 | 13 | protected final List fields = new ArrayList<>(); 14 | 15 | public ExecuteUpdate(ITable table, Map fields) { 16 | super(table); 17 | 18 | if (fields != null) this.fields(fields); 19 | } 20 | 21 | public ExecuteUpdate(ITable table) { 22 | this(table, null); 23 | } 24 | 25 | @Override 26 | public PreparedStatement executePrepare() throws SQLException { 27 | this.execute(); 28 | 29 | return prepare; 30 | } 31 | 32 | /** 33 | * Columns and values that you want to update/insert. 34 | * @param field Column name 35 | * @param value Row value 36 | * @return Query object. 37 | */ 38 | @SuppressWarnings("unchecked") 39 | public T field(String field, Object value) { 40 | this.fields.add(field); 41 | 42 | this.execute.add(value); 43 | 44 | return (T) this; 45 | } 46 | 47 | /** 48 | * Columns and values that you want to update/insert. 49 | * @param values Map 50 | * @return Query object. 51 | */ 52 | @SuppressWarnings("unchecked") 53 | public T fields(Map values) { 54 | for (Map.Entry entry: values.entrySet()) 55 | this.field(entry.getKey(), entry.getValue()); 56 | 57 | return (T) this; 58 | } 59 | 60 | /** 61 | * Execute the query. 62 | * @return An integer. 63 | * @throws SQLException Exception when something goes wrong. 64 | */ 65 | public int execute() throws SQLException { 66 | return this.executeUpdate(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/Alter.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.query.action.Add; 4 | import com.code.advancedsql.query.column.IColumn; 5 | import com.code.advancedsql.table.ITable; 6 | import com.code.advancedsql.query.action.Drop; 7 | import com.code.advancedsql.query.action.Modify; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | public class Alter extends ExecuteStatement { 13 | 14 | private final Add add = new Add(); 15 | private final Modify modify = new Modify(); 16 | private final Drop drop = new Drop(); 17 | 18 | public Alter(ITable table) { 19 | super(table); 20 | } 21 | 22 | /** 23 | * @return "Drop" Object that allows you to specify the columns that you want to drop. 24 | */ 25 | public Drop drop() { 26 | return drop; 27 | } 28 | 29 | /** 30 | * @return "Modify" Object that allows you to specify the columns that you want to modify. 31 | */ 32 | public Modify modify() { 33 | return modify; 34 | } 35 | 36 | /** 37 | * @return "Add" Object that allows you to specify the columns that you want to add. 38 | */ 39 | public Add add() { 40 | return add; 41 | } 42 | 43 | @Override 44 | public java.lang.String toQuery() { 45 | StringBuilder query = new StringBuilder("ALTER TABLE " + this.table + " "); 46 | 47 | List columns = new ArrayList<>(); 48 | 49 | columns.addAll(add.getColumns()); 50 | columns.addAll(modify.getColumns()); 51 | 52 | List dropColumns = drop.getColumns(); 53 | 54 | for (int i = 0; i < columns.size(); i++) query.append(i != columns.size() - 1 ? columns.get(i).toString() + ", " : columns.get(i).toString() + (dropColumns.size() > 0 ? ", " : ";")); 55 | 56 | for (int i = 0; i < dropColumns.size(); i++) query.append(i != dropColumns.size() - 1 ? drop.getPrefix() + dropColumns.get(i) + ", " : drop.getPrefix() + dropColumns.get(i) + ";"); 57 | 58 | return query.toString().trim(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/ExecuteQuery.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.ISQL; 4 | import com.code.advancedsql.table.ITable; 5 | 6 | import java.sql.PreparedStatement; 7 | import java.sql.ResultSet; 8 | import java.sql.ResultSetMetaData; 9 | import java.sql.SQLException; 10 | import java.util.HashMap; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | public abstract class ExecuteQuery extends Specifiable { 15 | 16 | public ExecuteQuery(ITable table) { 17 | super(table); 18 | } 19 | 20 | @Override 21 | public PreparedStatement executePrepare() throws SQLException { 22 | this.execute(); 23 | 24 | return this.prepare; 25 | } 26 | 27 | /** 28 | * @return A Map of the first result found. 29 | * @throws SQLException Exception when something goes wrong. 30 | */ 31 | public Map fetch() throws SQLException { 32 | this.limit(1); 33 | 34 | List> result = fetchAllAsList(); 35 | 36 | if (result.size() == 0) return null; 37 | 38 | return result.get(0); 39 | } 40 | 41 | /** 42 | * @return ResultSet of all the results found. 43 | * @throws SQLException Exception when something goes wrong. 44 | */ 45 | public ResultSet fetchAll() throws SQLException { 46 | return execute(); 47 | } 48 | 49 | /** 50 | * @return A List of all the results found. 51 | * @throws SQLException Exception when something goes wrong. 52 | */ 53 | public List> fetchAllAsList() throws SQLException { 54 | ResultSet resultSet = this.fetchAll(); 55 | 56 | List> list = ISQL.convertResultSetToList(resultSet); 57 | 58 | prepare.close(); 59 | 60 | return list; 61 | } 62 | 63 | /** 64 | * Execute query. 65 | * @return A ResultSet of all the results found. 66 | * @throws SQLException Exception when something goes wrong. 67 | */ 68 | public ResultSet execute() throws SQLException { 69 | return this.executeQuery(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/table/Table.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.table; 2 | 3 | import com.code.advancedsql.ISQL; 4 | import com.code.advancedsql.SQL; 5 | import com.code.advancedsql.query.*; 6 | 7 | import java.sql.DatabaseMetaData; 8 | import java.sql.ResultSet; 9 | import java.sql.SQLException; 10 | import java.util.Map; 11 | 12 | public class Table implements ITable { 13 | 14 | protected String name; 15 | 16 | protected ISQL sql; 17 | 18 | public Table(SQL sql, String name) { 19 | this.sql = sql; 20 | 21 | this.name = name; 22 | } 23 | 24 | public boolean exists() throws SQLException { 25 | DatabaseMetaData metaData = sql.getConnection().getMetaData(); 26 | 27 | ResultSet tables = metaData.getTables(null, null, this.name, null); 28 | 29 | boolean next = tables.next(); 30 | 31 | if (!next) return false; 32 | 33 | String name = tables.getString("TABLE_NAME"); 34 | 35 | return name != null && name.equals(this.name); 36 | } 37 | 38 | @Override 39 | public String getName() { 40 | return this.name; 41 | } 42 | 43 | @Override 44 | public ISQL getSQL() { 45 | return this.sql; 46 | } 47 | 48 | @Override 49 | public Select select() { 50 | return new Select(this); 51 | } 52 | 53 | @Override 54 | public Select select(String[] columns) { 55 | return new Select(this, columns); 56 | } 57 | 58 | @Override 59 | public Create create() { 60 | return new Create(this); 61 | } 62 | 63 | @Override 64 | public Alter alter() { 65 | return new Alter(this); 66 | } 67 | 68 | @Override 69 | public Insert insert() { 70 | return new Insert(this); 71 | } 72 | 73 | @Override 74 | public Insert insert(Map fields) { 75 | return new Insert(this, fields); 76 | } 77 | 78 | @Override 79 | public Update update() { 80 | return new Update(this); 81 | } 82 | 83 | @Override 84 | public Update update(Map fields) { 85 | return new Update(this, fields); 86 | } 87 | 88 | @Override 89 | public Delete delete() { 90 | return new Delete(this); 91 | } 92 | 93 | @Override 94 | public Truncate truncate() { 95 | return new Truncate(this); 96 | } 97 | 98 | @Override 99 | public Drop drop() { 100 | return new Drop(this); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/ISQL.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql; 2 | 3 | import com.code.advancedsql.query.IQuery; 4 | import com.code.advancedsql.table.ITable; 5 | 6 | import java.sql.*; 7 | import java.util.ArrayList; 8 | import java.util.HashMap; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | public interface ISQL { 13 | 14 | /** 15 | * Prepare SQL query. 16 | * @param query Query object that you want to prepare. 17 | * @return A PreparedStatement object. 18 | * @throws SQLException Exception when something goes wrong. 19 | */ 20 | PreparedStatement prepare(IQuery query) throws SQLException; 21 | 22 | /** 23 | * Get access to a table object to modify information. 24 | * @param name Name of the table. 25 | * @return A Table object which allows you to execute SQL query's. 26 | */ 27 | ITable table(String name); 28 | 29 | /** 30 | * Get connection state. 31 | * @return true if connected, false if not. 32 | */ 33 | boolean isConnected(); 34 | 35 | /** 36 | * @return Connection Object. 37 | */ 38 | Connection getConnection(); 39 | 40 | /** 41 | * Convert a ResultSet into a list. 42 | * @param resultSet Result set 43 | * @return Returns a list of map with the column name and value. 44 | * @throws SQLException Exception when something goes wrong. 45 | */ 46 | static List> convertResultSetToList(ResultSet resultSet) throws SQLException { 47 | ResultSetMetaData metaData = resultSet.getMetaData(); 48 | 49 | int columns = metaData.getColumnCount(); 50 | 51 | List> list = new ArrayList<>(); 52 | 53 | while (resultSet.next()) { 54 | Map row = new HashMap<>(columns); 55 | 56 | for (int i = 1; i <= columns; i++) row.put(metaData.getColumnName(i), resultSet.getObject(i)); 57 | 58 | list.add(row); 59 | } 60 | 61 | resultSet.close(); 62 | 63 | return list; 64 | } 65 | 66 | /** 67 | * Set statement parameters. 68 | * @param statement Statement to modify. 69 | * @param execute Parameters to assign. 70 | * @throws SQLException Exception when something goes wrong. 71 | */ 72 | static void setStatementParameters(PreparedStatement statement, List execute) throws SQLException { 73 | for (int i = 0; i < execute.size(); i++) { 74 | Object value = execute.get(i); 75 | 76 | statement.setObject(i + 1, value); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.code.advancedsql 6 | AdvancedSQL 7 | 2.3-SNAPSHOT 8 | 9 | 1.8 10 | 1.8 11 | 12 | 13 | 14 | 15 | libs 16 | file://${project.basedir}/lib 17 | 18 | 19 | 20 | 21 | org.apache.commons 22 | commons-math3 23 | 3.6.1 24 | compile 25 | 26 | 27 | 28 | mysql 29 | mysql-connector-java 30 | 8.0.21 31 | 32 | 33 | com.google.guava 34 | guava 35 | 29.0-jre 36 | runtime 37 | 38 | 39 | junit 40 | junit 41 | 4.13.1 42 | test 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.apache.maven.plugins 50 | maven-shade-plugin 51 | 3.1.0 52 | 53 | 54 | package 55 | 56 | shade 57 | 58 | 59 | 60 | 61 | mysql:mysql-connector-java 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | org.apache.maven.plugins 70 | maven-compiler-plugin 71 | 72 | 8 73 | 8 74 | 75 | 76 | 77 | 78 | 79 | 80 | src/main/resources 81 | true 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/MySQL.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql; 2 | 3 | import java.sql.DriverManager; 4 | import java.sql.SQLException; 5 | import java.sql.Statement; 6 | 7 | public class MySQL extends SQL { 8 | 9 | /** 10 | * Create MySQL connection. 11 | * Do not worry about creating the database, AdvancedSQL takes care of it for you. 12 | * @param host MySQL server host. 13 | * @param port MySQL server port. 14 | * @param username MySQL server username. 15 | * @param password MySQL server password. 16 | * @param database MySQL server database. If not exists AdvancedSQL creates it for you. 17 | * @param attributes MySQL server connection attributes. 18 | * @throws SQLException Exception when something goes wrong. 19 | */ 20 | public MySQL(String host, int port, String username, String password, String database, String attributes) throws SQLException { 21 | try { 22 | Class.forName("com.mysql.cj.jdbc.Driver"); 23 | 24 | this.connect(host, port, username, password, database, attributes); 25 | } catch (SQLException e) { 26 | if (e.getErrorCode() == 1049) { 27 | createDatabase(host, port, username, password, database, attributes); 28 | 29 | connect(host, port, username, password, database, attributes); 30 | 31 | return; 32 | } 33 | 34 | throw new SQLException(e.getMessage(), e); 35 | } catch (ClassNotFoundException e) { 36 | this.connected = false; 37 | 38 | throw new SQLException("Can't load JDBC Driver", e); 39 | } 40 | } 41 | 42 | /** 43 | * Create MySQL connection. 44 | * Do not worry about creating the database, AdvancedSQL takes care of it for you. 45 | * @param host MySQL server host. 46 | * @param port MySQL server port. 47 | * @param username MySQL server username. 48 | * @param password MySQL server password. 49 | * @param database MySQL server database. If not exists AdvancedSQL creates it for you. 50 | * @throws SQLException Exception when something goes wrong. 51 | */ 52 | public MySQL(String host, int port, String username, String password, String database) throws SQLException { 53 | this(host, port, username, password, database, "useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&autoReconnect=true"); 54 | } 55 | 56 | private void connect(String host, int port, String username, String password, String database, String attributes) throws SQLException { 57 | connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + (attributes.isEmpty() ? "" : "?" + attributes), username, password); 58 | 59 | this.connected = true; 60 | } 61 | 62 | private void createDatabase(String host, int port, String username, String password, String database, String attributes) throws SQLException { 63 | connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + (attributes.isEmpty() ? "" : "?" + attributes), username, password); 64 | 65 | Statement statement = connection.createStatement(); 66 | 67 | statement.executeUpdate("CREATE DATABASE " + database); 68 | 69 | connection.close(); 70 | 71 | connection = null; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/table/ITable.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.table; 2 | 3 | import com.code.advancedsql.ISQL; 4 | import com.code.advancedsql.query.*; 5 | 6 | import java.sql.SQLException; 7 | import java.util.Map; 8 | 9 | public interface ITable { 10 | 11 | /** 12 | * @return Table name. 13 | */ 14 | String getName(); 15 | 16 | /** 17 | * @return SQL object. 18 | */ 19 | ISQL getSQL(); 20 | 21 | /** 22 | * Check if the table exists. 23 | * @return true if table exists. 24 | * @throws SQLException Exception when something goes wrong. 25 | */ 26 | boolean exists() throws SQLException; 27 | 28 | /** 29 | * Generate a SELECT query. 30 | * Remember to execute the query after you specify the parameters. 31 | * @return A Select object that allows you to specify the parameters of your query. 32 | */ 33 | Select select(); 34 | 35 | /** 36 | * Generate a SELECT query. 37 | * Remember to execute the query after you specify the parameters. 38 | * @param columns Array of columns that you want to select. 39 | * @return A Select object that allows you to specify the parameters of your query. 40 | */ 41 | Select select(String[] columns); 42 | 43 | /** 44 | * Create table. 45 | * Set the columns type that you want to create and then execute the query. 46 | * Remember to execute the query after you specify the parameters. 47 | * @return A Create object that allows you to specify the parameters of your query. 48 | */ 49 | Create create(); 50 | 51 | /** 52 | * Alter the table. 53 | * Remember to execute the query after you specify the parameters. 54 | * @return An Alter object that allows you to specify the parameters of your query. 55 | */ 56 | Alter alter(); 57 | 58 | /** 59 | * Insert a row into the table. 60 | * Remember to execute the query after you specify the parameters. 61 | * @return An Insert object that allows you to specify the parameters of your query. 62 | */ 63 | Insert insert(); 64 | 65 | /** 66 | * Insert a row into the table. 67 | * Remember to execute the query after you specify the parameters. 68 | * @param fields Columns and values to insert. 69 | * @return An Insert object that allows you to specify the parameters of your query. 70 | */ 71 | Insert insert(Map fields); 72 | 73 | /** 74 | * Update rows from the table. 75 | * Remember to execute the query after you specify the parameters. 76 | * @return An Update object that allows you to specify the parameters of your query. 77 | */ 78 | Update update(); 79 | 80 | /** 81 | * Update rows from the table. 82 | * Remember to execute the query after you specify the parameters. 83 | * @param fields Columns and values to update. 84 | * @return An Update object that allows you to specify the parameters of your query. 85 | */ 86 | Update update(Map fields); 87 | 88 | /** 89 | * Delete rows from the table. 90 | * Remember to execute the query after you specify the parameters. 91 | * @return An Delete object that allows you to specify the parameters of your query. 92 | */ 93 | Delete delete(); 94 | 95 | /** 96 | * Empty the table. 97 | * Remember to execute the query after you specify the parameters. 98 | * @return A Truncate object that allows you to specify the parameters of your query. 99 | */ 100 | Truncate truncate(); 101 | 102 | /** 103 | * Delete the table. 104 | * Remember to execute the query after you specify the parameters. 105 | * @return A Drop object that allows you to specify the parameters of your query. 106 | */ 107 | Drop drop(); 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/join/Join.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query.join; 2 | 3 | import com.code.advancedsql.query.Select; 4 | 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | public class Join implements IJoin { 12 | 13 | protected Select query = null; 14 | 15 | protected String table = null; 16 | 17 | protected String on = null; 18 | 19 | protected String as = null; 20 | 21 | protected String[] using = null; 22 | 23 | public Join(Select query, String table) { 24 | this.query = query; 25 | 26 | this.table = table; 27 | } 28 | 29 | @Override 30 | public IJoin on(String conditional) { 31 | this.on = conditional; 32 | 33 | return this; 34 | } 35 | 36 | @Override 37 | public IJoin as(String name) { 38 | this.as = name; 39 | 40 | return this; 41 | } 42 | 43 | @Override 44 | public IJoin using(String[] columns) { 45 | this.using = columns; 46 | 47 | return this; 48 | } 49 | 50 | @Override 51 | public IJoin join(String table) { 52 | return this.query.join(table); 53 | } 54 | 55 | @Override 56 | public IJoin innerJoin(String table) { 57 | return this.query.innerJoin(table); 58 | } 59 | 60 | public IJoin leftJoin(String table) { 61 | return this.query.leftJoin(table); 62 | } 63 | 64 | @Override 65 | public IJoin rightJoin(String table) { 66 | return this.query.rightJoin(table); 67 | } 68 | 69 | @Override 70 | public IJoin fullJoin(String table) { 71 | return this.query.fullJoin(table); 72 | } 73 | 74 | @Override 75 | public Select where(String where) { 76 | return this.query.where(where); 77 | } 78 | 79 | @Override 80 | public Select where(String where, Object... execute) { 81 | return this.query.where(where, execute); 82 | } 83 | 84 | @Override 85 | public PreparedStatement executePrepare() throws SQLException { 86 | return this.query.executePrepare(); 87 | } 88 | 89 | @Override 90 | public ResultSet execute() throws SQLException { 91 | return this.executeQuery(); 92 | } 93 | 94 | @Override 95 | public Map fetch() throws SQLException { 96 | return this.query.fetch(); 97 | } 98 | 99 | @Override 100 | public ResultSet fetchAll() throws SQLException { 101 | return this.query.fetchAll(); 102 | } 103 | 104 | @Override 105 | public List> fetchAllAsList() throws SQLException { 106 | return this.query.fetchAllAsList(); 107 | } 108 | 109 | @Override 110 | public Boolean executeStatement() throws SQLException { 111 | return this.query.executeStatement(); 112 | } 113 | 114 | @Override 115 | public ResultSet executeQuery() throws SQLException { 116 | return this.query.executeQuery(); 117 | } 118 | 119 | @Override 120 | public int executeUpdate() throws SQLException { 121 | return this.query.executeUpdate(); 122 | } 123 | 124 | @Override 125 | public String getPrefix() { 126 | return "JOIN"; 127 | } 128 | 129 | @Override 130 | public Select getQuery() { 131 | return query; 132 | } 133 | 134 | @Override 135 | public String toQuery() { 136 | return this.getPrefix() + " " + this.table + (this.as != null ? " AS " + this.as : "") + 137 | (this.on != null ? " ON (" + this.on + ")" : "") + 138 | (this.using != null ? " USING (" + String.join(", ", this.using) + ")" : " "); 139 | } 140 | 141 | @Override 142 | public String toString() { 143 | return this.toQuery(); 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /src/main/java/com/code/advancedsql/query/Select.java: -------------------------------------------------------------------------------- 1 | package com.code.advancedsql.query; 2 | 3 | import com.code.advancedsql.table.ITable; 4 | import com.code.advancedsql.query.join.*; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | public class Select extends ExecuteQuery