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 // FIXME: 17 // The pointer returned by PQgetvalue points to storage that is part of the PGresult structure. 18 // One should not modify the data it points to, and one must explicitly copy the data into other 19 // storage if it is to be used past the lifetime of the PGresult structure itself. 20 // Thus, it is need to store reference to Answer here to ensure that result is still available. 21 22 this(ubyte[] data, in OidType oidType, bool isNull, in ValueFormat format = ValueFormat.BINARY) pure 23 { 24 this._data = data; 25 this.format = format; 26 this.oidType = oidType; 27 this.isNull = isNull; 28 } 29 30 /// Null Value constructor 31 this(in ValueFormat format, in OidType oidType) pure 32 { 33 this.format = format; 34 this.oidType = oidType; 35 } 36 37 @property 38 inout (ubyte[]) data() pure inout 39 { 40 import std.exception; 41 import core.exception; 42 43 enforceEx!AssertError(!isNull, "Attempt to read NULL value", __FILE__, __LINE__); 44 45 return _data; 46 } 47 48 @property 49 bool isSupportedArray() const 50 { 51 return dpq2.oids.isSupportedArray(oidType); 52 } 53 54 debug string toString() const @trusted 55 { 56 import vibe.data.bson: Bson; 57 import dpq2.conv.to_bson; 58 import std.conv: to; 59 60 return this.as!Bson.toString~"::"~oidType.to!string~"("~(format == ValueFormat.TEXT? "t" : "b")~")"; 61 } 62 } 63 64 @trusted unittest 65 { 66 import dpq2.conv.to_d_types; 67 import core.exception: AssertError; 68 69 Value v = Value(ValueFormat.BINARY, OidType.Int4); 70 71 bool exceptionFlag = false; 72 73 try 74 cast(void) v.as!int; 75 catch(AssertError e) 76 exceptionFlag = true; 77 78 assert(exceptionFlag); 79 } 80 81 enum ValueFormat : int { 82 TEXT, 83 BINARY 84 }