Back to project page androidata.
The source code is released under:
Apache License
If you think the Android project androidata listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.stanidesis.androidata; /*from www . j ava2 s.co m*/ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import com.stanidesis.androidata.column.Column; import com.stanidesis.androidata.table.ITable; /** * Created by Stanley Idesis on 8/15/14. */ public class AndroiDatabaseOpenHelper extends SQLiteOpenHelper implements AndroiDataUpgradeHelper { static final char SPACE = ' '; static final char COMMA = ','; static final char PERIOD = ','; static final char OPEN_PAREN = '('; static final char CLOSED_PAREN = ')'; static final String CREATE_TABLE = "CREATE TABLE"; static final String ALTER_TABLE = "ALTER TABLE"; static final String RENAME_TO = "RENAME TO"; static final String ADD_COLUMN = "ADD COLUMN"; private String mDatabaseName; private final ITable[] mTables; private SQLiteDatabase mUpgradeDatabase; AndroiDatabaseOpenHelper(Context context, String name, int version, ITable... tables) { super(context, name, null, version); mDatabaseName = name; mTables = tables; } @Override public void onCreate(SQLiteDatabase db) { if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "Creating tables"); } // Iterate over the available tables for (ITable table : mTables) { createTable(db, table); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { mUpgradeDatabase = db; for (ITable table : mTables) { if (table.versionIntroduced() > oldVersion) { if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "Creating table '" + table.getName() + "' during upgrade"); } createTable(db, table); } else { if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "Upgrading table '" + table.getName() + "'"); } table.onUpgrade(this, oldVersion, newVersion); } } } private void createTable(SQLiteDatabase db, ITable table) { StringBuilder tableCreate = new StringBuilder(CREATE_TABLE); tableCreate.append(SPACE) .append(table.getName()) .append(SPACE) .append(OPEN_PAREN); for (Column column : table.getColumns()) { tableCreate.append(column.getSQLCreateStatement()).append(COMMA); } tableCreate.setCharAt(tableCreate.length() - 1, CLOSED_PAREN); db.execSQL(tableCreate.toString()); if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "Executing raw SQL '" + tableCreate.toString() + "'"); } } /* * AndroiDataUpgradeHelper */ @Override public SQLiteDatabase getDatabase() { return mUpgradeDatabase; } @Override public void addColumn(ITable table, Column column) { StringBuilder sql = new StringBuilder(ALTER_TABLE).append(SPACE); sql.append(mDatabaseName).append(PERIOD).append(table.getName()).append(SPACE); sql.append(ADD_COLUMN).append(SPACE).append(column.getSQLCreateStatement()); getDatabase().execSQL(sql.toString()); if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "Executing raw SQL '" + sql.toString() + "' during upgrade of '" + table.getName() + "'"); } } @Override public void renameTableTo(String oldName, String newName) { StringBuilder sql = new StringBuilder(ALTER_TABLE).append(SPACE); sql.append(mDatabaseName).append(PERIOD).append(oldName).append(SPACE); sql.append(RENAME_TO).append(SPACE).append(newName); getDatabase().execSQL(sql.toString()); if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "Executing raw SQL '" + sql.toString() + "' during upgrade of '" + oldName + "'"); } } }