-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement /database
and /database/query
#1802
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e7b44f7
implement database
hry-gh 24efca2
additional documentation and cleanup
hry-gh d73b305
that's unpleasant
hry-gh 5fd0438
more comprehensive test suite for `/database`
hry-gh 83f4ed6
Merge branch 'master' into database
hry-gh d3f03af
ensure subtyped databases work correctly
hry-gh ac38cad
Merge branch 'master' into database
wixoaGit 1a49bc6
Apply suggestions from code review
wixoaGit 835f96e
Apply suggestions from code review
wixoaGit ce2071a
Merge remote-tracking branch 'upstream/master' into database
hry-gh fb1ee4c
sanitizes connectionstring control characters and invalid file name c…
hry-gh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"); | ||
hry-gh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / InspectCode
Use preferred style of 'new' expression when created type is evident Note