Skip to content

Commit

Permalink
Update docs (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
be-hase authored Jun 27, 2024
1 parent 37b2da3 commit 424bb85
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 108 deletions.
50 changes: 23 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,30 @@ writing is almost the same.
data class User(...)

class UserRepository(private val kueryClient: KueryClient) {
suspend fun findById(userId: Int): User? {
return kueryClient
.sql { +"SELECT * FROM users WHERE user_id = $userId" }
.singleOrNull()
}

suspend fun search(status: String, vip: Boolean?): List<User> {
return kueryClient
.sql {
+"SELECT * FROM users"
+"WHERE"
+"status = $status"
if (vip != null) {
+"vip = $vip"
}
suspend fun findById(userId: Int): User? = kueryClient
.sql { +"SELECT * FROM users WHERE user_id = $userId" }
.singleOrNull()

suspend fun search(status: String, vip: Boolean?): List<User> = kueryClient
.sql {
+"""
SELECT * FROM users
WHERE
status = $status
"""
if (vip != null) {
+"vip = $vip"
}
.list()
}

suspend fun insertMany(users: List<User>): Long {
return kueryClient
.sql {
+"INSERT INTO users (username, email)"
// useful helper function
values(users) { listOf(it.username, it.email) }
}
.rowsUpdated()
}
}
.list()

suspend fun insertMany(users: List<User>): Long = kueryClient
.sql {
+"INSERT INTO users (username, email)"
// useful helper function
values(users) { listOf(it.username, it.email) }
}
.rowsUpdated()
}
```

Expand Down
4 changes: 2 additions & 2 deletions docs/detekt.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Therefore, if you write it incorrectly as follows, problems may arise.
#### Noncompliant Code:

```kotlin
client.sql {
kueryClient.sql {
// BAD !!
val sql = "SELECT * FROM user WHERE id = $id"
+sql
Expand All @@ -52,7 +52,7 @@ client.sql {
#### Compliant Code:

```kotlin
client.sql {
kueryClient.sql {
+"SELECT * FROM user WHERE id = ${bind(id)}"
}
```
100 changes: 46 additions & 54 deletions docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,69 +35,61 @@ writing is almost the same.
data class User(...)

class UserRepository(private val kueryClient: KueryClient) {
suspend fun findById(userId: Int): User? {
return kueryClient
.sql { +"SELECT * FROM users WHERE user_id = $userId" }
.singleOrNull()
}

suspend fun search(status: String, vip: Boolean?): List<User> {
return kueryClient
.sql {
+"SELECT * FROM users"
+"WHERE"
+"status = $status"
if (vip != null) {
+"vip = $vip"
}
suspend fun findById(userId: Int): User? = kueryClient
.sql { +"SELECT * FROM users WHERE user_id = $userId" }
.singleOrNull()

suspend fun search(status: String, vip: Boolean?): List<User> = kueryClient
.sql {
+"""
SELECT * FROM users
WHERE
status = $status
"""
if (vip != null) {
+"vip = $vip"
}
.list()
}

suspend fun insertMany(users: List<User>): Long {
return kueryClient
.sql {
+"INSERT INTO users (username, email)"
// useful helper function
values(users) { listOf(it.username, it.email) }
}
.rowsUpdated()
}
}
.list()

suspend fun insertMany(users: List<User>): Long = kueryClient
.sql {
+"INSERT INTO users (username, email)"
// useful helper function
values(users) { listOf(it.username, it.email) }
}
.rowsUpdated()
}
```

```kotlin [kuery-client-spring-data-jdbc]
data class User(...)

class UserRepository(private val kueryClient: KueryBlockingClient) {
fun findById(userId: Int): User? {
return kueryClient
.sql { +"SELECT * FROM users WHERE user_id = $userId" }
.singleOrNull()
}

fun search(status: String, vip: Boolean?): List<User> {
return kueryClient
.sql {
+"SELECT * FROM users"
+"WHERE"
+"status = $status"
if (vip != null) {
+"vip = $vip"
}
}
.list()
}

fun insertMany(users: List<User>): Long {
return kueryClient
.sql {
+"INSERT INTO users (username, email)"
// useful helper function
values(users) { listOf(it.username, it.email) }
fun findById(userId: Int): User? = kueryClient
.sql { +"SELECT * FROM users WHERE user_id = $userId" }
.singleOrNull()

fun search(status: String, vip: Boolean?): List<User> = kueryClient
.sql {
+"""
SELECT * FROM users
WHERE
status = $status
"""
if (vip != null) {
+"vip = $vip"
}
.rowsUpdated()
}
}
.list()

fun insertMany(users: List<User>): Long = kueryClient
.sql {
+"INSERT INTO users (username, email)"
// useful helper function
values(users) { listOf(it.username, it.email) }
}
.rowsUpdated()
}
```

Expand Down
12 changes: 5 additions & 7 deletions docs/observation.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ package com.example.spring.data.r2dbc

@Repository
class UserRepository(private val kueryClient: KueryClient) {
suspend fun selectByUserId(userId: Int): User? {
return kueryClient
.sql {
+"SELECT * FROM users WHERE user_id = $userId"
}
.singleOrNull()
}
suspend fun selectByUserId(userId: Int): User? = kueryClient
.sql {
+"SELECT * FROM users WHERE user_id = $userId"
}
.singleOrNull()
}
```

Expand Down
8 changes: 4 additions & 4 deletions docs/transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class UserService(
}

@Repository
class UserRepository(private val client: KueryClient) {
class UserRepository(private val kueryClient: KueryClient) {
suspend fun insert(
username: String,
email: Email,
Expand Down Expand Up @@ -66,7 +66,7 @@ class UserService(
}

@Repository
class UserRepository(private val client: KueryClient) {
class UserRepository(private val kueryClient: KueryClient) {
suspend fun insert(
username: String,
email: Email,
Expand Down Expand Up @@ -107,7 +107,7 @@ class UserService(
}

@Repository
class UserRepository(private val client: KueryClient) {
class UserRepository(private val kueryClient: KueryClient) {
fun insert(
username: String,
email: Email,
Expand Down Expand Up @@ -138,7 +138,7 @@ class UserService(
}

@Repository
class UserRepository(private val client: KueryClient) {
class UserRepository(private val kueryClient: KueryClient) {
fun insert(
username: String,
email: Email,
Expand Down
24 changes: 10 additions & 14 deletions docs/type-conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,19 @@ val kueryClient = SpringR2dbcKueryClient.builder()
### Let's Try

```kotlin
suspend fun write(str: StringWrapper) {
kueryClient
.sql {
+"INSERT INTO test_table (text) VALUES ($str)"
}
.rowsUpdated()
}
suspend fun write(str: StringWrapper): Long = kueryClient
.sql {
+"INSERT INTO test_table (text) VALUES ($str)"
}
.rowsUpdated()

data class Record(
val text: StringWrapper,
)

suspend fun read(): List<Record> {
return kueryClient
.sql {
+"SELECT * FROM test_table"
}
.list()
}
suspend fun read(): List<Record> = kueryClient
.sql {
+"SELECT * FROM test_table"
}
.list()
```

0 comments on commit 424bb85

Please sign in to comment.