1 module dpq2.value; 2 3 @safe: 4 5 import dpq2.oids; 6 7 /// Minimal Postgres value 8 struct Value 9 { 10 bool isNull = true; 11 OidType oidType = OidType.Undefined; 12 13 package ValueFormat format; 14 package ubyte[] _data; 15 16 this(ubyte[] data, in OidType oidType, bool isNull, in ValueFormat format = ValueFormat.BINARY) pure 17 { 18 this._data = data; 19 this.format = format; 20 this.oidType = oidType; 21 this.isNull = isNull; 22 } 23 24 /// Null Value constructor 25 this(in ValueFormat format, in OidType oidType) pure 26 { 27 this.format = format; 28 this.oidType = oidType; 29 } 30 31 @property 32 inout (ubyte[]) data() pure inout 33 { 34 import std.exception; 35 import core.exception; 36 37 enforceEx!AssertError(!isNull, "Attempt to read NULL value", __FILE__, __LINE__); 38 39 return _data; 40 } 41 42 @property 43 bool isSupportedArray() const 44 { 45 return dpq2.oids.isSupportedArray(oidType); 46 } 47 48 debug string toString() const @trusted 49 { 50 import dpq2.types.to_bson; 51 import std.conv: to; 52 53 return toBson(this).toString~"::"~oidType.to!string~"("~(format == ValueFormat.TEXT? "t" : "b")~")"; 54 } 55 } 56 57 @trusted unittest 58 { 59 import dpq2.types.to_d_types; 60 import core.exception: AssertError; 61 62 Value v = Value(ValueFormat.BINARY, OidType.Int4); 63 64 bool exceptionFlag = false; 65 66 try 67 cast(void) v.as!int; 68 catch(AssertError e) 69 exceptionFlag = true; 70 71 assert(exceptionFlag); 72 } 73 74 enum ValueFormat : int { 75 TEXT, 76 BINARY 77 }