io.github.matteobertozzi.easerinsights.jdbc.sqlx.impl.DialectMySql Maven / Gradle / Ivy
The newest version!
package io.github.matteobertozzi.easerinsights.jdbc.sqlx.impl;
import io.github.matteobertozzi.easerinsights.jdbc.sqlx.Sql;
import io.github.matteobertozzi.easerinsights.jdbc.sqlx.SqlExpr;
public class DialectMySql implements Sql {
private static StringBuilder addName(final StringBuilder sql, final String name) {
return sql.append('`').append(name.toLowerCase()).append('`');
}
@Override
public InsertSqlBuilder insertInto(final String tableName, final Enum>... fields) {
return new MySqlInsertBuilder(tableName, fields);
}
@Override
public UpdateSqlBuilder update(final String tableName, final Enum>... fields) {
return new MySqlUpdateBuilder(tableName, fields);
}
@Override
public DeleteSqlBuilder deleteFrom(final String tableName) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'deleteFrom'");
}
@Override
public SelectSqlBuilder selectFrom(final String tableName) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'selectFrom'");
}
private static class MySqlInsertBuilder implements InsertSqlBuilder {
private final Enum>[] fields;
private final String tableName;
public MySqlInsertBuilder(final String tableName, final Enum>[] fields) {
this.fields = fields;
this.tableName = tableName;
}
}
private static class MySqlUpdateBuilder implements UpdateSqlBuilder {
private final StringBuilder sql = new StringBuilder(128);
private final Enum>[] fields;
private final String tableName;
public MySqlUpdateBuilder(final String tableName, final Enum>[] fields) {
this.fields = fields;
this.tableName = tableName;
sql.append("UPDATE ");
addName(tableName);
sql.append(" SET ");
for (int i = 0; i < fields.length; ++i) {
if (i > 0) sql.append(", ");
addName(fields[i].name());
sql.append(" = ?");
}
}
@Override
public SqlExpr where() {
sql.append(" WHERE ");
return new SqlExprImpl<>(this, sql, DialectMySql::addName);
}
@Override
public SqlUpdateDef build() {
return new SqlUpdateDef(tableName, fields, sql.toString());
}
private StringBuilder addName(final String name) {
return DialectMySql.addName(sql, name);
}
}
}