Skip to content

Commit

Permalink
Up to 1.7.0
Browse files Browse the repository at this point in the history
* Up SQLite to 3.38.0
* Column filters for resultset
* Jinja scripting
* Improve BLOB handling + issue #98
  • Loading branch information
little-brother committed Feb 24, 2022
1 parent 4fc76d2 commit e198d6f
Show file tree
Hide file tree
Showing 20 changed files with 37,473 additions and 6,625 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sqlite-gui is a lightweight Windows GUI for [SQLite](https://www.sqlite.org/index.html) powered by C++, WinAPI and [Code::Blocks 17](http://www.codeblocks.org/). <br>
sqlite-gui is a lightweight Windows GUI for [SQLite](https://www.sqlite.org/index.html) powered by C++/mingw and WinAPI.<br>
Small, simple, powerfull, fast, free.

|[**Download the latest version**](https://github.com/little-brother/sqlite-gui/releases/latest)|
Expand All @@ -23,7 +23,8 @@ Small, simple, powerfull, fast, free.
* [Query parameters](https://github.com/little-brother/sqlite-gui/wiki#query-parameters)
* [Query shortcuts](https://github.com/little-brother/sqlite-gui/wiki#query-shortcuts)
* [Custom functions](https://github.com/little-brother/sqlite-gui/wiki#custom-functions)
* Data generator
* [Jinja scripting](https://github.com/little-brother/sqlite-gui/wiki#jinja-scripting) (experimental)
* [Data generator](https://github.com/little-brother/sqlite-gui/wiki#data-generator)
* [Extension pack](https://github.com/little-brother/sqlite-gui/wiki#extensions)
* [Built-in REST API web server](https://github.com/little-brother/sqlite-gui/wiki#rest-api-web-server)
* Demo database "Bookstore" for beginners
Expand Down
107 changes: 107 additions & 0 deletions extensions/inja.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
inja(template)
Parse mustache template to plain sql.
select inja('{% for i in range (2) %}select {{ i }}; {% endfor %}'); --> select 1; select 2;
select inja('select {{ argv.1 }}; select {{ argv.2 }}', 'a', 'b'); --> select a; select b;
select inja('{% for row in query("select * from books") %} {{ row.author }} {% endfor %}'); --> Brandon Sanderson Cristin Terrill ...
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <stdio.h>
#include "inja.hpp"

static void _inja(sqlite3_context *ctx, int argc, sqlite3_value **argv){
if (sqlite3_value_type(argv[0]) == SQLITE_NULL)
return sqlite3_result_null(ctx);

sqlite3* db = sqlite3_context_db_handle(ctx);

inja::Environment env;
env.add_callback("query", 1, [db](inja::Arguments& args) {
inja::json res = inja::json::array();

sqlite3_stmt* stmt;
bool rc = SQLITE_OK == sqlite3_prepare_v2(db, (args.at(0)->get<std::string>()).c_str(), -1, &stmt, 0);
if (rc) {
int colCount = sqlite3_column_count(stmt);
while (SQLITE_ROW == sqlite3_step(stmt)) {
inja::json row;
for (int colNo = 0; colNo < colCount; colNo++)
row[(const char*)sqlite3_column_name(stmt, colNo)] = (const char*)sqlite3_column_text(stmt, colNo);

res.push_back(row);
}
}
sqlite3_finalize(stmt);

if (!rc)
throw std::logic_error((const char*)sqlite3_errmsg(db));

return res;
});

env.add_callback("query_row", 1, [db](inja::Arguments& args) {
inja::json res;

sqlite3_stmt* stmt;
bool rc = SQLITE_OK == sqlite3_prepare_v2(db, (args.at(0)->get<std::string>()).c_str(), -1, &stmt, 0);
if (rc) {
int colCount = sqlite3_column_count(stmt);
if (SQLITE_ROW == sqlite3_step(stmt)) {
for (int colNo = 0; colNo < colCount; colNo++)
res[(const char*)sqlite3_column_name(stmt, colNo)] = (const char*)sqlite3_column_text(stmt, colNo);
}
}
sqlite3_finalize(stmt);

if (!rc)
throw std::logic_error((const char*)sqlite3_errmsg(db));

return res;
});

env.add_callback("query_value", 1, [db](inja::Arguments& args) {
std::string res;

sqlite3_stmt* stmt;
bool rc = SQLITE_OK == sqlite3_prepare_v2(db, (args.at(0)->get<std::string>()).c_str(), -1, &stmt, 0);
if (rc) {
if (SQLITE_ROW == sqlite3_step(stmt))
res = (const char*)sqlite3_column_text(stmt, 0);
}
sqlite3_finalize(stmt);

if (!rc)
throw std::logic_error((const char*)sqlite3_errmsg(db));

return res;
});

env.add_callback("call", 2, [env](inja::Arguments& args) mutable {
inja::json data = args.at(1)->get<inja::json>();
return env.render(args.at(0)->get<std::string>(), data);
});

const char* tmpl = (const char*)sqlite3_value_text(argv[0]);
inja::json data;
for (int i = 0; i < argc; i++)
data["argv"][i] = (const char*)sqlite3_value_text(argv[i]);

try {
sqlite3_result_text(ctx, env.render(tmpl, data).c_str(), -1, SQLITE_TRANSIENT);
} catch(const std::exception& err){
sqlite3_result_error(ctx, err.what(), -1);
}
}

extern "C"
{
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_inja_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) {
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */
return sqlite3_create_function(db, "inja", -1, SQLITE_UTF8, 0, _inja, 0, 0);
}
}
Loading

0 comments on commit e198d6f

Please sign in to comment.