]> source.dussan.org Git - jackcess.git/commitdiff
Fix missing column names in AppendQuery SQL strings. Fixes #131
authorJames Ahlborn <jtahlborn@yahoo.com>
Tue, 12 Jan 2016 22:41:43 +0000 (22:41 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Tue, 12 Jan 2016 22:41:43 +0000 (22:41 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@977 f203690c-595d-4dc9-a70b-905162fa7fd2

src/changes/changes.xml
src/main/java/com/healthmarketscience/jackcess/impl/query/AppendQueryImpl.java
src/main/java/com/healthmarketscience/jackcess/query/AppendQuery.java
src/test/java/com/healthmarketscience/jackcess/query/QueryTest.java

index 621dd17c35e8a0b35f9345a4ceacf0cfff8d0907..208fb9c1b1a9604b7975cf0d23af1704f22bf19e 100644 (file)
@@ -4,6 +4,11 @@
     <author email="javajedi@users.sf.net">Tim McCune</author>
   </properties>
   <body>
+    <release version="2.1.4" date="TBD">
+      <action dev="jahlborn" type="fix" system="SourceForge2" issue="131">
+        Fix missing column names in AppendQuery SQL strings.
+      </action>
+    </release>
     <release version="2.1.3" date="2015-12-04">
       <action dev="jahlborn" type="fix" system="SourceForge2" issue="127">
         Throw a prettier exception when maxing out the row size during row
index 8fb3296421e438675b41c1d8e545a5a1b21ce0b6..140fde34c9282a856e917a5f179d0e87b1043a8d 100644 (file)
@@ -40,6 +40,14 @@ public class AppendQueryImpl extends BaseSelectQueryImpl implements AppendQuery
     return getTypeRow().name1;
   }
 
+  public List<String> getTargetColumns() {
+    return new RowFormatter(getTargetRows()) {
+        @Override protected void format(StringBuilder builder, Row row) {
+          toOptionalQuotedExpr(builder, row.name2, true);
+        }
+      }.format();
+  }
+
   public String getRemoteDbPath() {
     return getTypeRow().name2;
   }
@@ -48,6 +56,14 @@ public class AppendQueryImpl extends BaseSelectQueryImpl implements AppendQuery
     return getTypeRow().expression;
   }
 
+  public List<String> getValues() {
+    return new RowFormatter(getValueRows()) {
+        @Override protected void format(StringBuilder builder, Row row) {
+          builder.append(row.expression);
+        }
+      }.format();
+  }
+
   protected List<Row> getValueRows() {
     return filterRowsByFlag(super.getColumnRows(), APPEND_VALUE_FLAG);
   }
@@ -57,12 +73,12 @@ public class AppendQueryImpl extends BaseSelectQueryImpl implements AppendQuery
     return filterRowsByNotFlag(super.getColumnRows(), APPEND_VALUE_FLAG);
   }
 
-  public List<String> getValues() {
-    return new RowFormatter(getValueRows()) {
-        @Override protected void format(StringBuilder builder, Row row) {
-          builder.append(row.expression);
-        }
-      }.format();
+  protected List<Row> getTargetRows() {
+    return new RowFilter() {
+        @Override protected boolean keep(Row row) {
+          return (row.name2 != null);
+        }      
+    }.filter(super.getColumnRows());
   }
 
   @Override
@@ -70,6 +86,10 @@ public class AppendQueryImpl extends BaseSelectQueryImpl implements AppendQuery
   {
     builder.append("INSERT INTO ");
     toOptionalQuotedExpr(builder, getTargetTable(), true);
+    List<String> columns = getTargetColumns();
+    if(!columns.isEmpty()) {
+      builder.append(" (").append(columns).append(')');
+    }
     toRemoteDb(builder, getRemoteDbPath(), getRemoteDbType());
     builder.append(NEWLINE);
     List<String> values = getValues();
index a62c28668b5bb7e260ed9af5400e95aab0fd6e9f..ab4935ceb7234fff812ce30abc1b040c438ba228 100644 (file)
@@ -30,6 +30,8 @@ public interface AppendQuery extends BaseSelectQuery
 
   public String getTargetTable();
 
+  public List<String> getTargetColumns();
+
   public String getRemoteDbPath();
 
   public String getRemoteDbType();
index 3af50ec22f2388227dd6488d2043e887f63394d1..b9a284c25e093d9b758484775d225ab082688d62 100644 (file)
@@ -206,7 +206,7 @@ public class QueryTest extends TestCase
               "WHERE (((Table1.col1)>\"blah\"));"));
       expectedQueries.put(
           "AppendQuery",multiline(
-              "INSERT INTO Table3",
+              "INSERT INTO Table3 (col2, col2, col3)",
               "SELECT [Table1].[col2], [Table2].[col2], [Table2].[col3]",
               "FROM Table3, Table1 INNER JOIN Table2 ON [Table1].[col1]=[Table2].[col1];"));
       expectedQueries.put(
@@ -257,6 +257,27 @@ public class QueryTest extends TestCase
     }
   }
 
+  public void testAppendQuery() throws Exception
+  {
+    AppendQuery query = (AppendQuery)newQuery(
+        Query.Type.APPEND, null, "Table2",
+        // newRow(TABLE_ATTRIBUTE, null, "Table1", null),
+        newRow(COLUMN_ATTRIBUTE, "54", APPEND_VALUE_FLAG, null, null),
+        newRow(COLUMN_ATTRIBUTE, "'hello'", APPEND_VALUE_FLAG, null, null));
+
+    assertEquals(multiline("INSERT INTO Table2", 
+                           "VALUES (54, 'hello');"), query.toSQLString());
+
+    query = (AppendQuery)newQuery(
+        Query.Type.APPEND, null, "Table2",
+        // newRow(TABLE_ATTRIBUTE, null, "Table1", null),
+        newRow(COLUMN_ATTRIBUTE, "54", APPEND_VALUE_FLAG, null, "ID"),
+        newRow(COLUMN_ATTRIBUTE, "'hello'", APPEND_VALUE_FLAG, null, "Field 3"));
+
+    assertEquals(multiline("INSERT INTO Table2 (ID, [Field 3])", 
+                           "VALUES (54, 'hello');"), query.toSQLString());
+  }
+
   private void doTestColumns(SelectQuery query) throws Exception
   {
     addRows(query, newRow(COLUMN_ATTRIBUTE, "Table1.id", null, null));