From cfeed6c84fa2a8c582114497b035c9aab17efa91 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 26 Jul 2026 13:42:23 +0300
Subject: [PATCH 28/28] luasql: Update to version 2.8.1

See RM #2093

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 dependencies/luasql/Version.txt      | 15 +++++----
 dependencies/luasql/src/ls_odbc.c    | 46 +++++++++++++++++++---------
 dependencies/luasql/src/ls_sqlite3.c | 25 ++++++++-------
 dependencies/luasql/src/luasql.c     |  5 ++-
 dependencies/luasql/src/luasql.h     |  3 ++
 5 files changed, 55 insertions(+), 39 deletions(-)

diff --git a/dependencies/luasql/Version.txt b/dependencies/luasql/Version.txt
index 622a9fec8d..06bbdcd689 100644
--- a/dependencies/luasql/Version.txt
+++ b/dependencies/luasql/Version.txt
@@ -1,13 +1,12 @@
-Sources here are from luasql git tag 2.7.0
-(https://github.com/lunarmodules/luasql/tree/2.7.0)
+Sources here are from luasql git tag 2.8.1
+(https://github.com/lunarmodules/luasql/tree/2.8.1)
 Some modifications have been done to them:
-- Commit 10ed75b36bf909f57b3c1a070dd87669c323cff0 backported
-- Fixed compile error on ls_sqlite3.c raw_readparams_table()
-  Fix has been submitted to upstream.
-- Cleared trailing spaces
+- LUASQL_VERSION_NUMBER defined in luasql.h
+- Cleared trailing tabs from ls_mysql.c
   Equivalent patch submitted to upstream.
-- Fixed comment typos found by codespell
-  Fix has been submitted to upstream.
+- Fixed misleading indentation in ls_sqlite3.c
+- Removed unused variables from ls_odbc.c
+- Cleared trailing tabs from ls_odbc.c
 
 Only the files needed by freeciv are included here, not entire luasql
 source directory hierarchy.
diff --git a/dependencies/luasql/src/ls_odbc.c b/dependencies/luasql/src/ls_odbc.c
index c892c89cd8..63c14b80e5 100644
--- a/dependencies/luasql/src/ls_odbc.c
+++ b/dependencies/luasql/src/ls_odbc.c
@@ -11,6 +11,7 @@
 #include <string.h>
 #include <time.h>
 
+#define SQL_NOUNICODEMAP
 #if defined(_WIN32)
 #include <windows.h>
 #include <sqlext.h>
@@ -292,15 +293,6 @@ static int cur_shut(lua_State *L, cur_data *cur)
 */
 static const char *sqltypetolua (const SQLSMALLINT type) {
     switch (type) {
-        case SQL_UNKNOWN_TYPE: case SQL_CHAR: case SQL_VARCHAR:
-        case SQL_TYPE_DATE: case SQL_TYPE_TIME: case SQL_TYPE_TIMESTAMP:
-        case SQL_DATE: case SQL_INTERVAL: case SQL_TIMESTAMP:
-        case SQL_LONGVARCHAR:
-        case SQL_WCHAR: case SQL_WVARCHAR: case SQL_WLONGVARCHAR:
-#if (ODBCVER >= 0x0350)
-		case SQL_GUID:
-#endif
-            return "string";
         case SQL_BIGINT: case SQL_TINYINT:
         case SQL_INTEGER: case SQL_SMALLINT:
 #if LUA_VERSION_NUM>=503
@@ -314,8 +306,7 @@ static const char *sqltypetolua (const SQLSMALLINT type) {
         case SQL_BIT:
             return "boolean";
         default:
-            assert(0);
-            return NULL;
+            return "string";
     }
 }
 
@@ -360,7 +351,7 @@ static int push_column(lua_State *L, int coltypes, const SQLHSTMT hstmt,
 #if LUA_VERSION_NUM>=503
 		/* iNteger */
 		case 'n': {
-			SQLINTEGER num;
+			SQLLEN num;
 			SQLLEN got;
 			SQLRETURN rc = SQLGetData(hstmt, i, SQL_C_SLONG, &num, 0, &got);
 			if (error(rc))
@@ -707,7 +698,11 @@ static int raw_execute(lua_State *L, int istmt)
 			return fail(L, hSTMT, stmt->hstmt);
 		}
 
-		lua_pushnumber(L, numrows);
+#if LUA_VERSION_NUM >= 503
+		lua_pushinteger(L, (lua_Integer)numrows);
+#else
+		lua_pushnumber(L, (lua_Number)numrows);
+#endif
 		return 1;
 	}
 }
@@ -1096,9 +1091,30 @@ static int env_connect (lua_State *L) {
 	if (error(ret))
 		return luasql_faildirect (L, "connection allocation error.");
 
+	/* detect if sourcename is DSN / connection string by checking for '=' char */
+	int is_connection_string = 0;
+	const char *src = (const char*)sourcename;
+	if (strstr(src, "=") != NULL) {
+		is_connection_string = 1;
+	}
+
 	/* tries to connect handle */
-	ret = SQLConnect (hdbc, sourcename, SQL_NTS,
-		username, SQL_NTS, password, SQL_NTS);
+	if (is_connection_string) {
+		ret = SQLDriverConnect(
+			hdbc,
+			NULL, /* window handle */
+			sourcename,
+			SQL_NTS,
+			NULL, /* output connection string buffer */
+			0, /* sizeof output buffer */
+			NULL, /* actual length of output string buffer */
+			SQL_DRIVER_NOPROMPT
+		);
+	} else {
+		ret = SQLConnect (hdbc, sourcename, SQL_NTS,
+			username, SQL_NTS, password, SQL_NTS);
+	}
+
 	if (error(ret)) {
 		ret = fail(L, hDBC, hdbc);
 		SQLFreeHandle(hDBC, hdbc);
diff --git a/dependencies/luasql/src/ls_sqlite3.c b/dependencies/luasql/src/ls_sqlite3.c
index a2aab8ff70..cd55824e27 100644
--- a/dependencies/luasql/src/ls_sqlite3.c
+++ b/dependencies/luasql/src/ls_sqlite3.c
@@ -41,6 +41,7 @@ typedef struct
 typedef struct
 {
   short       closed;
+  short       first_fetch;
   int         conn;               /* reference to connection */
   int         numcols;            /* number of columns */
   int         colnames, coltypes; /* reference to column information tables */
@@ -162,14 +163,14 @@ static int cur_fetch (lua_State *L) {
   if (vm == NULL)
     return 0;
 
-  res = sqlite3_step(vm);
-
-  /* no more results? */
-  if (res == SQLITE_DONE)
-    return finalize(L, cur);
+  if (!cur->first_fetch) {
+    res = sqlite3_step(vm);
 
-  if (res != SQLITE_ROW)
-    return finalize(L, cur);
+    if (res == SQLITE_DONE || res != SQLITE_ROW)
+        return finalize(L, cur);
+  } else {
+    cur->first_fetch = 0;
+  }
 
   if (lua_istable (L, 2))
     {
@@ -287,6 +288,7 @@ static int create_cursor(lua_State *L, int o, conn_data *conn,
 
   /* fill in structure */
   cur->closed = 0;
+  cur->first_fetch = 1;
   cur->conn = LUA_NOREF;
   cur->numcols = numcols;
   cur->colnames = LUA_NOREF;
@@ -358,6 +360,8 @@ static int conn_close(lua_State *L)
   }
 
   conn->closed = 1;
+  luaL_unref(L, LUA_REGISTRYINDEX, conn->env);
+  sqlite3_close(conn->sql_conn);
 
   lua_pushboolean(L, 1);
   return 1;
@@ -462,11 +466,7 @@ static int raw_readparams_table(lua_State *L, sqlite3_stmt *vm, int arg)
 
   while (lua_next(L, arg)) {		// [arg]=table, [-2]=key, [-1]=val
 #if defined(lua_isinteger)
-    int tt =
-#endif
-      lua_type(L, -2);
-#if defined(lua_isinteger)
-    if (tt == LUA_TNUMBER && lua_isinteger(L, -2)) {
+    if (lua_type(L, -2) == LUA_TNUMBER && lua_isinteger(L, -2)) {
       param_nr = lua_tointeger(L, -2);
     } else {
 #endif
@@ -532,7 +532,6 @@ static int conn_execute(lua_State *L)
   /* real query? if empty, must have numcols!=0 */
   if ((res == SQLITE_ROW) || ((res == SQLITE_DONE) && numcols))
     {
-      sqlite3_reset(vm);
       return create_cursor(L, 1, conn, vm, numcols);
     }
 
diff --git a/dependencies/luasql/src/luasql.c b/dependencies/luasql/src/luasql.c
index 8c57860c97..0d2ac2c601 100644
--- a/dependencies/luasql/src/luasql.c
+++ b/dependencies/luasql/src/luasql.c
@@ -1,5 +1,4 @@
 /*
-** $Id: luasql.c,v 1.28 2009/02/11 12:08:50 tomas Exp $
 ** See Copyright Notice in license.html
 */
 
@@ -122,12 +121,12 @@ LUASQL_API void luasql_setmeta (lua_State *L, const char *name) {
 */
 LUASQL_API void luasql_set_info (lua_State *L) {
 	lua_pushliteral (L, "_COPYRIGHT");
-	lua_pushliteral (L, "Copyright (C) 2003-2025 Kepler Project");
+	lua_pushliteral (L, "Copyright (C) 2003-2026 Lunar Modules Project");
 	lua_settable (L, -3);
 	lua_pushliteral (L, "_DESCRIPTION");
 	lua_pushliteral (L, "LuaSQL is a simple interface from Lua to a DBMS");
 	lua_settable (L, -3);
 	lua_pushliteral (L, "_VERSION");
-	lua_pushliteral (L, "LuaSQL 2.7.0 (for "LUA_VERSION")");
+	lua_pushliteral (L, "LuaSQL "LUASQL_VERSION_NUMBER" (for "LUA_VERSION")");
 	lua_settable (L, -3);
 }
diff --git a/dependencies/luasql/src/luasql.h b/dependencies/luasql/src/luasql.h
index 92867349b3..09c8921458 100644
--- a/dependencies/luasql/src/luasql.h
+++ b/dependencies/luasql/src/luasql.h
@@ -10,6 +10,8 @@
 #define LUASQL_API
 #endif
 
+#define LUASQL_VERSION_NUMBER "2.8.1"
+
 #if !defined LUA_VERSION_NUM
 /* Lua 5.0 */
 #define luaL_Reg luaL_reg
@@ -42,6 +44,7 @@ void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
 #endif
 
 /* Driver initialization functions prototypes */
+LUASQL_API int luaopen_luasql_duckdb (lua_State *L);
 LUASQL_API int luaopen_luasql_firebird (lua_State *L);
 LUASQL_API int luaopen_luasql_mysql (lua_State *L);
 LUASQL_API int luaopen_luasql_oci8 (lua_State *L);
-- 
2.53.0

