Skip to content

Commit

Permalink
Up to 1.7.1
Browse files Browse the repository at this point in the history
* Schema selector (issue #99)
* Filter by value
* Up SQLite to 3.38.2
* Change comment/uncomment behaviour (issue #100)
* Improve scripting
* Fix bugs
  • Loading branch information
little-brother committed Apr 4, 2022
1 parent e198d6f commit 9539675
Show file tree
Hide file tree
Showing 15 changed files with 939 additions and 528 deletions.
2 changes: 1 addition & 1 deletion 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++/mingw and WinAPI.<br>
sqlite-gui is a lightweight Windows GUI for [SQLite](https://www.sqlite.org/index.html) powered by C++/mingw64/WinAPI.<br>
Small, simple, powerfull, fast, free.

|[**Download the latest version**](https://github.com/little-brother/sqlite-gui/releases/latest)|
Expand Down
62 changes: 59 additions & 3 deletions extensions/inja.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,59 @@
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 ...
Visit https://github.com/little-brother/sqlite-gui/wiki#jinja-scripting to get more examples
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <stdio.h>
#include "inja.hpp"

static int bindArgs(sqlite3_stmt* stmt, inja::Arguments& args) {
inja::json irgs = args.at(1)->get<inja::json>();
if (!irgs.is_object() && !irgs.is_array()) {
irgs = inja::json::array();
for (auto it = args.begin() + 1; it != args.end(); ++it)
irgs.push_back((*it)->get<inja::json>());
}

for (auto it = irgs.begin(); it != irgs.end(); ++it) {
int idx = irgs.is_object() ?
sqlite3_bind_parameter_index(stmt, it.key().c_str()) :
std::distance(irgs.begin(), it) + 1;

if (idx != -1) {
if (it.value().is_number()) {
double value = it.value().get<double>();
if (floorf(value) == value)
sqlite3_bind_int(stmt, idx, floorf(value));
else
sqlite3_bind_double(stmt, idx, value);
}

if (it.value().is_string()) {
const char* value8 = it.value().get<std::string>().c_str();
sqlite3_bind_text(stmt, idx, value8, strlen(value8), SQLITE_TRANSIENT);
}
}
}

return 1;
}

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) {
env.add_callback("query", [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) {
bindArgs(stmt, args);
int colCount = sqlite3_column_count(stmt);
while (SQLITE_ROW == sqlite3_step(stmt)) {
inja::json row;
Expand All @@ -40,12 +74,13 @@ static void _inja(sqlite3_context *ctx, int argc, sqlite3_value **argv){
return res;
});

env.add_callback("query_row", 1, [db](inja::Arguments& args) {
env.add_callback("query_row", [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) {
bindArgs(stmt, args);
int colCount = sqlite3_column_count(stmt);
if (SQLITE_ROW == sqlite3_step(stmt)) {
for (int colNo = 0; colNo < colCount; colNo++)
Expand All @@ -60,12 +95,13 @@ static void _inja(sqlite3_context *ctx, int argc, sqlite3_value **argv){
return res;
});

env.add_callback("query_value", 1, [db](inja::Arguments& args) {
env.add_callback("query_value", [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) {
bindArgs(stmt, args);
if (SQLITE_ROW == sqlite3_step(stmt))
res = (const char*)sqlite3_column_text(stmt, 0);
}
Expand All @@ -81,7 +117,27 @@ static void _inja(sqlite3_context *ctx, int argc, sqlite3_value **argv){
inja::json data = args.at(1)->get<inja::json>();
return env.render(args.at(0)->get<std::string>(), data);
});

env.add_callback("fromjson", 1, [](inja::Arguments& args) {
return inja::json::parse(args.at(0)->get<std::string>());
});

env.add_callback("tojson", 1, [](inja::Arguments& args) {
return args.at(0)->get<inja::json>().dump();
});

env.add_callback("quote", 1, [db](inja::Arguments& args) {
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, "select quote(?1)", -1, &stmt, 0);
std::string text8 = args.at(0)->get<std::string>();
sqlite3_bind_text(stmt, 1, text8.c_str(), text8.size(), SQLITE_TRANSIENT);
sqlite3_step(stmt);
std::string res((const char*)sqlite3_column_text(stmt, 0));
sqlite3_finalize(stmt);

return res;
});

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

0 comments on commit 9539675

Please sign in to comment.