/*
 * Copyright 2016 requery.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.requery.sql.gen;

import io.requery.meta.Attribute;
import io.requery.query.Expression;
import io.requery.sql.QueryBuilder;

import java.util.Map;

import static io.requery.sql.Keyword.DEFAULT;
import static io.requery.sql.Keyword.INSERT;
import static io.requery.sql.Keyword.INTO;
import static io.requery.sql.Keyword.VALUES;

class InsertGenerator implements Generator<Map<Expression<?>, Object>> {

    @Override
    public void write(final Output output, Map<Expression<?>, Object> values) {
        QueryBuilder qb = output.builder();
        qb.keyword(INSERT, INTO);
        output.appendTables();
        qb.openParenthesis()
            .commaSeparated(values.entrySet(),
                new QueryBuilder.Appender<Map.Entry<Expression<?>, Object>>() {
                    @Override
                    public void append(QueryBuilder qb, Map.Entry<Expression<?>, Object> value) {
                        Expression<?> key = value.getKey();
                        switch (key.getExpressionType()) {
                            case ATTRIBUTE:
                                Attribute attribute = (Attribute) key;
                                if (attribute.isGenerated()) {
                                    throw new IllegalStateException();
                                }
                                qb.attribute(attribute);
                                break;
                            default:
                                qb.append(key.getName()).space();
                                break;
                        }
                    }
                })
            .closeParenthesis()
            .space();

            if (!values.isEmpty()) {
                qb.keyword(VALUES)
                .openParenthesis()
                .commaSeparated(values.entrySet(),
                    new QueryBuilder.Appender<Map.Entry<Expression<?>, Object>>() {
                        @Override
                        public void append(QueryBuilder qb, Map.Entry<Expression<?>, Object> value) {
                            output.appendConditionValue(value.getKey(), value.getValue());
                        }
                    })
                .closeParenthesis();
            } else {
                qb.keyword(DEFAULT, VALUES);
            }
    }
}

@@ -1200,24 +1200,119 @@
 ndTables();%0A
+%0A        if (values.isEmpty()) %7B%0A            qb.keyword(DEFAULT, VALUES);%0A        %7D else %7B%0A    
         qb.o
@@ -1320,32 +1320,36 @@
 enParenthesis()%0A
+    
             .com
@@ -2298,32 +2298,36 @@
  %7D)%0A            
+    
 .closeParenthesi
@@ -2342,16 +2342,20 @@
         
+    
 .space()
@@ -2361,49 +2361,8 @@
 );%0A%0A
-            if (!values.isEmpty()) %7B%0A    
     
@@ -2400,20 +2400,16 @@
         
-    
 .openPar
@@ -2411,36 +2411,32 @@
 enParenthesis()%0A
-    
             .com
@@ -2474,36 +2474,32 @@
                 
-  
-  
 new QueryBuilder
@@ -2558,36 +2558,32 @@
                 
-    
 @Override%0A      
@@ -2568,36 +2568,32 @@
       @Override%0A
-    
                 
@@ -2694,28 +2694,24 @@
             
-    
 output.appen
@@ -2773,36 +2773,32 @@
                 
-    
 %7D%0A              
@@ -2795,35 +2795,27 @@
             
- 
-   %7D)%0A    
+%7D)%0A
             
@@ -2839,78 +2839,8 @@
 ();%0A
-            %7D else %7B%0A                qb.keyword(DEFAULT, VALUES);%0A    
     

Resolve #341 Fix empty expression list in empty insert statement