Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/db/sqlite/db.janet
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,9 @@
(set params (put (table ;(kvs (args 0))) :db/table nil))
(set options (struct ;(drop 1 args)))))

(->> (sql/insert table-name params options)
(execute2)
(last-inserted table-name)))

(-> (sql/insert table-name params options)
(query2 table-name)
(get 0)))

(defn insert-all
`Takes an optional db connection, a table name and an array of dictionaries,
Expand Down
3 changes: 2 additions & 1 deletion src/db/sqlite/sql.janet
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@
sql (string "insert into " (snake-case table-name) " (" columns ") values (" vals ")")
sql (if (options :on-conflict)
(string sql " " (on-conflict-clause options))
sql)]
sql)
sql (string sql " returning *")]
[sql ;(array/concat (sql-params params) (if (options :set) (sql-params (options :set)) @[]))]))


Expand Down
17 changes: 16 additions & 1 deletion test/db/sqlite/db-test.janet
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@


(test "upsert with nothing"
(is (deep= @{:id 1 :title "title 1" :db/table :post :author-id 1}
(is (deep= nil
(db/insert :post
{:title "title 1" :author-id 1 :id 1}
:on-conflict :id
Expand Down Expand Up @@ -244,6 +244,21 @@
(is (deep= @{:id 6 :title "title 6" :body "body 6" :db/table :post}
(db/delete :post 6))))

(test "insert new row"
(is (deep= @{:id 8 :title "title 2" :db/table :post :author-id 2}
(db/insert :post {:title "title 2" :author-id 2}))))

(test "insert new row"
(is (deep= @{:id 9 :title "title 2" :db/table :post :author-id 2}
(db/insert :post {:title "title 2" :author-id 2}))))

(test "upsert second to last row inserted, expecting to get back the relevant row updated"
(is (deep= @{:id 8 :title "title 1" :db/table :post :author-id 2}
(db/insert :post
{:title "title 1" :author-id 2 :id 8}
:on-conflict :id
:do :update
:set {:title "title 1"}))))

(test "delete all"
(is (deep= @[] (do
Expand Down
8 changes: 4 additions & 4 deletions test/db/sqlite/sql-test.janet
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@


(test "insert"
(is (= ["insert into account (name) values (?)" "joy"]
(is (= ["insert into account (name) values (?) returning *" "joy"]
(sql/insert :account {:name "joy"}))))


(test "insert with multiple params"
(is (= ["insert into account (password, name) values (?, ?)" "secret" "joy"]
(is (= ["insert into account (password, name) values (?, ?) returning *" "secret" "joy"]
(sql/insert :account {:name "joy" :password "secret"}))))


(test "insert with null param"
(is (= ["insert into account (name) values (?)" nil]
(is (= ["insert into account (name) values (?) returning *" nil]
(sql/insert :account {:name 'null}))))


(test "insert with multiple params with dashes"
(is (= ["insert into account (password, name, created_at) values (?, ?, ?)"
(is (= ["insert into account (password, name, created_at) values (?, ?, ?) returning *"
"secret" "joy" "created-at"]
(sql/insert :account {:name "joy" :password "secret" :created-at "created-at"}))))

Expand Down