# Macro or DSL for easier DBInterface usage?

**URL:** <https://discourse.julialang.org/t/macro-or-dsl-for-easier-dbinterface-usage/93942>\
**Category:** General Usage\
**Tags:** question, metaprogramming\
**Created:** [February 2, 2023, 5:11pm UTC](https://discourse.julialang.org/t/macro-or-dsl-for-easier-dbinterface-usage/93942 "2023-02-02T17:11:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 2, 2023, 5:11pm UTC](https://discourse.julialang.org/t/macro-or-dsl-for-easier-dbinterface-usage/93942/1 "2023-02-02T17:11:37Z")

</div>

DBInterface is great for using SQL and database queries. However, SQL queries can get lengthy, and the mixing of SQL and Julia code is a bit ugly. I am wondering if there are packages that would replace lines like this

```julia
DBInterface.execute(con, "CREATE TABLE integers(i INTEGER)")

```

and instead allow someone to write something like this

```julia
@dbexec con begin
    CREATE TABLE integers(i INTEGER)
end

```

where the stuff between `begin ... end` would be assumed to be proper syntax for whatever backend it is being sent to.

Also I am aware of FunSQL.jl which is a cool project but not what I’m looking for right now.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [February 2, 2023, 5:33pm UTC](https://discourse.julialang.org/t/macro-or-dsl-for-easier-dbinterface-usage/93942/2 "2023-02-02T17:33:30Z")

</div>

No, macros still need to be valid Julia syntax, i.e. it has to parse to a valid Julia expression. to do something like `integers( INTEGER)` you will need a string macro.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 2, 2023, 5:38pm UTC](https://discourse.julialang.org/t/macro-or-dsl-for-easier-dbinterface-usage/93942/3 "2023-02-02T17:38:11Z")

</div>

Not quite sure I understand. Presumably what this `@dbexec` macro I’m proposing would do is wrap stuff between `begin ... end` in quotes and pass it to `DBInterface.execute`. It doesn’t execute in Julia, it is just a string.

But, to be clear, the reason I don’t understand is that I have no idea how macros actually work. I just use Chain.jl all the time and I think it works wonders, so that was clear inspiration for this question.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [February 2, 2023, 5:51pm UTC](https://discourse.julialang.org/t/macro-or-dsl-for-easier-dbinterface-usage/93942/4 "2023-02-02T17:51:32Z")

</div>

See the following:

```julia
julia> macro donothing(x)
           prinln(x)
           nothing
       end;

julia> @donothing begin 
       A B C
       end
ERROR: syntax: "begin" at REPL[8]:1 expected "end", got "B"
Stacktrace:
 [1] top-level scope
   @ none:1

```

The Julia parser happens before the stuff gets to the macro. The macro gets a julia expression (a tree-like object), and then can just re-arrange that expression.

But the rules about what is allowed happens before the macro gets stuff.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 3, 2023, 9:12am UTC](https://discourse.julialang.org/t/macro-or-dsl-for-easier-dbinterface-usage/93942/5 "2023-02-03T09:12:48Z")

</div>

> [@tbeason](#):
>
> ```julia
> @dbexec con begin
> CREATE TABLE integers(i INTEGER)
> end
> 
> ```

Do you just want to write the query in multiple lines? Ie, is

```julia
DBInterface.execute(con, """
CREATE TABLE integers(i INTEGER)
""")

```

close enough?

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [February 3, 2023, 11:33am UTC](https://discourse.julialang.org/t/macro-or-dsl-for-easier-dbinterface-usage/93942/6 "2023-02-03T11:33:04Z")

</div>

Have you considered [Octo.jl](https://github.com/wookay/Octo.jl)?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 3, 2023, 2:02pm UTC](https://discourse.julialang.org/t/macro-or-dsl-for-easier-dbinterface-usage/93942/7 "2023-02-03T14:02:31Z")

</div>

> [@aplavin](#):
>
> Do you just want to write the query in multiple lines?

No, I do multiple lines right now but I don’t love the look. I kind of want it to stand out more from the julia code.

> [@jd-foster](#):
>
> Have you considered [Octo.jl](https://github.com/wookay/Octo.jl)?

I have not, it seems might they support DuckDB, which is what I am using now, so I may try it out.
