Git Product home page Git Product logo

ppx_rapper's People

Contributors

andreasdahl avatar anmonteiro avatar apeschar avatar bikallem avatar darioteixeira avatar kit-ty-kate avatar leonidas-from-xiv avatar rizo avatar roddyyaga 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  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  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  avatar  avatar

ppx_rapper's Issues

Need Lower Bound on the Caqti Dependencies

The Caqti_request.Infix module was introduced in version 1.7.0, so I think that must be the lower bond for all sub-packages. We can probably sort out the published opams in ocaml/opam-repository#22070. It's only failing for the lwt package, but I presume because that's where the runtime-level testing happens.

insert and return id

hey @roddyyaga , I can't understand how to simply insert a record and get the resulting id

module User = struct
  type t = { name: string; address: string }
end
    [%rapper execute {sql|
        INSERT INTO public.users(name, address)
        VALUES (
          %string{name},
          %string{address}
        )
        RETURNING @int{id}
     |sql} record_in]

but it seems that the resulting type is incorrect - I get Type unit is not compatible with type int

also is there a way to omit the types after % and @ when using record, since those are already defined in the records themselves?

Deprecation alert with Caqti

Alert deprecated: Caqti_request.exec
Replaced by the Infix module.
File "lib/storage.ml", lines 2-11, characters 2-15:
 2 | ..[%rapper
 3 |     execute
 4 |       {sql|CREATE TABLE IF NOT EXISTS logs (
 5 |             id uuid PRIMARY KEY NOT NULL,
 6 |             created_at timestamp NOT NULL,
 7 |             message varchar NOT NULL,
 8 |             level varchar NOT NULL,
 9 |             origin varchar NOT NULL,
10 |             stack varchar NOT NULL
11 |         )|sql}]

Which infix operator replaced execute and how would I use it with rapper?

insert multiple values

I'm trying to insert multiple values in a table

Inserting a single value works:

type my_thing = { a: int; b: string }
let insert_my_thing = [%rapper execute {sql|
  INSERT INTO my_things (a, b)
  VALUES (%int{a}, %string{b});
|sql} record_in]

(*
my_thing ->
(module Caqti_lwt.CONNECTION) ->
(unit, [> Caqti_error.call_or_retrieve ]) result Lwt.t
*)

but I can't get how to use the list syntax to insert my_thing list:

type my_thing = { a: int; b: string }
let insert_my_thing = [%rapper execute {sql|
  INSERT INTO my_things (a, b)
  VALUES %list{(%int{a}, %string{b})};
|sql} record_in]
Fatal error: exception "Assert_failure ppx/ppx_rapper.ml:51:6"

How to represent SELECT EXISTS and the like

hey @roddyyaga
I tried to write some simple Postgres queries, but can't figure out how I'm supposed to represent these:

  type regclass_response = { to_regclass : string option }
  let get_something = [%rapper get_opt {sql| SELECT to_regclass(my_table_name) |sql} record_out]
|| Fatal error: exception Error in ppx_rapper: 'record_out' should not be set when there are no output parameters

So I guess I'm supposed to somehow include @string?{to_regclass} in the query, but in practice the SELECT doesn't include such slot like SELECT @string?{to_regclass} FROM table

It's the same with:

SELECT EXISTS (
  SELECT 1
  FROM pg_tables
  WHERE schemaname = 'schema_name' AND tablename = 'table_name'
);

what should the record_out for SELECT EXISTS be and how it should be represented?

Support a list of tuples as input parameters.

I'd like to be able to write a query like this:

WITH foo(a, b) AS (values (1::int, 'a'::text), (2, 'b') ) SELECT * FROM foo;

that could translate to:

 [%rapper
    get_many
      {sql|
      WITH foo(a, b) AS (values %list{%tup2{int, string}}) ...
      |sql}]

The tup2, tup3, ... is not supported at this time though and although you can create a custom type with a tuple they generate only one parameter, so as an input type it doesn't work.

(as discussed on discord)

Proposed breaking change to API - generated queries to have DB connection as last parameter rather than first

Heads up for any ppx_rapper users: I'm planning on reordering the signature of generated queries by moving the connection parameter.

let update =
  [%rapper
    execute
      {sql|
      UPDATE things
      SET value = %string{value}
      WHERE id = %int{id}
      |sql}
      record_in]

will go from having type

(module Caqti_lwt.CONNECTION) ->
{id: int; value: string} -> (unit, [> Caqti_error.call_or_retrieve ]) result Lwt.t

to

{id: int; value: string} -> (module Caqti_lwt.CONNECTION) -> (unit, [> Caqti_error.call_or_retrieve ]) result Lwt.t

This is to make using queries with connection pools nicer. With a function execute defined as

let execute query = Caqti_lwt.Pool.use query pool

you will be able to do

let calls_update { id; value } =
  execute @@ update { id; key; value; account_id }

rather than

let calls_update { id; value } =
  execute @@ fun conn -> update conn { id; key; value; account_id }

Please let me know if this would cause any problems for you (other than having to update affected code).

Support all request processing functions

Hi, thanks for the work!
The ppx only allows bindings to exec, find, find_opt and collect_list. Are there plans to also support fold, fold_s and iter_s (and rev_collect_list for the sake of completeness), or were they omitted on purpose?

dune utop fails

I'm using ppx_rapper in a project that builds and tests successfully, but I cannot run utop in it. dune utop test (for example) results in:

File "_none_", line 1:
Error: Module `Pg_query' is unavailable (required by `Ppx_rapper')

Pg_query is provided by the pg_query package, which is available: ppx_wrapper depends on it directly, and regular builds would fail otherwise.

I've created a repo that minimally reproduces the failure; see the README for an invocation to create a fresh switch, build, and try to run a toplevel:

https://github.com/cemerick/rapper_utop

Maybe I'm doing something wrong, but I've been stumped so far. ๐Ÿค”

MariaDB/SQLite support

Although the README states that ppx_rapper is intended for use with PostgreSQL, it actually seems to work fine with MariaDB. Queries that are not also valid PostgreSQL syntax (mainly INSERT ... SET ...) need syntax_off, but that seems to be it.

Perhaps that should be documented? I'm willing to submit a PR for that, I just wanted to know if you have any thoughts on this, or whether I'm missing something that would make this library not (yet) useful with MariaDB.

schema creation and semicolon

Hello,
How do I create a schema using ppx_rapper with the semicolon restriction, here's my schema creation code, which happen to not work.
I'm using the sqlite3 backend on ubuntu 20.04 and OCaml 4.13.1:

Thanks!

let init_db =
    [%rapper execute {sql|
        DROP TABLE IF EXISTS client

        CREATE TABLE IF NOT EXISTS client(
                    host TEXT NON NULL,
                    useragent TEXT
                )

        DROP TABLE IF EXISTS request

        CREATE TABLE IF NOT EXISTS request(
                    since_begin REAL,
                    request TEXT NON NULL,
                    retcode INTEGER,
                    size INTEGER,
                    referrer TEXT
                )

        CREATE INDEX i0 ON request(request)

        CREATE INDEX i1 ON request(referrer)

        DROP TABLE IF EXISTS visit

        CREATE TABLE IF NOT EXISTS visit(
                    begin REAL,
                    id_client INTEGER,
                    spam BOOLEAN,
                    resources INTEGER,
                    pages INTEGER
                )

        CREATE INDEX i2 ON visit(id_client)

        DROP TABLE IF EXISTS req_visit

        CREATE TABLE IF NOT EXISTS req_visit(
                    id_visit INTEGER,
                    id_request INTEGER
                )

        CREATE INDEX i3 ON req_visit(id_visit)

        CREATE INDEX i4 ON req_visit(id_request)
    |sql} syntax_off]

Don't allow trailing semicolon in SQL statement

I'm not sure about other drivers but at least with SQLite3 driver, it raises error if a statement ended with a semicolon.
I think ppx_rapper should at least catch this because I ran into this issues so many times

CREATE TABLE %string{table_name} results in an error

When creating a table, this works:

    [%rapper execute {sql| CREATE TABLE my_table ( ... ) |sql}]

but this:

    let table_name = "my_table"
    [%rapper execute {sql| CREATE TABLE %string{table_name} ( ... ) |sql}] ~table_name

results in

Fatal error: exception Error in ppx_rapper: Syntax error in SQL: 'syntax error at or near "?"'

is this expected or my syntax is wrong?

Align function_out semantics with ppx_deriving's make plugin when fields are optional

First, thank you so much for this ppx, it makes working with postgresql in OCaml extremely pleasant ๐Ÿค—

I am regularly running into cases where I need to emit multiple output types, but cannot use the record_out option because of same-named columns and fields among the records involved. Conveniently, the function_out option pairs perfectly with ppx_deriving's make plugin, but unfortunately only when no optional fields / arguments are involved. When a type does have an optional field, the ppx generates a make function like so:

val make: id:int -> name:string -> ?created:Ptime.t -> unit -> 'a

...while ppx_rapper continues to expect a function like:

id:int -> name:string -> created:Ptime.t option -> 'a

ppx_deriving's approach makes a lot of sense for typical usage (a mandatory labeled option argument does seem sort of odd), so I can't fault it much.

Are you interested in squaring this circle? Of course one option is to simply change (break) how function_out works; alternatively, another _out option could be added to accommodate ppx_deriving's make convention.

Unbound module Rapper_helper

I am trying to use a rapper to create sql queries but I keep getting that error. Do I need to open something? I am not used to preprocessors so I don't really know what I need to have in scope.

more examples of connection creation, please!

Hello! My first encounter with Lwt is by using ppx_rapper_lwt (even first encounter with a monadic pattern to conform to) and I lack something which will probably looks trivial to you: a connection (db handler) creation and use example! a non pooled one, because I'm using sqlite3. I know, I know, shame on me, I'm an eternal beginner with ocaml.

Also, is the Db handler always a whole module or is there a lighter alternative?

Thank U!

Async

Hi,

Thanks for the nice library. Are you open to make this ppx/library compatible with async library in addition to lwt? It probably needs to be split into two packages, ppx_rapper-lwt and ppx_rapper.async ?

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.