added sqlite::column_name() and enhanced sqlite::escape_string() to differentiate mbs and str
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-12-25 11:42:36 +09:00
parent 64250aa80c
commit 262ec3421f
2 changed files with 60 additions and 4 deletions

View File

@@ -1326,7 +1326,7 @@ Assuming `/tmp/test.db` with the following schema,
``` ```
sqlite> .schema sqlite> .schema
CREATE TABLE a(x int, y int); CREATE TABLE a(x int, y varchar(255));
``` ```
You can retreive all rows as shown below: You can retreive all rows as shown below:
@@ -1335,7 +1335,7 @@ You can retreive all rows as shown below:
@pragma implicit off @pragma implicit off
function main() { function main() {
@local db, stmt, row, i, sql; @local db, stmt, row, i, ncols;
db = sqlite::open(); db = sqlite::open();
if (db <= -1) { if (db <= -1) {
@@ -1352,7 +1352,14 @@ function main() {
sqlite::exec(db, "begin transaction"); sqlite::exec(db, "begin transaction");
sqlite::exec(db, "delete from a"); sqlite::exec(db, "delete from a");
for (i = 0; i < 10; i++) { for (i = 0; i < 10; i++) {
sql=sprintf("insert into a(x,y) values(%d,%d)", math::rand() * 100, math::rand() * 100); @local sql, fld;
if (sqlite::escape_string(db, ((i % 2)? @b"'STXETX'": "'␂␃'") %% (math::rand() * 100), fld) <= -1) {
print "escape_string error -", sqlite::errmsg();
sqlite::exec(db, "rollback");
sqlite::close(db);
return;
}
sql=sprintf("insert into a(x,y) values(%d,'%s')", math::rand() * 100, fld);
print sql; print sql;
if (sqlite::exec(db, sql) <= -1) { if (sqlite::exec(db, sql) <= -1) {
print "exec error -", sqlite::errmsg(); print "exec error -", sqlite::errmsg();
@@ -1377,6 +1384,11 @@ function main() {
return; return;
} }
ncols = sqlite::column_count(stmt);
printf ("TOTAL %d COLUMNS:\n", ncols);
for (i = 1; i <= ncols; i++) {
print "-", i, sqlite::column_name(stmt, i);
}
while (sqlite::fetch_row(stmt, row, sqlite::FETCH_ROW_ARRAY) > 0) { while (sqlite::fetch_row(stmt, row, sqlite::FETCH_ROW_ARRAY) > 0) {
print "[id]", row[1], "[name]", row[2]; print "[id]", row[1], "[name]", row[2];
} }

View File

@@ -703,6 +703,7 @@ static int fnc_escape_string (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
if (sql_node) if (sql_node)
{ {
int n; int n;
hawk_val_type_t vtype;
ENSURE_CONNECT_EVER_ATTEMPTED(rtx, sql_list, sql_node); ENSURE_CONNECT_EVER_ATTEMPTED(rtx, sql_list, sql_node);
@@ -717,7 +718,10 @@ static int fnc_escape_string (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
goto done; goto done;
} }
retv = hawk_rtx_makestrvalwithbcstr(rtx, (hawk_bch_t*)ebuf); vtype = HAWK_RTX_GETVALTYPE(rtx, a1);
retv = (vtype == HAWK_VAL_BCHR || vtype == HAWK_VAL_MBS || vtype == HAWK_VAL_BOB)?
hawk_rtx_makembsvalwithbcstr(rtx, (hawk_bch_t*)ebuf):
hawk_rtx_makestrvalwithbcstr(rtx, (hawk_bch_t*)ebuf);
if (HAWK_UNLIKELY(!retv)) if (HAWK_UNLIKELY(!retv))
{ {
take_rtx_err = 1; take_rtx_err = 1;
@@ -860,6 +864,45 @@ static int fnc_column_count (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
return 0; return 0;
} }
static int fnc_column_name (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
{
sql_list_t* sql_list;
stmt_list_t* stmt_list;
stmt_node_t* stmt_node;
hawk_val_t* retv = HAWK_NULL;
hawk_int_t index;
sql_list = rtx_to_sql_list(rtx, fi);
stmt_list = rtx_to_stmt_list(rtx, fi);
stmt_node = get_stmt_list_node_with_arg(rtx, sql_list, stmt_list, hawk_rtx_getarg(rtx, 0));
if (stmt_node)
{
if (hawk_rtx_valtoint(rtx, hawk_rtx_getarg(rtx, 1), &index) <= -1)
{
set_error_on_sql_list(rtx, sql_list, HAWK_T("invalid column index"));
retv = hawk_rtx_makenilval(rtx);
}
else if (index <= 0 || index > stmt_node->col_count)
{
retv = hawk_rtx_makenilval(rtx);
}
else
{
const char* name = sqlite3_column_name(stmt_node->stmt, (int)index - 1);
if (name) retv = hawk_rtx_makestrvalwithbcstr(rtx, (const hawk_bch_t*)name);
else retv = hawk_rtx_makenilval(rtx);
}
}
else
{
retv = hawk_rtx_makenilval(rtx);
}
if (HAWK_UNLIKELY(!retv)) return -1;
hawk_rtx_setretval(rtx, retv);
return 0;
}
#define FETCH_ROW_ARRAY (1) #define FETCH_ROW_ARRAY (1)
#define FETCH_ROW_MAP (2) #define FETCH_ROW_MAP (2)
@@ -1063,6 +1106,7 @@ static hawk_mod_fnc_tab_t fnctab[] =
{ HAWK_T("changes"), { { 1, 1, HAWK_NULL }, fnc_changes, 0 } }, { HAWK_T("changes"), { { 1, 1, HAWK_NULL }, fnc_changes, 0 } },
{ HAWK_T("close"), { { 1, 1, HAWK_NULL }, fnc_close, 0 } }, { HAWK_T("close"), { { 1, 1, HAWK_NULL }, fnc_close, 0 } },
{ HAWK_T("column_count"), { { 1, 1, HAWK_NULL }, fnc_column_count, 0 } }, { HAWK_T("column_count"), { { 1, 1, HAWK_NULL }, fnc_column_count, 0 } },
{ HAWK_T("column_name"), { { 2, 2, HAWK_NULL }, fnc_column_name, 0 } },
{ HAWK_T("connect"), { { 2, 4, HAWK_NULL }, fnc_connect, 0 } }, { HAWK_T("connect"), { { 2, 4, HAWK_NULL }, fnc_connect, 0 } },
{ HAWK_T("errmsg"), { { 0, 0, HAWK_NULL }, fnc_errmsg, 0 } }, { HAWK_T("errmsg"), { { 0, 0, HAWK_NULL }, fnc_errmsg, 0 } },
{ HAWK_T("escape_string"), { { 3, 3, HAWK_T("vvr") }, fnc_escape_string, 0 } }, { HAWK_T("escape_string"), { { 3, 3, HAWK_T("vvr") }, fnc_escape_string, 0 } },