Skip to content
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

Expose column names in row struct #188

Merged
merged 1 commit into from
Feb 23, 2019
Merged
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
5 changes: 3 additions & 2 deletions source/mysql/protocol/comms.d
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ body

// Moved here from `struct Row.this`
package(mysql) void ctorRow(Connection conn, ref ubyte[] packet, ResultSetHeaders rh, bool binary,
out Variant[] _values, out bool[] _nulls)
out Variant[] _values, out bool[] _nulls, out string[] _names)
in
{
assert(rh.fieldCount <= uint.max);
Expand All @@ -646,7 +646,7 @@ body
scope(failure) conn.kill();

uint fieldCount = cast(uint)rh.fieldCount;
_values.length = _nulls.length = fieldCount;
_values.length = _nulls.length = _names.length = fieldCount;

if(binary)
{
Expand All @@ -669,6 +669,7 @@ body
do
{
FieldDescription fd = rh[i];
_names[i] = fd.name;
sqlValue = packet.consumeIfComplete(fd.type, binary, fd.unsigned, fd.charSet);
// TODO: Support chunk delegate
if(sqlValue.isIncomplete)
Expand Down
11 changes: 10 additions & 1 deletion source/mysql/result.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ package:
Variant[] _values; // Temporarily "package" instead of "private"
private:
bool[] _nulls;
string[] _names;

public:

Expand All @@ -51,7 +52,7 @@ public:
+/
this(Connection con, ref ubyte[] packet, ResultSetHeaders rh, bool binary)
{
ctorRow(con, packet, rh, binary, _values, _nulls);
ctorRow(con, packet, rh, binary, _values, _nulls, _names);
}

/++
Expand All @@ -72,6 +73,14 @@ public:
return _values[i];
}

/++
Get the name of the column with specified index.
+/
inout(string) getName(size_t index) inout
{

Choose a reason for hiding this comment

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

First of all, thanks for your patience, and I apologize for all the delays this has taken. I'm going to merge this PR now (it looks good, I like it) and tag a new release.

But regardless of that, I wanted to ask (probably a stupid question) about this particular line. I have to admit I'm not 100% up-to-date with the details of D's inout. IIUC, I think this function signature means that if the user has a Row, const(Row) or immutable(Row), then the return value of getName will be either string, const(string) or immutable(string) respectively. Is that correct?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for merging this and sorry for the late answer: I don't know too much about inout either, I just copied it from inout(Variant) opIndex(size_t i) inout. However, as far as I know your understanding is correct. Thinking about it now, the inout here is probably useless.

return _names[index];
}

/++
Check if a column in the result row was NULL

Expand Down