Skip to content

Commit

Permalink
check for array type
Browse files Browse the repository at this point in the history
  • Loading branch information
denizzzka committed Mar 15, 2016
1 parent 9f65a71 commit 6921253
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
10 changes: 4 additions & 6 deletions src/dpq2/query.d
Expand Up @@ -199,21 +199,20 @@ void _integration_test( string connParam ) @trusted
import vibe.data.bson: Bson;

const string sql_query =
"select $1::text, $2::integer, $3::text, $4, $5::integer[], $6 as arr";
"select $1::text, $2::integer, $3::text, $4, $5::integer[]";

Value[6] args;
Value[5] args;
args[0] = toValue("абвгд");
args[1] = Value(ValueFormat.BINARY, OidType.Undefined); // undefined type NULL value
args[2] = toValue("123");
args[3] = Value(ValueFormat.BINARY, OidType.Int8); // NULL value
args[4] = bsonToValue(Bson.emptyArray);

Bson binArray = Bson([
Bson([Bson(null), Bson(123), Bson(456)]),
Bson([Bson(0), Bson(789), Bson(null)])
]);

args[5] = bsonToValue(binArray);
args[4] = bsonToValue(binArray);

QueryParams p;
p.sqlCommand = sql_query;
Expand All @@ -229,10 +228,9 @@ void _integration_test( string connParam ) @trusted
assert( a.OID(2) == OidType.Text );
assert( a.OID(3) == OidType.Int8 );
assert( a.OID(4) == OidType.Int4Array );
assert( a.OID(5) == OidType.Int4Array );

// binary args array test
assert( a[0][5].toBson == binArray );
assert( a[0][4].toBson == binArray );
}

// checking prepared statements
Expand Down
21 changes: 9 additions & 12 deletions src/dpq2/types/from_bson.d
Expand Up @@ -7,25 +7,25 @@ import vibe.data.bson;
import std.bitmanip: nativeToBigEndian;

/// Default type will be used for NULL value and for array without detected type
@property Value bsonToValue(Bson v, OidType defaultType = OidType.Unknown)
@property Value bsonToValue(Bson v)
{
if(v.type == Bson.Type.array)
return bsonArrayToValue(v, defaultType);
return bsonArrayToValue(v);
else
return bsonValueToValue(v, defaultType);
return bsonValueToValue(v);
}

private:

Value bsonValueToValue(Bson v, OidType defaultType)
Value bsonValueToValue(Bson v)
{
Value ret;

with(Bson.Type)
switch(v.type)
{
case null_:
ret = Value(ValueFormat.BINARY, defaultType);
ret = Value(ValueFormat.BINARY, OidType.Unknown);
break;

case int_:
Expand Down Expand Up @@ -70,13 +70,9 @@ unittest

assert(v1.as!string == v2.as!string);
}

{
Value v = bsonToValue(Bson.emptyArray);
}
}

Value bsonArrayToValue(ref Bson bsonArr, OidType defaultType)
Value bsonArrayToValue(ref Bson bsonArr)
{
ubyte[] nullValue() pure
{
Expand Down Expand Up @@ -126,7 +122,7 @@ Value bsonArrayToValue(ref Bson bsonArr, OidType defaultType)
break;

default:
Value v = bsonValueToValue(bElem, defaultType);
Value v = bsonValueToValue(bElem);

if(ap.OID == OidType.Unknown)
{
Expand All @@ -149,7 +145,8 @@ Value bsonArrayToValue(ref Bson bsonArr, OidType defaultType)

recursive(bsonArr, 0);

if(ap.OID == OidType.Unknown) ap.OID = defaultType;
if(ap.OID == OidType.Unknown)
throw new AnswerConvException(ConvExceptionType.NOT_ARRAY, "Unknown array type", __FILE__, __LINE__);

ArrayHeader_net h;
h.ndims = nativeToBigEndian(ap.dimsSize.length.to!int);
Expand Down

0 comments on commit 6921253

Please sign in to comment.