Skip to content

Commit 06b5143

Browse files
cleanup
1 parent f857c52 commit 06b5143

File tree

1 file changed

+70
-37
lines changed
  • core/src/commonMain/kotlin/com/powersync/db

1 file changed

+70
-37
lines changed

core/src/commonMain/kotlin/com/powersync/db/Queries.kt

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ public fun interface ThrowableLockCallback<R> {
1818

1919
public interface Queries {
2020
/**
21-
* Execute a write query (INSERT, UPDATE, DELETE)
21+
* Executes a write query (INSERT, UPDATE, DELETE).
22+
*
23+
* @param sql The SQL query to execute.
24+
* @param parameters The parameters for the query, or an empty list if none.
25+
* @return The number of rows affected by the query.
26+
* @throws PowerSyncException If a database error occurs.
27+
* @throws CancellationException If the operation is cancelled.
2228
*/
2329
@Throws(PowerSyncException::class, CancellationException::class)
2430
public suspend fun execute(
@@ -27,9 +33,14 @@ public interface Queries {
2733
): Long
2834

2935
/**
30-
* Execute a read-only (SELECT) query and return a single result.
31-
* If there is no result, throws an [IllegalArgumentException].
32-
* See [getOptional] for queries where the result might be empty.
36+
* Executes a read-only (SELECT) query and returns a single result.
37+
*
38+
* @param sql The SQL query to execute.
39+
* @param parameters The parameters for the query, or an empty list if none.
40+
* @param mapper A function to map the result set to the desired type.
41+
* @return The single result of the query.
42+
* @throws PowerSyncException If a database error occurs or no result is found.
43+
* @throws CancellationException If the operation is cancelled.
3344
*/
3445
@Throws(PowerSyncException::class, CancellationException::class)
3546
public suspend fun <RowType : Any> get(
@@ -39,7 +50,14 @@ public interface Queries {
3950
): RowType
4051

4152
/**
42-
* Execute a read-only (SELECT) query and return the results.
53+
* Executes a read-only (SELECT) query and returns all results.
54+
*
55+
* @param sql The SQL query to execute.
56+
* @param parameters The parameters for the query, or an empty list if none.
57+
* @param mapper A function to map the result set to the desired type.
58+
* @return A list of results.
59+
* @throws PowerSyncException If a database error occurs.
60+
* @throws CancellationException If the operation is cancelled.
4361
*/
4462
@Throws(PowerSyncException::class, CancellationException::class)
4563
public suspend fun <RowType : Any> getAll(
@@ -49,7 +67,14 @@ public interface Queries {
4967
): List<RowType>
5068

5169
/**
52-
* Execute a read-only (SELECT) query and return a single optional result.
70+
* Executes a read-only (SELECT) query and returns a single optional result.
71+
*
72+
* @param sql The SQL query to execute.
73+
* @param parameters The parameters for the query, or an empty list if none.
74+
* @param mapper A function to map the result set to the desired type.
75+
* @return The single result of the query, or null if no result is found.
76+
* @throws PowerSyncException If a database error occurs.
77+
* @throws CancellationException If the operation is cancelled.
5378
*/
5479
@Throws(PowerSyncException::class, CancellationException::class)
5580
public suspend fun <RowType : Any> getOptional(
@@ -59,77 +84,85 @@ public interface Queries {
5984
): RowType?
6085

6186
/**
62-
* @returns a [Flow] which emits whenever the source tables are modified.
87+
* Returns a [Flow] that emits whenever the source tables are modified.
88+
*
89+
* @param tables The set of tables to monitor for changes.
90+
* @param throttleMs The minimum interval, in milliseconds, between queries. Defaults to null.
91+
* @return A [Flow] emitting the set of modified tables.
92+
* @throws PowerSyncException If a database error occurs.
93+
* @throws CancellationException If the operation is cancelled.
6394
*/
6495
@Throws(PowerSyncException::class, CancellationException::class)
6596
public fun onChange(
6697
tables: Set<String>,
67-
/**
68-
* Specify the minimum interval, in milliseconds, between queries.
69-
*/
7098
throttleMs: Long? = null,
7199
): Flow<Set<String>>
72100

73101
/**
74-
* Execute a read-only (SELECT) query every time the source tables are modified and return the results as a List in [Flow].
102+
* Executes a read-only (SELECT) query every time the source tables are modified and returns the results as a [Flow] of lists.
103+
*
104+
* @param sql The SQL query to execute.
105+
* @param parameters The parameters for the query, or an empty list if none.
106+
* @param throttleMs The minimum interval, in milliseconds, between queries. Defaults to null.
107+
* @param mapper A function to map the result set to the desired type.
108+
* @return A [Flow] emitting lists of results.
109+
* @throws PowerSyncException If a database error occurs.
110+
* @throws CancellationException If the operation is cancelled.
75111
*/
76112
@Throws(PowerSyncException::class, CancellationException::class)
77113
public fun <RowType : Any> watch(
78114
sql: String,
79115
parameters: List<Any?>? = listOf(),
80-
/**
81-
* Specify the minimum interval, in milliseconds, between queries.
82-
*/
83116
throttleMs: Long? = null,
84117
mapper: (SqlCursor) -> RowType,
85118
): Flow<List<RowType>>
86119

87120
/**
88-
* Takes a global lock, without starting a transaction.
89-
*
90-
* This takes a global lock - only one write transaction can execute against
91-
* the database at a time. This applies even when constructing separate
92-
* database instances for the same database file.
121+
* Takes a global lock without starting a transaction.
93122
*
94-
* Locks for separate database instances on the same database file
95-
* may be held concurrently.
123+
* This lock ensures that only one write transaction can execute against the database at a time, even across separate database instances for the same file.
96124
*
97-
* In most cases, [writeTransaction] should be used instead.
125+
* @param callback The callback to execute while holding the lock.
126+
* @return The result of the callback.
127+
* @throws PowerSyncException If a database error occurs.
128+
* @throws CancellationException If the operation is cancelled.
98129
*/
99130
@Throws(PowerSyncException::class, CancellationException::class)
100131
public suspend fun <R> writeLock(callback: ThrowableLockCallback<R>): R
101132

102133
/**
103-
* Open a read-write transaction.
134+
* Opens a read-write transaction.
104135
*
105-
* This takes a global lock - only one write transaction can execute against
106-
* the database at a time. This applies even when constructing separate
107-
* database instances for the same database file.
136+
* This takes a global lock, ensuring that only one write transaction can execute against the database at a time, even across separate database instances for the same file.
108137
*
109-
* Statements within the transaction must be done on the provided
110-
* [PowerSyncTransaction] - attempting statements on the database
111-
* instance will error cause a dead-lock.
138+
* @param callback The callback to execute within the transaction.
139+
* @return The result of the callback.
140+
* @throws PowerSyncException If a database error occurs.
141+
* @throws CancellationException If the operation is cancelled.
112142
*/
113143
@Throws(PowerSyncException::class, CancellationException::class)
114144
public suspend fun <R> writeTransaction(callback: ThrowableTransactionCallback<R>): R
115145

116146
/**
117-
* Takes a read lock, without starting a transaction.
147+
* Takes a read lock without starting a transaction.
118148
*
119-
* The lock only applies to a single SQLite connection, and multiple
120-
* connections may hold read locks at the same time.
149+
* The lock applies only to a single SQLite connection, allowing multiple connections to hold read locks simultaneously.
121150
*
122-
* In most cases, [readTransaction] should be used instead.
151+
* @param callback The callback to execute while holding the lock.
152+
* @return The result of the callback.
153+
* @throws PowerSyncException If a database error occurs.
154+
* @throws CancellationException If the operation is cancelled.
123155
*/
124156
@Throws(PowerSyncException::class, CancellationException::class)
125157
public suspend fun <R> readLock(callback: ThrowableLockCallback<R>): R
126158

127159
/**
128-
* Open a read-only transaction.
160+
* Opens a read-only transaction.
129161
*
130-
* Statements within the transaction must be done on the provided
131-
* [PowerSyncTransaction] - executing statements on the database level
132-
* will be executed on separate connections.
162+
* @param callback The callback to execute within the transaction.
163+
* @return The result of the callback.
164+
* @throws PowerSyncException If a database error occurs.
165+
* @throws CancellationException If the operation is cancelled.
133166
*/
134167
@Throws(PowerSyncException::class, CancellationException::class)
135168
public suspend fun <R> readTransaction(callback: ThrowableTransactionCallback<R>): R

0 commit comments

Comments
 (0)