$ sbt run
--- Creating Table ---
Table 'persons' created (or verified).
--- Inserting Data ---
Inserted data: Person(id=1, name=Alice, age=30)
Inserted data: Person(id=2, name=Bob, age=N/A)
Inserted data: Person(id=3, name=Charlie, age=25)
--- Querying Data (All) ---
Person(id=1, name=Alice, age=30)
Person(id=2, name=Bob, age=N/A)
Person(id=3, name=Charlie, age=25)
--- Querying Data (Conditional) ---
Persons older than 28:
Person(id=1, name=Alice, age=30)
--- Updating Data ---
Updated Alice's age.
Updated Alice: Person(id=1, name=Alice, age=31)
--- Deleting Data ---
Deleted Charlie.
--- Querying Data Again (All) ---
Person(id=1, name=Alice, age=31)
Person(id=2, name=Bob, age=N/A)
Processing finished.
Created
April 18, 2025 07:32
-
-
Save y-yoshinoya/897dc9ccba3f6c1dfaf4a3f63a288629 to your computer and use it in GitHub Desktop.
Squeryl sample (Scala 3, SQLite)
This file contains hidden or 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
ThisBuild / scalaVersion := "3.6.4" | |
val squerylVersion = "0.10.1" | |
val sqliteJdbcVersion = "3.49.1.0" | |
libraryDependencies ++= Seq( | |
"org.squeryl" %% "squeryl" % squerylVersion, | |
"org.xerial" % "sqlite-jdbc" % sqliteJdbcVersion, | |
"org.slf4j" % "slf4j-nop" % "2.0.17" | |
) |
This file contains hidden or 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
import org.squeryl.adapters.SQLiteAdapter | |
import org.squeryl.{Schema, SessionFactory, Session, KeyedEntity} | |
import java.sql.DriverManager | |
import org.squeryl.PrimitiveTypeMode._ | |
class Person( | |
val id: Long, | |
var name: String, | |
var age: Option[Int] | |
) extends KeyedEntity[Long] { | |
def this() = this(0, "", None) | |
override def toString: String = s"Person(id=$id, name=$name, age=${age.getOrElse("N/A")})" | |
} | |
object AppSchema extends Schema { | |
val persons = table[Person]("persons") | |
} | |
object Main extends App { | |
val databaseFileName = "mydatabase.db" | |
val jdbcUrl = s"jdbc:sqlite:$databaseFileName" | |
val driverClass = "org.sqlite.JDBC" | |
def setupDatabase(): Unit = { | |
Class.forName(driverClass) | |
SessionFactory.concreteFactory = Some(() => | |
Session.create( | |
DriverManager.getConnection(jdbcUrl), | |
new SQLiteAdapter | |
) | |
) | |
} | |
def run(): Unit = { | |
setupDatabase() | |
transaction { | |
try { | |
println("\n--- Creating Table ---") | |
try { AppSchema.create } catch { _ => } | |
println("Table 'persons' created (or verified).") | |
println("\n--- Inserting Data ---") | |
val alice = AppSchema.persons.insert(new Person(0, "Alice", Some(30))) // id=0 expects auto-increment | |
val bob = AppSchema.persons.insert(new Person(0, "Bob", None)) // age is NULL | |
val charlie = AppSchema.persons.insert(new Person(0, "Charlie", Some(25))) | |
println(s"Inserted data: $alice") | |
println(s"Inserted data: $bob") | |
println(s"Inserted data: $charlie") | |
println("\n--- Querying Data (All) ---") | |
val allPersons = from(AppSchema.persons)(select(_)) // SELECT * FROM persons | |
allPersons.foreach(println) | |
println("\n--- Querying Data (Conditional) ---") | |
val personsOver28 = from(AppSchema.persons)(p => where(p.age > Some(28)) select(p)) | |
println("Persons older than 28:") | |
personsOver28.foreach(println) | |
println("\n--- Updating Data ---") | |
update(AppSchema.persons) { p => where(p.id === alice.id).set(p.age := Some(31)) } | |
println("Updated Alice's age.") | |
val updatedAlice = from(AppSchema.persons)(p => where(p.id === alice.id) select(p)).headOption | |
println(s"Updated Alice: ${updatedAlice.getOrElse("Not found")}") | |
println("\n--- Deleting Data ---") | |
AppSchema.persons.deleteWhere(p => p.name === "Charlie") | |
println("Deleted Charlie.") | |
println("\n--- Querying Data Again (All) ---") | |
val remainingPersons = from(AppSchema.persons)(select(_)) | |
remainingPersons.foreach(println) | |
} catch { | |
case e: Exception => | |
println(s"An error occurred: ${e.getMessage}") | |
e.printStackTrace() | |
} | |
} | |
println("\nProcessing finished.") | |
} | |
run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment