<commit_before>/*
 * 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);
            }
    }
}
<commit_msg>Resolve #341 Fix empty expression list in empty insert statement<commit_after>/*
 * 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();

        if (values.isEmpty()) {
            qb.keyword(DEFAULT, VALUES);
        } else {
            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();

            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();
        }
    }
}

{'fraction_non_alphanumeric': 0.09582267689684569, 'fraction_numerical': 0.003239556692242114, 'mean_word_length': 1.6152474364690148, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4228803', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1364, 'n_words': 373}