Code Monkey home page Code Monkey logo

pod-babashka-go-sqlite3's Introduction

pod-babashka-go-sqlite3

A babashka pod for interacting with sqlite3.

Implemented using the Go go-sqlite3 and transit libraries.

Usage

Load the pod and pod.babashka.go-sqlite3 namespace:

(ns sqlite3-script
  (:require [babashka.pods :as pods]))

(pods/load-pod 'org.babashka/go-sqlite3 "0.1.0")
(require '[pod.babashka.go-sqlite3 :as sqlite])

The namespace exposes two functions: execute! and query. Both accept a path to the sqlite database and a query vector:

(sqlite/execute! "/tmp/foo.db"
  ["create table if not exists foo (the_text TEXT, the_int INTEGER, the_real REAL, the_blob BLOB)"])

;; This pod also supports storing blobs, so lets store a picture.
(def png (java.nio.file.Files/readAllBytes (.toPath (io/file "resources/babashka.png"))))

(sqlite/execute! "/tmp/foo.db"
  ["insert into foo (the_text, the_int, the_real, the_blob) values (?,?,?,?)" "foo" 1 3.14 png])
;;=> {:rows-affected 1, :last-inserted-id 1}

(def results (sqlite/query "/tmp/foo.db" ["select * from foo order by the_int asc"]))
(count results) ;;=> 1

(def row (first results))
(keys row) ;;=> (:the_text :the_int :the_real :the_blob)
(:the_text row) ;;=> "foo"

;; Should be true:
(= (count png) (count (:the_blob row)))

Additionally, unparameterised queries are supported if a string is passed

(sqlite/query "/tmp/foo.db" "select * from foo")

Passing any other kind of data apart from a string or a vector will throw.

See test/script.clj for an example test script.

HoneySQL

HoneySQL is a babashka-compatible library for turning Clojure data structures into SQL.

(ns honeysql-script
  (:require [babashka.deps :as deps]
            [babashka.pods :as pods]))

;; Load HoneySQL from Clojars:
(deps/add-deps '{:deps {honeysql/honeysql {:mvn/version "1.0.444"}}})

(require '[honeysql.core :as sql]
         '[honeysql.helpers :as helpers])

(pods/load-pod 'org.babashka/go-sqlite3 "0.1.0")
(require '[pod.babashka.go-sqlite3 :as sqlite])

(sqlite/execute! "/tmp/foo.db" ["create table if not exists foo (col1 TEXT, col2 TEXT)"])

(def insert
  (-> (helpers/insert-into :foo)
      (helpers/columns :col1 :col2)
      (helpers/values
       [["Foo" "Bar"]
        ["Baz" "Quux"]])
      sql/format))
;; => ["INSERT INTO foo (col1, col2) VALUES (?, ?), (?, ?)" "Foo" "Bar" "Baz" "Quux"]

(sqlite/execute! "/tmp/foo.db" insert)
;; => {:rows-affected 2, :last-inserted-id 2}

(def sqlmap {:select [:col1 :col2]
             :from   [:foo]
             :where  [:= :col1 "Foo"]})

(def select (sql/format sqlmap))
;; => ["SELECT col1, col2 FROM foo WHERE col1 = ?" "Foo"]

(sqlite/query "/tmp/foo.db" select)
;; => [{:col1 "Foo", :col2 "Bar"}]

See test/honeysql.clj for a HoneySQL example script.

Build

Requirements

  • Go 1.15+ should be installed.
  • Clone this repo.
  • Run go build -o pod-babashka-go-sqlite3 main.go to compile the binary.

License

Copyright © 2020-2021 Michiel Borkent and Rahul De

License: BSD 3-Clause

pod-babashka-go-sqlite3's People

Contributors

borkdude avatar eval avatar hangyas avatar judepayne avatar lispyclouds avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pod-babashka-go-sqlite3's Issues

java.net.UnknownHostException while executing (pods/load-pod 'org.babashka/go-sqlite3 "0.0.1")

Trying to load the sqlite3 pod gives me the java.net.UnknownHostException.

----- Error --------------------------------------------------------------------
Type: java.net.UnknownHostException
Message: raw.githubusercontent.com
Location: /home/mz/update_env_details.clj:2:1

----- Context ------------------------------------------------------------------
1: (require '[babashka.pods :as pods])
2: (pods/load-pod 'org.babashka/go-sqlite3 "0.0.1")
^--- raw.githubusercontent.com
3: (require '[pod.babashka.go-sqlite3 :as sqlite])
.
.
.

How can I load the pod locally by manually downloading from github?

The pod gets stuck when execute! is called with a string

I've played around a bit with this pod and I mistakenly called execute! with a string instead of a vector a couple of times.

(ns foo
  (:require [babashka.pods :as pods]))

(pods/load-pod 'org.babashka/go-sqlite3 "0.0.1")
(require '[pod.babashka.go-sqlite3 :as sqlite])

(sqlite/execute! "temp.db" "create table if not exists foo(my_text TEXT)")

What happens is that you get the following error message and babashka (v 0.7.4) never exits:

% bb foo.clj 
panic: interface conversion: interface {} is string, not []interface {}

goroutine 1 [running]:
main.parseQuery(0xc00001e050, 0x45, 0x0, 0x42e92c0, 0xc000010048, 0x42e92c0, 0xc000010048, 0xc00012df50, 0x4108a05, 0x436dca0, ...)
	/Users/distiller/project/main.go:102 +0x286
main.processMessage(0xc00006c180)
	/Users/distiller/project/main.go:150 +0x151
main.main()
	/Users/distiller/project/main.go:205 +0x39

A bit more descriptive error message and not getting stuck would be handy.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.