2012-10-09 20:51:00 +00:00
|
|
|
package com.dougkeen.bart.data;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
2013-03-24 23:11:43 +00:00
|
|
|
|
|
|
|
import com.dougkeen.bart.BartRunnerApplication;
|
|
|
|
import com.dougkeen.bart.model.StationPair;
|
2012-10-09 20:51:00 +00:00
|
|
|
|
|
|
|
public class DatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
|
|
private static final String DATABASE_NAME = "bart.dougkeen.db";
|
2013-03-24 23:11:43 +00:00
|
|
|
private static final int DATABASE_VERSION = 6;
|
2012-10-09 20:51:00 +00:00
|
|
|
|
|
|
|
public static final String FAVORITES_TABLE_NAME = "Favorites";
|
|
|
|
|
2013-03-24 23:11:43 +00:00
|
|
|
private BartRunnerApplication app;
|
|
|
|
|
2012-10-09 20:51:00 +00:00
|
|
|
public DatabaseHelper(Context context) {
|
|
|
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
2013-03-24 23:11:43 +00:00
|
|
|
app = (BartRunnerApplication) context.getApplicationContext();
|
2012-10-09 20:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createFavoritesTable(SQLiteDatabase db) {
|
|
|
|
db.execSQL("CREATE TABLE IF NOT EXISTS " + FAVORITES_TABLE_NAME + " ("
|
|
|
|
+ RoutesColumns._ID.getColumnDef() + " PRIMARY KEY, "
|
|
|
|
+ RoutesColumns.FROM_STATION.getColumnDef() + ", "
|
|
|
|
+ RoutesColumns.TO_STATION.getColumnDef() + ", "
|
|
|
|
+ RoutesColumns.FARE.getColumnDef() + ", "
|
|
|
|
+ RoutesColumns.FARE_LAST_UPDATED.getColumnDef() + ", "
|
|
|
|
+ RoutesColumns.AVERAGE_TRIP_SAMPLE_COUNT.getColumnDef() + ", "
|
|
|
|
+ RoutesColumns.AVERAGE_TRIP_LENGTH.getColumnDef() + ");");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
db.beginTransaction();
|
|
|
|
try {
|
|
|
|
createFavoritesTable(db);
|
|
|
|
|
2013-03-24 23:11:43 +00:00
|
|
|
Cursor query = db.query(FAVORITES_TABLE_NAME, RoutesColumns.all(),
|
|
|
|
null, null, null, null, null);
|
2012-10-09 20:51:00 +00:00
|
|
|
|
2013-03-24 23:11:43 +00:00
|
|
|
List<StationPair> favorites = new ArrayList<StationPair>();
|
2012-10-09 20:51:00 +00:00
|
|
|
|
2013-03-24 23:11:43 +00:00
|
|
|
while (query.moveToNext()) {
|
|
|
|
favorites.add(StationPair.createFromCursor(query));
|
|
|
|
}
|
2012-10-09 20:51:00 +00:00
|
|
|
|
2013-03-24 23:11:43 +00:00
|
|
|
query.close();
|
2012-10-09 20:51:00 +00:00
|
|
|
|
2013-03-24 23:11:43 +00:00
|
|
|
new FavoritesPersistence(app).persist(favorites);
|
2012-10-09 20:51:00 +00:00
|
|
|
|
2013-03-24 23:11:43 +00:00
|
|
|
db.execSQL("DROP TABLE " + FAVORITES_TABLE_NAME);
|
2012-10-09 20:51:00 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|