Giter VIP home page Giter VIP logo

janet-pq's Introduction

janet-pq

Bindings to libpq.

quick-examples

Basic usage:

(import pq)
(def conn (pq/connect "postgresql://localhost?dbname=postgres"))
(pq/exec conn "create table users(name text, data jsonb);")
(pq/exec conn "insert into users(name, data) values($1, $2);" "ac" (pq/jsonb @{"some" "data"}))
(pq/row conn "select * from users where name = $1;" "ac")
{:name "ac" :data @{"some" "data"}}
(pq/all conn "select * from users")
[{:name "ac" :data @{"some" "data"}} ...]
(pq/val conn "select data from users where name = $1;" "ac")
@{"some" "data"}

Transactions:

(import pq)
...
(pq/tx conn {:mode "isolation serializable read only" :retry true}
  (unless (pq/val conn "select ....")
    (pq/rollback conn))
  (pq/val conn "select ...."))

Custom type encoding/decoding:

(import pq)
...

# directly insert custom encoding.
(pq/exec conn "insert into tab(x) values($1);" [TYPEOID ISBINARY BYTES])

# use a method for custom type encoding.
(pq/exec conn "insert into tab(x) values($1);" {:pq/marshal (fn [self] [TYPEOID ISBINARY BYTES])})

# Add a custom type decoder.
(put pg/*decoders* TYPEOID custom-decoder)
(pq/all conn "select * from tab;")

Error handling:

(import pq)
...
(try
  ...
  ([err] (when (pq/error? err)
    (def msg (pq/result-error-field err ...)))))

Special thanks

Jon Staab - The author of the first janet postgres library from which this was inspired, and a core contributor to this library.

janet-pq's People

Contributors

ahungry avatar andrewchambers avatar sonlamho avatar source-c avatar uvtc 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

Watchers

 avatar  avatar  avatar  avatar  avatar

janet-pq's Issues

fast path for decoders

We can use an abstract type to encode type decoders that don't require the initial buffer copy. It would just be a function pointer that takes in a char* and length, and returns a Janet.

This would be especially useful for number types.

fix macos build

on MacOS (especialy for arm64) there is an error saying:

compiling pq.c to build/pq.o...
pq.c:347:31: error: implicitly declaring library function 'floor' with type 'double (double)' [-Werror,-Wimplicit-function-declaration]
const char *fmt = (d == floor(d) && d <= ((double)INT32_MAX) &&
^
pq.c:347:31: note: include the header <math.h> or explicitly provide a declaration for 'floor'
1 error generated.

to avoid this - conditional include to be added, like:

diff --git a/pq.c b/pq.c
index af933b8..c9b8473 100644
--- a/pq.c
+++ b/pq.c
@@ -3,6 +3,10 @@
 #include <janet.h>
 #include <libpq-fe.h>

+#if defined(__APPLE_CC__)
+#include <math.h>
+#endif
+
 typedef struct {
   PGresult *r;
 } JPQresult;

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.