Author: cbegin
Date: Sun Apr 12 04:09:28 2009
New Revision: 764280

URL: http://svn.apache.org/viewvc?rev=764280&view=rev
Log:
added support for <where> and <set> elements... dynamicSqlSource implementation 
complete, ready for parsing!

Added:
    
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/xml/dynamic/SetSqlNode.java
Modified:
    
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/xml/dynamic/WhereSqlNode.java
    
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/parser/xml/dynamic/DynamicSqlSourceTest.java

Added: 
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/xml/dynamic/SetSqlNode.java
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/xml/dynamic/SetSqlNode.java?rev=764280&view=auto
==============================================================================
--- 
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/xml/dynamic/SetSqlNode.java
 (added)
+++ 
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/xml/dynamic/SetSqlNode.java
 Sun Apr 12 04:09:28 2009
@@ -0,0 +1,56 @@
+package org.apache.ibatis.parser.xml.dynamic;
+
+import java.util.Map;
+
+public class SetSqlNode implements SqlNode {
+
+  private MixedSqlNode contents;
+
+  public SetSqlNode(MixedSqlNode contents) {
+    this.contents = contents;
+  }
+
+  public boolean apply(DynamicContext context) {
+    return contents.apply(new FilteredDynamicContext(context));
+  }
+
+
+  private static class FilteredDynamicContext extends DynamicContext {
+    private DynamicContext delegate;
+    private boolean filtered;
+
+    public FilteredDynamicContext(DynamicContext delegate) {
+      super(null);
+      this.delegate = delegate;
+      this.filtered = false;
+    }
+
+    public Map<String, Object> getBindings() {
+      return delegate.getBindings();
+    }
+
+    public void bind(String name, Object value) {
+      delegate.bind(name, value);
+    }
+
+    public void appendSql(String sql) {
+      if (!filtered) {
+        filtered = true;
+        String filteredSql = sql.trim().toUpperCase();
+        if (filteredSql.startsWith(",")) {
+          sql = sql.trim().substring(1).trim();
+        }
+        delegate.appendSql("SET");
+        delegate.appendSql(sql);
+      } else {
+        delegate.appendSql(sql);
+      }
+    }
+
+    public String getSql() {
+      return delegate.getSql();
+    }
+  }
+
+
+}

Modified: 
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/xml/dynamic/WhereSqlNode.java
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/xml/dynamic/WhereSqlNode.java?rev=764280&r1=764279&r2=764280&view=diff
==============================================================================
--- 
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/xml/dynamic/WhereSqlNode.java
 (original)
+++ 
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/main/java/org/apache/ibatis/parser/xml/dynamic/WhereSqlNode.java
 Sun Apr 12 04:09:28 2009
@@ -11,8 +11,7 @@
   }
 
   public boolean apply(DynamicContext context) {
-
-    return false;
+    return contents.apply(new FilteredDynamicContext(context));
   }
 
 
@@ -36,12 +35,18 @@
 
     public void appendSql(String sql) {
       if (!filtered) {
-        final String filteredSql = sql.trim().toUpperCase();
-        if (filteredSql.startsWith("AND")) {
+        filtered = true;
+        String filteredSql = sql.trim().toUpperCase();
+        if (filteredSql.startsWith("AND ")) {
+          sql = sql.trim().substring(3).trim();
+        } else if (filteredSql.startsWith("OR ")) {
+          sql = sql.trim().substring(2).trim();
         }
+        delegate.appendSql("WHERE");
+        delegate.appendSql(sql);
+      } else {
+        delegate.appendSql(sql);
       }
-      delegate.appendSql(sql);
-      filtered = true;
     }
 
     public String getSql() {

Modified: 
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/parser/xml/dynamic/DynamicSqlSourceTest.java
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/parser/xml/dynamic/DynamicSqlSourceTest.java?rev=764280&r1=764279&r2=764280&view=diff
==============================================================================
--- 
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/parser/xml/dynamic/DynamicSqlSourceTest.java
 (original)
+++ 
ibatis/trunk/java/ibatis-3/ibatis-3-core/src/test/java/org/apache/ibatis/parser/xml/dynamic/DynamicSqlSourceTest.java
 Sun Apr 12 04:09:28 2009
@@ -101,6 +101,96 @@
   }
 
   @Test
+  public void shouldPrefixWHEREInsteadOfANDForFirstCondition() throws 
Exception {
+    final String expected = "SELECT * FROM BLOG WHERE ID = ?";
+    DynamicSqlSource source = createDynamicSqlSource(
+        new TextSqlNode("SELECT * FROM BLOG"),
+        new WhereSqlNode(mixedContents(
+          new IfSqlNode("true",
+            mixedContents(new TextSqlNode("   and ID = ?  "))),
+          new IfSqlNode("false",
+            mixedContents(new TextSqlNode("   or NAME = ?  ")))
+        )));
+    BoundSql boundSql = source.getBoundSql(null);
+    assertEquals(expected, boundSql.getSql());
+  }
+
+  @Test
+  public void shouldPrefixWHEREInsteadOfORForSecondCondition() throws 
Exception {
+    final String expected = "SELECT * FROM BLOG WHERE NAME = ?";
+    DynamicSqlSource source = createDynamicSqlSource(
+        new TextSqlNode("SELECT * FROM BLOG"),
+        new WhereSqlNode(mixedContents(
+          new IfSqlNode("false",
+            mixedContents(new TextSqlNode("   and ID = ?  "))),
+          new IfSqlNode("true",
+            mixedContents(new TextSqlNode("   or NAME = ?  ")))
+        )));
+    BoundSql boundSql = source.getBoundSql(null);
+    assertEquals(expected, boundSql.getSql());
+  }
+
+  @Test
+  public void shouldPrefixWHEREInsteadOfANDForBothConditions() throws 
Exception {
+    final String expected = "SELECT * FROM BLOG WHERE ID = ? OR NAME = ?";
+    DynamicSqlSource source = createDynamicSqlSource(
+        new TextSqlNode("SELECT * FROM BLOG"),
+        new WhereSqlNode(mixedContents(
+          new IfSqlNode("true",
+            mixedContents(new TextSqlNode("   and ID = ?   "))),
+          new IfSqlNode("true",
+            mixedContents(new TextSqlNode("OR NAME = ?  ")))
+        )));
+    BoundSql boundSql = source.getBoundSql(null);
+    assertEquals(expected, boundSql.getSql());
+  }
+
+  @Test
+  public void shouldPrefixNoWhereClause() throws Exception {
+    final String expected = "SELECT * FROM BLOG";
+    DynamicSqlSource source = createDynamicSqlSource(
+        new TextSqlNode("SELECT * FROM BLOG"),
+        new WhereSqlNode(mixedContents(
+          new IfSqlNode("false",
+            mixedContents(new TextSqlNode("   and ID = ?   "))),
+          new IfSqlNode("false",
+            mixedContents(new TextSqlNode("OR NAME = ?  ")))
+        )));
+    BoundSql boundSql = source.getBoundSql(null);
+    assertEquals(expected, boundSql.getSql());
+  }
+
+  @Test
+  public void shouldPrefixSETInsteadOfCOMMAForBothConditions() throws 
Exception {
+    final String expected = "UPDATE BLOG SET ID = ? , NAME = ?";
+    DynamicSqlSource source = createDynamicSqlSource(
+        new TextSqlNode("UPDATE BLOG"),
+        new SetSqlNode(mixedContents(
+          new IfSqlNode("true",
+            mixedContents(new TextSqlNode("   , ID = ?   "))),
+          new IfSqlNode("true",
+            mixedContents(new TextSqlNode(", NAME = ?  ")))
+        )));
+    BoundSql boundSql = source.getBoundSql(null);
+    assertEquals(expected, boundSql.getSql());
+  }
+
+  @Test
+  public void shouldPrefixNoSetClause() throws Exception {
+    final String expected = "UPDATE BLOG";
+    DynamicSqlSource source = createDynamicSqlSource(
+        new TextSqlNode("UPDATE BLOG"),
+        new SetSqlNode(mixedContents(
+          new IfSqlNode("false",
+            mixedContents(new TextSqlNode("   , ID = ?   "))),
+          new IfSqlNode("false",
+            mixedContents(new TextSqlNode(", NAME = ?  ")))
+        )));
+    BoundSql boundSql = source.getBoundSql(null);
+    assertEquals(expected, boundSql.getSql());
+  }
+
+  @Test
   public void shouldIterateOnceForEachItemInCollection() throws Exception {
     final HashMap<String,String[]> parameterObject = new HashMap() {{
         put("array", new String[]{"one", "two", "three"});


Reply via email to