use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.
the class PreparedStatement method getSQLStatementByBindValue.
public SQLStatement getSQLStatementByBindValue(BindValue[] values) {
if (this.bindValues != values) {
throw new AssertionError();
}
SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(this.statement.toString());
boolean hasBlob = !(sqlStatement instanceof SQLSelectStatement);
sqlStatement.accept(new MySqlASTVisitorAdapter() {
int index;
@Override
public void endVisit(SQLVariantRefExpr x) {
if ("?".equalsIgnoreCase(x.getName())) {
Object o = null;
if (index < bindValues.length) {
io.mycat.BindValue value = bindValues[index++];
if (!value.isNull) {
o = value.getJavaObject(hasBlob);
}
}
SQLReplaceable parent = (SQLReplaceable) x.getParent();
parent.replace(x, PreparedStatement.fromJavaObject(o));
}
super.endVisit(x);
}
});
return sqlStatement;
}
use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.
the class NewMycatConnectionImpl method paramize.
public static String paramize(String sql, List<Object> params) {
if (params.isEmpty())
return sql;
if (sql.startsWith("be")) {
return sql;
}
SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
sqlStatement.accept(new MySqlASTVisitorAdapter() {
int index;
@Override
public void endVisit(SQLVariantRefExpr x) {
if ("?".equalsIgnoreCase(x.getName())) {
if (index < params.size()) {
Object value = params.get(index++);
SQLReplaceable parent = (SQLReplaceable) x.getParent();
parent.replace(x, io.mycat.PreparedStatement.fromJavaObject(value));
}
}
super.endVisit(x);
}
});
sql = sqlStatement.toString();
return sql;
}
use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.
the class NewVertxConnectionImpl method update.
@Override
public Future<SqlResult> update(String sql, List<Object> params) {
LOGGER.debug("sql:{}", sql);
if (!sql.startsWith("begin") && !sql.startsWith("XA")) {
SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
sqlStatement.accept(new MySqlASTVisitorAdapter() {
int index;
@Override
public void endVisit(SQLVariantRefExpr x) {
if ("?".equalsIgnoreCase(x.getName())) {
if (index < params.size()) {
Object value = params.get(index++);
SQLReplaceable parent = (SQLReplaceable) x.getParent();
parent.replace(x, io.mycat.PreparedStatement.fromJavaObject(value));
}
}
super.endVisit(x);
}
});
sql = sqlStatement.toString();
}
Query<io.vertx.sqlclient.RowSet<Row>> preparedStatementFuture = this.mySQLConnection.query(sql);
Future<SqlResult> sqlResultFuture = preparedStatementFuture.execute().map(rows -> {
int affectRows = rows.rowCount();
long insertId = Optional.ofNullable(rows.property(MySQLClient.LAST_INSERTED_ID)).orElse(0L);
return SqlResult.of(affectRows, insertId);
});
return mapException(sqlResultFuture);
}
use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.
the class VertxExecuter method rewriteInsertBatchedStatements.
public static List<EachSQL> rewriteInsertBatchedStatements(Iterable<EachSQL> eachSQLs, int batchSize) {
@Data
@AllArgsConstructor
@EqualsAndHashCode
class key {
String target;
String sql;
}
Map<key, SQLInsertStatement> map = new ConcurrentHashMap<>();
final Function<Map.Entry<key, SQLInsertStatement>, EachSQL> finalFunction = i -> {
key key = i.getKey();
SQLInsertStatement value = i.getValue();
return new EachSQL(key.getTarget(), value.toString(), Collections.emptyList());
};
LinkedList<EachSQL> res = new LinkedList<>();
for (EachSQL eachSQL : eachSQLs) {
String target = eachSQL.getTarget();
String sql = eachSQL.getSql();
SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
List<Object> sqlParams = eachSQL.getParams();
if (sqlStatement instanceof SQLInsertStatement) {
SQLInsertStatement insertStatement = (SQLInsertStatement) sqlStatement;
key key = new key(target, sql);
final SQLInsertStatement nowInsertStatement = map.computeIfAbsent(key, key1 -> {
SQLInsertStatement clone = insertStatement.clone();
clone.getValuesList().clear();
return clone;
});
MySqlASTVisitorAdapter mySqlASTVisitorAdapter = new MySqlASTVisitorAdapter() {
@Override
public boolean visit(SQLVariantRefExpr x) {
SQLReplaceable parent = (SQLReplaceable) x.getParent();
parent.replace(x, PreparedStatement.fromJavaObject(sqlParams.get(x.getIndex())));
return false;
}
};
List<EachSQL> list = new LinkedList<>();
for (SQLInsertStatement.ValuesClause valuesClause : insertStatement.getValuesList()) {
valuesClause = valuesClause.clone();
nowInsertStatement.addValueCause(valuesClause);
valuesClause.accept(mySqlASTVisitorAdapter);
if (nowInsertStatement.getValuesList().size() >= batchSize) {
EachSQL e = finalFunction.apply(new AbstractMap.SimpleEntry(key, nowInsertStatement.clone()));
list.add(e);
nowInsertStatement.getValuesList().clear();
} else {
continue;
}
}
res.addAll(list);
} else {
res.add(eachSQL);
}
}
res.addAll(map.entrySet().stream().map(finalFunction).collect(Collectors.toList()));
return res;
}
use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.
the class AbstractMySqlConnectionImpl method apply.
public static String apply(String parameterizedSql, List<Object> parameters) {
SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(parameterizedSql);
sqlStatement.accept(new MySqlASTVisitorAdapter() {
@Override
public void endVisit(SQLVariantRefExpr x) {
SQLReplaceable parent = (SQLReplaceable) x.getParent();
parent.replace(x, fromJavaObject(parameters.get(x.getIndex())));
}
});
return sqlStatement.toString();
}
Aggregations