Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Saturday, 26 September 2009

Clojure: Getting started with Clojure + Oracle

I had a hard time googling on how to connect clojure to an oracle database using clojure's sql library (i.e. clojure.contrib.sql). In the end I had to look into the code to see how they were contructing the jdbc url. At work, we already have a properties file which contains the driver class and url to connect to oracle via jdbc. The content looks something like this:

DBURL=jdbc\:oracle\:thin\:@localhost\:1655\:ORCL
DRIVER=oracle.jdbc.OracleDriver
DBUSER=john
DBPASSWORD=password

To translate that to a database spec to be used by clojure.contrib.sql, I had this function:

(defn get-db-spec
  [properties]
  (let [classname (.getProperty properties "DRIVER")
        url (.getProperty properties "DBURL")
        user (.getProperty properties "DBUSER")
        password (.getProperty properties "DBPASSWORD")
        groups (next(first (re-seq #"jdbc:(\w+):(.+)" url)))]
    {:classname classname
     :subprotocol (first groups)
     :subname (second groups)
     :url url
     :user user
     :password password}))

It's kinda odd that I had to deconstruct the url and then have the library reconstruct it again. Probably there's a better way to do this, but there are more interesting things to worry about :P

Thursday, 28 February 2008

Haskell: A simple database application with takusen + sqlite3

Takusen is a Haskell library to access databases. The easiest way to get takusen by darcs. If you don't have darcs installed, you can learn about it from here. If you don't want to use darcs then you would have to download the tar file and extract it. Instructions to install Takusen can be found here. If you're too lazy to read that here's how would install takusen with darcs:
$ mkdir takusen
$ cd takusen
$ darcs get http://darcs.haskell.org/takusen
$ ghc --make Setup
$ Setup configure
$ Setup build
$ Setup install

Next, we will create a sqlite3 database with a simple table tbl1:
$ mkdir sqlite_demo
$ cd sqlite_demo
$ sqlite3 demo.db
SQLite version 3.5.6
Enter ".help" for instructions
sqlite> create table tbl1 (key TEXT, value TEXT);
sqlite> insert into table tbl1 values('k1', 'v1');
sqlite> insert into table tbl1 values('k2', 'v2');
sqlite> insert into table tbl1 values('k3', 'v3');
sqlite> .quit

Create sqlite_demo.hs and enter this:
{-# OPTIONS -fglasgow-exts #-}
{-# OPTIONS -fallow-overlapping-instances #-}
module Main (main) where
import Database.Sqlite.Enumerator
import Control.Monad.Trans (liftIO)
import System.Environment

query1Iteratee :: (Monad m) => String -> String -> IterAct m [(String, String)]
query1Iteratee a b accum = result' ((a,b):accum)

main :: IO ()
main = do
flip catchDB reportRethrow $
withSession (connect "demo.db") (do
let iter (s::String) (_::String) = result s
result <- doQuery (sql "select * from tbl1") query1Iteratee []      
liftIO $ putStrLn $ show result            
)
Compile with the following command and run:
$ ghc --make sqlite_demo -o sqlite_demo
$ ./sqlite_demo
You should get this:
[("k3","v3"),("k2","v2"),("k1","v1")]

Wednesday, 16 January 2008

F#: A simple oracle database application

The following is an example of how to connect to an Oracle database, retrieve some results and display them in a DataGrid in F#:
(* Use lightweight F# syntax *)
#light

#r @"System.Data.OracleClient.dll";;

(* Import managed assemblies *)
open System
open System.Data
open System.Data.Common
open System.Data.OracleClient
open System.Collections
open System.Text
open System.Windows.Forms

let form =
let f = new Form()
f.Visible <- true
f.TopMost <- true
f.Text <- "F# Query"
f.Height <- 600
f.Width <- 800
f
let grid =
let g = new DataGrid()
g.Dock <- DockStyle.Fill
g
form.Controls.Add(grid)
let connection_string = "Server=SID;Uid=johndoe;Pwd=password;"
let sql = "select * from tblwb_job"
let data_table = new Data.DataTable()
try
let connection = new OracleClient.OracleConnection(connection_string)
let command = new OracleClient.OracleCommand (sql, connection)
let adapter = new OracleDataAdapter(command)
let r = adapter.Fill(data_table)
connection.Close()
with
| :? System.InvalidOperationException as ex ->
ignore(MessageBox.Show(ex.Message))
| _ ->
ignore(MessageBox.Show("Unknown Exception"))

grid.DataSource <- data_table
do Application.Run(form)

Pretty sad that I couldn't play with Haskell as much as I want. At least, I can work on some functional language while working on real work.