<commit_before>/*
 * Copyright © 2013, 2016 IBM Corp. All rights reserved.
 *
 * Original iOS version by  <NAME>, ported to Android by <NAME>
 * Copyright © 2012 Couchbase, Inc. All rights reserved.
 *
 * Modifications for this distribution by Cloudant, Inc., Copyright © 2013 Cloudant, Inc.
 *
 * 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.cloudant.sync.internal.sqlite;

import com.cloudant.sync.documentstore.encryption.KeyProvider;
import com.cloudant.sync.documentstore.encryption.NullKeyProvider;
import com.cloudant.sync.internal.documentstore.migrations.Migration;
import com.cloudant.sync.internal.util.DatabaseUtils;
import com.cloudant.sync.internal.util.Misc;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Factory class to create {@link SQLDatabase}, and update schema
 *
 * @api_private
 */
public class SQLDatabaseFactory {

    public static final boolean FTS_AVAILABLE;
    private static final String FTS_CHECK_TABLE_NAME = "_t_cloudant_sync_query_fts_check";
    private final static Logger logger = Logger.getLogger(SQLDatabaseFactory.class.getCanonicalName());

    static {
        FTS_AVAILABLE = isFtsAvailable();
    }

    private static boolean isFtsAvailable() {
        SQLDatabase tempInMemoryDB = null;
        try {
            tempInMemoryDB = internalOpenSQLDatabase(null, new NullKeyProvider());
            tempInMemoryDB.beginTransaction();
            try {
                tempInMemoryDB.execSQL(String.format("CREATE VIRTUAL TABLE %s USING FTS4 ( col )",
                        FTS_CHECK_TABLE_NAME));
                return true;
            } finally {
                // End the transaction and rollback the virtual table we created because we never
                // set transaction success.
                tempInMemoryDB.endTransaction();
            }
        } catch (SQLException sqle) {
            return false;
        } finally {
            if (tempInMemoryDB != null) {
                tempInMemoryDB.close();
            }
        }
    }

    /**
     * <p>
     * Open or create a database file, and return a connection to it, optionally backed by a<commit_msg>Drop the FTS table and commit the transaction instead of rolling back.<commit_after>/*
 * Copyright © 2013, 2016 IBM Corp. All rights reserved.
 *
 * Original iOS version by  <NAME>, ported to Android by <NAME>
 * Copyright © 2012 Couchbase, Inc. All rights reserved.
 *
 * Modifications for this distribution by Cloudant, Inc., Copyright © 2013 Cloudant, Inc.
 *
 * 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.cloudant.sync.internal.sqlite;

import com.cloudant.sync.documentstore.encryption.KeyProvider;
import com.cloudant.sync.documentstore.encryption.NullKeyProvider;
import com.cloudant.sync.internal.documentstore.migrations.Migration;
import com.cloudant.sync.internal.util.DatabaseUtils;
import com.cloudant.sync.internal.util.Misc;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Factory class to create {@link SQLDatabase}, and update schema
 *
 * @api_private
 */
public class SQLDatabaseFactory {

    public static final boolean FTS_AVAILABLE;
    private static final String FTS_CHECK_TABLE_NAME = "_t_cloudant_sync_query_fts_check";
    private final static Logger logger = Logger.getLogger(SQLDatabaseFactory.class.getCanonicalName());

    static {
        FTS_AVAILABLE = isFtsAvailable();
    }

    private static boolean isFtsAvailable() {
        SQLDatabase tempInMemoryDB = null;
        try {
            tempInMemoryDB = internalOpenSQLDatabase(null, new NullKeyProvider());
            tempInMemoryDB.beginTransaction();
            try {
                tempInMemoryDB.execSQL(String.format("CREATE VIRTUAL TABLE %s USING FTS4 ( col )",
                        FTS_CHECK_TABLE_NAME));
                tempInMemoryDB.execSQL(String.format("DROP TABLE %s",
                    FTS_CHECK_TABLE_NAME));
                return true;
            } finally {
                // Some Android devices crashed when we used to roll back the transaction. Now we
                // drop the table if it was created and then commit it regardless.
                tempInMemoryDB.setTransactionSuccessful();
                tempInMemoryDB.endTransaction();
            }
        } catch (SQLException sqle) {
            return false;
        } finally {
            if (tempInMemoryDB != null) {
                tempInMemoryDB.close();
            }
        }
    }

    /**
     * <p>
     * Open or create a database file, and return a connection to it, optionally backed by a
{'fraction_non_alphanumeric': 0.08148148148148149, 'fraction_numerical': 0.007235142118863049, 'mean_word_length': 2.855245683930943, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5049174', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1501, 'n_words': 513}