Vice City Multiplayer

VC:MP 0.4 (Beta) => Bugs and Crashes => Resolved Bug Reports => Topic started by: S.L.C on September 18, 2014, 11:15:47 pm

Title: [Not a Bug] Cannot open SQLite database inside class member function.
Post by: S.L.C on September 18, 2014, 11:15:47 pm
Description
I cannot open SQLite databases inside class member functions.

Reproducible
Always

What you were doing when the bug happened
N/A

What you think caused the bug
I'm too tired to find out right now. Hopefully I'll dig deeper when wake up tomorrow.

Code that can reproduce the error:
Code: [Select]
// ------------------------------------------------------------------------------------------------
DB <- {

}
// ------------------------------------------------------------------------------------------------
class DB.Database
{
// --------------------------------------------------------------------------------------------
_File = null
// --------------------------------------------------------------------------------------------
_Handle = null
// --------------------------------------------------------------------------------------------
constructor(db)
{
// Is this a path to a database?
if (typeof db == "string") _File = db, _Handle = null;
// Is this a handle to an already opened database?
else if (typeof db == "userdata") _File = null, _Handle = db;
// Invalid arguments were passed to the constructor
else return print(format("Database constructor requires parh or handle as it's first argument. Got: %s", typeof db));
}
function Open()
{
// Make sure the previous handle was closed or that there wasn't one to begin with
if (typeof _Handle == "userdata") return print("Database is already opened. Operation will be aborted.");
// Make sure that a database file was specified and that
else if (typeof _File != "string") return print("Unknown or missing database path. Operation will be aborted.");
// Try to open the file as an SQLite database now

print("member: " + _File + " is of type " + typeof _File);

_Handle = ConnectSQL(_File);

// Check if the previous operation succeeded and notify someone in case of failure
if (_Handle == null) return print(format("Unable to open the SQLite database from: %s", _File));
// The database was opened successfully and the operation is complete
else print(format("Successfully opened SQLite database from: %s", _File));
}
}

function onServerStart()
{
local db1 = ConnectSQL("mydb.db");

if (db1 == null) print("Method 1 failed");
else print("Method 1 succeeded");

local file = "mydb.db";
local db2 = ConnectSQL(file);

if (db2 == null) print("Method 2 failed");
else print("Method 2 succeeded");

// Try method

local db = DB.Database("mydb.db");

db.Open();

print("break point.");
}

Code image including line numbers and console output:
(http://s23.postimg.org/y3i8l9o13/code.jpg) (http://postimg.org/image/y3i8l9o13/) (http://s17.postimg.org/qfifimfy3/output.jpg) (http://postimg.org/image/qfifimfy3/)
Title: Re: [Bug] Cannot open SQLite database inside class member function.
Post by: stormeus on September 19, 2014, 12:56:12 am
Try using ::ConnnectSQL within class members instead of simply using ConnectSQL. The :: should signal to the Squirrel VM that you want to use a function in the root table instead of the class.

This should also fix problems caused by using other global functions in class functions.
Title: Re: [Bug] Cannot open SQLite database inside class member function.
Post by: S.L.C on September 19, 2014, 02:45:39 am
Thanks stormeus. I had almost forgot about the execution context (http://www.squirrel-lang.org/doc/squirrel3.html#d0e584) and how things are done in Squirrel. Problem solved and this topic can now be locked :)