denizzzka / dpq2
1
/**
2
*   PostgreSQL major types oids.
3
*
4
*   Copyright: © 2014 DSoftOut
5
*   Authors: NCrashed <ncrashed@gmail.com>
6
*/
7

8
module dpq2.oids;
9

10
@safe:
11

12
package OidType oid2oidType(Oid oid) pure
13
{
14
    static assert(Oid.sizeof == OidType.sizeof);
15

16 1
    return cast(OidType)(oid);
17
}
18

19
/**
20
 * Convert between array Oid and element Oid or vice versa
21
 *
22
 * Params:
23
 *  s = "array" or "element"
24
 *  type = source object type
25
 */
26
OidType oidConvTo(string s)(OidType type)
27
{
28 1
    foreach(ref a; appropriateArrOid)
29
    {
30
        static if(s == "array")
31
        {
32 1
            if(a.value == type)
33 1
                return a.array;
34
        }
35
        else
36
        static if(s == "element")
37
        {
38 0
            if(a.array == type)
39 0
                return a.value;
40
        }
41
        else
42
        static assert(false, "Wrong oidConvTo type "~s);
43
    }
44

45
    import dpq2.value: ValueConvException, ConvExceptionType;
46
    import std.conv: to;
47

48 0
    throw new ValueConvException(
49
            ConvExceptionType.NOT_IMPLEMENTED,
50
            "Conv to "~s~" for type "~type.to!string~" isn't defined",
51
            __FILE__, __LINE__
52
        );
53
}
54

55
/// Checks if Oid type can be mapped to native D integer
56
bool isNativeInteger(OidType t) pure
57
{
58
    with(OidType)
59 1
    switch(t)
60
    {
61 1
        case Int8:
62 1
        case Int2:
63 1
        case Int4:
64 1
            return true;
65 1
        default:
66 1
            break;
67
    }
68

69 1
    return false;
70
}
71

72
/// Checks if Oid type can be mapped to native D float
73
bool isNativeFloat(OidType t) pure
74
{
75
    with(OidType)
76 1
    switch(t)
77
    {
78 0
        case Float4:
79 1
        case Float8:
80 1
            return true;
81 1
        default:
82 1
            break;
83
    }
84

85 1
    return false;
86
}
87

88
package:
89

90
private struct AppropriateArrOid
91
{
92
    OidType value;
93
    OidType array;
94
}
95

96
private static immutable AppropriateArrOid[] appropriateArrOid;
97

98
shared static this()
99
{
100
    alias A = AppropriateArrOid;
101

102
    with(OidType)
103
    {
104 1
        immutable AppropriateArrOid[] a =
105
        [
106
            A(Text, TextArray),
107
            A(Bool, BoolArray),
108
            A(Int2, Int2Array),
109
            A(Int4, Int4Array),
110
            A(Int8, Int8Array),
111
            A(Float4, Float4Array),
112
            A(Float8, Float8Array),
113
            A(Date, DateArray),
114
            A(Time, TimeArray),
115
            A(TimeStampWithZone, TimeStampWithZoneArray),
116
            A(TimeStamp, TimeStampArray),
117
            A(Json, JsonArray),
118
            A(UUID, UUIDArray)
119
        ];
120

121 1
        appropriateArrOid = a;
122
    }
123
}
124

125
import derelict.pq.pq: Oid;
126

127
bool isSupportedArray(OidType t) pure nothrow @nogc
128
{
129
    with(OidType)
130 1
    switch(t)
131
    {
132 0
        case BoolArray:
133 0
        case ByteArrayArray:
134 0
        case CharArray:
135 0
        case Int2Array:
136 1
        case Int4Array:
137 1
        case TextArray:
138 1
        case Int8Array:
139 1
        case Float4Array:
140 1
        case Float8Array:
141 1
        case TimeStampArray:
142 1
        case TimeStampWithZoneArray:
143 1
        case DateArray:
144 1
        case TimeArray:
145 1
        case TimeWithZoneArray:
146 1
        case NumericArray:
147 1
        case UUIDArray:
148 1
        case JsonArray:
149 1
        case JsonbArray:
150 1
            return true;
151 1
        default:
152 1
            break;
153
    }
154

155 1
    return false;
156
}
157

158
OidType detectOidTypeFromNative(T)()
159
{
160
    import std.typecons : Nullable;
161

162
    static if(is(T == Nullable!R,R))
163 1
        return detectOidTypeNotCareAboutNullable!(typeof(T.get));
164
    else
165 1
        return detectOidTypeNotCareAboutNullable!T;
166
}
167

168
private OidType detectOidTypeNotCareAboutNullable(T)()
169
{
170
    import std.bitmanip : BitArray;
171
    import std.datetime.date : StdDate = Date, TimeOfDay, DateTime;
172
    import std.datetime.systime : SysTime;
173
    import std.traits : Unqual, isSomeString;
174
    import std.uuid : StdUUID = UUID;
175
    static import dpq2.conv.time;
176
    import vibe.data.json : VibeJson = Json;
177

178
    alias UT = Unqual!T;
179

180
    with(OidType)
181
    {
182 1
        static if(isSomeString!UT){ return Text; } else
183 1
        static if(is(UT == ubyte[])){ return ByteArray; } else
184 1
        static if(is(UT == bool)){ return Bool; } else
185 1
        static if(is(UT == short)){ return Int2; } else
186 1
        static if(is(UT == int)){ return Int4; } else
187 1
        static if(is(UT == long)){ return Int8; } else
188 0
        static if(is(UT == float)){ return Float4; } else
189 1
        static if(is(UT == double)){ return Float8; } else
190 1
        static if(is(UT == StdDate)){ return Date; } else
191 1
        static if(is(UT == TimeOfDay)){ return Time; } else
192 1
        static if(is(UT == DateTime)){ return TimeStamp; } else
193 1
        static if(is(UT == SysTime)){ return TimeStampWithZone; } else
194 1
        static if(is(UT == dpq2.conv.time.TimeStamp)){ return TimeStamp; } else
195 1
        static if(is(UT == dpq2.conv.time.TimeStampUTC)){ return TimeStampWithZone; } else
196 0
        static if(is(UT == VibeJson)){ return Json; } else
197 0
        static if(is(UT == StdUUID)){ return UUID; } else
198 1
        static if(is(UT == BitArray)){ return VariableBitString; } else
199

200
        static assert(false, "Unsupported D type: "~T.stringof);
201
    }
202
}
203

204
/// Enum of Oid types defined in PG
205
public enum OidType : Oid
206
{
207
    Undefined = 0, ///
208

209
    Bool = 16, ///
210
    ByteArray = 17, ///
211
    Char = 18, ///
212
    Name = 19, ///
213
    Int8 = 20, ///
214
    Int2 = 21, ///
215
    Int2Vector = 22, ///
216
    Int4 = 23, ///
217
    RegProc = 24, ///
218
    Text = 25, ///
219
    Oid = 26, ///
220
    Tid = 27, ///
221
    Xid = 28, ///
222
    Cid = 29, ///
223
    OidVector = 30, ///
224

225
    AccessControlList = 1033, ///
226
    TypeCatalog = 71, ///
227
    AttributeCatalog = 75, ///
228
    ProcCatalog = 81, ///
229
    ClassCatalog = 83, ///
230

231
    Json = 114, ///
232
    Jsonb = 3802, ///
233
    Xml = 142, ///
234
    NodeTree = 194, ///
235
    StorageManager = 210, ///
236

237
    Point = 600, ///
238
    LineSegment = 601, ///
239
    Path = 602, ///
240
    Box = 603, ///
241
    Polygon = 604, ///
242
    Line = 628, ///
243

244
    Float4 = 700, ///
245
    Float8 = 701, ///
246
    AbsTime = 702, ///
247
    RelTime = 703, ///
248
    Interval = 704, ///
249
    Unknown = 705, ///
250

251
    Circle = 718, ///
252
    Money = 790, ///
253
    MacAddress = 829, ///
254
    HostAddress = 869, ///
255
    NetworkAddress = 650, ///
256

257
    FixedString = 1042, ///
258
    VariableString = 1043, ///
259

260
    Date = 1082, ///
261
    Time = 1083, ///
262
    TimeStamp = 1114, ///
263
    TimeStampWithZone = 1184, ///
264
    TimeInterval = 1186, ///
265
    TimeWithZone = 1266, ///
266

267
    FixedBitString = 1560, ///
268
    VariableBitString = 1562, ///
269

270
    Numeric = 1700, ///
271
    RefCursor = 1790, ///
272
    RegProcWithArgs = 2202, ///
273
    RegOperator = 2203, ///
274
    RegOperatorWithArgs = 2204, ///
275
    RegClass = 2205, ///
276
    RegType = 2206, ///
277

278
    UUID = 2950, ///
279
    TSVector = 3614, ///
280
    GTSVector = 3642, ///
281
    TSQuery = 3615, ///
282
    RegConfig = 3734, ///
283
    RegDictionary = 3769, ///
284
    TXidSnapshot = 2970, ///
285

286
    Int4Range = 3904, ///
287
    NumRange = 3906, ///
288
    TimeStampRange = 3908, ///
289
    TimeStampWithZoneRange = 3910, ///
290
    DateRange = 3912, ///
291
    Int8Range = 3926, ///
292

293
    // Arrays
294
    XmlArray = 143, ///
295
    JsonbArray = 3807, ///
296
    JsonArray = 199, ///
297
    BoolArray = 1000, ///
298
    ByteArrayArray = 1001, ///
299
    CharArray = 1002, ///
300
    NameArray = 1003, ///
301
    Int2Array = 1005, ///
302
    Int2VectorArray = 1006, ///
303
    Int4Array = 1007, ///
304
    RegProcArray = 1008, ///
305
    TextArray = 1009, ///
306
    OidArray  = 1028, ///
307
    TidArray = 1010, ///
308
    XidArray = 1011, ///
309
    CidArray = 1012, ///
310
    OidVectorArray = 1013, ///
311
    FixedStringArray = 1014, ///
312
    VariableStringArray = 1015, ///
313
    Int8Array = 1016, ///
314
    PointArray = 1017, ///
315
    LineSegmentArray = 1018, ///
316
    PathArray = 1019, ///
317
    BoxArray = 1020, ///
318
    Float4Array = 1021, ///
319
    Float8Array = 1022, ///
320
    AbsTimeArray = 1023, ///
321
    RelTimeArray = 1024, ///
322
    IntervalArray = 1025, ///
323
    PolygonArray = 1027, ///
324
    AccessControlListArray = 1034, ///
325
    MacAddressArray = 1040, ///
326
    HostAdressArray = 1041, ///
327
    NetworkAdressArray = 651, ///
328
    CStringArray = 1263, ///
329
    TimeStampArray = 1115, ///
330
    DateArray = 1182, ///
331
    TimeArray = 1183, ///
332
    TimeStampWithZoneArray = 1185, ///
333
    TimeIntervalArray = 1187, ///
334
    NumericArray = 1231, ///
335
    TimeWithZoneArray = 1270, ///
336
    FixedBitStringArray = 1561, ///
337
    VariableBitStringArray = 1563, ///
338
    RefCursorArray = 2201, ///
339
    RegProcWithArgsArray = 2207, ///
340
    RegOperatorArray = 2208, ///
341
    RegOperatorWithArgsArray = 2209, ///
342
    RegClassArray = 2210, ///
343
    RegTypeArray = 2211, ///
344
    UUIDArray = 2951, ///
345
    TSVectorArray = 3643, ///
346
    GTSVectorArray = 3644, ///
347
    TSQueryArray = 3645, ///
348
    RegConfigArray = 3735, ///
349
    RegDictionaryArray = 3770, ///
350
    TXidSnapshotArray = 2949, ///
351
    Int4RangeArray = 3905, ///
352
    NumRangeArray = 3907, ///
353
    TimeStampRangeArray = 3909, ///
354
    TimeStampWithZoneRangeArray = 3911, ///
355
    DateRangeArray = 3913, ///
356
    Int8RangeArray = 3927, ///
357

358
    // Pseudo types
359
    Record = 2249, ///
360
    RecordArray = 2287, ///
361
    CString = 2275, ///
362
    AnyVoid = 2276, ///
363
    AnyArray = 2277, ///
364
    Void = 2278, ///
365
    Trigger = 2279, ///
366
    EventTrigger = 3838, ///
367
    LanguageHandler = 2280, ///
368
    Internal = 2281, ///
369
    Opaque = 2282, ///
370
    AnyElement = 2283, ///
371
    AnyNoArray = 2776, ///
372
    AnyEnum = 3500, ///
373
    FDWHandler = 3115, ///
374
    AnyRange = 3831, ///
375
}

Read our documentation on viewing source code .

Loading