Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Content.Tests/DMProject/Tests/Database/clear.dm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
query.Add()

// Execute without a command does nothing
query.Execute()
query.Execute(db)

// and shouldn't report an error
ASSERT(!query.Error())
Expand Down
7 changes: 3 additions & 4 deletions Content.Tests/DMProject/Tests/Database/no_entries.dm
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

query.Add("SELECT * FROM foobar")
query.Execute(db)
query.NextRow()
ASSERT(!query.NextRow())

query.GetRowData()

ASSERT(query.Error() && query.ErrorMsg())
ASSERT(query.GetRowData()["id"] == null)
ASSERT(!query.Error())

del(query)
del(db)
Expand Down
19 changes: 9 additions & 10 deletions Content.Tests/DMProject/Tests/Database/read.dm
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@
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)
ASSERT(query.GetColumn(1) == 1)
ASSERT(query.GetColumn(2) == "foo")
ASSERT(query.GetColumn(3) == 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(1) == "id")
ASSERT(query.Columns(2) == "name")
ASSERT(query.Columns(3) == "points")

ASSERT(!query.Columns(10))

ASSERT(query.Error() && query.ErrorMsg())
ASSERT(!query.Error())

query.Close()
db.Close()
Expand All @@ -46,10 +45,10 @@
query.Execute(db)
query.NextRow()

ASSERT(query.GetColumn(0) == 1)
ASSERT(query.GetColumn(1) == 1)

ASSERT(!query.GetColumn(10))
ASSERT(query.Error() && query.ErrorMsg())
ASSERT(!query.Error())

del(query)
del(db)
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/DreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void PreInitialize(string? jsonPath) {
_sawmill = Logger.GetSawmill("opendream");
ListPoolThreshold = _config.GetCVar(OpenDreamCVars.ListPoolThreshold);
ListPoolSize = _config.GetCVar(OpenDreamCVars.ListPoolSize);

ByondApi.ByondApi.Initialize(this, _refManager, _atomManager, _dreamMapManager, _objectTree);

InitializeConnectionManager();
Expand Down
21 changes: 14 additions & 7 deletions OpenDreamRuntime/Objects/Types/DreamObjectDatabaseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public sealed class DreamObjectDatabaseQuery(DreamObjectDefinition objectDefinit
private SqliteCommand? _command;
private SqliteDataReader? _reader;

public DreamObjectDatabase? DreamObjectDatabase;

private string? _errorMessage;
private int? _errorCode;

Expand All @@ -29,6 +31,7 @@ protected override void HandleDeletion(bool possiblyThreaded) {

ClearCommand();
CloseReader();
DreamObjectDatabase = null;
base.HandleDeletion(possiblyThreaded);
}

Expand Down Expand Up @@ -98,12 +101,13 @@ public DreamValue GetColumn(int id) {
return DreamValue.Null;
}

id = Math.Max(--id, 0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So <= 0 is equal to 1 here in byond?

Should be added to our tests if so


try {
var name = _reader.GetName(id);
return new DreamValue(name);
} catch (IndexOutOfRangeException exception) {
_errorCode = 1;
_errorMessage = exception.Message;
// BYOND just ignores this, and doesn't report it to Error() or ErrorMsg()
}

return DreamValue.Null;
Expand All @@ -130,12 +134,14 @@ public void CloseReader() {
/// <summary>
/// Executes the currently held query against the SQLite database
/// </summary>
/// <param name="database">The <see cref="DreamObjectDatabase"/> that this query is being run against.</param>
/// <param name="database">The <see cref="Types.DreamObjectDatabase"/> that this query is being run against.</param>
public void ExecuteCommand(DreamObjectDatabase database) {
if (!database.TryGetConnection(out var connection)) {
throw new DMCrashRuntime("Bad database");
}

DreamObjectDatabase = database;

if (_command == null) {
return;
}
Expand Down Expand Up @@ -167,12 +173,13 @@ public bool TryGetColumn(int column, out DreamValue value) {
return false;
}

column = Math.Max(--column, 0);

try {
value = GetDreamValueFromDbObject(_reader.GetValue(column));
return true;
} catch (Exception exception) {
_errorCode = 1;
_errorMessage = exception.Message;
// DM ignores any errors here, and just returns null
}

value = DreamValue.Null;
Expand All @@ -194,8 +201,8 @@ public bool TryGetColumn(int column, out DreamValue value) {
dict[name] = GetDreamValueFromDbObject(value);
}
} catch (InvalidOperationException exception) {
_errorCode = 1;
_errorMessage = exception.Message;
// DM does not care for "no data" errors
// and does not report if this happens
}

return dict;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public static DreamValue NativeProc_Execute(NativeProc.Bundle bundle, DreamObjec
var query = (DreamObjectDatabaseQuery)src!;

if (!bundle.GetArgument(0, "database").TryGetValueAsDreamObject(out DreamObjectDatabase? database))
return DreamValue.Null;
if (query.DreamObjectDatabase != null) {
database = query.DreamObjectDatabase;
} else {
throw new DMCrashRuntime("Bad Database: no database provided");
}

query.ExecuteCommand(database);

Expand Down
Loading