Skip to content

Commit b3f4ff7

Browse files
committed
Fixed some problems with SQLite databases and non-ANSI encoding
1 parent a42e7f7 commit b3f4ff7

File tree

3 files changed

+125
-16
lines changed

3 files changed

+125
-16
lines changed

MUSHclient.dsp

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripting/lua_methods.cpp

Lines changed: 121 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,11 +1787,29 @@ static int L_DatabaseColumnName (lua_State *L)
17871787
static int L_DatabaseColumnText (lua_State *L)
17881788
{
17891789
CMUSHclientDoc *pDoc = doc (L);
1790-
BSTR str = pDoc->DatabaseColumnText (
1791-
my_checkstring (L, 1), // Name
1792-
my_checknumber (L, 2) // Column
1793-
);
1794-
return pushBstr (L, str); // number of result fields
1790+
1791+
LPCTSTR Name = my_checkstring (L, 1); // Name
1792+
int Column = my_checknumber (L, 2); // Column
1793+
1794+
tDatabaseMapIterator it = pDoc->m_Databases.find (Name);
1795+
1796+
if (it != pDoc->m_Databases.end () && // database exists
1797+
it->second->db != NULL && // and is open
1798+
it->second->pStmt != NULL && // and we have a prepared statement
1799+
it->second->bValidRow && // and we stepped to a valid row
1800+
Column >= 1 &&
1801+
Column <= it->second->iColumns)
1802+
{
1803+
const char * p = (const char *) sqlite3_column_text (it->second->pStmt, Column - 1);
1804+
if (p)
1805+
lua_pushstring (L, p);
1806+
else
1807+
lua_pushnil (L);
1808+
}
1809+
else
1810+
lua_pushnil (L);
1811+
1812+
return 1; // number of result fields
17951813
} // end of L_DatabaseColumnText
17961814

17971815
//----------------------------------------
@@ -1800,11 +1818,51 @@ static int L_DatabaseColumnText (lua_State *L)
18001818
static int L_DatabaseColumnValue (lua_State *L)
18011819
{
18021820
CMUSHclientDoc *pDoc = doc (L);
1803-
VARIANT v = pDoc->DatabaseColumnValue (
1804-
my_checkstring (L, 1), // Name
1805-
my_checknumber (L, 2) // Column
1806-
);
1807-
return pushVariant (L, v); // number of result fields
1821+
LPCTSTR Name = my_checkstring (L, 1); // Name
1822+
int Column = my_checknumber (L, 2); // Column
1823+
1824+
tDatabaseMapIterator it = pDoc->m_Databases.find (Name);
1825+
1826+
if (it != pDoc->m_Databases.end () && // database exists
1827+
it->second->db != NULL && // and is open
1828+
it->second->pStmt != NULL && // and we have a prepared statement
1829+
it->second->bValidRow && // and we stepped to a valid row
1830+
Column >= 1 &&
1831+
Column <= it->second->iColumns)
1832+
{
1833+
// switch on type of column data
1834+
switch (sqlite3_column_type(it->second->pStmt, Column - 1))
1835+
{
1836+
case SQLITE3_TEXT:
1837+
case SQLITE_BLOB:
1838+
default:
1839+
{
1840+
const char * p = (const char *) sqlite3_column_text (it->second->pStmt, Column - 1);
1841+
if (p)
1842+
lua_pushstring (L, p);
1843+
else
1844+
lua_pushnil (L);
1845+
}
1846+
break;
1847+
1848+
case SQLITE_NULL:
1849+
lua_pushnil (L);
1850+
break;
1851+
1852+
case SQLITE_INTEGER:
1853+
lua_pushinteger (L, sqlite3_column_int (it->second->pStmt, Column - 1));
1854+
break;
1855+
1856+
case SQLITE_FLOAT:
1857+
lua_pushnumber(L, sqlite3_column_double (it->second->pStmt, Column - 1));
1858+
break;
1859+
1860+
} // end of switch
1861+
}
1862+
else
1863+
lua_pushnil (L);
1864+
1865+
return 1; // number of result fields
18081866
} // end of L_DatabaseColumnValue
18091867

18101868
//----------------------------------------
@@ -1919,8 +1977,59 @@ static int L_DatabaseColumnNames (lua_State *L)
19191977
//----------------------------------------
19201978
static int L_DatabaseColumnValues (lua_State *L)
19211979
{
1922-
VARIANT v = doc (L)->DatabaseColumnValues (my_checkstring (L, 1));
1923-
return pushVariant (L, v); // number of result fields
1980+
CMUSHclientDoc *pDoc = doc (L);
1981+
LPCTSTR Name = my_checkstring (L, 1); // Name
1982+
lua_newtable(L); // table of results
1983+
1984+
tDatabaseMapIterator it = pDoc->m_Databases.find (Name);
1985+
1986+
if (it != pDoc->m_Databases.end () && // database exists
1987+
it->second->db != NULL && // and is open
1988+
it->second->pStmt != NULL && // and we have a prepared statement
1989+
it->second->bValidRow) // and have a valid row
1990+
{
1991+
1992+
for (long i = 0; i < it->second->iColumns; i++)
1993+
{
1994+
1995+
// switch on type of column data
1996+
switch (sqlite3_column_type(it->second->pStmt, i))
1997+
{
1998+
case SQLITE3_TEXT:
1999+
case SQLITE_BLOB:
2000+
default:
2001+
{
2002+
const char * p = (const char *) sqlite3_column_text (it->second->pStmt, i);
2003+
if (p)
2004+
lua_pushstring (L, p);
2005+
else
2006+
lua_pushnil (L);
2007+
}
2008+
break;
2009+
2010+
case SQLITE_NULL:
2011+
lua_pushnil (L);
2012+
break;
2013+
2014+
case SQLITE_INTEGER:
2015+
lua_pushinteger (L, sqlite3_column_int (it->second->pStmt, i));
2016+
break;
2017+
2018+
case SQLITE_FLOAT:
2019+
lua_pushnumber(L, sqlite3_column_double (it->second->pStmt, i));
2020+
break;
2021+
2022+
} // end of switch
2023+
2024+
lua_rawseti (L, -2, i + 1); // one-relative
2025+
2026+
} // end of for
2027+
2028+
}
2029+
else
2030+
lua_pushnil (L);
2031+
2032+
return 1; // number of result fields
19242033
} // end of L_DatabaseColumnValues
19252034

19262035

scripting/methods/methods_database.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ VARIANT CMUSHclientDoc::DatabaseColumnValues(LPCTSTR Name)
567567
const unsigned char * p = sqlite3_column_text (it->second->pStmt, i);
568568
if (p)
569569
strResult = p;
570-
v = strResult;
570+
v = strResult;
571571
}
572572
break;
573573

0 commit comments

Comments
 (0)