Basic testing done
[Sunshine.git] / app / src / main / java / uk / me / njae / sunshine / data / WeatherDbHelper.java
1 package uk.me.njae.sunshine.data;
2
3 import android.content.Context;
4 import android.database.sqlite.SQLiteDatabase;
5 import android.database.sqlite.SQLiteOpenHelper;
6
7 import uk.me.njae.sunshine.data.WeatherContract.LocationEntry;
8 import uk.me.njae.sunshine.data.WeatherContract.WeatherEntry;
9
10
11 /**
12 * Manages a local database for weather data.
13 */
14 public class WeatherDbHelper extends SQLiteOpenHelper {
15
16 // If you change the database schema, you must increment the database version.
17 private static final int DATABASE_VERSION = 1;
18
19 public static final String DATABASE_NAME = "weather.db";
20
21 public WeatherDbHelper(Context context) {
22 super(context, DATABASE_NAME, null, DATABASE_VERSION);
23 }
24
25 @Override
26 public void onCreate(SQLiteDatabase sqLiteDatabase) {
27 // Create a table to hold locations. A location consists of the string supplied in the
28 // location setting, the city name, and the latitude and longitude
29 final String SQL_CREATE_LOCATION_TABLE = "CREATE TABLE " + LocationEntry.TABLE_NAME + " (" +
30 LocationEntry._ID + " INTEGER PRIMARY KEY," +
31 LocationEntry.COLUMN_LOCATION_SETTING + " TEXT UNIQUE NOT NULL, " +
32 LocationEntry.COLUMN_CITY_NAME + " TEXT NOT NULL, " +
33 LocationEntry.COLUMN_COORD_LAT + " REAL NOT NULL, " +
34 LocationEntry.COLUMN_COORD_LONG + " REAL NOT NULL, " +
35 "UNIQUE (" + LocationEntry.COLUMN_LOCATION_SETTING +") ON CONFLICT IGNORE"+
36 " );";
37
38 final String SQL_CREATE_WEATHER_TABLE = "CREATE TABLE " + WeatherEntry.TABLE_NAME + " (" +
39 // Why AutoIncrement here, and not above?
40 // Unique keys will be auto-generated in either case. But for weather
41 // forecasting, it's reasonable to assume the user will want information
42 // for a certain date and all dates *following*, so the forecast data
43 // should be sorted accordingly.
44 WeatherEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
45
46 // the ID of the location entry associated with this weather data
47 WeatherEntry.COLUMN_LOC_KEY + " INTEGER NOT NULL, " +
48 WeatherEntry.COLUMN_DATETEXT + " TEXT NOT NULL, " +
49 WeatherEntry.COLUMN_SHORT_DESC + " TEXT NOT NULL, " +
50 WeatherEntry.COLUMN_WEATHER_ID + " INTEGER NOT NULL," +
51
52 WeatherEntry.COLUMN_MIN_TEMP + " REAL NOT NULL, " +
53 WeatherEntry.COLUMN_MAX_TEMP + " REAL NOT NULL, " +
54
55 WeatherEntry.COLUMN_HUMIDITY + " REAL NOT NULL, " +
56 WeatherEntry.COLUMN_PRESSURE + " REAL NOT NULL, " +
57 WeatherEntry.COLUMN_WIND_SPEED + " REAL NOT NULL, " +
58 WeatherEntry.COLUMN_DEGREES + " REAL NOT NULL, " +
59
60 // Set up the location column as a foreign key to location table.
61 " FOREIGN KEY (" + WeatherEntry.COLUMN_LOC_KEY + ") REFERENCES " +
62 LocationEntry.TABLE_NAME + " (" + LocationEntry._ID + "), " +
63
64 // To assure the application have just one weather entry per day
65 // per location, it's created a UNIQUE constraint with REPLACE strategy
66 " UNIQUE (" + WeatherEntry.COLUMN_DATETEXT + ", " +
67 WeatherEntry.COLUMN_LOC_KEY + ") ON CONFLICT REPLACE);";
68
69 sqLiteDatabase.execSQL(SQL_CREATE_LOCATION_TABLE);
70 sqLiteDatabase.execSQL(SQL_CREATE_WEATHER_TABLE);
71 }
72
73 @Override
74 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
75 sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + LocationEntry.TABLE_NAME);
76 sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WeatherEntry.TABLE_NAME);
77 onCreate(sqLiteDatabase);
78 }
79 }