Skip to content

Commit bddf1f8

Browse files
committed
Also fixed DatabaseGetField and did some code cleanups
1 parent b3f4ff7 commit bddf1f8

File tree

2 files changed

+186
-54
lines changed

2 files changed

+186
-54
lines changed

scripting/lua_methods.cpp

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,14 +1812,8 @@ static int L_DatabaseColumnText (lua_State *L)
18121812
return 1; // number of result fields
18131813
} // end of L_DatabaseColumnText
18141814

1815-
//----------------------------------------
1816-
// world.DatabaseColumnValue
1817-
//----------------------------------------
1818-
static int L_DatabaseColumnValue (lua_State *L)
1815+
static void GetDatabaseColumnValue (lua_State *L, CMUSHclientDoc *pDoc, LPCTSTR Name, int Column)
18191816
{
1820-
CMUSHclientDoc *pDoc = doc (L);
1821-
LPCTSTR Name = my_checkstring (L, 1); // Name
1822-
int Column = my_checknumber (L, 2); // Column
18231817

18241818
tDatabaseMapIterator it = pDoc->m_Databases.find (Name);
18251819

@@ -1862,6 +1856,19 @@ static int L_DatabaseColumnValue (lua_State *L)
18621856
else
18631857
lua_pushnil (L);
18641858

1859+
} // end of GetDatabaseColumnValue
1860+
1861+
//----------------------------------------
1862+
// world.DatabaseColumnValue
1863+
//----------------------------------------
1864+
static int L_DatabaseColumnValue (lua_State *L)
1865+
{
1866+
CMUSHclientDoc *pDoc = doc (L);
1867+
LPCTSTR Name = my_checkstring (L, 1); // Name
1868+
int Column = my_checknumber (L, 2); // Column
1869+
1870+
GetDatabaseColumnValue (L, pDoc, Name, Column);
1871+
18651872
return 1; // number of result fields
18661873
} // end of L_DatabaseColumnValue
18671874

@@ -1908,11 +1915,30 @@ static int L_DatabaseChanges (lua_State *L)
19081915
static int L_DatabaseGetField (lua_State *L)
19091916
{
19101917
CMUSHclientDoc *pDoc = doc (L);
1911-
VARIANT v = pDoc->DatabaseGetField (
1912-
my_checkstring (L, 1), // Name
1913-
my_checkstring (L, 2) // Sql
1914-
);
1915-
return pushVariant (L, v); // number of result fields
1918+
LPCTSTR Name = my_checkstring (L, 1); // Name
1919+
LPCTSTR Sql = my_checkstring (L, 2); // Sql
1920+
1921+
// prepare the SQL statement
1922+
long rc = pDoc->DatabasePrepare (Name, Sql);
1923+
1924+
if (rc != SQLITE_OK)
1925+
lua_pushnil (L); // could not prepare statement, give up
1926+
else
1927+
{
1928+
// step to get one row
1929+
rc = pDoc->DatabaseStep (Name);
1930+
1931+
// if we got one, extract the value from column 1
1932+
if (rc == SQLITE_ROW)
1933+
GetDatabaseColumnValue (L, pDoc, Name, 1);
1934+
else
1935+
lua_pushnil (L); // did not get a row
1936+
1937+
// finalize the current statement
1938+
pDoc->DatabaseFinalize (Name);
1939+
} // end of if could prepare SQL
1940+
1941+
return 1; // number of result fields
19161942
} // end of L_DatabaseGetField
19171943

19181944
//----------------------------------------

0 commit comments

Comments
 (0)