Skip to content

Commit

Permalink
Included character escape in SQL strings
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinAlpert committed Aug 30, 2019
1 parent f5bfb10 commit 28a7aaa
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ You can include the Maven dependency:
<dependency>
<groupId>com.github.collinalpert</groupId>
<artifactId>lambda2sql</artifactId>
<version>2.1.3</version>
<version>2.1.4</version>
</dependency>
```

Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.collinalpert</groupId>
<artifactId>lambda2sql</artifactId>
<version>2.1.3</version>
<version>2.1.4</version>
<packaging>jar</packaging>

<name>lambda2sql</name>
Expand Down Expand Up @@ -66,7 +66,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<version>5.5.1</version>
<scope>test</scope>
</dependency>

Expand All @@ -77,7 +77,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
Expand All @@ -99,7 +99,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -112,7 +112,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
<version>3.1.1</version>
<configuration>
<additionalOptions>-html5</additionalOptions>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public StringBuilder visit(ConstantExpression e) {
}

if (e.getValue() instanceof String || e.getValue() instanceof Temporal) {
return sb.append("'").append(e.getValue()).append("'");
return sb.append("'").append(escapeString(e.getValue().toString())).append("'");
}

return sb.append(e.getValue().toString());
Expand Down Expand Up @@ -318,4 +318,9 @@ private StringBuilder doStringOperation(Expression member, Expression argument,
modifier.accept(valueBuilder);
return member.accept(new SqlVisitor(this.tableName, this.withBackticks, this.arguments)).append(negated ? " NOT" : "").append(" LIKE ").append(valueBuilder);
}

private String escapeString(String input) {
input = input.replace("\\", "\\\\").replace("'", "\\'");
return input;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.collinalpert.lambda2sql.Lambda2Sql;
import com.github.collinalpert.lambda2sql.functions.SqlFunction;
import com.github.collinalpert.lambda2sql.functions.SqlPredicate;
import com.trigersoft.jaque.expression.LambdaExpression;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -110,8 +111,11 @@ void testNotNull() {
var age = 17;
SqlPredicate<IPerson> p = person -> person.getAge() == age || person.getName() != isNull;
SqlPredicate<IPerson> p2 = person -> person.getName() != null;
SqlPredicate<IPerson> p3 = person -> person.getName() != null;
p3 = p3.and(t -> t.getAge() == 18);
assertPredicateEqual("`person`.`age` = 17 OR `person`.`name` IS NOT NULL", p);
assertPredicateEqual("`person`.`name` IS NOT NULL", p2);
assertPredicateEqual("`person`.`name` IS NOT NULL AND `person`.`age` = 18", p3);
}

@Test
Expand Down

0 comments on commit 28a7aaa

Please sign in to comment.