package com.android.xuxm.data;
import java.util.HashMap;
import com.android.xuxm.data.AccountProviderMetaData.accountTableMetaData;
import com.android.xuxm.data.AccountProviderMetaData.accountTypeTableMetaData;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
public class AccountProvider extends ContentProvider {
private final Context mCtx;
public AccountProvider()
{
this.mCtx =null;
}
public AccountProvider(Context ctx) {
this.mCtx = ctx;
}
public AccountProvider open() throws SQLException {
mOpenHelper = new DatabaseHelper(mCtx);
return this;
}
public void close() {
mOpenHelper.close();
}
private static HashMap<String,String> AccountProjectionMap;
static
{
AccountProjectionMap=new HashMap<String,String>();
AccountProjectionMap.put(accountTableMetaData._ID, accountTableMetaData._ID);
AccountProjectionMap.put(accountTableMetaData.ACCOUNT_TITLE, accountTableMetaData.ACCOUNT_TITLE);
AccountProjectionMap.put(accountTableMetaData.ACCOUNT_DESCRIPTION, accountTableMetaData.ACCOUNT_DESCRIPTION);
AccountProjectionMap.put(accountTableMetaData.ACCOUNT_TYPEID,accountTableMetaData.ACCOUNT_TYPEID );
AccountProjectionMap.put(accountTableMetaData.ACCOUNT_AMOUNT,accountTableMetaData.ACCOUNT_AMOUNT );
AccountProjectionMap.put(accountTableMetaData.CREATED_DATE, accountTableMetaData.CREATED_DATE);
AccountProjectionMap.put(accountTableMetaData.MODIFIED_DATE,accountTableMetaData.MODIFIED_DATE );
}
private static HashMap<String,String> AccountTypeProjectionMap;
static
{
AccountTypeProjectionMap=new HashMap<String,String>();
AccountTypeProjectionMap.put(accountTypeTableMetaData._ID, accountTypeTableMetaData._ID);
AccountTypeProjectionMap.put(accountTypeTableMetaData.ACCOUNTTYPE_DELETESIGN, accountTypeTableMetaData.ACCOUNTTYPE_DELETESIGN);
AccountTypeProjectionMap.put(accountTypeTableMetaData.ACCOUNTTYPE_TYPENAME, accountTypeTableMetaData.ACCOUNTTYPE_TYPENAME);
AccountTypeProjectionMap.put(accountTypeTableMetaData.MODIFIED_DATE,accountTypeTableMetaData.MODIFIED_DATE );
AccountTypeProjectionMap.put(accountTypeTableMetaData.CREATED_DATE, accountTypeTableMetaData.CREATED_DATE);
}
private static final UriMatcher sUriMatcher;
private static final int INCOMING_ACCOUNT_COLLECTION_URI_INDICATOR=1;
private static final int INCOMING_ACCOUNT_SINGE_URI_INDICATOR=2;
private static final int INCOMING_ACCOUNTTYPE_COLLECTION_URI_INDICATOR=3;
private static final int INCOMING_ACCOUNTTYPE_SINGE_URI_INDICATOR=4;
static
{
sUriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(AccountProviderMetaData.AUTHORITY, "account",INCOMING_ACCOUNT_COLLECTION_URI_INDICATOR);
sUriMatcher.addURI(AccountProviderMetaData.AUTHORITY, "account/#",INCOMING_ACCOUNT_SINGE_URI_INDICATOR);
sUriMatcher.addURI(AccountProviderMetaData.AUTHORITY, "accounttype",INCOMING_ACCOUNTTYPE_COLLECTION_URI_INDICATOR);
sUriMatcher.addURI(AccountProviderMetaData.AUTHORITY, "accounttype/#",INCOMING_ACCOUNTTYPE_SINGE_URI_INDICATOR);
}
private DatabaseHelper mOpenHelper;
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context,AccountProviderMetaData.DATABASE_NAME,null,AccountProviderMetaData.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//建立记账内容表
db.execSQL("CREATE TABLE "+AccountProviderMetaData.ACCOUNT_TABLE_NAME+" ("
+accountTableMetaData._ID+" INTEGER PRIMARY KEY autoincrement,"
+accountTableMetaData.ACCOUNT_TITLE+" TEXT,"
+accountTableMetaData.ACCOUNT_TYPEID+" INTEGER,"
+accountTableMetaData.ACCOUNT_AMOUNT+" INTEGER,"
+accountTableMetaData.ACCOUNT_DESCRIPTION+" TEXT,"
+accountTableMetaData.CREATED_DATE+" INTEGER,"
+accountTableMetaData.MODIFIED_DATE+" INTEGER"+");"
);
//建立记账类别表
db.execSQL("CREATE TABLE "+AccountProviderMetaData.ACCOUNTTYPE_TABLE_NAME+"("
+accountTypeTableMetaData._ID+" INTEGER PRIMARY KEY,"
+accountTypeTableMetaData.ACCOUNTTYPE_TYPENAME+" TEXT,"
+accountTypeTableMetaData.ACCOUNTTYPE_DELETESIGN+" INTEGER,"
+accountTypeTableMetaData.CREATED_DATE+" INTEGER,"
+accountTypeTableMetaData.MODIFIED_DATE+" INTEGER"+");"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS"+AccountProviderMetaData.ACCOUNT_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS"+AccountProviderMetaData.ACCOUNTTYPE_TABLE_NAME);
onCreate(db);
}
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
SQLiteDatabase db=mOpenHelper.getReadableDatabase();
String tableName="";
switch(sUriMatcher.match(arg0)){
case INCOMING_ACCOUNT_COLLECTION_URI_INDICATOR:
case INCOMING_ACCOUNT_SINGE_URI_INDICATOR:
tableName=AccountProviderMetaData.ACCOUNT_TABLE_NAME;
break;
case INCOMING_ACCOUNTTYPE_COLLECTION_URI_INDICATOR:
case INCOMING_ACCOUNTTYPE_SINGE_URI_INDICATOR:
tableName=AccountProviderMetaData.ACCOUNTTYPE_TABLE_NAME;
break;
default:
//throw new IllegalArgumentExcepion("unknown uri"+uri);
}
return db.delete(accountTableMetaData.TABLE_NAME, arg1, null);
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
switch(sUriMatcher.match(uri)){
case INCOMING_ACCOUNT_COLLECTION_URI_INDICATOR:
return accountTableMetaData.CONTENT_TYPE;
case INCOMING_ACCOUNT_SINGE_URI_INDICATOR:
return accountTableMetaData.CONTENT_ITEM_TYPE;
case INCOMING_ACCOUNTTYPE_COLLECTION_URI_INDICATOR:
return accountTypeTableMetaData.CONTENT_TYPE;
case INCOMING_ACCOUNTTYPE_SINGE_URI_INDICATOR:
return accountTypeTableMetaData.CONTENT_ITEM_TYPE;
default:
//throw new IllegalArgumentExcepion("unknown uri"+uri);
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
String tableName="";
switch(sUriMatcher.match(uri)){
case INCOMING_ACCOUNT_COLLECTION_URI_INDICATOR:
case INCOMING_ACCOUNT_SINGE_URI_INDICATOR:
tableName=AccountProviderMetaData.ACCOUNT_TABLE_NAME;
break;
case INCOMING_ACCOUNTTYPE_COLLECTION_URI_INDICATOR:
case INCOMING_ACCOUNTTYPE_SINGE_URI_INDICATOR:
tableName=AccountProviderMetaData.ACCOUNTTYPE_TABLE_NAME;
break;
default:
//throw new IllegalArgumentExcepion("unknown uri"+uri);
}
SQLiteDatabase db=mOpenHelper.getReadableDatabase();
if(db.insert(tableName, null, values)==-1)
{
return null;
}
return uri;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
SQLiteQueryBuilder qb=new SQLiteQueryBuilder();
switch(sUriMatcher.match(uri)){
case INCOMING_ACCOUNT_COLLECTION_URI_INDICATOR:
qb.setTables(AccountProviderMetaData.ACCOUNT_TABLE_NAME);
qb.setProjectionMap(AccountProjectionMap);
break;
case INCOMING_ACCOUNT_SINGE_URI_INDICATOR:
qb.setTables(AccountProviderMetaData.ACCOUNT_TABLE_NAME);
qb.setProjectionMap(AccountProjectionMap);
qb.appendWhere(accountTableMetaData._ID+"="+
uri.getPathSegments().get(1));
break;
case INCOMING_ACCOUNTTYPE_COLLECTIO
- 1
- 2
- 3
前往页