2323import java .util .List ;
2424import java .util .StringJoiner ;
2525import java .util .function .BiConsumer ;
26+ import java .util .function .Consumer ;
2627import java .util .function .Function ;
2728import java .util .function .Supplier ;
2829import java .util .regex .Matcher ;
5657import org .seasar .doma .internal .jdbc .sql .node .HavingClauseNode ;
5758import org .seasar .doma .internal .jdbc .sql .node .IfBlockNode ;
5859import org .seasar .doma .internal .jdbc .sql .node .IfNode ;
60+ import org .seasar .doma .internal .jdbc .sql .node .LiteralVariableNode ;
5961import org .seasar .doma .internal .jdbc .sql .node .LogicalOperatorNode ;
6062import org .seasar .doma .internal .jdbc .sql .node .OptionClauseNode ;
6163import org .seasar .doma .internal .jdbc .sql .node .OrderByClauseNode ;
6870import org .seasar .doma .internal .jdbc .sql .node .SqlLocation ;
6971import org .seasar .doma .internal .jdbc .sql .node .UpdateClauseNode ;
7072import org .seasar .doma .internal .jdbc .sql .node .UpdateStatementNode ;
73+ import org .seasar .doma .internal .jdbc .sql .node .ValueNode ;
7174import org .seasar .doma .internal .jdbc .sql .node .WhereClauseNode ;
7275import org .seasar .doma .internal .jdbc .sql .node .WhitespaceNode ;
7376import org .seasar .doma .internal .jdbc .sql .node .WordNode ;
8184import org .seasar .doma .jdbc .SqlNode ;
8285import org .seasar .doma .jdbc .SqlNodeVisitor ;
8386import org .seasar .doma .message .Message ;
87+ import org .seasar .doma .wrapper .WrapperVisitor ;
8488
8589/**
8690 * @author taedium
@@ -199,21 +203,45 @@ public Void visitCommentNode(CommentNode node, Context p) {
199203
200204 @ Override
201205 public Void visitBindVariableNode (BindVariableNode node , Context p ) {
206+ return visitValueNode (node , p , p ::addBindValue );
207+ }
208+
209+ @ Override
210+ public Void visitLiteralVariableNode (final LiteralVariableNode node ,
211+ Context p ) {
212+ Consumer <Scalar <?, ?>> validator = (scalar ) -> {
213+ Object value = scalar .get ();
214+ if (value == null ) {
215+ return ;
216+ }
217+ String text = value .toString ();
218+ if (text .indexOf ('\'' ) > -1 ) {
219+ SqlLocation location = node .getLocation ();
220+ throw new JdbcException (Message .DOMA2224 , location .getSql (),
221+ location .getLineNumber (), location .getPosition (),
222+ node .getText ());
223+ }
224+ };
225+ return visitValueNode (node , p , validator .andThen (p ::addLiteralValue ));
226+ }
227+
228+ protected Void visitValueNode (ValueNode node , Context p ,
229+ Consumer <Scalar <?, ?>> valueHandler ) {
202230 SqlLocation location = node .getLocation ();
203231 String name = node .getVariableName ();
204232 EvaluationResult result = p .evaluate (location , name );
205233 Object value = result .getValue ();
206234 Class <?> valueClass = result .getValueClass ();
207235 p .setAvailable (true );
208236 if (node .isWordNodeIgnored ()) {
209- handleSingleBindVarialbeNode (node , p , value , valueClass );
237+ handleSingleValueNode (node , p , value , valueClass , valueHandler );
210238 } else if (node .isParensNodeIgnored ()) {
211239 ParensNode parensNode = node .getParensNode ();
212240 OtherNode openedFragmentNode = parensNode .getOpenedFragmentNode ();
213241 openedFragmentNode .accept (this , p );
214242 if (Iterable .class .isAssignableFrom (valueClass )) {
215- handleIterableBindVarialbeNode (node , p , (Iterable <?>) value ,
216- valueClass );
243+ handleIterableValueNode (node , p , (Iterable <?>) value ,
244+ valueClass , valueHandler );
217245 } else {
218246 throw new JdbcException (Message .DOMA2112 , location .getSql (),
219247 location .getLineNumber (), location .getPosition (),
@@ -273,16 +301,17 @@ protected boolean startsWithClauseKeyword(String fragment) {
273301 return matcher .lookingAt ();
274302 }
275303
276- protected Void handleSingleBindVarialbeNode ( BindVariableNode node ,
277- Context p , Object value , Class <?> valueClass ) {
304+ protected Void handleSingleValueNode ( ValueNode node , Context p ,
305+ Object value , Class <?> valueClass , Consumer < Scalar <?, ?>> consumer ) {
278306 Supplier <Scalar <?, ?>> supplier = wrap (node .getLocation (),
279307 node .getText (), value , valueClass );
280- p . addBindValue (supplier .get ());
308+ consumer . accept (supplier .get ());
281309 return null ;
282310 }
283311
284- protected void handleIterableBindVarialbeNode (BindVariableNode node ,
285- Context p , Iterable <?> values , Class <?> valueClass ) {
312+ protected void handleIterableValueNode (ValueNode node , Context p ,
313+ Iterable <?> values , Class <?> valueClass ,
314+ Consumer <Scalar <?, ?>> consumer ) {
286315 int index = 0 ;
287316 for (Object v : values ) {
288317 if (v == null ) {
@@ -293,7 +322,7 @@ protected void handleIterableBindVarialbeNode(BindVariableNode node,
293322 }
294323 Supplier <Scalar <?, ?>> supplier = wrap (node .getLocation (),
295324 node .getText (), v , v .getClass ());
296- p . addBindValue (supplier .get ());
325+ consumer . accept (supplier .get ());
297326 p .appendRawSql (", " );
298327 p .appendFormattedSql (", " );
299328 index ++;
@@ -635,7 +664,7 @@ public Void visitFragmentNode(FragmentNode node, Context p) {
635664
636665 @ Override
637666 public Void visitParensNode (ParensNode node , Context p ) {
638- if (node .isAttachedWithBindVariable ()) {
667+ if (node .isAttachedWithValue ()) {
639668 return null ;
640669 }
641670 Context context = new Context (p );
@@ -758,6 +787,15 @@ protected CharSequence getFormattedSqlBuf() {
758787 return formattedSqlBuf ;
759788 }
760789
790+ protected <BASIC , CONTAINER > void addLiteralValue (
791+ Scalar <BASIC , CONTAINER > scalar ) {
792+ String literal = scalar .getWrapper ().accept (
793+ config .getDialect ().getSqlLogFormattingVisitor (),
794+ formattingFunction , null );
795+ rawSqlBuf .append (literal );
796+ formattedSqlBuf .append (literal );
797+ }
798+
761799 protected <BASIC , CONTAINER > void addBindValue (
762800 Scalar <BASIC , CONTAINER > scalar ) {
763801 appendParameterInternal (new ScalarInParameter <BASIC , CONTAINER >(
@@ -819,4 +857,9 @@ public String toString() {
819857 return rawSqlBuf .toString ();
820858 }
821859 }
860+
861+ protected static class LiteralValueVisitor implements
862+ WrapperVisitor <String , Void , Void , RuntimeException > {
863+
864+ }
822865}
0 commit comments