Skip to content

Instantly share code, notes, and snippets.

@mprokopov
Created May 21, 2021 10:23
Show Gist options
  • Save mprokopov/22abef8524f7b0cc19bde4dad1061b96 to your computer and use it in GitHub Desktop.
Save mprokopov/22abef8524f7b0cc19bde4dad1061b96 to your computer and use it in GitHub Desktop.
babashka honeysql with mysql example
;; CREATE TABLE `station` (
;; `id` int(11) NOT NULL AUTO_INCREMENT,
;; `location` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
;; PRIMARY KEY (`id`)
;; ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
;; CREATE TABLE `rental_order` (
;; `id` int(11) NOT NULL AUTO_INCREMENT,
;; `campervan_id` int(11) NOT NULL,
;; `start_station_id` int(11) NOT NULL,
;; `end_station_id` int(11) NOT NULL,
;; `start_date` date NOT NULL,
;; `end_date` date NOT NULL,
;; PRIMARY KEY (`id`),
;; KEY `IDX_6EC21D77B9D53E94` (`campervan_id`),
;; KEY `IDX_6EC21D7753721DCB` (`start_station_id`),
;; KEY `IDX_6EC21D772FF5EABB` (`end_station_id`),
;; CONSTRAINT `FK_6EC21D772FF5EABB` FOREIGN KEY (`end_station_id`) REFERENCES `station` (`id`),
;; CONSTRAINT `FK_6EC21D7753721DCB` FOREIGN KEY (`start_station_id`) REFERENCES `station` (`id`),
;; CONSTRAINT `FK_6EC21D77B9D53E94` FOREIGN KEY (`campervan_id`) REFERENCES `campervan` (`id`)
;; ) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
(require '[babashka.pods :as pods])
(deps/add-deps '{:deps {honeysql/honeysql {:mvn/version "1.0.444"}}})
(require '[honeysql.core :as hsql])
(pods/load-pod 'org.babashka/mysql "0.0.7")
(def db {:dbtype "mysql"
:host "127.0.0.1"
:dbname "campervan"
:user "root"
:password ""
:port 3306})
(->>
(hsql/format {:select [:rental_order.id, [:s.location :start], [:e.location :end]]
:from [:rental_order]
:join [[:station :s] [:= :s.id :rental_order.start_station_id]
[:station :e] [:= :e.id :rental_order.end_station_id]]
:order-by [[:rental_order.id :asc]]})
(pod.babashka.mysql/execute! db))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment