package jp.gihyo.mitoma.android.lesson2301sqlite;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;


public class FriendsProvider extends ContentProvider {

    public static final String AUTHORITY = "jp.gihyo.mitoma.android.friends.provider";
    public static final Uri CONTENT_URI =
            Uri.parse("content://" + AUTHORITY + "/friends");
    private SQLiteDatabase db;
    private static final String table = "friends";

    @Override
    public boolean onCreate() {
        FriendsOpenHelper helper = new FriendsOpenHelper(getContext());
        this.db = helper.getReadableDatabase();
        return true;
    }
    @Override
    public Cursor query(Uri uri, String[] select, String where,
                        String[] whereArgs, String sortOrder) {
        //SQLiteDatabase db = helper.getReadableDatabase();
        return db.query(table, select, where, whereArgs,
                null, null, sortOrder);
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        //SQLiteDatabase db = helper.getWritableDatabase();
        long id = db.insert(table, null, values);
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }

    @Override
    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
        return db.update(table, values, where, whereArgs);
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        return db.delete(table, where, whereArgs);
    }

    @Override
    public String getType(Uri uri) { return null; }
}
