-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: wixoa <[email protected]>
- Loading branch information
Showing
17 changed files
with
721 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/proc/RunTest() | ||
var/database/db = new("clear.db") | ||
|
||
var/database/query/query = new("invalid command text") | ||
query.Clear() | ||
|
||
// Add with no parameters does nothing | ||
query.Add() | ||
|
||
// Execute without a command does nothing | ||
query.Execute() | ||
|
||
// and shouldn't report an error | ||
ASSERT(!query.Error()) | ||
|
||
del(query) | ||
del(db) | ||
|
||
fdel("clear.db") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/proc/RunTest() | ||
var/database/db = new("database.db") | ||
|
||
var/database/query/query = new("I am the greatest SQL query writer of all time.") | ||
query.Execute(db) | ||
|
||
ASSERT(query.Error() == 1) | ||
ASSERT(length(query.ErrorMsg()) > 1) | ||
|
||
ASSERT(db.Error() == 1) | ||
ASSERT(length(db.ErrorMsg()) > 1) | ||
|
||
fdel("database.db") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// RUNTIME ERROR | ||
|
||
/proc/RunTest() | ||
var/database/db = new() | ||
var/database/query/query = new("CREATE TABLE foobar (id int)") | ||
|
||
query.Execute(db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/proc/RunTest() | ||
var/database/db = new("noentries.db") | ||
|
||
var/database/query/query = new("CREATE TABLE foobar (id int)") | ||
query.Execute(db) | ||
|
||
query.Add("SELECT * FROM foobar") | ||
query.Execute(db) | ||
query.NextRow() | ||
|
||
query.GetRowData() | ||
|
||
ASSERT(query.Error() && query.ErrorMsg()) | ||
|
||
fdel("noentries.db") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/proc/RunTest() | ||
var/database/db = new("foo.db") | ||
|
||
var/database/query/query = new("CREATE TABLE foo (id int)") | ||
query.Execute(db) | ||
|
||
query.Add("INSERT INTO foo VALUES (1)") | ||
query.Execute(db) | ||
|
||
ASSERT(query.RowsAffected() == 1) | ||
|
||
db.Open("bar.db") | ||
query.Add("CREATE TABLE bar (id int)") | ||
query.Execute(db) | ||
|
||
query.Add("INSERT INTO bar VALUES (1)") | ||
query.Execute(db) | ||
|
||
ASSERT(query.RowsAffected() == 1) | ||
|
||
fdel("foo.db") | ||
fdel("bar.db") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/proc/RunTest() | ||
var/database/db = new("database.db") | ||
|
||
var/database/query/query = new("CREATE TABLE test (id int, name string, points float)") | ||
query.Execute(db) | ||
|
||
query.Add("INSERT INTO test VALUES (?, ?, ?)", 1, "foo", 1.5) | ||
query.Execute(db) | ||
|
||
ASSERT(query.RowsAffected() == 1) | ||
|
||
query.Add("SELECT * FROM test WHERE id = ?", 1) | ||
query.Execute(db) | ||
query.NextRow() | ||
|
||
var/list/assoc = query.GetRowData() | ||
ASSERT(length(assoc) == 3) | ||
|
||
ASSERT(assoc["id"] == 1) | ||
ASSERT(assoc["name"] == "foo") | ||
ASSERT(assoc["points"] == 1.5) | ||
|
||
ASSERT(query.GetColumn(0) == 1) | ||
ASSERT(query.GetColumn(1) == "foo") | ||
ASSERT(query.GetColumn(2) == 1.5) | ||
|
||
var/list/columns = query.Columns() | ||
ASSERT(columns[1] == "id") | ||
ASSERT(columns[2] == "name") | ||
ASSERT(columns[3] == "points") | ||
|
||
ASSERT(query.Columns(0) == "id") | ||
ASSERT(query.Columns(1) == "name") | ||
ASSERT(query.Columns(2) == "points") | ||
|
||
ASSERT(!query.Columns(10)) | ||
|
||
ASSERT(query.Error() && query.ErrorMsg()) | ||
|
||
query.Close() | ||
db.Close() | ||
|
||
db.Open("database.db") | ||
|
||
query.Add("SELECT * FROM test WHERE id = ?", 1) | ||
query.Execute(db) | ||
query.NextRow() | ||
|
||
ASSERT(query.GetColumn(0) == 1) | ||
|
||
ASSERT(!query.GetColumn(10)) | ||
ASSERT(query.Error() && query.ErrorMsg()) | ||
|
||
fdel("database.db") |
14 changes: 14 additions & 0 deletions
14
Content.Tests/DMProject/Tests/Database/too_many_or_few_params.dm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/proc/RunTest() | ||
var/database/db = new("params.db") | ||
|
||
var/database/query/query = new("CREATE TABLE test (id int)", 1, "foo", 1.5) | ||
query.Execute(db) | ||
|
||
ASSERT(!query.Error()) // no error for too many parameters | ||
|
||
query.Add("INSERT INTO test VALUES (?, ?, ?)", 1) | ||
query.Execute(db) | ||
|
||
ASSERT(query.Error()) // but there is an error for too few parameters | ||
|
||
fdel("params.db") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/database | ||
parent_type = /datum | ||
proc/Close() | ||
proc/Error() | ||
proc/ErrorMsg() | ||
New(filename) | ||
proc/Open(filename) | ||
|
||
/database/query | ||
var/_binobj as opendream_unimplemented | ||
proc/Add(text, ...) | ||
proc/Clear() | ||
Close() | ||
proc/Columns(column) | ||
Error() | ||
ErrorMsg() | ||
proc/Execute(database) | ||
proc/GetColumn(column) | ||
proc/GetRowData() | ||
New(text, ...) | ||
proc/NextRow() | ||
proc/Reset() | ||
proc/RowsAffected() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System.Data; | ||
using System.IO; | ||
using Microsoft.Data.Sqlite; | ||
using OpenDreamRuntime.Procs; | ||
|
||
namespace OpenDreamRuntime.Objects.Types; | ||
|
||
public sealed class DreamObjectDatabase(DreamObjectDefinition objectDefinition) : DreamObject(objectDefinition) { | ||
private SqliteConnection? _connection; | ||
|
||
private string? _errorMessage; | ||
private int? _errorCode; | ||
|
||
public override void Initialize(DreamProcArguments args) { | ||
base.Initialize(args); | ||
|
||
if (!args.GetArgument(0).TryGetValueAsString(out var filename)) { | ||
return; | ||
} | ||
|
||
if(Open(filename)) return; | ||
|
||
throw new DMCrashRuntime("Unable to open database."); | ||
} | ||
|
||
protected override void HandleDeletion() { | ||
Close(); | ||
base.HandleDeletion(); | ||
} | ||
|
||
/// <summary> | ||
/// Establish the connection to our SQLite database | ||
/// </summary> | ||
/// <param name="filename">The path to the SQLite file</param> | ||
public bool Open(string filename) { | ||
if (_connection?.State == ConnectionState.Open) { | ||
Close(); | ||
} | ||
|
||
filename = SanitizeFilename(filename); | ||
|
||
_connection = new SqliteConnection($"Data Source={filename};Mode=ReadWriteCreate"); | ||
|
||
try { | ||
_connection.Open(); | ||
return true; | ||
} catch (SqliteException exception) { | ||
Logger.GetSawmill("opendream.db").Error($"Failed to open database {filename} - {exception}"); | ||
return false; | ||
} | ||
} | ||
|
||
private static string SanitizeFilename(string filename) { | ||
foreach (var character in Path.GetInvalidFileNameChars()) { | ||
filename = filename.Replace(character.ToString(), ""); | ||
} | ||
|
||
return filename.Replace("=", "").Replace(";", ""); | ||
} | ||
|
||
/// <summary> | ||
/// Attempts to get the current connection to the SQLite database, if it is open | ||
/// </summary> | ||
/// <param name="connection">Variable to be populated with the connection.</param> | ||
/// <returns>Boolean of the success of the operation.</returns> | ||
public bool TryGetConnection(out SqliteConnection? connection) { | ||
if (_connection?.State == ConnectionState.Open) { | ||
connection = _connection; | ||
return true; | ||
} | ||
|
||
connection = null; | ||
return false; | ||
} | ||
|
||
public void SetError(int code, string message) { | ||
_errorCode = code; | ||
_errorMessage = message; | ||
} | ||
|
||
public int? GetErrorCode() { | ||
return _errorCode; | ||
} | ||
|
||
public string? GetErrorMessage() { | ||
return _errorMessage; | ||
} | ||
|
||
/// <summary> | ||
/// Closes the current SQLite connection, if it is established. | ||
/// </summary> | ||
public void Close() { | ||
_connection?.Close(); | ||
} | ||
} |
Oops, something went wrong.