Skip to content

Commit

Permalink
Add PostgreSQL storage backend, add/get messages
Browse files Browse the repository at this point in the history
  • Loading branch information
fhd committed Aug 16, 2012
1 parent 6adca70 commit 9395f31
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
2 changes: 2 additions & 0 deletions project.clj
@@ -1,8 +1,10 @@
(defproject de.viaboxx.flurfunk/flurfunk-server "0.1.0-SNAPSHOT"
:description "The Funk of the Flur"
:dependencies [[org.clojure/clojure "1.3.0"]
[org.clojure/java.jdbc "0.2.3"]
[compojure/compojure "0.6.5"]
[fleetdb-client "0.2.2"]
[postgresql/postgresql "9.1-901.jdbc4"]
[ring/ring-jetty-adapter "0.3.11"]]
:plugins [[lein-ring "0.6.3"]]
:ring {:handler flurfunk.server.routes/app}
Expand Down
78 changes: 68 additions & 10 deletions src/flurfunk/server/storage.clj
@@ -1,8 +1,9 @@
(ns flurfunk.server.storage
"Storage and retrieval of objects."
(:require [fleetdb.client :as db]
[clojure.string :as string]
[clojure.walk :as walk]))
(:require [clojure.string :as string]
[clojure.java.jdbc :as sql]
[clojure.walk :as walk]
[fleetdb.client :as db]))

(def message-limit 200)

Expand Down Expand Up @@ -80,14 +81,71 @@
[this]
(client ["delete", "messages"])))

(def ^{:private true} postgresql-db
(or (System/getenv "DATABASE_URL")
"postgresql://flurfunk:flurfunk@localhost:5432/flurfunk"))

(defn- postgresql-messages-table-exists?
[]
(sql/with-query-results results
[(str "SELECT EXISTS(SELECT * FROM information_schema.tables"
" WHERE table_name = 'messages')")]
(boolean (Boolean. (:?column? (first results))))))

(deftype PostgreSQLStorage [] Storage
(storage-get-messages
[this]
(try (sql/with-connection postgresql-db
(if (postgresql-messages-table-exists?)
(vec (sql/with-query-results results
[(str "SELECT akeys(attributes), avals(attributes)"
" FROM messages LIMIT " message-limit)]
(map #(zipmap (map keyword (vec (.getArray (:akeys %))))
(vec (.getArray (:avals %))))
(into [] results))))))
(catch Exception e (.printStackTrace e))))

(storage-get-messages
[this options]
;; TODO: If db exists, query based on options
[])

(storage-add-message
[this message]
(sql/with-connection postgresql-db
(let [entries (map #(str \" (name (first %))\" " => \"" (second %) \")
message)
value-string (string/join ",\n" entries)]
(sql/do-commands
(str "CREATE TABLE IF NOT EXISTS messages"
" (id serial PRIMARY KEY, attributes hstore)")
(str "INSERT INTO messages (attributes) VALUES ('"
value-string "')")))))

(storage-find-message
[this id]
;; TODO: if db exists, query by id
)

(storage-clear-messages
[this]
(sql/with-connection postgresql-db
(sql/do-commands "drop table if exists messages"))))

(defn- make-storage []
(if (= (System/getProperty "flurfunk.db") "fleetdb")
(FleetDBStorage. (db/connect {:host "127.0.0.1" :port 3400}))
(do
(println (string/trim "
Using in-memory database. To use a persistent database, set the system property
\"flurfunk.db\" to \"fleetdb\" or \"postgresql\"."))
(MemoryStorage. (atom [])))))
(case (System/getProperty "flurfunk.db")
"fleetdb" (do
(println "Using FleetDB storage backend.")
(FleetDBStorage. (db/connect {:host "127.0.0.1"
:port 3400})))
"postgresql" (do
(println "Using PostgreSQL storage backend.")
(PostgreSQLStorage.))
(do
(println (string/trim "
Using in-memory storage backend. To use a persistent backend, set the system
property \"flurfunk.db\" to \"fleetdb\" or \"postgresql\"."))
(MemoryStorage. (atom [])))))

(def ^{:private true} storage (make-storage))

Expand Down

0 comments on commit 9395f31

Please sign in to comment.