1 module dpq2.conv.from_d_types; 2 3 @safe: 4 5 import dpq2; 6 import std.bitmanip: nativeToBigEndian; 7 import std.traits: isNumeric; 8 9 @property Value toValue(T)(T v) 10 if(isNumeric!(T)) 11 { 12 return Value(v.nativeToBigEndian.dup, detectOidTypeFromNative!T, false, ValueFormat.BINARY); 13 } 14 15 @property Value toValue(T)(T v, ValueFormat valueFormat = ValueFormat.BINARY) @trusted 16 if(is(T == string)) 17 { 18 if(valueFormat == ValueFormat.TEXT) v = v~'\0'; // for prepareArgs only 19 20 ubyte[] buf = cast(ubyte[]) v; 21 22 return Value(buf, detectOidTypeFromNative!T, false, valueFormat); 23 } 24 25 @property Value toValue(T)(T v) @trusted 26 if(is(T == bool)) 27 { 28 ubyte[] buf; 29 buf.length = 1; 30 buf[0] = (v ? 1 : 0); 31 32 return Value(buf, detectOidTypeFromNative!T, false, ValueFormat.BINARY); 33 } 34 35 unittest 36 { 37 { 38 Value v = toValue(cast(short) 123); 39 40 assert(v.oidType == OidType.Int2); 41 assert(v.as!short == 123); 42 } 43 44 { 45 Value v = toValue(-123.456); 46 47 assert(v.oidType == OidType.Float8); 48 assert(v.as!double == -123.456); 49 } 50 51 { 52 Value v = toValue("Test string"); 53 54 assert(v.oidType == OidType.Text); 55 assert(v.as!string == "Test string"); 56 } 57 58 { 59 Value t = toValue(true); 60 Value f = toValue(false); 61 62 assert(t.as!bool == true); 63 assert(f.as!bool == false); 64 } 65 }