1 /* 2 * Copyright (C) 2019, HuntLabs 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 module hunt.database.driver.postgresql.impl.codec.DataTypeDesc; 18 19 import hunt.database.driver.postgresql.impl.codec.DataType; 20 21 import hunt.database.driver.postgresql.data.Box; 22 import hunt.database.driver.postgresql.data.Circle; 23 import hunt.database.driver.postgresql.data.Line; 24 import hunt.database.driver.postgresql.data.LineSegment; 25 import hunt.database.driver.postgresql.data.Interval; 26 import hunt.database.driver.postgresql.data.Path; 27 import hunt.database.driver.postgresql.data.Point; 28 import hunt.database.driver.postgresql.data.Polygon; 29 30 import hunt.database.base.Numeric; 31 32 import hunt.util.ObjectUtils; 33 34 import std.format; 35 import std.conv; 36 37 /** 38 * PostgreSQL <a href="https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.h">object 39 * identifiers (OIDs)</a> for data types 40 * 41 * @author <a href="mailto:emad.albloushi@gmail.com">Emad Alblueshi</a> 42 */ 43 struct DataTypeDesc { 44 45 int id; 46 bool supportsBinary; 47 string[] encodingType; // Not really used for now 48 string[] decodingType; 49 50 this(int id, bool supportsBinary, string[] type) { 51 this.id = id; 52 this.supportsBinary = supportsBinary; 53 this.decodingType = type; 54 this.encodingType = type; 55 } 56 57 this(int id, bool supportsBinary, string[] encodingType, string[] decodingType) { 58 this.id = id; 59 this.supportsBinary = supportsBinary; 60 this.encodingType = encodingType; 61 this.decodingType = decodingType; 62 } 63 64 string toString() { 65 return format("DataType=%s(%d), supportsBinary=%s", cast(DataType)id, id, supportsBinary); 66 } 67 68 // static DataType valueOf(int oid) { 69 // DataType value = oidToDataType.get(oid); 70 // if (value is null) { 71 // logger.debug("Postgres type OID=" ~ oid ~ " not handled - using unknown type instead"); 72 // return UNKNOWN; 73 // } else { 74 // return value; 75 // } 76 // } 77 78 // private static IntObjectMap!(DataType) oidToDataType = new IntObjectHashMap<>(); 79 80 // static { 81 // for (DataType dataType : values()) { 82 // oidToDataType.put(dataType.id, dataType); 83 // } 84 // } 85 } 86 87 88 89 struct DataTypes { 90 91 enum DataTypeDesc BOOL = DataTypeDesc(16, true, [bool.stringof]); // Boolean.class); 92 enum DataTypeDesc BOOL_ARRAY = DataTypeDesc(1000, true, null); // Boolean[].class); 93 enum DataTypeDesc INT2 = DataTypeDesc(21, true, [short.stringof, ushort.stringof]); // Short.class, Number.class); 94 enum DataTypeDesc INT2_ARRAY = DataTypeDesc(1005, true, null); // Short[].class, Number[].class); 95 enum DataTypeDesc INT4 = DataTypeDesc(23, true, [int.stringof, uint.stringof]); // Integer.class, Number.class); 96 enum DataTypeDesc INT4_ARRAY = DataTypeDesc(1007, true, null); // Integer[].class, Number[].class); 97 enum DataTypeDesc INT8 = DataTypeDesc(20, true, [long.stringof, ulong.stringof]); // Long.class, Number.class); 98 enum DataTypeDesc INT8_ARRAY = DataTypeDesc(1016, true, null); // Long[].class, Number[].class); 99 enum DataTypeDesc FLOAT4 = DataTypeDesc(700, true, [float.stringof]); // Float.class, Number.class); 100 enum DataTypeDesc FLOAT4_ARRAY = DataTypeDesc(1021, true, null); // Float[].class, Number[].class); 101 enum DataTypeDesc FLOAT8 = DataTypeDesc(701, true, [double.stringof, float.stringof]); // Double.class, Number.class); 102 enum DataTypeDesc FLOAT8_ARRAY = DataTypeDesc(1022, true, null); // Double[].class, Number[].class); 103 enum DataTypeDesc NUMERIC = DataTypeDesc(1700, false, null); // Numeric.class, Number.class); 104 enum DataTypeDesc NUMERIC_ARRAY = DataTypeDesc(1231, false, null); // Numeric[].class, Number[].class); 105 enum DataTypeDesc MONEY = DataTypeDesc(790, true, null); // Object.class); 106 enum DataTypeDesc MONEY_ARRAY = DataTypeDesc(791, true, null); // Object[].class); 107 enum DataTypeDesc BIT = DataTypeDesc(1560, true, null); // Object.class); 108 enum DataTypeDesc BIT_ARRAY = DataTypeDesc(1561, true, null); // Object[].class); 109 enum DataTypeDesc VARBIT = DataTypeDesc(1562, true, null); // Object.class); 110 enum DataTypeDesc VARBIT_ARRAY = DataTypeDesc(1563, true, null); // Object[].class); 111 enum DataTypeDesc CHAR = DataTypeDesc(18, true, ["char", "string", "immutable(char)[]"]); // String.class); 112 enum DataTypeDesc CHAR_ARRAY = DataTypeDesc(1002, true, null); // String[].class); 113 enum DataTypeDesc VARCHAR = DataTypeDesc(1043, true, ["string", "immutable(char)[]"]); // String.class); 114 enum DataTypeDesc VARCHAR_ARRAY = DataTypeDesc(1015, true, null); // String[].class); 115 enum DataTypeDesc BPCHAR = DataTypeDesc(1042, true, null); // String.class); 116 enum DataTypeDesc BPCHAR_ARRAY = DataTypeDesc(1014, true, null); // String[].class); 117 enum DataTypeDesc TEXT = DataTypeDesc(25, true, ["string", "immutable(char)[]"]); // String.class); 118 enum DataTypeDesc TEXT_ARRAY = DataTypeDesc(1009, true, null); // String[].class); 119 enum DataTypeDesc NAME = DataTypeDesc(19, true, ["string", "immutable(char)[]"]); // String.class); 120 enum DataTypeDesc NAME_ARRAY = DataTypeDesc(1003, true, null); // String[].class); 121 enum DataTypeDesc DATE = DataTypeDesc(1082, true, null); // LocalDate.class); 122 enum DataTypeDesc DATE_ARRAY = DataTypeDesc(1182, true, null); // LocalDate[].class); 123 enum DataTypeDesc TIME = DataTypeDesc(1083, true, null); // LocalTime.class); 124 enum DataTypeDesc TIME_ARRAY = DataTypeDesc(1183, true, null); // LocalTime[].class); 125 enum DataTypeDesc TIMETZ = DataTypeDesc(1266, true, null); // OffsetTime.class); 126 enum DataTypeDesc TIMETZ_ARRAY = DataTypeDesc(1270, true, null); // OffsetTime[].class); 127 enum DataTypeDesc TIMESTAMP = DataTypeDesc(1114, true, null); // LocalDateTime.class); 128 enum DataTypeDesc TIMESTAMP_ARRAY = DataTypeDesc(1115, true, null); // LocalDateTime[].class); 129 enum DataTypeDesc TIMESTAMPTZ = DataTypeDesc(1184, true, null); // OffsetDateTime.class); 130 enum DataTypeDesc TIMESTAMPTZ_ARRAY = DataTypeDesc(1185, true, null); // OffsetDateTime[].class); 131 enum DataTypeDesc INTERVAL = DataTypeDesc(1186, true, null); // Interval.class); 132 enum DataTypeDesc INTERVAL_ARRAY = DataTypeDesc(1187, true, null); // Interval[].class); 133 enum DataTypeDesc BYTEA = DataTypeDesc(17, true, null); // Buffer.class); 134 enum DataTypeDesc BYTEA_ARRAY = DataTypeDesc(1001, true, null); // Buffer[].class); 135 enum DataTypeDesc MACADDR = DataTypeDesc(829, true, null); // Object.class); 136 enum DataTypeDesc INET = DataTypeDesc(869, true, null); // Object[].class); 137 enum DataTypeDesc CIDR = DataTypeDesc(650, true, null); // Object.class); 138 enum DataTypeDesc MACADDR8 = DataTypeDesc(774, true, null); // Object[].class); 139 enum DataTypeDesc UUID = DataTypeDesc(2950, true, null); // UUID.class); 140 enum DataTypeDesc UUID_ARRAY = DataTypeDesc(2951, true, null); // UUID[].class); 141 enum DataTypeDesc JSON = DataTypeDesc(114, true, null); // Object.class); 142 enum DataTypeDesc JSON_ARRAY = DataTypeDesc(199, true, null); // Object[].class); 143 enum DataTypeDesc JSONB = DataTypeDesc(3802, true, null); // Object.class); 144 enum DataTypeDesc JSONB_ARRAY = DataTypeDesc(3807, true, null); // Object[].class); 145 enum DataTypeDesc XML = DataTypeDesc(142, true, null); // Object.class); 146 enum DataTypeDesc XML_ARRAY = DataTypeDesc(143, true, null); // Object[].class); 147 enum DataTypeDesc POINT = DataTypeDesc(600, true, null); // Point.class); 148 enum DataTypeDesc POINT_ARRAY = DataTypeDesc(1017, true, null); // Point[].class); 149 enum DataTypeDesc LINE = DataTypeDesc(628, true, null); // Line.class); 150 enum DataTypeDesc LINE_ARRAY = DataTypeDesc(629, true, null); // Line[].class); 151 enum DataTypeDesc LSEG = DataTypeDesc(601, true, null); // LineSegment.class); 152 enum DataTypeDesc LSEG_ARRAY = DataTypeDesc(1018, true, null); // LineSegment[].class); 153 enum DataTypeDesc BOX = DataTypeDesc(603, true, null); // Box.class); 154 enum DataTypeDesc BOX_ARRAY = DataTypeDesc(1020, true, null); // Box[].class); 155 enum DataTypeDesc PATH = DataTypeDesc(602, true, null); // Path.class); 156 enum DataTypeDesc PATH_ARRAY = DataTypeDesc(1019, true, null); // Path[].class); 157 enum DataTypeDesc POLYGON = DataTypeDesc(604, true, null); // Polygon.class); 158 enum DataTypeDesc POLYGON_ARRAY = DataTypeDesc(1027, true, null); // Polygon[].class); 159 enum DataTypeDesc CIRCLE = DataTypeDesc(718, true, null); // Circle.class); 160 enum DataTypeDesc CIRCLE_ARRAY = DataTypeDesc(719, true, null); // Circle[].class); 161 enum DataTypeDesc HSTORE = DataTypeDesc(33670, true, null); // Object.class); 162 enum DataTypeDesc OID = DataTypeDesc(26, true, null); // Object.class); 163 enum DataTypeDesc OID_ARRAY = DataTypeDesc(1028, true, null); // Object[].class); 164 enum DataTypeDesc VOID = DataTypeDesc(2278, true, null); // Object.class); 165 enum DataTypeDesc UNKNOWN = DataTypeDesc(705, false, null); // String.class); 166 167 mixin ValuesMemberTempate!DataTypeDesc; 168 169 static DataTypeDesc valueOf(int oid) { 170 foreach(ref DataTypeDesc d; values()) { 171 if(d.id == oid) 172 return d; 173 } 174 version(HUNT_DEBUG) { 175 import hunt.logging; 176 warningf("Postgres type OID= %d not handled - using unknown type instead", oid); 177 } 178 return UNKNOWN; 179 } 180 }