Skip to content

Commit

Permalink
Up to 1.7.4
Browse files Browse the repository at this point in the history
* Support scripting in terminal mode (issue #116)
* Fix crushes (issue #117)
  • Loading branch information
little-brother committed Jun 19, 2022
1 parent 4b154d4 commit e2ba9d6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 40 deletions.
3 changes: 3 additions & 0 deletions extensions/inja.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ SQLITE_EXTENSION_INIT1
#include "inja.hpp"

static int bindArgs(sqlite3_stmt* stmt, inja::Arguments& args) {
if (args.size() <= 1)
return 0;

inja::json irgs = args.at(1)->get<inja::json>();
if (!irgs.is_object() && !irgs.is_array()) {
irgs = inja::json::array();
Expand Down
106 changes: 71 additions & 35 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3254,7 +3254,7 @@ LRESULT CALLBACK cbNewEditor(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
SELCHANGE *pSc = (SELCHANGE*)lParam;
bool isSelectionEmpty = pSc->seltyp == SEL_EMPTY;
if (isSelectionEmpty)
SetProp(hWnd, EDITOR_SELECTION_START, IntToPtr(pSc->chrg.cpMin));
SetProp(hWnd, EDITOR_SELECTION_START, LongToPtr(pSc->chrg.cpMin));

bool isRequireParenthesis = GetProp(hWnd, EDITOR_PARENTHESIS) == 0 && isSelectionEmpty;
bool isRequireOccurrence = GetProp(hWnd, EDITOR_HASOCCURRENCE) || !isSelectionEmpty;
Expand Down Expand Up @@ -3393,18 +3393,19 @@ int CALLBACK cbListComparator(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
return order * (isNum ? (num > num2 ? 1 : -1) : _tcscoll(buf, buf2));
}

void onCLIQueryEnd(TCHAR* sql16, TCHAR* result16, bool isSave, int elapsed) {
void onCLIQueryEnd(const TCHAR* sql16, TCHAR* result16, bool isSave, int elapsed) {
if (_tcslen(result16) > 0) {
_tcscat(result16, TEXT("\n\n============================================================\n\n"));
SendMessage(cli.hResultWnd, EM_SETSEL, 0, 0);
SendMessage(cli.hResultWnd, EM_REPLACESEL, 0, (LPARAM)result16);

SendMessage(cli.hResultWnd, EM_SETSEL, 0, 0);
SendMessage(cli.hResultWnd, EM_REPLACESEL, 0, (LPARAM)TEXT("\n\n"));
SendMessage(cli.hResultWnd, EM_SETSEL, 0, 0);
SendMessage(cli.hResultWnd, EM_REPLACESEL, 0, (LPARAM)sql16);
if (sql16 && _tcslen(sql16) > 0) {
SendMessage(cli.hResultWnd, EM_SETSEL, 0, 0);
SendMessage(cli.hResultWnd, EM_REPLACESEL, 0, (LPARAM)TEXT("\n\n"));
SendMessage(cli.hResultWnd, EM_SETSEL, 0, 0);
SendMessage(cli.hResultWnd, EM_REPLACESEL, 0, (LPARAM)sql16);
}
}
SetWindowText(cli.hEditorWnd, 0);

if (isSave) {
sqlite3_stmt* stmt;
Expand All @@ -3428,6 +3429,38 @@ void onCLIQueryEnd(TCHAR* sql16, TCHAR* result16, bool isSave, int elapsed) {
}
sqlite3_finalize(stmt);
}

// Scroll to top
int cnt = SendMessage(cli.hResultWnd, EM_GETLINECOUNT, 0, 0);
SendMessage(cli.hResultWnd, EM_LINESCROLL, 0, -cnt);
}

TCHAR* parseInja(TCHAR* buf) {
TCHAR* res = 0;
if (isInjaSupport && (
(_tcsstr(buf, TEXT("{{")) && _tcsstr(buf, TEXT("}}"))) ||
(_tcsstr(buf, TEXT("{%")) && _tcsstr(buf, TEXT("%}")))
)) {
sqlite3_stmt *stmt;
bool rc = SQLITE_OK == sqlite3_prepare_v2(db, "select inja(?1)", -1, &stmt, 0);
if (rc) {
char* buf8 = utils::utf16to8(buf);
sqlite3_bind_text(stmt, 1, buf8, strlen(buf8), SQLITE_TRANSIENT);
delete [] buf8;
rc = SQLITE_ROW == sqlite3_step(stmt);
if (rc)
res = utils::utf8to16((const char*)sqlite3_column_text(stmt, 0));
}
sqlite3_finalize(stmt);

if (!rc)
showDbError(hMainWnd);
} else {
res = new TCHAR[_tcslen(buf) + 1] {0};
_tcscpy(res, buf);
}

return res;
}

unsigned int __stdcall processCliQuery (void* data) {
Expand Down Expand Up @@ -3573,6 +3606,7 @@ unsigned int __stdcall processCliQuery (void* data) {
}

onCLIQueryEnd(sql16, result16, rc == SQLITE_OK, elapsed);
SetWindowText(cli.hEditorWnd, NULL);
delete [] sql8;

RedrawWindow(cli.hResultWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME);
Expand Down Expand Up @@ -3682,12 +3716,34 @@ int executeCLIQuery(bool isPlan) {
}

onCLIQueryEnd(sql16, result16, isSave, 0);
SetWindowText(cli.hEditorWnd, NULL);

LocalFree(args);
return 0;
}

cli.isPlan = isPlan;
cli.thread = (HANDLE)_beginthreadex(0, 0, &processCliQuery, 0, 0, 0);
TCHAR* inja16 = parseInja(sql16);
if (!inja16)
return 0;

if (_tcscmp(inja16, sql16) == 0) {
cli.isPlan = isPlan;
cli.thread = (HANDLE)_beginthreadex(0, 0, &processCliQuery, 0, 0, 0);
} else {
int len = _tcslen(inja16) + 255;
TCHAR* res16 = new TCHAR[len + 1];
_stprintf(res16, len, TEXT("Inja script result\n------------------------------------------------------------\n%ls"), inja16);

if (prefs::get("cli-preserve-inja")) {
onCLIQueryEnd(TEXT(""), res16, false, 0);
} else {
onCLIQueryEnd(sql16, res16, true, 0);
SetWindowText(cli.hEditorWnd, NULL);
}

delete [] res16;
}
delete [] inja16;
return 1;
}

Expand Down Expand Up @@ -4061,33 +4117,13 @@ int executeEditorQuery(bool isPlan, bool isBatch, bool onlyCurrent, int vkKey) {
for (int i = 0; isSelection && _tcschr(buf, TEXT('\r')) && (i < size + 1); i++)
buf[i] = buf[i] == TEXT('\r') && buf[i + 1] != TEXT('\n') ? TEXT('\n') : buf[i];

if (isInjaSupport && (
(_tcsstr(buf, TEXT("{{")) && _tcsstr(buf, TEXT("}}"))) ||
(_tcsstr(buf, TEXT("{%")) && _tcsstr(buf, TEXT("%}")))
)) {

sqlite3_stmt *stmt;
bool rc = SQLITE_OK == sqlite3_prepare_v2(db, "select inja(?1)", -1, &stmt, 0);
if (rc) {
char* buf8 = utils::utf16to8(buf);
sqlite3_bind_text(stmt, 1, buf8, strlen(buf8), SQLITE_TRANSIENT);
delete [] buf8;
rc = SQLITE_ROW == sqlite3_step(stmt);
if (rc) {
TCHAR* buf16 = utils::utf8to16((const char*)sqlite3_column_text(stmt, 0));
delete [] buf;
buf = buf16;
size = _tcslen(buf16);
}
}
sqlite3_finalize(stmt);
TCHAR* inja = parseInja(buf);
if (!inja)
return false;

if (!rc) {
showDbError(hMainWnd);
delete [] buf;
return false;
}
}
delete [] buf;
buf = inja;
size = _tcslen(buf);

TCHAR bufcopy[size + 1] {0};
_tcscpy(bufcopy, buf);
Expand Down
6 changes: 3 additions & 3 deletions src/prefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace prefs {
sqlite3* db = NULL;

const int ICOUNT = 76;
const int ICOUNT = 77;
const char* iprops[ICOUNT] = {
"x", "y", "width", "height", "splitter-position-x", "splitter-position-y",
"maximized", "font-size", "max-query-count", "exit-by-escape", "beep-query-duration", "synchronous-off",
"cli-font-size", "cli-row-limit",
"cli-font-size", "cli-row-limit", "cli-preserve-inja",
"backup-prefs", "autoload-extensions", "restore-db", "restore-editor", "use-highlight", "use-legacy-rename", "editor-indent", "editor-tab-count", "editor-tab-current", "highlight-delay",
"ask-delete", "word-wrap", "clear-values", "recent-count",
"csv-export-is-unix-line", "csv-export-delimiter", "csv-export-is-columns",
Expand All @@ -33,7 +33,7 @@ namespace prefs {
int ivalues[ICOUNT] = {
100, 100, 800, 600, 200, 200,
0, 10, 1000, 1, 3000, 1,
8, 10,
8, 10, 1,
0, 1, 1, 1, 1, 0, 0, 1, 0, 30,
0, 0, 0, 10,
0, 0, 1,
Expand Down
4 changes: 2 additions & 2 deletions src/resource.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define GUI_VERSION "1.7.3"
#define GUI_VERSION2 1, 7, 3, 0
#define GUI_VERSION "1.7.4"
#define GUI_VERSION2 1, 7, 4, 0
#ifdef __MINGW64__
#define GUI_PLATFORM 64
#else
Expand Down

0 comments on commit e2ba9d6

Please sign in to comment.