Created
March 4, 2015 12:34
-
-
Save caioketo/d55674334abca64312ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.enew.timetracker.database; | |
public interface BaseTable { | |
public String getColumnID(); | |
public String getTable(); | |
public String[] getColumns(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.enew.timetracker.database; | |
import android.database.sqlite.SQLiteDatabase; | |
public class BookTable implements BaseTable { | |
private static BookTable instance; | |
public static BookTable getInstance() { | |
if (instance == null) { | |
instance = new BookTable(); | |
} | |
return instance; | |
} | |
public static final String TABLE = "book"; | |
public static final String COLUMN_ID = "_id"; | |
// SQL para criar a tabela | |
private static final String DATABASE_CREATE = "create table if not exists " | |
+ TABLE | |
+ "(" | |
+ COLUMN_ID + " integer primary key autoincrement" | |
+ ");"; | |
public static String addPrefix(String column) { | |
return TABLE + "." + column; | |
} | |
public static void onCreate(SQLiteDatabase database) { | |
database.execSQL(DATABASE_CREATE); | |
} | |
public static void onUpgrade(SQLiteDatabase database, int oldVersion, | |
int newVersion) { | |
database.execSQL("DROP TABLE IF EXISTS " + TABLE); | |
onCreate(database); | |
} | |
@Override | |
public String getColumnID() { | |
return COLUMN_ID; | |
} | |
@Override | |
public String getTable() { | |
return TABLE; | |
} | |
@Override | |
public String[] getColumns() { | |
String[] retorno = { COLUMN_ID }; | |
return retorno; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.enew.timetracker.database.provider; | |
import android.content.ContentProvider; | |
import android.content.ContentValues; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.net.Uri; | |
import android.provider.Settings; | |
import android.util.Log; | |
import com.enew.timetracker.database.handler.CategoryHandler; | |
/** | |
* Created by amorenew on 2/27/2015. | |
*/ | |
public class BookProvider extends ContentProvider { | |
private DatabaseHelper database; | |
static final String AUTHORITY = "com.enew.timetracker.provider"; | |
public static final Uri CONTENT_URI = Uri.parse(AUTHORITY); | |
private static final String PATH_BOOK = "book"; | |
public static final Uri CONTENT_URI_BOOK = Uri.parse("content://" + AUTHORITY | |
+ "/" + PATH_BOOK); | |
SQLiteDatabase db; | |
CategoryHandler categoryHandler; | |
// ------- Configuring UriMatcher | |
private static final int BOOK = 10; | |
private static final int BOOK_ID = BOOK + 1; | |
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH); | |
static { | |
sURIMatcher.addURI(AUTHORITY, PATH_BOOK, BOOK); | |
sURIMatcher.addURI(AUTHORITY, PATH_BOOK + "/#", BOOK_ID); | |
} | |
@Override | |
public boolean onCreate() { | |
database = new DatabaseHelper(getContext()); | |
return false; | |
} | |
@Override | |
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { | |
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); | |
BaseTable table; | |
StringBuilder sb = new StringBuilder(); | |
boolean add_id = false; | |
int uriType = sURIMatcher.match(uri); | |
SQLiteDatabase db; | |
Cursor cursor; | |
switch (uriType) { | |
case BOOK_ID: | |
add_id = true; | |
case BOOK: | |
table = BookTable.getInstance(); | |
break; | |
} | |
assert (table != null); | |
if (add_id) { | |
queryBuilder.appendWhere(table.getColumnID() + "=" | |
+ uri.getLastPathSegment()); | |
} | |
queryBuilder.setTables(table.getTable()); | |
db = database.getWritableDatabase(); | |
cursor = queryBuilder.query(db, projection, selection, | |
selectionArgs, null, null, sortOrder); | |
// Notify it if there is some listener | |
cursor.setNotificationUri(getContext().getContentResolver(), uri); | |
return cursor; | |
} | |
public String getPATH(int pathID) { | |
switch (pathID) { | |
case BOOK: | |
return PATH_BOOK; | |
} | |
} | |
@Override | |
public String getType(Uri uri) { | |
return null; | |
} | |
@Override | |
public Uri insert(Uri uri, ContentValues values) { | |
int uriType = sURIMatcher.match(uri); | |
SQLiteDatabase sqlDB = database.getWritableDatabase(); | |
int rowsDeleted = 0; | |
BaseTable table; | |
long id = 0; | |
switch (uriType) { | |
case BOOK: | |
table = BookTable.getInstance(); | |
break; | |
} | |
id = sqlDB.insert(table.getTable(), null, values); | |
Log.d("INSERT", Long.toString(id)); | |
getContext().getContentResolver().notifyChange(uri, null); | |
return Uri.parse(getPATH(uriType) + "/" + id); | |
} | |
@Override | |
public int delete(Uri uri, String selection, String[] selectionArgs) { | |
int uriType = sURIMatcher.match(uri); | |
SQLiteDatabase sqlDB = database.getWritableDatabase(); | |
int rowsDeleted = 0; | |
String id; | |
BaseTable table; | |
boolean is_id = false; | |
switch (uriType) { | |
case BOOK_ID: | |
is_id = true; | |
case BOOK: | |
table = BookTable.getInstance(); | |
break; | |
} | |
if (!is_id) { | |
rowsDeleted = sqlDB.delete(table.getTable(), selection, | |
selectionArgs); | |
} | |
else { | |
id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsDeleted = sqlDB.delete(table.getTable(), | |
table.getColumnID() + "=" + id, | |
null); | |
} else { | |
rowsDeleted = sqlDB.delete(table.getTable(), | |
table.getColumnID() + "=" + id | |
+ " and " + selection, | |
selectionArgs); | |
} | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return rowsDeleted; | |
} | |
@Override | |
public int update(Uri uri, ContentValues values, String selection, | |
String[] selectionArgs) { | |
int uriType = sURIMatcher.match(uri); | |
SQLiteDatabase sqlDB = database.getWritableDatabase(); | |
int rowsUpdated = 0; | |
String id; | |
BaseTable table; | |
boolean is_id = false; | |
switch (uriType) { | |
case BOOK_ID: | |
is_id = true; | |
case BOOK: | |
table = BookTable.getInstance(); | |
break; | |
} | |
if (!is_id) { | |
rowsUpdated = sqlDB.update(table.getTable(), | |
values, | |
selection, | |
selectionArgs); | |
} | |
else { | |
id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsUpdated = sqlDB.update(table.getTable(), | |
values, | |
table.getColumnID() + "=" + id, | |
null); | |
} else { | |
rowsUpdated = sqlDB.update(table.getTable(), | |
values, | |
table.getColumnID() + "=" + id | |
+ " and " | |
+ selection, | |
selectionArgs); | |
} | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return rowsUpdated; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.enew.timetracker.database; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
public class DatabaseHelper extends SQLiteOpenHelper { | |
private static final String DATABASE_NAME = "database.db"; | |
private static final int DATABASE_VERSION = 1; | |
public DatabaseHelper(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase database) { | |
BookTable.onCreate(database); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase database, int oldVersion, | |
int newVersion) { | |
BookTable.onUpgrade(database, oldVersion, newVersion); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment