PhotoDatabase.java revision fcdcdd7627bcebe97284e200daee9e8e284aa7f4
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.photos.data;
17
18import android.content.Context;
19import android.database.sqlite.SQLiteDatabase;
20import android.database.sqlite.SQLiteOpenHelper;
21
22import com.android.photos.data.PhotoProvider.Accounts;
23import com.android.photos.data.PhotoProvider.Albums;
24import com.android.photos.data.PhotoProvider.Metadata;
25import com.android.photos.data.PhotoProvider.Photos;
26
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * Used in PhotoProvider to create and access the database containing
32 * information about photo and video information stored on the server.
33 */
34public class PhotoDatabase extends SQLiteOpenHelper {
35    @SuppressWarnings("unused")
36    private static final String TAG = PhotoDatabase.class.getSimpleName();
37    static final int DB_VERSION = 1;
38
39    private static final String SQL_CREATE_TABLE = "CREATE TABLE ";
40
41    private static final String[][] CREATE_PHOTO = {
42        { Photos._ID, "INTEGER PRIMARY KEY AUTOINCREMENT" },
43        // Photos.ACCOUNT_ID is a foreign key to Accounts._ID
44        { Photos.ACCOUNT_ID, "INTEGER NOT NULL" },
45        { Photos.WIDTH, "INTEGER NOT NULL" },
46        { Photos.HEIGHT, "INTEGER NOT NULL" },
47        { Photos.DATE_TAKEN, "INTEGER NOT NULL" },
48        // Photos.ALBUM_ID is a foreign key to Albums._ID
49        { Photos.ALBUM_ID, "INTEGER" },
50        { Photos.MIME_TYPE, "TEXT NOT NULL" },
51        { Photos.TITLE, "TEXT" },
52        { Photos.DATE_MODIFIED, "INTEGER" },
53        { Photos.ROTATION, "INTEGER" },
54    };
55
56    private static final String[][] CREATE_ALBUM = {
57        { Albums._ID, "INTEGER PRIMARY KEY AUTOINCREMENT" },
58        // Albums.ACCOUNT_ID is a foreign key to Accounts._ID
59        { Albums.ACCOUNT_ID, "INTEGER NOT NULL" },
60        // Albums.PARENT_ID is a foreign key to Albums._ID
61        { Albums.PARENT_ID, "INTEGER" },
62        { Albums.VISIBILITY, "INTEGER NOT NULL" },
63        { Albums.LOCATION_STRING, "TEXT" },
64        { Albums.TITLE, "TEXT NOT NULL" },
65        { Albums.SUMMARY, "TEXT" },
66        { Albums.DATE_PUBLISHED, "INTEGER" },
67        { Albums.DATE_MODIFIED, "INTEGER" },
68        createUniqueConstraint(Albums.PARENT_ID, Albums.TITLE),
69    };
70
71    private static final String[][] CREATE_METADATA = {
72        { Metadata._ID, "INTEGER PRIMARY KEY AUTOINCREMENT" },
73        // Metadata.PHOTO_ID is a foreign key to Photos._ID
74        { Metadata.PHOTO_ID, "INTEGER NOT NULL" },
75        { Metadata.KEY, "TEXT NOT NULL" },
76        { Metadata.VALUE, "TEXT NOT NULL" },
77        createUniqueConstraint(Metadata.PHOTO_ID, Metadata.KEY),
78    };
79
80    private static final String[][] CREATE_ACCOUNT = {
81        { Accounts._ID, "INTEGER PRIMARY KEY AUTOINCREMENT" },
82        { Accounts.ACCOUNT_NAME, "TEXT NOT NULL" },
83    };
84
85    @Override
86    public void onCreate(SQLiteDatabase db) {
87        createTable(db, Accounts.TABLE, getAccountTableDefinition());
88        createTable(db, Albums.TABLE, getAlbumTableDefinition());
89        createTable(db, Photos.TABLE, getPhotoTableDefinition());
90        createTable(db, Metadata.TABLE, getMetadataTableDefinition());
91    }
92
93    public PhotoDatabase(Context context, String dbName, int dbVersion) {
94        super(context, dbName, null, dbVersion);
95    }
96
97    public PhotoDatabase(Context context, String dbName) {
98        super(context, dbName, null, DB_VERSION);
99    }
100
101    @Override
102    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
103        dropTable(db, Metadata.TABLE);
104        dropTable(db, Photos.TABLE);
105        dropTable(db, Albums.TABLE);
106        dropTable(db, Accounts.TABLE);
107        onCreate(db);
108    }
109
110    protected List<String[]> getAlbumTableDefinition() {
111        return tableCreationStrings(CREATE_ALBUM);
112    }
113
114    protected List<String[]> getPhotoTableDefinition() {
115        return tableCreationStrings(CREATE_PHOTO);
116    }
117
118    protected List<String[]> getMetadataTableDefinition() {
119        return tableCreationStrings(CREATE_METADATA);
120    }
121
122    protected List<String[]> getAccountTableDefinition() {
123        return tableCreationStrings(CREATE_ACCOUNT);
124    }
125
126    protected static void createTable(SQLiteDatabase db, String table, List<String[]> columns) {
127        StringBuilder create = new StringBuilder(SQL_CREATE_TABLE);
128        create.append(table).append('(');
129        boolean first = true;
130        for (String[] column : columns) {
131            if (!first) {
132                create.append(',');
133            }
134            first = false;
135            for (String val: column) {
136                create.append(val).append(' ');
137            }
138        }
139        create.append(')');
140        db.beginTransaction();
141        try {
142            db.execSQL(create.toString());
143            db.setTransactionSuccessful();
144        } finally {
145            db.endTransaction();
146        }
147    }
148
149    protected static String[] createUniqueConstraint(String column1, String column2) {
150        return new String[] {
151                "UNIQUE(", column1, ",", column2, ")"
152        };
153    }
154
155    protected static List<String[]> tableCreationStrings(String[][] createTable) {
156        ArrayList<String[]> create = new ArrayList<String[]>(createTable.length);
157        for (String[] line: createTable) {
158            create.add(line);
159        }
160        return create;
161    }
162
163    protected static void addToTable(List<String[]> createTable, String[][] columns, String[][] constraints) {
164        if (columns != null) {
165            for (String[] column: columns) {
166                createTable.add(0, column);
167            }
168        }
169        if (constraints != null) {
170            for (String[] constraint: constraints) {
171                createTable.add(constraint);
172            }
173        }
174    }
175
176    protected static void dropTable(SQLiteDatabase db, String table) {
177        db.beginTransaction();
178        try {
179            db.execSQL("drop table if exists " + table);
180            db.setTransactionSuccessful();
181        } finally {
182            db.endTransaction();
183        }
184    }
185}
186