-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
558 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
153 changes: 153 additions & 0 deletions
153
pretixscan/app/src/main/java/eu/pretix/pretixscan/droid/db/SqlCipherConnection.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Based on code | ||
* Copyright 2019 requery.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package eu.pretix.pretixscan.droid.db | ||
|
||
|
||
import android.database.sqlite.SQLiteConstraintException | ||
import android.database.sqlite.SQLiteException | ||
import io.requery.android.sqlite.BaseConnection | ||
import net.zetetic.database.sqlcipher.SQLiteDatabase | ||
import java.sql.DatabaseMetaData | ||
import java.sql.PreparedStatement | ||
import java.sql.ResultSet | ||
import java.sql.SQLException | ||
import java.sql.SQLFeatureNotSupportedException | ||
import java.sql.SQLIntegrityConstraintViolationException | ||
import java.sql.Statement | ||
|
||
/** | ||
* [java.sql.Connection] implementation using SQLCipher SQLite Android API. | ||
* | ||
* @author Nikhil Purushe | ||
*/ | ||
internal class SqlCipherConnection(val database: SQLiteDatabase) : BaseConnection() { | ||
private val metaData: SqlCipherMetaData | ||
private var enteredTransaction: Boolean = false | ||
|
||
init { | ||
autoCommit = true | ||
metaData = SqlCipherMetaData(this) | ||
} | ||
|
||
override fun ensureTransaction() { | ||
if (!autoCommit) { | ||
if (!database.inTransaction()) { | ||
database.beginTransaction() | ||
enteredTransaction = true | ||
} | ||
} | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun execSQL(sql: String) { | ||
try { | ||
database.execSQL(sql) | ||
} catch (e: SQLiteException) { | ||
throwSQLException(e) | ||
} | ||
|
||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun commit() { | ||
if (autoCommit) { | ||
throw SQLException("commit called while in autoCommit mode") | ||
} | ||
if (database.inTransaction() && enteredTransaction) { | ||
try { | ||
database.setTransactionSuccessful() | ||
} catch (e: IllegalStateException) { | ||
throw SQLException(e) | ||
} finally { | ||
database.endTransaction() | ||
enteredTransaction = false | ||
} | ||
} | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun createStatement(): Statement { | ||
return SqlCipherStatement(this) | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun createStatement(resultSetType: Int, resultSetConcurrency: Int): Statement { | ||
return createStatement(resultSetType, | ||
resultSetConcurrency, ResultSet.HOLD_CURSORS_OVER_COMMIT) | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun createStatement(resultSetType: Int, resultSetConcurrency: Int, | ||
resultSetHoldability: Int): Statement { | ||
if (resultSetConcurrency == ResultSet.CONCUR_UPDATABLE) { | ||
throw SQLFeatureNotSupportedException("CONCUR_UPDATABLE not supported") | ||
} | ||
return SqlCipherStatement(this) | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun getMetaData(): DatabaseMetaData { | ||
return metaData | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun isClosed(): Boolean { | ||
return !database.isOpen() | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun isReadOnly(): Boolean { | ||
return database.isReadOnly() | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun prepareStatement(sql: String, autoGeneratedKeys: Int): PreparedStatement { | ||
return SqlCipherPreparedStatement(this, sql, autoGeneratedKeys) | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun prepareStatement(sql: String, | ||
resultSetType: Int, | ||
resultSetConcurrency: Int, | ||
resultSetHoldability: Int): PreparedStatement { | ||
return SqlCipherPreparedStatement(this, sql, Statement.NO_GENERATED_KEYS) | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun prepareStatement(sql: String, columnNames: Array<String>): PreparedStatement { | ||
return SqlCipherPreparedStatement(this, sql, Statement.RETURN_GENERATED_KEYS) | ||
} | ||
|
||
@Throws(SQLException::class) | ||
override fun rollback() { | ||
if (autoCommit) { | ||
throw SQLException("commit called while in autoCommit mode") | ||
} | ||
database.endTransaction() | ||
} | ||
|
||
companion object { | ||
@Throws(SQLException::class) | ||
fun throwSQLException(exception: SQLiteException) { | ||
if (exception is SQLiteConstraintException) { | ||
throw SQLIntegrityConstraintViolationException(exception) | ||
} | ||
throw SQLException(exception) | ||
} | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
pretixscan/app/src/main/java/eu/pretix/pretixscan/droid/db/SqlCipherDatabaseSource.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Based on code | ||
* Copyright 2019 requery.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package eu.pretix.pretixscan.droid.db | ||
|
||
import android.content.Context | ||
import io.requery.android.DefaultMapping | ||
import io.requery.android.LoggingListener | ||
import io.requery.android.sqlite.DatabaseProvider | ||
import io.requery.android.sqlite.SchemaUpdater | ||
import io.requery.meta.EntityModel | ||
import io.requery.sql.Configuration | ||
import io.requery.sql.ConfigurationBuilder | ||
import io.requery.sql.Mapping | ||
import io.requery.sql.Platform | ||
import io.requery.sql.SchemaModifier | ||
import io.requery.sql.TableCreationMode | ||
import io.requery.sql.platform.SQLite | ||
import io.requery.util.function.Function | ||
import net.zetetic.database.sqlcipher.SQLiteDatabase | ||
import net.zetetic.database.sqlcipher.SQLiteOpenHelper | ||
import java.sql.Connection | ||
|
||
open class SqlCipherDatabaseSource(context: Context, | ||
private val model: EntityModel, | ||
name: String, | ||
private val password: String, | ||
version: Int) | ||
: SQLiteOpenHelper(context, name, password, null, version, 0, null, null, true), DatabaseProvider<SQLiteDatabase> { | ||
|
||
private val platform: Platform | ||
private val mapping: Mapping | ||
private var db: SQLiteDatabase? = null | ||
private var _configuration: Configuration? = null | ||
private var loggingEnabled: Boolean = false | ||
private var mode: TableCreationMode? = null | ||
|
||
init { | ||
this.platform = SQLite() | ||
this.mapping = onCreateMapping(platform) | ||
this.mode = TableCreationMode.CREATE_NOT_EXISTS | ||
System.loadLibrary("sqlcipher"); | ||
} | ||
|
||
override fun setLoggingEnabled(enable: Boolean) { | ||
this.loggingEnabled = enable | ||
} | ||
|
||
override fun setTableCreationMode(mode: TableCreationMode) { | ||
this.mode = mode | ||
} | ||
|
||
protected fun onCreateMapping(platform: Platform): Mapping { | ||
return DefaultMapping(platform) | ||
} | ||
|
||
protected fun onConfigure(builder: ConfigurationBuilder) { | ||
if (loggingEnabled) { | ||
val loggingListener = LoggingListener() | ||
builder.addStatementListener(loggingListener) | ||
} | ||
} | ||
|
||
private fun getConnection(db: SQLiteDatabase): SqlCipherConnection { | ||
synchronized(this) { | ||
return SqlCipherConnection(db) | ||
} | ||
} | ||
|
||
override fun getConfiguration(): Configuration { | ||
if (_configuration == null) { | ||
val builder = ConfigurationBuilder(this, model) | ||
.setMapping(mapping) | ||
.setPlatform(platform) | ||
.setBatchUpdateSize(1000) | ||
onConfigure(builder) | ||
_configuration = builder.build() | ||
} | ||
return _configuration!! | ||
} | ||
|
||
override fun onCreate(db: SQLiteDatabase) { | ||
this.db = db | ||
SchemaModifier(configuration).createTables(TableCreationMode.CREATE) | ||
} | ||
|
||
override fun onConfigure(db: SQLiteDatabase) {} | ||
|
||
override fun onOpen(db: SQLiteDatabase?) { | ||
super.onOpen(db) | ||
if (!db!!.isReadOnly) { | ||
db.execSQL("PRAGMA foreign_keys = ON") | ||
} | ||
} | ||
|
||
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { | ||
this.db = db | ||
val updater = SchemaUpdater(configuration, | ||
Function { s -> db.rawQuery(s, null) }, mode) | ||
updater.update() | ||
} | ||
|
||
override fun getConnection(): Connection { | ||
synchronized(this) { | ||
if (db == null) { | ||
db = getWritableDatabase() | ||
} | ||
return getConnection(db!!) | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
pretixscan/app/src/main/java/eu/pretix/pretixscan/droid/db/SqlCipherMetaData.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2018 requery.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package eu.pretix.pretixscan.droid.db | ||
|
||
import android.database.Cursor | ||
import android.database.sqlite.SQLiteException | ||
import io.requery.android.sqlite.BaseConnection | ||
import io.requery.android.sqlite.SqliteMetaData | ||
import io.requery.util.function.Function | ||
import net.zetetic.database.sqlcipher.SQLiteDatabase | ||
import java.io.Closeable | ||
|
||
import java.sql.SQLException | ||
|
||
internal class SqlCipherMetaData(connection: BaseConnection) : SqliteMetaData(connection) { | ||
|
||
@Throws(SQLException::class) | ||
override fun <R> queryMemory(function: Function<Cursor, R>, query: String): R { | ||
try { | ||
val database = SQLiteDatabase.openOrCreateDatabase(":memory:", "", null, null) | ||
val cursor = database.rawQuery(query, null) | ||
return function.apply(closeWithCursor(Closeable { database.close() }, cursor)) | ||
} catch (e: SQLiteException) { | ||
throw SQLException(e) | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.