Created
August 13, 2018 07:53
-
-
Save darekmydlarz/5376531d8bc79b4e9bd01e7a8c3420e1 to your computer and use it in GitHub Desktop.
ROOP - radical object oriented programming - fetching data from jdbc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// before | |
case class Query(table: String, orderKey: String, whereClause: Option[String] = None) { | |
def fetchQuery(): String = { | |
Seq( | |
s"SELECT * FROM $table", | |
whereClause.map(w => s"WHERE $w").getOrElse(""), | |
s"ORDER BY $orderKey ASC" | |
).mkString | |
} | |
def countQuery(): String = { | |
Seq( | |
s"SELECT count(*) AS rowcount FROM $table", | |
whereClause.map(w => s"WHERE $w").getOrElse("") | |
).mkString | |
} | |
} | |
def foobar() { | |
Query q = Query("", "") | |
statement.execute(q.fetchQuery()) | |
} | |
// AFTER | |
case class Query(table: String, orderKey: String, whereClause: Option[String] = None) { | |
private fetchQuery(): String = { | |
Seq( | |
s"SELECT * FROM $table", | |
whereClause.map(w => s"WHERE $w").getOrElse(""), | |
s"ORDER BY $orderKey ASC" | |
).mkString | |
} | |
private countQuery(): String = { | |
Seq( | |
s"SELECT count(*) AS rowcount FROM $table", | |
whereClause.map(w => s"WHERE $w").getOrElse("") | |
).mkString | |
} | |
def rowCount(statement: Statement): Long = { | |
try { | |
val countSet = statement.executeQuery(countQuery()) | |
try { | |
countSet.next() | |
countSet.getLong("rowcount") | |
} finally { | |
countSet.close() | |
} | |
} | |
} | |
def resultSet(statement: Statement): ResultSet = { | |
// execute query and return | |
} | |
} | |
def foobar() { | |
Query q = Query("", "") | |
q.fetchQuery(statement); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment