Author Topic: [Not a Bug] Cannot open SQLite database inside class member function.  (Read 1539 times)

0 Members and 1 Guest are viewing this topic.

Offline S.L.C

  • Street Thug
  • *
  • Posts: 42
  • Sorry if you weren't impressed!
    • View Profile
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:
« Last Edit: September 19, 2014, 04:29:37 am by stormeus »

Offline stormeus

  • VC:MP Developer
  • VC:MP Veteran
  • *
  • Posts: 1122
    • View Profile
Re: [Bug] Cannot open SQLite database inside class member function.
« Reply #1 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.
Do not PM me for support.




Offline S.L.C

  • Street Thug
  • *
  • Posts: 42
  • Sorry if you weren't impressed!
    • View Profile
Re: [Bug] Cannot open SQLite database inside class member function.
« Reply #2 on: September 19, 2014, 02:45:39 am »
Thanks stormeus. I had almost forgot about the execution context and how things are done in Squirrel. Problem solved and this topic can now be locked :)