Skip to content

Commit

Permalink
Merge branch 'master' of github.com:denizzzka/dpq2
Browse files Browse the repository at this point in the history
  • Loading branch information
denizzzka committed May 10, 2018
2 parents 33853aa + b3baad1 commit fc24dc1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/dpq2/conv/native_tests.d
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void _integration_test( string connParam ) @system
C!PGbytea([0x44, 0x20, 0x72, 0x75, 0x6c, 0x65, 0x73, 0x00, 0x21],
"bytea", r"E'\\x44 20 72 75 6c 65 73 00 21'"); // "D rules\x00!" (ASCII)
C!PGuuid(UUID("8b9ab33a-96e9-499b-9c36-aad1fe86d640"), "uuid", "'8b9ab33a-96e9-499b-9c36-aad1fe86d640'");
C!(Nullable!PGuuid)(Nullable!UUID(UUID("8b9ab33a-96e9-499b-9c36-aad1fe86d640")), "uuid", "'8b9ab33a-96e9-499b-9c36-aad1fe86d640'");

// numeric testing
C!PGnumeric("NaN", "numeric", "'NaN'");
Expand Down
24 changes: 12 additions & 12 deletions src/dpq2/conv/to_bson.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import dpq2.conv.to_d_types;
import dpq2.conv.numeric: rawValueToNumeric;
import vibe.data.bson;
import std.uuid;
import std.datetime: SysTime, dur, TimeZone;
import std.datetime: SysTime, dur, TimeZone, UTC;
import std.bitmanip: bigEndianToNative;
import std.conv: to;

///
Bson as(T)(in Value v, immutable TimeZone tz = null)
Bson as(T)(in Value v)
if(is(T == Bson))
{
if(v.isNull)
Expand All @@ -23,15 +23,15 @@ if(is(T == Bson))
else
{
if(v.isSupportedArray && ValueFormat.BINARY)
return arrayValueToBson(v, tz);
return arrayValueToBson(v);
else
return rawValueToBson(v, tz);
return rawValueToBson(v);
}
}

private:

Bson arrayValueToBson(in Value cell, immutable TimeZone tz)
Bson arrayValueToBson(in Value cell)
{
const ap = ArrayProperties(cell);

Expand Down Expand Up @@ -68,7 +68,7 @@ Bson arrayValueToBson(in Value cell, immutable TimeZone tz)
else
{
auto v = Value(cast(ubyte[]) cell.data[curr_offset .. curr_offset + size], ap.OID, false);
b = v.as!Bson(tz);
b = v.as!Bson;
}

curr_offset += size;
Expand All @@ -82,7 +82,7 @@ Bson arrayValueToBson(in Value cell, immutable TimeZone tz)
return recursive(0);
}

Bson rawValueToBson(in Value v, immutable TimeZone tz = null)
Bson rawValueToBson(in Value v)
{
if(v.format == ValueFormat.TEXT)
{
Expand Down Expand Up @@ -146,9 +146,9 @@ Bson rawValueToBson(in Value v, immutable TimeZone tz = null)
res = Bson(v.tunnelForBinaryValueAsCalls!PGuuid);
break;

case TimeStamp:
auto ts = v.tunnelForBinaryValueAsCalls!(dpq2.conv.time.TimeStamp);
auto time = BsonDate(SysTime(ts.dateTime, tz));
case TimeStampWithZone:
auto ts = v.tunnelForBinaryValueAsCalls!(dpq2.conv.time.TimeStampUTC);
auto time = BsonDate(SysTime(ts.dateTime, UTC()));
long usecs = ts.fracSec.total!"usecs";
res = Bson(["time": Bson(time), "usecs": Bson(usecs)]);
break;
Expand Down Expand Up @@ -208,7 +208,7 @@ public void _integration_test( string connParam )
auto answer = conn.execParams(params);

immutable Value v = answer[0][0];
Bson bsonRes = v.as!Bson(UTC());
Bson bsonRes = v.as!Bson;

if(v.isNull || !v.isSupportedArray) // standalone
{
Expand Down Expand Up @@ -254,7 +254,7 @@ public void _integration_test( string connParam )

C(Bson.emptyArray, "text[]", "'{}'");

C(Bson(["time": Bson(BsonDate(SysTime(DateTime(1997, 12, 17, 7, 37, 16), UTC()))), "usecs": Bson(cast(long) 12)]), "timestamp without time zone", "'1997-12-17 07:37:16.000012'");
C(Bson(["time": Bson(BsonDate(SysTime(DateTime(1997, 12, 17, 7, 37, 16), UTC()))), "usecs": Bson(cast(long) 12)]), "timestamp with time zone", "'1997-12-17 07:37:16.000012 UTC'");

C(Bson(Json(["float_value": Json(123.456), "text_str": Json("text string")])), "json", "'{\"float_value\": 123.456,\"text_str\": \"text string\"}'");

Expand Down
2 changes: 2 additions & 0 deletions src/dpq2/oids.d
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private OidType detectOidTypeNotCareAboutNullable(T)()
import std.datetime.date : StdDate = Date, TimeOfDay;
import std.datetime.systime : SysTime;
import std.traits : Unqual;
import std.uuid : StdUUID = UUID;
static import dpq2.conv.time;
import vibe.data.json : VibeJson = Json;

Expand All @@ -189,6 +190,7 @@ private OidType detectOidTypeNotCareAboutNullable(T)()
static if(is(UT == dpq2.conv.time.TimeStamp)){ return TimeStamp; } else
static if(is(UT == dpq2.conv.time.TimeStampUTC)){ return TimeStampWithZone; } else
static if(is(UT == VibeJson)){ return Json; } else
static if(is(UT == StdUUID)){ return UUID; } else

static assert(false, "Unsupported D type: "~T.stringof);
}
Expand Down

0 comments on commit fc24dc1

Please sign in to comment.