Skip to content

Commit

Permalink
RowBinding supports Nullable members now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Heromyth committed Dec 15, 2021
1 parent e8931c9 commit 12b0a32
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
50 changes: 35 additions & 15 deletions source/hunt/database/base/Row.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ import hunt.database.base.Tuple;

import hunt.logging.ConsoleLogger;

// import java.math.BigDecimal;
// import java.time.*;
// import java.time.temporal.Temporal;

import std.array;
import std.format;
import std.functional;
import std.meta;
import std.traits;
import std.typecons : Nullable;
import std.variant;

/**
Expand Down Expand Up @@ -345,7 +343,9 @@ interface Row : Tuple {

static if(hasUDA!(currentMember, Ignore)) {
version(HUNT_DEBUG) { warningf("Field %s.%s ignored.", T.stringof, member); }
} else static if(is(memberType == class) || is(memberType == struct) ) {
} else static if(is(memberType == class)) {
__traits(getMember, obj, member) = bind!(memberType, getColumnNameFun)();
} else static if(is(memberType == struct) && !is(memberType : Nullable!U, U)) {
__traits(getMember, obj, member) = bind!(memberType, getColumnNameFun)();
} else {
static if(hasUDA!(currentMember, Column)) {
Expand Down Expand Up @@ -390,7 +390,7 @@ interface Row : Tuple {

static if(hasUDA!(currentMember, Ignore)) {
version(HUNT_DEBUG) { warningf("Field %s.%s ignored.", T.stringof, member); }
} else static if(is(memberType == struct) ) {
} else static if((is(memberType == struct) && !is(memberType : Nullable!U, U))) {
__traits(getMember, obj, member) = bind!(memberType, getColumnNameFun)();
} else static if(is(memberType == class)) {
__traits(getMember, obj, member) = bind!(memberType, traverseBase, getColumnNameFun)(); // bug
Expand Down Expand Up @@ -435,19 +435,39 @@ interface Row : Tuple {
return T.init;
}

version(HUNT_DB_DEBUG) tracef("column, name=%s, index=%d", memberColumnName, columnIndex);

Variant currentColumnValue = getValue(columnIndex);
version(HUNT_DB_DEBUG) {
tracef("column, name=%s, index=%d, type {target: %s, source: %s}",
memberColumnName, columnIndex, T.stringof, currentColumnValue.type);
}

auto memberTypeInfo = typeid(T);
if(memberTypeInfo == currentColumnValue.type || currentColumnValue.convertsTo!(T)) {
// 1) If the types are same, or the column's type can convert to the member's type
return currentColumnValue.get!T();
static if(is(T : Nullable!U, U)) {
auto memberTypeInfo = typeid(U);
if(memberTypeInfo == currentColumnValue.type || currentColumnValue.convertsTo!(U)) {
// 1) If the types are same, or the column's type can convert to the member's type
U tmp = currentColumnValue.get!U();
return T(tmp);
} else if(currentColumnValue == null) {
return T.init;
} else {
// 2) try to coerce to T
U tmp = currentColumnValue.coerce!U();
return T(tmp);
}
} else {
// 2) try to coerce to T
return currentColumnValue.coerce!T();
// assert(false, format("Can't convert a value from %s to %s",
// currentColumnValue.type, memberTypeInfo));
auto memberTypeInfo = typeid(T);
if(memberTypeInfo == currentColumnValue.type || currentColumnValue.convertsTo!(T)) {
// 1) If the types are same, or the column's type can convert to the member's type
return currentColumnValue.get!T();
} else if(currentColumnValue == null) {
return T.init;
} else {
// 2) try to coerce to T
return currentColumnValue.coerce!T();
// assert(false, format("Can't convert a value from %s to %s",
// currentColumnValue.type, memberTypeInfo));
}
}

}
}
2 changes: 1 addition & 1 deletion source/hunt/database/base/impl/SocketConnectionBase.d
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ abstract class SocketConnectionBase : DbConnection {
}

private void handleClose(Throwable t) {
version(HUNT_DEBUG) {
version(HUNT_DB_DEBUG) {
infof("Connection closed. Throwable: %s", t is null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class PgConnectionFactory {
}

override void connectionClosed(Connection connection) {
version(HUNT_DEBUG) infof("Connection closed: %s", connection.getRemoteAddress());
version(HUNT_DB_DEBUG) infof("Connection closed: %s", connection.getRemoteAddress());
if(pgConn !is null)
pgConn.handleClosed(connection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RowResultDecoder(R) : RowDecoder {
row = new PgRowImpl(desc);
}

version(HUNT_DB_DEBUG) infof("row: %d", _size+1);
version(HUNT_DB_DEBUG) infof("row: %d, size: %d", _size+1, len);
Row row = new PgRowImpl(desc);
for (int c = 0; c < len; ++c) {
int length = buffer.readInt();
Expand Down

0 comments on commit 12b0a32

Please sign in to comment.