1
1
package com .albertprz .logograph
2
2
3
- import java .sql .{Connection , ResultSet , PreparedStatement , Statement , Date , Time }
3
+ import java .sql .{
4
+ Connection ,
5
+ Date ,
6
+ PreparedStatement ,
7
+ ResultSet ,
8
+ Statement ,
9
+ Time
10
+ }
4
11
import java .math .BigDecimal
5
- import scala .util .{Try , Success , Failure }
12
+ import scala .util .{Failure , Success , Try }
6
13
import scala .collection .mutable .ListBuffer
7
14
import cats .Monad
8
15
import cats .syntax .all .*
9
- import cats .effect .{Sync , Resource }
16
+ import cats .effect .{Resource , Sync }
10
17
11
18
import com .albertprz .logograph .config .LogographConfig
12
19
import utils .TypeInfo
13
20
import utils .StringUtils .*
14
21
import utils .Error .{InvalidParameterType , InvalidQueryResultType }
15
22
import java .lang .reflect .Method
16
23
17
-
18
- class LogographContext [F [_] : Sync : Monad ] (conn : Connection ):
24
+ class LogographContext [F [_]: Sync : Monad ](conn : Connection ):
19
25
20
26
import LogographContext .*
21
27
22
28
conn.setAutoCommit(false )
23
29
24
- def run [T <: DbDataSet ] (query : SelectStatement [T ]): F [List [T ]] =
25
- runQuery (query)
26
-
27
- def run (stmts : SQLStatefulStatement * ): F [Unit ] =
28
- runStatefulStatement (stmts)
30
+ def run [T <: DbDataSet ](query : SelectStatement [T ]): F [List [T ]] =
31
+ runQuery(query)
29
32
33
+ def run (stmts : SQLStatefulStatement * ): F [Unit ] = runStatefulStatement(stmts)
30
34
31
- private def runQuery [T <: DbDataSet ] (query : SelectStatement [T ]): F [List [T ]] =
35
+ private def runQuery [T <: DbDataSet ](query : SelectStatement [T ]): F [List [T ]] =
32
36
prepareStatement(query.sql, query.paramList).use { stmt =>
33
-
34
- for resultSet <- Sync [F ].blocking { stmt.executeQuery() }
35
- yield extractResults(resultSet, query.typeInfo)
37
+ for resultSet <-
38
+ Sync [F ].blocking {
39
+ stmt.executeQuery()
40
+ }
41
+ yield extractResults(resultSet, query.typeInfo)
36
42
}
37
43
38
- private def runStatefulStatement (stmts : Seq [SQLStatefulStatement ]) =
44
+ private def runStatefulStatement (stmts : Seq [SQLStatefulStatement ]) =
39
45
40
- val preparedStmts = for (sql, paramList) <- stmts.map(x => (x.sql, x.paramList))
41
- yield prepareStatement(sql, paramList)
42
- .use { stmt => Sync [F ].blocking { stmt.executeUpdate() } }
46
+ val preparedStmts =
47
+ for (sql, paramList) <- stmts.map(x => (x.sql, x.paramList))
48
+ yield prepareStatement(sql, paramList)
49
+ .use(stmt => Sync [F ].blocking(stmt.executeUpdate()))
43
50
44
- preparedStmts.fold (Sync [F ].pure(0 )) { (acc, curr) => acc *> curr } *>
45
- Sync [F ].blocking { conn.commit() }
46
-
47
- private def prepareStatement (querySql : String , paramList : List [? ]) =
48
- Resource .make { Sync [F ].blocking { conn.prepareStatement(querySql) } }
49
- { stmt => Sync [F ].blocking { stmt.close() } }
50
- .map { stmt => parameteriseStatement(stmt, paramList) }
51
+ preparedStmts.fold(Sync [F ].pure(0 ))((acc, curr) => acc *> curr) *>
52
+ Sync [F ].blocking(conn.commit())
51
53
54
+ private def prepareStatement (querySql : String , paramList : List [? ]) =
55
+ Resource
56
+ .make(Sync [F ].blocking(conn.prepareStatement(querySql))) { stmt =>
57
+ Sync [F ].blocking(stmt.close())
58
+ }
59
+ .map(stmt => parameteriseStatement(stmt, paramList))
52
60
53
61
private object LogographContext :
54
62
55
- def extractResults [T <: DbDataSet ] (resultSet : ResultSet , typeInfo : TypeInfo ) =
63
+ def extractResults [T <: DbDataSet ](resultSet : ResultSet , typeInfo : TypeInfo ) =
56
64
57
65
val results = ListBuffer [T ]()
58
66
59
- val constructor = Class .forName(typeInfo.fullClassName)
60
- .getConstructors()
61
- .head
62
-
67
+ val constructor =
68
+ Class
69
+ .forName(typeInfo.fullClassName)
70
+ .getConstructors()
71
+ .head
63
72
64
73
while resultSet.next() do
65
74
val ctorArgs = getCtorArgs(resultSet, typeInfo.elemTypes)
66
- results += constructor.newInstance(ctorArgs : _ * ).asInstanceOf [T ]
75
+ results += constructor.newInstance(ctorArgs* ).asInstanceOf [T ]
67
76
68
77
results.toList
69
78
70
- def parameteriseStatement (stmt : PreparedStatement , params : List [? ]) =
79
+ def parameteriseStatement (stmt : PreparedStatement , params : List [? ]) =
71
80
72
81
for i <- 0 to params.size - 1 do
73
82
params(i) match
@@ -83,17 +92,17 @@ private object LogographContext:
83
92
case other @ _ => throw InvalidParameterType (other)
84
93
stmt
85
94
86
- def getCtorArgs (resultSet : ResultSet , paramTypes : List [String ]) =
95
+ def getCtorArgs (resultSet : ResultSet , paramTypes : List [String ]) =
87
96
88
97
for i <- 0 to paramTypes.size - 1
89
- yield paramTypes(i) match
90
- case " Int" => resultSet.getInt(i + 1 )
91
- case " Long" => resultSet.getLong(i + 1 )
92
- case " Float" => resultSet.getFloat(i + 1 )
93
- case " Double" => resultSet.getDouble(i + 1 )
94
- case " Boolean" => resultSet.getBoolean(i + 1 )
95
- case " String" => resultSet.getString(i + 1 )
96
- case " BigDecimal" => resultSet.getBigDecimal(i + 1 )
97
- case " date" => resultSet.getDate(i + 1 )
98
- case " time" => resultSet.getTime(i + 1 )
99
- case other @ _ => throw InvalidQueryResultType (other)
98
+ yield paramTypes(i) match
99
+ case " Int" => resultSet.getInt(i + 1 )
100
+ case " Long" => resultSet.getLong(i + 1 )
101
+ case " Float" => resultSet.getFloat(i + 1 )
102
+ case " Double" => resultSet.getDouble(i + 1 )
103
+ case " Boolean" => resultSet.getBoolean(i + 1 )
104
+ case " String" => resultSet.getString(i + 1 )
105
+ case " BigDecimal" => resultSet.getBigDecimal(i + 1 )
106
+ case " date" => resultSet.getDate(i + 1 )
107
+ case " time" => resultSet.getTime(i + 1 )
108
+ case other @ _ => throw InvalidQueryResultType (other)
0 commit comments