little-brother / sqlite-gui

Lightweight SQLite editor for Windows
GNU General Public License v2.0
1.07k stars 51 forks source link

Jinja with query #116

Closed NSP-0123456 closed 2 years ago

NSP-0123456 commented 2 years ago

I'm trying to use Jinja template with query latest version inja.

// ref SQLite database editor

GUI version: 1.7.3 Architecture: x86-64 SQLite version: 3.38.5 Encryption support: Yes Build date: May 21 2022

OS: 10.0.19044 x64

little-brother commented 2 years ago

You don't need to write select inja('. It's for internal usage.

Write in an editor

{% for row in query("select * from books") %} {{ row.author }} {% endfor %}

and then run the query

P.S. I reproduced the error :(

little-brother commented 2 years ago

I found the error and fixed it.

After a line https://github.com/little-brother/sqlite-gui/blob/master/extensions/inja.cpp#L14 should add

    if (args.size() <= 1)
        return 0;

I updated files of the latest realease (but didn't change git sources).

The query

{% for row in query("select * from books") %} {{ row.author }} {% endfor %}

will be converted to

Brandon Sanderson Cristin Terrill Jill McCorkle Kate Atkinson ...

It's a not a valid query, so you will not get a result.

Use

{% for row in query("select * from books limit 3") %} select {{ quote(row.author) }}; {% endfor %}

There are 3 changes:

P.S. Thanks for the report!

NSP-0123456 commented 2 years ago

Using the template, i prefer to use it like :

select '{% for row in query("select * from books") %}"{{ row.author }}" {% endfor %}'

It could be good to have an export option using Jinja template file to generate report without having to be captured as a resultset.

little-brother commented 2 years ago

Hmm, the interesting idea. I didn't suppose that someone wants to use scripting like this. The direct call select inja('{%... has quotes and highlighting disadvantages.

I should figure out how to implement it in the most natural way. Perhaps, run the inja-template in the terminal to return a inja-generated result is a solution e.g.

изображение

But it slighty unobvious.

little-brother commented 2 years ago

Done. Wiki was updated.