Skip to content

Commit 0bd7046

Browse files
authored
Add ORM and 4.3.0 updates (#198)
1 parent f256ac9 commit 0bd7046

20 files changed

+625
-478
lines changed

config.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
I18n.enforce_available_locales = false
6262

6363
# Latest versions
64-
@latest_version = "4.2.1"
64+
@latest_version = "4.3.0"
6565
@latest_play_support_version = "3.0.0-scalikejdbc-4.2"
6666
@v2_play_support_version = "2.5.1"
6767
@v2_version = "2.5.2"
@@ -76,8 +76,8 @@
7676
set :v1_version, @v1_version
7777
set :v18_version, @v18_version
7878
set :v1_latest_version, @v1_version
79-
set :h2_version, "1.4.200"
80-
set :logback_version, "1.2.12"
79+
set :h2_version, "2.2.224"
80+
set :logback_version, "1.5.6"
8181

8282
# Build-specific configuration
8383
configure :build do

source/documentation/auto-macros.html.md.erb

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ title: Auto Macros - ScalikeJDBC
88
### Avoid Boilerplate Code
99
<hr/>
1010

11-
You can avoid writing boilerplate code when using `scalikejdbc-syntax-support-macro`.
11+
If you want to avoid writing lots of boilerplate code, `scalikejdbc-syntax-support-macro` can be greatly helpful for reducing such troublesome coding tasks.
1212

1313
<hr/>
1414
### Setup
1515
<hr/>
1616

17-
Add the following aditional dependency to your sbt project.
17+
In addition to the core library, you can add the following optional dependency in `buid.sbt`:
1818

1919
```scala
2020
libraryDependencies += "org.scalikejdbc" %% "scalikejdbc-syntax-support-macro" % "<%= version %>"
@@ -26,10 +26,15 @@ libraryDependencies += "org.scalikejdbc" %% "scalikejdbc-syntax-support-macro" %
2626

2727
#### autoConstruct for extracting entities from ResultSet
2828

29-
Usually, we should write ResultSet extractor method as follows.
29+
When you don't use the macros, the usual code to extract data from `ResultSet` should look like below:
3030

3131
```scala
32-
case class Company(id: Long, name: String, countryId: Option[Long], country: Option[Country] = None)
32+
case class Company(
33+
id: Long,
34+
name: String,
35+
countryId: Option[Long],
36+
country: Option[Country] = None
37+
)
3338

3439
object Company extends SQLSyntaxSupport[Company] {
3540

@@ -41,31 +46,36 @@ object Company extends SQLSyntaxSupport[Company] {
4146
}
4247
```
4348

44-
When using scalikejdbc-syntax-support-macro, you can use `#autoConstruct` macro.
49+
When using scalikejdbc-syntax-support-macro, you can use `#autoConstruct` macro instead. As you can see, now the code is significantly simpler and much easier to maintain for the future.
4550

4651
```scala
47-
case class Company(id: Long, name: String, countryId: Option[Long], country: Option[Country] = None)
52+
case class Company(
53+
id: Long,
54+
name: String,
55+
countryId: Option[Long],
56+
// This property never comes from ResultSet
57+
country: Option[Country] = None
58+
)
4859

4960
object Company extends SQLSyntaxSupport[Company] {
5061

5162
def apply(rs: WrappedResultSet, rn: ResultName[Company]): Company =
52-
autoConstruct(rs, rn, "country") // "country" will be ignored when binding values from ResultSet
63+
// "country" is execluded when binding values from ResultSet
64+
// Note that the property neeeds to have the default `None` value
65+
autoConstruct(rs, rn, "country")
5366
}
5467
```
5568

5669
The `#autoConstruct` method binds all the fields defined at the primary constructor automatically.
5770

58-
The `country` field in the above example class should be ignored. In such cases, you should specify additional String parameter such as `"country"`.
59-
Of course, the `"country"` will be verified at Scala compilation time. We believe that's pretty cool and useful.
60-
71+
The `country` field in the above example class should be ignored. In such cases, you should specify an additional String parameter such as "country". Of course, the "country" will be verified at Scala compilation time!
6172

6273
<hr/>
6374
#### autoColumns to avoid accessing JDBC metadata
6475

65-
When your code load ScalikeJDBC DAO objects, ScalikeJDBC automatically fetches all the column names for the table specified by SQLSyntaxSupport's `tableName` (via JDBC metadata API).
76+
When your code loads ScalikeJDBC DAO objects, ScalikeJDBC automatically fetches all the column names for the table specified by `SQLSyntaxSupport`'s `tableName` via the JDBC metadata API.
6677

67-
If you don't prefer the behavior, you can choose loading column names from the entity class's field names instead.
68-
The following code won't access JDBC metadata and will resolve column names from Company class's fields, primary constructor's parameters, by simply converting them to snake-cased ones or applying nameConverters to them.
78+
If you don't prefer the behavior, you can choose to load column names from the entity class's field names instead. The following code won't access JDBC metadata and will resolve column names from the `Company` class's fields and primary constructor's parameters by simply converting them to snake-cased ones or applying name converters to them.
6979

7080
```scala
7181
case class Company(id: Long, name: String, countryId: Option[Long], country: Option[Country] = None)

source/documentation/auto-session.html.md

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ title: Auto Session - ScalikeJDBC
88
### Why AutoSession?
99
<hr/>
1010

11-
Basic usage of ScalikeJDBC is using `DB.autoCommit/readOnly/localTx/withinTx { ...}` blocks.
12-
13-
However, if you'd like to re-use methods, they might not be available.
11+
Typically, ScalikeJDBC operations are encapsulated within `DB.autoCommit`, `DB.readOnly`, and other transaction blocks. For reusing methods across different transaction contexts as below,
1412

1513
```scala
1614
def findById(id: Long) = DB readOnly {
@@ -19,9 +17,7 @@ def findById(id: Long) = DB readOnly {
1917
}
2018
```
2119

22-
When you use the above method in a transaction block, the code won't work as you expected.
23-
24-
The reason is that since `#findById(Long)` uses another session(=connection), it couldn't access uncommitted data.
20+
`AutoSession` becomes essential. In a transaction block, this method may not perform as expected because it uses a separate session and cannot access uncommitted data. The reason is that since `#findById(Long)` uses another session(=connection), it couldn't access uncommitted data.
2521

2622
```scala
2723
DB localTx { implicit session =>
@@ -30,15 +26,15 @@ DB localTx { implicit session =>
3026
}
3127
```
3228

33-
You need to change method's API to accept implicit parameters and now you don't need `DB` block inside the method.
29+
With this change, the method can access the current transaction context. Instead of having `DB` blocks inside, your method can accept an implict DBSession paramter from external code to join an existing transactional session.
3430

3531
```scala
3632
def findById(id: Long)(implicit session: DBSession) =
3733
sql"select id, name from members where id = ${id}"
3834
.map(rs => Member(rs)).single.apply()
3935
```
4036

41-
This one works as expected.
37+
With the change, the following code works as you expect.
4238

4339
```scala
4440
DB localTx { implicit session =>
@@ -47,7 +43,7 @@ DB localTx { implicit session =>
4743
}
4844
```
4945

50-
But unfortunately, now we need to pass implicit parameter to `#findById` every time to use it.
46+
But unfortunately, now that we need to pass implicit parameter to `#findById` every time to use the method, it could be troublesome especially for simple code snippets.
5147

5248
```scala
5349
// now we cannot use this method directly
@@ -56,22 +52,22 @@ findById(id) // implicit parameter not found!
5652
DB readOnly { implicit session => findById(id) }
5753
```
5854

59-
`AutoSession` is a solution for this issue. Use `AutoSession` as default value of the implicit parameter.
55+
`AutoSession` is a solution for the issue. You can have `AutoSession` as default value of the implicit parameter.
6056

6157
```scala
6258
def findById(id: Long)(implicit session: DBSession = AutoSession) =
6359
sql"select id, name from members where id = ${id}"
6460
.map(rs => Member(rs)).single.apply()
6561
```
6662

67-
This change made `#findById` flexible.
63+
Having the default implement value can make `#findById` even more flexible plus much simpler.
6864

6965
```scala
7066
findById(id) // borrows a read-only session and gives it back
7167
DB localTx { implicit session => findById(id) } // using implicit session
7268
```
7369

74-
If you do the same with `NamedDB`, use `NamedAutoSession` as follows.
70+
When you do the same with `NamedDB`, you can use `NamedAutoSession` as below:
7571

7672
```scala
7773
def findById(id: Long)(implicit session: DBSession = NamedAutoSession("named")) =
@@ -82,8 +78,5 @@ def findById(id: Long)(implicit session: DBSession = NamedAutoSession("named"))
8278
### ReadOnlyAutoSession
8379
<hr/>
8480

85-
Since version 1.7.4, `ReadOnlyAutoSession` and `NamedReadOnlyAutoSession` is also available.
86-
87-
These auto sessions disallow update/execute operations.
88-
81+
Since version 1.7.4, `ReadOnlyAutoSession` and `NamedReadOnlyAutoSession` is also available, which are tailored for read-only operations, preventing any update or execute operations.
8982

source/documentation/configuration.html.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ title: Configuration - ScalikeJDBC
55
## Configuration
66

77
<hr/>
8-
The following 3 things should be configured.
8+
To use ScalikeJDBC, the following three factors need to be proplery configured.
99

1010
<hr/>
1111
### Loading JDBC Drivers
1212
<hr/>
1313

14-
In advance, some JDBC drivers must be loaded by using
14+
Before using JDBC drivers, they must be explicitly loaded using either:
1515

1616
```
1717
Class.forName(String)
@@ -23,15 +23,13 @@ or
2323
java.sql.DriverManager.registerDriver(java.sql.Driver)
2424
```
2525

26-
However many modern JDBC implementations will be automatically loaded when they are present on the classpath.
27-
28-
If you use `scalikejdbc-config` or `scalikejdbc-play-plugin`, they do the legacy work for you.
26+
Many modern JDBC drivers, however, automatically load themselves when included in the classpath. Nonetheless, when you're using `scalikejdbc-config` or `scalikejdbc-play-plugin`, these handle the above loading process for safety.
2927

3028
<hr/>
3129
### Connection Pool Settings
3230
<hr/>
3331

34-
ConnectionPool should be initialized when starting your applications.
32+
It's required to initialize a ConnectionPool at the start of your applications:
3533

3634
```scala
3735
import scalikejdbc._
@@ -50,7 +48,7 @@ val settings = ConnectionPoolSettings(
5048
ConnectionPool.add("foo", url, user, password, settings)
5149
```
5250

53-
When you use external DataSource (e.g. application server's connection pool), use javax.sql.DataSource via JNDI:
51+
For using an external DataSource, such as an application server's connection pool, connect via JNDI:
5452

5553
```scala
5654
import javax.naming._
@@ -64,7 +62,7 @@ ConnectionPool.singleton(new DataSourceConnectionPool(ds))
6462
ConnectionPool.add("foo", new DataSourceConnectionPool(ds))
6563
```
6664

67-
`ConnectionPool` and `ConnectionPoolSettings`'s parameters are like this:
65+
Here's how `ConnectionPool` and `ConnectionPoolSettings` parameters look:
6866

6967
```scala
7068
abstract class ConnectionPool(
@@ -82,14 +80,14 @@ case class ConnectionPoolSettings(
8280
validationQuery: String)
8381
```
8482

85-
FYI: [Source Code](https://github.com/scalikejdbc/scalikejdbc/blob/master/scalikejdbc-core/src/main/scala/scalikejdbc/ConnectionPool.scala)
83+
Further details in the [source code](https://github.com/scalikejdbc/scalikejdbc/blob/master/scalikejdbc-core/src/main/scala/scalikejdbc/ConnectionPool.scala)
8684

8785

8886
<hr/>
8987
### Global Settings
9088
<hr/>
9189

92-
Global settings for logging for query inspection and so on.
90+
Configure global settings for SQL error logging, query inspection, and more:
9391

9492
```scala
9593
object GlobalSettings {
@@ -102,17 +100,17 @@ object GlobalSettings {
102100
}
103101
```
104102

105-
FYI: [Source Code](https://github.com/scalikejdbc/scalikejdbc/blob/master/scalikejdbc-core/src/main/scala/scalikejdbc/GlobalSettings.scala)
103+
Reference the [source code](https://github.com/scalikejdbc/scalikejdbc/blob/master/scalikejdbc-core/src/main/scala/scalikejdbc/GlobalSettings.scala) for more details.
106104

107105
<hr/>
108106
### scalikejdbc-config
109107
<hr/>
110108

111-
If you use `scalikejdbc-config` which is an easy-to-use configuration loader for ScalikeJDBC which reads typesafe config, configuration is much simple.
109+
The `scalikejdbc-config` library simplifies the configuration process by utilizing Typesafe Config to read settings:
112110

113111
[Typesafe Config](https://github.com/lightbend/config)
114112

115-
If you'd like to setup `scalikejdbc-config`, see setup page.
113+
To learn how to configure `scalikejdbc-config`, see setup page.
116114

117115
[/documentation/setup](/documentation/setup.html)
118116

@@ -148,9 +146,9 @@ db.default.driver="org.postgresql.Driver"
148146
db.default.url="jdbc:postgresql://localhost:5432/scalikejdbc"
149147
```
150148

151-
After just calling `scalikejdbc.config.DBs.setupAll()`, Connection pools are prepared. `DBs.setup/DBs.setupAll` loads specified JDBC driver classes as well.
149+
When setting up with `scalikejdbc.config.DBs.setupAll()`, the module automatically loads the specified JDBC drivers and prepares connection pools.
152150

153-
Note that due to the way JDBC works, these drivers are loaded globally for the entire JVM, and then a particular driver is selected from the global JVM list by locating the first which is able to handle the connection URL. This usually produces the expected behaviour anyway, unless you have multiple JDBC drivers in your classpath which handle the same URL (such as MySQL and MariaDB JDBC implementations, which both handle URLs of the form `jdbc:mysql:`). In these cases you may not get the implementation you are expecting, since the presence of JDBC packages in the classpath is, for many drivers, enough to have them registered globally.
151+
DBC drivers, once loaded, are globally available to the entire Java Virtual Machine (JVM). The selection process for a specific driver from the global list typically targets the first one capable of managing the given connection URL. This approach generally yields the correct behavior, except when multiple drivers capable of handling the same URL type (such as MySQL and MariaDB drivers, both supporting `jdbc:mysql:` URLs) are present in the classpath. In such cases, the expected driver might not be used, as the mere presence of JDBC drivers on the classpath often leads to their global registration, irrespective of their intended use.
154152

155153
```scala
156154
import scalikejdbc._
@@ -180,7 +178,7 @@ DBs.closeAll()
180178
### scalikejdbc-config with Environment
181179
<hr/>
182180

183-
It's also possible to add prefix(e.g. environment).
181+
You can manage different configurations for multiple environments:
184182

185183
```
186184
development.db.default.driver="org.h2.Driver"
@@ -199,8 +197,7 @@ prod {
199197
}
200198
}
201199
```
202-
203-
Use `DBsWithEnv` instead of `DBs`.
200+
To activate these settings, use `DBsWithEnv` instead of `DBs`.
204201

205202
```scala
206203
DBsWithEnv("development").setupAll()
@@ -211,7 +208,7 @@ DBsWithEnv("prod").setup("sandbox")
211208
### scalikejdbc-config for Global Settings
212209
<hr/>
213210

214-
The following settings are available.
211+
Global settings can be adjusted to log SQL errors, connection issues, and more:
215212

216213
```
217214
# Global settings

0 commit comments

Comments
 (0)