Set up the database tables and connection

This commit is contained in:
Aaron Mueller 2013-09-28 23:41:28 +02:00
parent b6ede2c2b0
commit d2d60cf48d
6 changed files with 81 additions and 37 deletions

View file

@ -18,7 +18,11 @@
[javax.mail/mail
javax.jms/jms
com.sun.jdmk/jmxtools
com.sun.jmx/jmxri]]]
com.sun.jmx/jmxri]]
[clj-http "0.7.7"]
[image-resizer "0.1.6"]
[enlive "1.1.1"]
[me.raynes/fs "1.4.5"]]
:ring
{:handler ldview.handler/war-handler,
:init ldview.handler/init,

View file

@ -4,7 +4,8 @@
[noir.util.middleware :as middleware]
[compojure.route :as route]
[taoensso.timbre :as timbre]
[com.postspectacular.rotor :as rotor]))
[com.postspectacular.rotor :as rotor]
[ldview.models.schema :as schema]))
(defroutes app-routes
(route/resources "/")
@ -27,6 +28,8 @@
(timbre/set-config!
[:shared-appender-config :rotor]
{:path "ldview.log" :max-size (* 512 1024) :backlog 10})
(if-not (schema/initialized?) (schema/create-tables))
(timbre/info "ldview started successfully"))

View file

@ -5,20 +5,37 @@
(defdb db schema/db-spec)
(defentity users)
(declare entities contests users sources)
(defn create-user [user]
(insert users
(values user)))
(defentity entries
(belongs-to contests)
(belongs-to users)
(has-many sources))
(defn update-user [id first-name last-name email]
(update users
(set-fields {:first_name first-name
:last_name last-name
:email email})
(where {:id id})))
(defentity contests
(has-many entries))
(defn get-user [id]
(first (select users
(where {:id id})
(limit 1))))
(defentity users
(has-many entities))
(defentity sources
(belongs-to entities))
; (defn create-entry [entry]
; (insert entries
; (values entry)))
;
; (defn update-entry [id id_uid title description user_id contest_id]
; (update entries
; (set-fields {:first_name first-name
; :last_name last-name
; :email email})
; (where {:id id})))
;
; (defn get-user [id]
; (first (select users
; (where {:id id})
; (limit 1))))

View file

@ -0,0 +1,9 @@
(ns ldview.models.entity
(:use korma.core
[ldview.models.db]))
(defn all []
(select entries))
(defn insert [fields]
(insert entries (values fields)))

View file

@ -11,26 +11,37 @@
:password ""
:naming {:keys clojure.string/lower-case
:fields clojure.string/upper-case}})
(defn initialized?
"checks to see if the database schema is present"
[]
(defn initialized? []
(.exists (new java.io.File (str (io/resource-path) db-store ".h2.db"))))
(defn create-users-table
[]
(sql/with-connection db-spec
(sql/create-table
:users
[:id "varchar(20) PRIMARY KEY"]
[:first_name "varchar(30)"]
[:last_name "varchar(30)"]
[:email "varchar(30)"]
[:admin :boolean]
[:last_login :time]
[:is_active :boolean]
[:pass "varchar(100)"])))
(defn create-tables
"creates the database tables used by the application"
[]
(create-users-table))
(defn create-initial-tables []
(sql/with-connection db-spec
(sql/create-table :entries
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
[:ld_uid "INT"]
[:title "VARCHAR(255)"]
[:description "TEXT"]
[:user_id "INT"]
[:contest_id "INT"])
(sql/create-table :users
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
[:name "VARCHAR(255)"])
(sql/create-table :contests
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
[:name "VARCHAR(255)"])
(sql/create-table :sources
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
[:entry_id "INT"]
[:type "VARCHAR(100)"]
[:uri "VARCHAR(255)"])
(sql/do-commands "CREATE INDEX ld_uid_index ON entries (ld_uid)")
(sql/do-commands "CREATE INDEX entry_id_index ON sources (entry_id)")))
(defn create-tables []
(create-initial-tables))

View file

@ -1,7 +1,7 @@
(ns ldview.tasks.scrape
(:require [net.cgrand.enlive-html :as html]
[clj-http.client :as http]
[clojure.java.io :as io]
[clojure.java.io :as io])
(:use [clojure.string :only (split)]
[ldview.util]))