|
| 1 | +package io.sentry.android.sqlite |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.database.Cursor |
| 5 | +import android.database.SQLException |
| 6 | +import android.os.Build |
| 7 | +import android.os.CancellationSignal |
| 8 | +import androidx.annotation.RequiresApi |
| 9 | +import androidx.sqlite.db.SupportSQLiteDatabase |
| 10 | +import androidx.sqlite.db.SupportSQLiteQuery |
| 11 | +import androidx.sqlite.db.SupportSQLiteStatement |
| 12 | + |
| 13 | +/** |
| 14 | + * The Sentry's [SentrySupportSQLiteDatabase], it will automatically add a span |
| 15 | + * out of the active span bound to the scope for each database query. |
| 16 | + * It's a wrapper around [SupportSQLiteDatabase], and it's created automatically |
| 17 | + * by the [SentrySupportSQLiteOpenHelper]. |
| 18 | + * |
| 19 | + * @param delegate The [SupportSQLiteDatabase] instance to delegate calls to. |
| 20 | + * @param sqLiteSpanManager The [SQLiteSpanManager] responsible for the creation of the spans. |
| 21 | + */ |
| 22 | +internal class SentrySupportSQLiteDatabase( |
| 23 | + private val delegate: SupportSQLiteDatabase, |
| 24 | + private val sqLiteSpanManager: SQLiteSpanManager |
| 25 | +) : SupportSQLiteDatabase by delegate { |
| 26 | + |
| 27 | + /** |
| 28 | + * Compiles the given SQL statement. It will return Sentry's wrapper around SupportSQLiteStatement. |
| 29 | + * |
| 30 | + * @param sql The sql query. |
| 31 | + * @return Compiled statement. |
| 32 | + */ |
| 33 | + override fun compileStatement(sql: String): SupportSQLiteStatement { |
| 34 | + return SentrySupportSQLiteStatement(delegate.compileStatement(sql), sqLiteSpanManager, sql) |
| 35 | + } |
| 36 | + |
| 37 | + @Suppress("AcronymName") // To keep consistency with framework method name. |
| 38 | + override fun execPerConnectionSQL( |
| 39 | + sql: String, |
| 40 | + @SuppressLint("ArrayReturn") bindArgs: Array<out Any?>? |
| 41 | + ) { |
| 42 | + sqLiteSpanManager.performSql(sql) { |
| 43 | + delegate.execPerConnectionSQL(sql, bindArgs) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + override fun query(query: String): Cursor { |
| 48 | + return sqLiteSpanManager.performSql(query) { |
| 49 | + delegate.query(query) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + override fun query(query: String, bindArgs: Array<out Any?>): Cursor { |
| 54 | + return sqLiteSpanManager.performSql(query) { |
| 55 | + delegate.query(query, bindArgs) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + override fun query(query: SupportSQLiteQuery): Cursor { |
| 60 | + return sqLiteSpanManager.performSql(query.sql) { |
| 61 | + delegate.query(query) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) |
| 66 | + override fun query( |
| 67 | + query: SupportSQLiteQuery, |
| 68 | + cancellationSignal: CancellationSignal? |
| 69 | + ): Cursor { |
| 70 | + return sqLiteSpanManager.performSql(query.sql) { |
| 71 | + delegate.query(query, cancellationSignal) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Throws(SQLException::class) |
| 76 | + override fun execSQL(sql: String) { |
| 77 | + sqLiteSpanManager.performSql(sql) { |
| 78 | + delegate.execSQL(sql) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Throws(SQLException::class) |
| 83 | + override fun execSQL(sql: String, bindArgs: Array<out Any?>) { |
| 84 | + sqLiteSpanManager.performSql(sql) { |
| 85 | + delegate.execSQL(sql, bindArgs) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments