mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 14:32:04 +00:00
#1115 - Trim SQL for SqlUpdate, SqlQuery and CallableSql - also replace \n with space
This commit is contained in:
@@ -1034,17 +1034,24 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public SqlQuery createSqlQuery(String sql) {
|
||||
return new DefaultRelationalQuery(this, sql.trim());
|
||||
return new DefaultRelationalQuery(this, trimSql(sql));
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlUpdate createSqlUpdate(String sql) {
|
||||
return new DefaultSqlUpdate(this, sql.trim());
|
||||
return new DefaultSqlUpdate(this, trimSql(sql));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableSql createCallableSql(String sql) {
|
||||
return new DefaultCallableSql(this, sql.trim());
|
||||
return new DefaultCallableSql(this, trimSql(sql));
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim and remove new line chars for better support in multi-line sql strings (logging etc).
|
||||
*/
|
||||
private String trimSql(String sql) {
|
||||
return sql.trim().replace('\n',' ');
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,12 +3,35 @@ package org.tests.update;
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.SqlUpdate;
|
||||
import org.tests.idkeys.db.AuditLog;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.tests.idkeys.db.AuditLog;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestSqlUpdateInTxn extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testSqlTrim() {
|
||||
|
||||
String sql = " this\nis\ntrimmed ";
|
||||
|
||||
SqlUpdate sqlUpdate = Ebean.createSqlUpdate(sql);
|
||||
assertThat(sqlUpdate.getSql()).isEqualTo("this is trimmed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSqlUpdateWithWhitespace() {
|
||||
|
||||
String sql = " \nupdate audit_log \nset description = description \nwhere id = id";
|
||||
|
||||
SqlUpdate sqlUpdate = Ebean.createSqlUpdate(sql);
|
||||
sqlUpdate.execute();
|
||||
|
||||
assertThat(sqlUpdate.getSql()).isEqualTo("update audit_log set description = description where id = id");
|
||||
assertThat(sqlUpdate.getGeneratedSql()).isEqualTo("update audit_log set description = description where id = id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasic() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user