Skip to content

Commit

Permalink
Define get-record in terms of find-record.
Browse files Browse the repository at this point in the history
  • Loading branch information
duelinmarkers committed Jul 23, 2009
1 parent 185165d commit 669e85d
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/clj_record/core.clj
Expand Up @@ -64,21 +64,6 @@
(sql/with-query-results rows [(id-query-for (db-spec-for model-name) (table-name model-name))]
(val (first (first rows))))))

(defn get-record
"Retrieves record by id, throwing if not found."
[model-name id]
(connected (db-spec-for model-name)
(sql/with-query-results rows [(format "select * from %s where id = ?" (table-name model-name)) id]
(if (empty? rows) (throw (IllegalArgumentException. "Record does not exist")))
(run-callbacks (merge {} (first rows)) model-name :after-load))))

(defn create
"Inserts a record populated with attributes and returns it."
[model-name attributes]
(let [id (insert model-name attributes)]
(connected (db-spec-for model-name)
(get-record model-name id))))

(defn find-by-sql
"Returns a vector of matching records.
select-query-and-values should be something like
Expand Down Expand Up @@ -106,6 +91,19 @@
[model-name attributes-or-where-params]
(first (find-records model-name attributes-or-where-params)))

(defn get-record
"Retrieves record by id, throwing if not found."
[model-name id]
(or (find-record model-name {:id id})
(throw (IllegalArgumentException. "Record does not exist"))))

(defn create
"Inserts a record populated with attributes and returns it."
[model-name attributes]
(let [id (insert model-name attributes)]
(connected (db-spec-for model-name)
(get-record model-name id))))

(defn update
"Updates by (partial-record :id), updating only those columns included in partial-record."
[model-name partial-record]
Expand Down

0 comments on commit 669e85d

Please sign in to comment.