public static final String CREATED_AT = "created_at"; public static final String REMOTE_ID = "remote_id"; public static final String UPDATED_AT = "updated_at"; static final String CREATE_ENTRY_TYPES = "CREATE TABLE "
    + escape(TABLE_ENTRY_TYPES) + " ("
    + declareField(REMOTE_ID, "STRING", false, ", ")
    + declareField("type_id", "STRING", false, ", ")
    + "UNIQUE(" + escape(REMOTE_ID) + ")"
    + ");"; static final String CREATE_SYNC_INFO = "CREATE TABLE "
    + escape(TABLE_SYNC_INFO) + " ("
    + declareField("token", "STRING", false, ", ")
    + declareField("last_sync_ts", "TIMESTAMP", false, " DEFAULT CURRENT_TIMESTAMP")
    + ");"; static final String TABLE_ASSETS = "assets"; static final String TABLE_ENTRY_TYPES = "entry_types"; static final String TABLE_LINKS = "links"; static final String TABLE_SYNC_INFO = "sync_info"; static int assetColumnIndex(String name) {
  int i = ASSET_COLUMN_INDEXES.indexOf(name);
  if (i == -1) {
    throw new IllegalArgumentException("Invalid asset column name '" + name + '.');
  }
  return RESOURCE_COLUMNS.length + i;
} static String createAssets(String locale) {
  return "CREATE TABLE " + escape(localizeName(TABLE_ASSETS, locale)) + " ("
      + StringUtils.join(RESOURCE_COLUMNS, ", ") + ","
      + declareField(Asset.Fields.URL, "STRING", true, ", ")
      + declareField(Asset.Fields.MIME_TYPE, "STRING", true, ", ")
      + declareField(Asset.Fields.TITLE, "STRING", true, ", ")
      + declareField(Asset.Fields.DESCRIPTION, "STRING", true, ", ")
      + declareField(Asset.Fields.FILE, "BLOB", true, null)
      + ");";
} static String createLinks(String locale) {
  return "CREATE TABLE " + escape(localizeName(TABLE_LINKS, locale)) + " ("
      + declareField("parent", "STRING", false, ", ")
      + declareField("child", "STRING", false, ", ")
      + declareField("field", "STRING", false, ", ")
      + declareField("is_asset", "INT", false, ", ")
      + declareField("position", "INT", false, ", ")
      + "UNIQUE ("
      + escape("parent") + ", "
      + escape("child") + ", "
      + escape("field") + ", "
      + escape("is_asset") + ", "
      + escape("position") + ")"
      + ");";
} static String escape(String name) {
  return String.format("`%s`", name);
} static String localizeName(String name, String locale) {
  return String.format("%s$%s", name, locale);
} static int resourceColumnIndex(String name) {
  int i = RESOURCE_COLUMN_INDEXES.indexOf(name);
  if (i == -1) {
    throw new IllegalArgumentException("Invalid resource column name '" + name + '.');
  }
  return i;
}
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.io.FileUtils;
import static com.contentful.vault.BaseFields.CREATED_AT;
import static com.contentful.vault.BaseFields.REMOTE_ID;
import static com.contentful.vault.BaseFields.UPDATED_AT;
import static com.contentful.vault.Sql.CREATE_ENTRY_TYPES;
import static com.contentful.vault.Sql.CREATE_SYNC_INFO;
import static com.contentful.vault.Sql.TABLE_ASSETS;
import static com.contentful.vault.Sql.TABLE_ENTRY_TYPES;
import static com.contentful.vault.Sql.TABLE_LINKS;
import static com.contentful.vault.Sql.TABLE_SYNC_INFO;
import static com.contentful.vault.Sql.assetColumnIndex;
import static com.contentful.vault.Sql.createAssets;
import static com.contentful.vault.Sql.createLinks;
import static com.contentful.vault.Sql.escape;
import static com.contentful.vault.Sql.localizeName;
import static com.contentful.vault.Sql.resourceColumnIndex;
/*
 * Copyright (C) 2018 Contentful GmbH
 *
 * 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.contentful.vault;



final class SqliteHelper extends SQLiteOpenHelper {
  private final Context context;

  private final SpaceHelper spaceHelper;

  public SqliteHelper(Context context, SpaceHelper spaceHelper) {
    super(context, spaceHelper.getDatabaseName(), null, spaceHelper.getDatabaseVersion());
    this.context = context.getApplicationContext();
    this.spaceHelper = spaceHelper;
    copyDatabase();
  }

  @Override public void onCreate(SQLiteDatabase db) {
    db.beginTransaction();
    try {
      execCreate(spaceHelper, db);
      db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
    }
  }

  @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (spaceHelper.getCopyPath() == null) {
      deleteTables(db);
      onCreate(db);
    }
  }

  static void execCreate(SpaceHelper helper, SQLiteDatabase db) {
    db.execSQL(CREATE_ENTRY_TYPES);
    db.execSQL(CREATE_SYNC_INFO);
    for (String code : helper.getLocales()) {
      db.execSQL(createAssets(code));