Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 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  */
     16 package com.android.providers.contacts.util;
     17 
     18 import android.content.ContentValues;
     19 import android.database.Cursor;
     20 import android.database.sqlite.SQLiteDatabase;
     21 
     22 /**
     23  * Utilities related to "database properties", which are similar to shard preferences, but
     24  * are transaction-aware.
     25  */
     26 public class PropertyUtils {
     27     private PropertyUtils() {
     28     }
     29 
     30     public interface Tables {
     31         String PROPERTIES = "properties";
     32     }
     33 
     34     public interface PropertiesColumns {
     35         String PROPERTY_KEY = "property_key";
     36         String PROPERTY_VALUE = "property_value";
     37     }
     38 
     39     /**
     40      * Creates the properties table.
     41      */
     42     public static void createPropertiesTable(SQLiteDatabase db) {
     43         db.execSQL("CREATE TABLE " + Tables.PROPERTIES + " (" +
     44                 PropertiesColumns.PROPERTY_KEY + " TEXT PRIMARY KEY, " +
     45                 PropertiesColumns.PROPERTY_VALUE + " TEXT " +
     46                 ");");
     47 
     48     }
     49 
     50     /**
     51      * Gets a property.
     52      */
     53     public static String getProperty(SQLiteDatabase db, String key, String defaultValue) {
     54         final Cursor cursor = db.query(Tables.PROPERTIES,
     55                 new String[] {PropertiesColumns.PROPERTY_VALUE},
     56                 PropertiesColumns.PROPERTY_KEY + "=?",
     57                 new String[] {key}, null, null, null);
     58         String value = null;
     59         try {
     60             if (cursor.moveToFirst()) {
     61                 value = cursor.getString(0);
     62             }
     63         } finally {
     64             cursor.close();
     65         }
     66 
     67         return value != null ? value : defaultValue;
     68     }
     69 
     70     /**
     71      * Sets a property.
     72      */
     73     public static void setProperty(SQLiteDatabase db, String key, String value) {
     74         ContentValues values = new ContentValues();
     75         values.put(PropertiesColumns.PROPERTY_KEY, key);
     76         values.put(PropertiesColumns.PROPERTY_VALUE, value);
     77         db.replace(Tables.PROPERTIES, null, values);
     78     }
     79 }
     80