<commit_before>/*
   forsuredbsqlitelib, sqlite library for the forsuredb project

   Copyright 2015 <NAME>

   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 com.fsryan.forsuredb.sqlitelib;

import com.fsryan.forsuredb.api.migration.Migration;
import com.fsryan.forsuredb.api.migration.MigrationSet;
import com.fsryan.forsuredb.api.sqlgeneration.DBMSIntegrator;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.google.common.base.Strings.isNullOrEmpty;

public class SqlGenerator implements DBMSIntegrator {

    @VisibleForTesting
    /*package*/ static final String EMPTY_SQL = ";";
    private static final Set<String> nonInsertableColumns = Sets.newHashSet("_id", "created", "modified");

    public SqlGenerator() {}

    @Override
    public List<String> generateMigrationSql(MigrationSet migrationSet) {
        if (migrationSet == null || !migrationSet.containsMigrations() || migrationSet.getTargetSchema() == null) {
            return new ArrayList<>();
        }

        QueryGeneratorFactory qgf = new QueryGeneratorFactory(migrationSet);
        List<String> sqlList = new ArrayList<>();
        for (Migration m : migrationSet.getOrderedMigrations()) {
            sqlList.addAll(qgf.getFor(m, migrationSet.getTargetSchema()).generate());
        }

        return sqlList;
    }

    @Override
    public String singleRowInsertionSql(String tableName, Map<String, String> columnValueMap) {
        if (isNullOrEmpty(tableName) || columnValueMap == null || columnValueMap.isEmpty()) {
            return EMPTY_SQL;
        }

        final StringBuilder queryBuf = new StringBuilder("INSERT INTO " + tableName + " (");
        final StringBuilder valueBuf = new StringBuilder();

        for (Map.Entry<String, String> colValEntry : columnValueMap.entrySet()) {
            final String columnName = colValEntry.getKey();
            if (columnName.isEmpty() || nonInsertableColumns.contains(columnName)) {
                continue;   // <-- never insert an _id column
            }
            final String val = colValEntry.getValue();<commit_msg>slight refactor of names<commit_after>/*
   forsuredbsqlitelib, sqlite library for the forsuredb project

   Copyright 2015 <NAME>

   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 com.fsryan.forsuredb.sqlitelib;

import com.fsryan.forsuredb.api.migration.Migration;
import com.fsryan.forsuredb.api.migration.MigrationSet;
import com.fsryan.forsuredb.api.sqlgeneration.DBMSIntegrator;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.google.common.base.Strings.isNullOrEmpty;

public class SqlGenerator implements DBMSIntegrator {

    @VisibleForTesting
    /*package*/ static final String EMPTY_SQL = ";";
    private static final Set<String> columnExclusionFilter = Sets.newHashSet("_id", "created", "modified");

    public SqlGenerator() {}

    @Override
    public List<String> generateMigrationSql(MigrationSet migrationSet) {
        if (migrationSet == null || !migrationSet.containsMigrations() || migrationSet.getTargetSchema() == null) {
            return new ArrayList<>();
        }

        QueryGeneratorFactory qgf = new QueryGeneratorFactory(migrationSet);
        List<String> sqlList = new ArrayList<>();
        for (Migration m : migrationSet.getOrderedMigrations()) {
            sqlList.addAll(qgf.getFor(m, migrationSet.getTargetSchema()).generate());
        }

        return sqlList;
    }

    @Override
    public String newSingleRowInsertionSql(String tableName, Map<String, String> columnValueMap) {
        if (isNullOrEmpty(tableName) || columnValueMap == null || columnValueMap.isEmpty()) {
            return EMPTY_SQL;
        }

        final StringBuilder queryBuf = new StringBuilder("INSERT INTO " + tableName + " (");
        final StringBuilder valueBuf = new StringBuilder();

        for (Map.Entry<String, String> colValEntry : columnValueMap.entrySet()) {
            final String columnName = colValEntry.getKey();
            if (columnName.isEmpty() || columnExclusionFilter.contains(columnName)) {
                continue;   // <-- never insert an _id column
            }
            final String val = colValEntry.getValue();
{'fraction_non_alphanumeric': 0.09388646288209607, 'fraction_numerical': 0.002911208151382824, 'mean_word_length': 3.4618506493506493, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4554316', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1446, 'n_words': 426}