Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - S.L.C

Pages: [1]
1
Script Discussion / How to use transactions with the SQLite module?
« on: September 21, 2014, 04:50:16 am »
I need to be able to use transactions in my scripts however the server hangs when I commit. Extending the code on my previous thread here I have the following.

Creating the database structure:
Code: [Select]
// Make sure that the database has the Streaks table
Exec(@"CREATE TABLE IF NOT EXISTS [Streaks] (
[Message] VARCHAR(128)  NOT NULL
)");

All queries in one string:
Code: [Select]
// Insert the default values into the Streaks table
Exec(@"BEGIN;
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
INSERT INTO [Streaks] ([Message]) VALUES ('%s');
COMMIT;",
@"%s got the taste of blood. Victim: %s Killstreak: %d",
@"%s starts to enjoy it. Victim: %s Killstreak: %d",
@"%s is feeling bold. Victim: %s Killstreak: %d",
@"%s wants to be heroic. Victim: %s Killstreak: %d",
@"%s just became fearless. Victim: %s Killstreak: %d",
@"%s feels the excitement. Victim: %s Killstreak: %d",
@"%s is a freak. Victim: %s Killstreak: %d",
@"%s went on a killing spree. Victim: %s Killstreak: %d",
@"%s is unstoppable. Victim: %s Killstreak: %d",
@"%s starts to dominate. Victim: %s Killstreak: %d",
@"%s is relentless. Victim: %s Killstreak: %d",
@"%s went on a rampage. Victim: %s Killstreak: %d",
@"%s is delusional. Victim: %s Killstreak: %d",
@"%s became a nemesis. Victim: %s Killstreak: %d",
@"%s is a monster. Victim: %s Killstreak: %d",
@"%s annihilates everything. Victim: %s Killstreak: %d",
@"%s is out of this world. Victim: %s Killstreak: %d",
@"%s massacred his opponents. Victim: %s Killstreak: %d",
@"%s became a savage. Victim: %s Killstreak: %d",
@"%s is a deamon. Victim: %s Killstreak: %d",
@"%s made a genocide. Victim: %s Killstreak: %d",
@"%s is a mass murderer. Victim: %s Killstreak: %d",
@"%s slaughtered all hopes. Victim: %s Killstreak: %d",
@"%s is immortal. Victim: %s Killstreak: %d",
@"%s reached divinity. Victim: %s Killstreak: %d",
@"%s is god-like. Victim: %s Killstreak: %d"
);

Queries executed individually:
Code: [Select]
// Insert the default values into the Streaks table
Exec(@"BEGIN;");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s got the taste of blood. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s starts to enjoy it. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is feeling bold. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s wants to be heroic. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s just became fearless. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s feels the excitement. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is a freak. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s went on a killing spree. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is unstoppable. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s starts to dominate. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is relentless. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s went on a rampage. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is delusional. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s became a nemesis. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is a monster. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s annihilates everything. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is out of this world. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s massacred his opponents. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s became a savage. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is a deamon. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s made a genocide. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is a mass murderer. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s slaughtered all hopes. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is immortal. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s reached divinity. Victim: %s Killstreak: %d");
Exec(@"INSERT INTO [Streaks] ([Message]) VALUES ('%s');", @"%s is god-like. Victim: %s Killstreak: %d");
Exec(@"COMMIT;");

Both methods hang when I commit. I've tested the generated query string in other software and it works perfectly.

2
Description
When the ConnectSQL function is called on a database that doesn't exists then it tries to create one. The thing is that it's creating an SQLite2 database instead of an SQLite3 one.

Reproducible
Always

What you were doing when the bug happened
I was trying to create a table using "CREATE TABLE IF NOT EXISTS" and I noticed it doesn't work. Always fails with the message "Error while executing query: near "NOT": syntax error". Mainly because SQLite2 doesn't support "IF NOT EXISTS".

What you think caused the bug
I'm not sure at the moment because the module code seems to be correct. Probably an SQLite issue but just to be sure.

In whatever IDE/Studio/Editor I load the database it shows as an SQLite2 database. And if I create the database manually as an SQLite3 database then my queries work.

3
Description
I'm trying to use the player instance as the key inside a table, but when the player leaves the server the player instance isn't pointing to the same thing it was previously pointing in the table.

Reproducible
Always

What you were doing when the bug happened
N/A

What you think caused the bug
I'm not sure if this is a feature of the module and not a bug. If this is not expected behavior then I'll look for a bug.

Code that can reproduce the issue that I'm having:
Code: [Select]
local t_Players = {}

function onPlayerJoin(i_player)
{     
t_Players[i_player] <- null;

print("OnJoin slot search result: " + t_Players.rawin(i_player).tostring());
}

function onPlayerPart(i_player, reason)
{
print("OnPart slot search result: " + t_Players.rawin(i_player).tostring());
}

Screenshot of the console output:

4
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:

Pages: [1]