Skip to content

Synesso/scweery

Repository files navigation

Scweery

Ohai there! Thanks for coming to see Scweery. I am happy to have you here!

Scweery was a tiny plaything of mine back in 2009 when I was first getting into Scala. It was only ever just a pup. It can only do a few simple tricks and will probably poop on your rug. But please feel free to play with it anyway. And do let me know what you think.

But what is it?

Scweery is a teeny weeny wrapper around JDBC that could be useful to you if you want to run SQL against databases and instantiate Scala objects from the data.

For instance, just say you want to manipulate some data in a table as a list of objects, you could do something like this:

using(petsDB) { connection =>
  val findHogsQuery = "select name, gender from pets where family='erinaceidae' order by cuteness" 
  val listOfHedgehogs = connection.inferListOf[Hedgehog](findHogsQuery) { row =>
    new Hedgehog(row.string("name"), row.string("gender"))
  }
  listOfHedgehogs.foreach(_.hug)
}

Scweery will take care of opening the connection, and closing it for you when you're done. Removing the hedgehog quills from your flesh is your problem.

How do I build it?

You should use Simple Build Tool - it is the most triumphant of build tools for cool Scala projects. When you have it installed, go to the project base and create the package. Do you need a hint?

$ sbt package

Is there a repository?

Rather than build it, you can just fetch it from the repo. Use the following magic in your SBT project definition:

  val badgerhunt = "Scweery Repo" at "http://github.com/Synesso/scweery/raw/master/repo/"
  val scweery = "net.badgerhunt" % "scweery" % "0.1.2"

How do I use it?

Make sure both the Scweery jar and the JDBC driver for your database are in your classpath. Then import the Scweery classes and static methods into your Scala class/object (or the REPL) with:

import scweery._  // Connection
import Scweery._  // use & infer methods

Defining a DB connection

To work with a database connection, you need to define the JDBC connection details in a happy little container called Connection. It needs a JDBC connection string, a username and a password. Yup, a plaintext password.

val petsDB = new Connection("jdbc:vendorx:localhost", "synesso", "e1337^hacksaw")

Using a connection

Nothing's happened with the database yet. It's just a bean. To open a connection you first declare that you are goin to use it.

use(petsDB) { connection =>
  // connection is open!
}

The connection is opened at the start of the block and will be closed automatically when the block is done.

Infering an object from a connection

You can also use the database to make things. You can, as long as you know what it is you want to make. That would look more like this:

infer[FurryFriend](petsDB) { connection =>
  // connection is open and ready for you to make a FurryFriend!
}

Invoking the SQL

With one of these crazy mongrels in your namespace you can use it or you can infer lists of stuff with it. Use it when you don't need to create a list of objects.

use(friendsDB) { connection =>
  connection.use("select facebookid from friends") { row =>
    SpamMachine sendFreeGiftTo Friend(row.int("facebookid"))
  }
}

In the example above, the spam machine will be asked to send virtual teddy bears, mafia hits or farmyard animals to all of your "lucky" friends.

Invoking the SQL to infer a list of objects

If you're more of a functional type, you might want to infer a list of things.

infer[Tray[Espresso]](cafeDB) { connection =>
  val drinks = connection.inferListOf[Espresso]("select name, cup_of_excellence from cafes where city='melbourne'") { row =>
    val name = row.string(0)
    val cupOfExcellence = row.string(1)
    new Espresso(name, cupOfExcellence)
  }
  new Tray(drinks)
}

Now you have drinks! It is a Tray of Espresso drinks selected from many well-known Melbourne cafes. Mint as.

Halp! It doesn't do what I want (or what it should).

Please tweet it @scweery.

scweerybird

About

A thin wrapper around JDBC for creating Scala objects from SQL. Designed for use in lightweight scripts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages