Location>code7788 >text

Write a simple SQL generation tool

Popularity:624 ℃/2025-03-10 16:17:04
import .; import .; import ; public interface BaseService { default Query query() { return new Query(); } default Update update() { return new Update(); } default Insert insert() { return new Insert(); } static String value(Object value) { if ((value)) { return " NULL "; } if (value instanceof Date) { return "'" + ((Date) value, "yyyy-MM-dd HH:mm:ss") + "'"; } if (value instanceof String) { return "'" + value + "'"; } if (value instanceof Number) { return (); } throw newRuntimeException("Unresolved field:" + value); } class Insert { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder inertFieldBuilder = new StringBuilder(); StringBuilder inertValueBuilder = new StringBuilder(); public Insert set(String column, Object value) { if (() > 0) (" , "); (column); if (() > 0) (" , "); (" "); (value(value)); (" "); return this; } public String toString() { if (() > 0 && () > 0) { (" ( "); (()); (" ) "); (" VALUES( "); (()); (" ) "); } return (); } } class Update { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder updateBuilder = new StringBuilder(); public Update set(String column, Object value) { if (() > 0) (" , "); (" "); (column); (" = "); (value(value)); (" "); return this; } public String toString() { if (() > 0) { (()); } return (); } } class Query { StringBuilder whereBuilder = new StringBuilder(); StringBuilder sqlBuilder = new StringBuilder(); public Query eq(String column, Object value) { if ((value)) return this; if (() > 0) (" AND "); (" "); (column); (" = "); (value(value)); (" "); return this; } public Query or(String column, Object value) { if ((value)) return this; if (() > 0) (" OR "); (" "); (column); (" = "); (value(value)); (" "); return this; } public Query like(String column, String value) { if ((value)) return this; if (() > 0) (" AND "); (" "); (column); (" LIKE '%"); (value); ("%' "); return this; } public Query in(String column, Object... values) { if ((values)) return this; if (() > 0) (" AND "); (" "); (column); (" IN( "); int i; i = 0; for (Object value : values) { if (i > 0) (" , "); i++; (value(value)); } (" )"); return this; } public Query noIn(String column, Object... values) { if ((values)) return this; if (() > 0) (" AND "); (" "); (column); (" NOT IN( "); int i; i = 0; for (Object value : values) { if (i > 0) (" , "); i++; (value(value)); } (" )"); return this; } public Query isNull(String column) { if (() > 0) (" AND "); (" ( LENGTH( "); (column); (" ) <=0 OR IS NULL ) "); return this; } public Query notNull(String column) { if (() > 0) (" AND "); (" ( LENGTH( "); (column); (" ) >0 OR IS NOT NULL ) "); return this; } public Query sql(Object sql) { if ((sql)) return this; (()); return this; } public Query and(String whereSql) { if ((whereSql)) return this; if (() > 0) (" AND "); ("( "); (whereSql); (" )"); return this; } public Query or(String whereSql) { if ((whereSql)) return this; if (() > 0) (" OR "); ("( "); (whereSql); (" )"); return this; } public String toString() { if (() > 0) { (()); } return (); } } }