Skip to content

Instantly share code, notes, and snippets.

@darekmydlarz
Created August 13, 2018 07:53
Show Gist options
  • Save darekmydlarz/5376531d8bc79b4e9bd01e7a8c3420e1 to your computer and use it in GitHub Desktop.
Save darekmydlarz/5376531d8bc79b4e9bd01e7a8c3420e1 to your computer and use it in GitHub Desktop.
ROOP - radical object oriented programming - fetching data from jdbc
// 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