Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package fr.free.nrw.commons.bookmarks.items

import android.content.ContentValues
import android.database.Cursor
import android.database.sqlite.SQLiteQueryBuilder
import android.database.sqlite.SQLiteDatabase
import android.net.Uri
import fr.free.nrw.commons.BuildConfig
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.TABLE_NAME
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
import androidx.core.net.toUri
import androidx.sqlite.db.SupportSQLiteQueryBuilder
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_ID

/**
Expand All @@ -28,14 +29,12 @@ class BookmarkItemsContentProvider : CommonsDaggerContentProvider() {
uri: Uri, projection: Array<String>?, selection: String?,
selectionArgs: Array<String>?, sortOrder: String?
): Cursor {
val queryBuilder = SQLiteQueryBuilder().apply {
tables = TABLE_NAME
}

return queryBuilder.query(
requireDb(), projection, selection,
selectionArgs, null, null, sortOrder
).apply {
val query = SupportSQLiteQueryBuilder
.builder(TABLE_NAME)
.columns(projection)
.selection(selection, selectionArgs)
.orderBy(sortOrder).create()
return requireDb().query(query).apply {
setNotificationUri(context?.contentResolver, uri)
}
}
Expand All @@ -51,21 +50,23 @@ class BookmarkItemsContentProvider : CommonsDaggerContentProvider() {
uri: Uri, contentValues: ContentValues?,
selection: String?, selectionArgs: Array<String>?
): Int {
val rowsUpdated: Int
val rowsUpdated: Int =
if (selection.isNullOrEmpty()) {
val id = uri.lastPathSegment!!.toInt()
rowsUpdated = requireDb().update(
TABLE_NAME,
contentValues,
"$COLUMN_ID = ?",
arrayOf(id.toString())
)
contentValues?.let {
requireDb().update(
TABLE_NAME,
SQLiteDatabase.CONFLICT_NONE,
it,
"$COLUMN_ID = ?",
arrayOf(id.toString())
)
} ?: 0
} else {
throw IllegalArgumentException(
"Parameter `selection` should be empty when updating an ID"
)
}

context?.contentResolver?.notifyChange(uri, null)
return rowsUpdated
}
Expand All @@ -74,7 +75,8 @@ class BookmarkItemsContentProvider : CommonsDaggerContentProvider() {
* Handles the insertion of new bookmark items record to local SQLite Database
*/
override fun insert(uri: Uri, contentValues: ContentValues?): Uri? {
val id = requireDb().insert(TABLE_NAME, null, contentValues)
val id =
contentValues?.let { requireDb().insert(TABLE_NAME, SQLiteDatabase.CONFLICT_NONE, it) }
context?.contentResolver?.notifyChange(uri, null)
return "$BASE_URI/$id".toUri()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.free.nrw.commons.bookmarks.items

import android.database.sqlite.SQLiteDatabase
import androidx.sqlite.db.SupportSQLiteDatabase

/**
* Table of bookmarksItems data
Expand Down Expand Up @@ -47,30 +47,30 @@ object BookmarkItemsTable {
/**
* Creates table
*
* @param db SQLiteDatabase
* @param db SupportSQLiteDatabase
*/
fun onCreate(db: SQLiteDatabase) {
fun onCreate(db: SupportSQLiteDatabase) {
db.execSQL(CREATE_TABLE_STATEMENT)
}

/**
* Deletes database
*
* @param db SQLiteDatabase
* @param db SupportSQLiteDatabase
*/
fun onDelete(db: SQLiteDatabase) {
fun onDelete(db: SupportSQLiteDatabase) {
db.execSQL(DROP_TABLE_STATEMENT)
onCreate(db)
}

/**
* Updates database
*
* @param db SQLiteDatabase
* @param db SupportSQLiteDatabase
* @param from starting
* @param to end
*/
fun onUpdate(db: SQLiteDatabase, from: Int, to: Int) {
fun onUpdate(db: SupportSQLiteDatabase, from: Int, to: Int) {
if (from == to) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package fr.free.nrw.commons.bookmarks.pictures

import android.content.ContentValues
import android.database.Cursor
import android.database.sqlite.SQLiteQueryBuilder
import android.database.sqlite.SQLiteDatabase
import android.net.Uri
import fr.free.nrw.commons.BuildConfig
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
import androidx.core.net.toUri
import androidx.sqlite.db.SupportSQLiteQueryBuilder
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.COLUMN_MEDIA_NAME
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.TABLE_NAME

Expand All @@ -28,17 +29,14 @@ class BookmarkPicturesContentProvider : CommonsDaggerContentProvider() {
uri: Uri, projection: Array<String>?, selection: String?,
selectionArgs: Array<String>?, sortOrder: String?
): Cursor {
val queryBuilder = SQLiteQueryBuilder().apply {
tables = TABLE_NAME
val query = SupportSQLiteQueryBuilder
.builder(TABLE_NAME)
.columns(projection)
.selection(selection, selectionArgs)
.orderBy(sortOrder).create()
return requireDb().query(query).apply {
setNotificationUri(context?.contentResolver, uri)
}

val cursor = queryBuilder.query(
requireDb(), projection, selection,
selectionArgs, null, null, sortOrder
)
cursor.setNotificationUri(context?.contentResolver, uri)

return cursor
}

/**
Expand All @@ -52,15 +50,18 @@ class BookmarkPicturesContentProvider : CommonsDaggerContentProvider() {
uri: Uri, contentValues: ContentValues?, selection: String?,
selectionArgs: Array<String>?
): Int {
val rowsUpdated: Int
val rowsUpdated: Int =
if (selection.isNullOrEmpty()) {
val id = uri.lastPathSegment!!.toInt()
rowsUpdated = requireDb().update(
TABLE_NAME,
contentValues,
"$COLUMN_MEDIA_NAME = ?",
arrayOf(id.toString())
)
contentValues?.let {
requireDb().update(
TABLE_NAME,
SQLiteDatabase.CONFLICT_NONE,
it,
"$COLUMN_MEDIA_NAME = ?",
arrayOf(id.toString())
)
} ?: 0
} else {
throw IllegalArgumentException(
"Parameter `selection` should be empty when updating an ID"
Expand All @@ -74,7 +75,8 @@ class BookmarkPicturesContentProvider : CommonsDaggerContentProvider() {
* Handles the insertion of new bookmark pictures record to local SQLite Database
*/
override fun insert(uri: Uri, contentValues: ContentValues?): Uri {
val id = requireDb().insert(TABLE_NAME, null, contentValues)
val id =
contentValues?.let { requireDb().insert(TABLE_NAME, SQLiteDatabase.CONFLICT_NONE, it) }
context?.contentResolver?.notifyChange(uri, null)
return "$BASE_URI/$id".toUri()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.free.nrw.commons.bookmarks.pictures

import android.database.sqlite.SQLiteDatabase
import androidx.sqlite.db.SupportSQLiteDatabase

object BookmarksTable {
const val TABLE_NAME: String = "bookmarks"
Expand All @@ -20,15 +20,15 @@ object BookmarksTable {
"$COLUMN_CREATOR STRING" +
");")

fun onCreate(db: SQLiteDatabase) =
fun onCreate(db: SupportSQLiteDatabase) =
db.execSQL(CREATE_TABLE_STATEMENT)

fun onDelete(db: SQLiteDatabase) {
fun onDelete(db: SupportSQLiteDatabase) {
db.execSQL(DROP_TABLE_STATEMENT)
onCreate(db)
}

fun onUpdate(db: SQLiteDatabase, from: Int, to: Int) {
fun onUpdate(db: SupportSQLiteDatabase, from: Int, to: Int) {
if (from == to) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import android.content.ContentValues
import android.content.UriMatcher
import android.content.UriMatcher.NO_MATCH
import android.database.Cursor
import android.database.sqlite.SQLiteQueryBuilder
import android.database.sqlite.SQLiteDatabase
import android.net.Uri
import android.text.TextUtils
import androidx.core.net.toUri
import androidx.sqlite.db.SupportSQLiteQueryBuilder
import fr.free.nrw.commons.BuildConfig
import fr.free.nrw.commons.category.CategoryTable.ALL_FIELDS
import fr.free.nrw.commons.category.CategoryTable.COLUMN_ID
Expand All @@ -25,31 +26,27 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
@SuppressWarnings("ConstantConditions")
override fun query(uri: Uri, projection: Array<String>?, selection: String?,
selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
val queryBuilder = SQLiteQueryBuilder().apply {
tables = TABLE_NAME
}

val uriType = uriMatcher.match(uri)
val db = requireDb()

val cursor: Cursor? = when (uriType) {
CATEGORIES -> queryBuilder.query(
db,
projection,
selection,
selectionArgs,
null,
null,
sortOrder
CATEGORIES -> db.query(
SupportSQLiteQueryBuilder.builder(
TABLE_NAME
).selection(selection, selectionArgs)
.columns(projection)
.orderBy(sortOrder)
.create()
)
CATEGORIES_ID -> queryBuilder.query(
db,
ALL_FIELDS,
"_id = ?",
arrayOf(uri.lastPathSegment),
null,
null,
sortOrder

CATEGORIES_ID -> db.query(
SupportSQLiteQueryBuilder.builder(
TABLE_NAME
).selection("_id = ?", arrayOf(uri.lastPathSegment))
.columns(ALL_FIELDS)
.orderBy(sortOrder)
.create()
)
else -> throw IllegalArgumentException("Unknown URI $uri")
}
Expand All @@ -63,10 +60,12 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
@SuppressWarnings("ConstantConditions")
override fun insert(uri: Uri, contentValues: ContentValues?): Uri {
val uriType = uriMatcher.match(uri)
val id: Long
var id: Long = 0L
when (uriType) {
CATEGORIES -> {
id = requireDb().insert(TABLE_NAME, null, contentValues)
contentValues?.let {
id = requireDb().insert(TABLE_NAME, SQLiteDatabase.CONFLICT_NONE, it)
}
}
else -> throw IllegalArgumentException("Unknown URI: $uri")
}
Expand All @@ -85,7 +84,7 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
when (uriType) {
CATEGORIES -> {
for (value in values) {
sqlDB.insert(TABLE_NAME, null, value)
sqlDB.insert(TABLE_NAME, SQLiteDatabase.CONFLICT_NONE, value)
}
sqlDB.setTransactionSuccessful()
}
Expand All @@ -100,18 +99,21 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
override fun update(uri: Uri, contentValues: ContentValues?, selection: String?,
selectionArgs: Array<String>?): Int {
val uriType = uriMatcher.match(uri)
val rowsUpdated: Int
var rowsUpdated: Int = 0
when (uriType) {
CATEGORIES_ID -> {
if (TextUtils.isEmpty(selection)) {
val id = uri.lastPathSegment?.toInt()
?: throw IllegalArgumentException("Invalid ID")
rowsUpdated = requireDb().update(
TABLE_NAME,
contentValues,
"$COLUMN_ID = ?",
arrayOf(id.toString())
)
contentValues?.let {
rowsUpdated = requireDb().update(
TABLE_NAME,
SQLiteDatabase.CONFLICT_NONE,
it,
"$COLUMN_ID = ?",
arrayOf(id.toString())
)
}
} else {
throw IllegalArgumentException(
"Parameter `selection` should be empty when updating an ID")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.free.nrw.commons.category

import android.database.sqlite.SQLiteDatabase
import androidx.sqlite.db.SupportSQLiteDatabase

object CategoryTable {
const val TABLE_NAME = "categories"
Expand Down Expand Up @@ -33,14 +33,14 @@ object CategoryTable {
"$COLUMN_TIMES_USED INTEGER" +
");"

fun onCreate(db: SQLiteDatabase) = db.execSQL(CREATE_TABLE_STATEMENT)
fun onCreate(db: SupportSQLiteDatabase) = db.execSQL(CREATE_TABLE_STATEMENT)

fun onDelete(db: SQLiteDatabase) {
fun onDelete(db: SupportSQLiteDatabase) {
db.execSQL(DROP_TABLE_STATEMENT)
onCreate(db)
}

fun onUpdate(db: SQLiteDatabase, from: Int, to: Int) {
fun onUpdate(db: SupportSQLiteDatabase, from: Int, to: Int) {
if (from == to) return
if (from < 4) {
// doesn't exist yet
Expand Down
Loading
Loading