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 vibe.data.bson: Bson; 51 import dpq2.conv.to_bson; 52 import std.conv: to; 53 54 return this.as!Bson.toString~"::"~oidType.to!string~"("~(format == ValueFormat.TEXT? "t" : "b")~")"; 55 } 56 } 57 58 @trusted unittest 59 { 60 import dpq2.conv.to_d_types; 61 import core.exception: AssertError; 62 63 Value v = Value(ValueFormat.BINARY, OidType.Int4); 64 65 bool exceptionFlag = false; 66 67 try 68 cast(void) v.as!int; 69 catch(AssertError e) 70 exceptionFlag = true; 71 72 assert(exceptionFlag); 73 } 74 75 enum ValueFormat : int { 76 TEXT, 77 BINARY 78 }