001// Licensed under the MIT license. See LICENSE file in the project root for full license information.
002
003package de.bytefish.pgbulkinsert.pgsql.constants;
004
005import java.util.HashMap;
006import java.util.Map;
007
008
009// https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.h
010public class ObjectIdentifier {
011
012    // region OID 1 - 99
013
014    // boolean, 'true'/'false'
015    public static int Boolean = 16;
016
017    // variable-length string, binary values escaped
018    public static int Bytea = 17;
019
020    // single character
021    public static int Char = 18;
022
023    // 63-byte type for storing system identifiers
024    public static int Name = 19;
025
026    // ~18 digit integer, 8-byte storage
027    public static int Int8 = 20;
028
029    // -32 thousand to 32 thousand, 2-byte storage
030    public static int Int2 = 21;
031
032    // -2 billion to 2 billion integer, 4-byte storage
033    public static int Int4 = 23;
034
035    // variable-length string, no limit specified
036    public static int Text = 25;
037
038    // object identifier(oid), maximum 4 billion
039    public static int Oid = 26;
040
041    // (block, offset), physical location of tuple
042    public static int Tid = 27;
043
044    // transaction id
045    public static int Xid = 28;
046
047    // command identifier type, sequence in transaction id
048    public static int Cid = 29;
049
050
051    // endregion
052
053    // region OID 100 - 199
054
055    // JSON
056    public static int Jsonb = 114;
057
058    // XML content
059    public static int Xml = 115;
060
061    // endregion
062
063    // region OID 600 - 699
064
065    // geometric point '(x, y)'
066    public static int Point = 600;
067
068    // geometric line segment '(pt1, pt2)'
069    public static int LineSegment = 601;
070
071    // geometric path '(pt1,...)'
072    public static int Path = 602;
073
074    // geometric box '(lower left, upper right)'
075    public static int Box = 603;
076
077    // geometric polygon '(pt1, ...)'
078    public static int Polygon = 604;
079
080    // geometric line
081    public static int Line = 628;
082
083    // endregion
084
085    // region OID 700 - 799
086
087    // single-precision floating point number, 4-byte storage
088    public static int SinglePrecision = 700;
089
090    // double-precision floating point number, 8-byte storage
091    public static int DoublePrecision = 701;
092
093    // absolute, limited-range date and time (Unix system time)
094    public static int AbsTime = 702;
095
096    // relative, limited-range time interval (Unix delta time)
097    public static int RelTime = 703;
098
099    // (abstime, abstime), time interval
100    public static int TInterval = 704;
101
102    // unknown
103    public static int Unknown = 705;
104
105    // geometric circle '(center, radius)'
106    public static int Circle =  705;
107
108    // monetary amounts, $d,ddd.cc
109    public static int Cash = 790;
110
111    // money
112    public static int Money = 791;
113
114    // endregion
115
116    // region OID 800 - 899
117
118    // XX:XX:XX:XX:XX:XX, MAC address
119    public static int MacAddress = 829;
120
121    // IP address/netmask, host address, netmask optional
122    public static int Inet = 869;
123
124    // network IP address/netmask, network address
125    public static int Cidr = 650;
126
127    // XX:XX:XX:XX:XX:XX:XX:XX, MAC address
128    public static int MacAddress8 = 774;
129
130    // endregion
131
132    // region OIDS 1000 - 1099
133
134    // char(length), blank-padded string, fixed storage length
135    public static int CharLength = 1042;
136
137    // varchar(length), non-blank-padded string, variable storage length
138    public static int VarCharLength = 1043;
139
140    // Date
141    public static int Date = 1082;
142
143    // Time Of Day
144    public static int Time = 1083;
145
146    // endregion
147
148    // region OIDS 1100 - 1199
149
150    // date and time
151    public static int Timestamp = 1114;
152
153    // date and time with time zone
154    public static int TimestampTz = 1184;
155
156    // Interval
157    public static int Interval = 1186;
158
159    // endregion
160
161    // region OIDS 1200 - 1299
162
163    // time of day with time zone
164    public static int TimeTz = 1266;
165
166    // endregion
167
168    // region OIDS 1500 - 1599
169
170    // fixed-length bit string
171    public static int Bit = 1560;
172
173    // variable-length bit string
174    public static int VarBit = 1562;
175
176    // endregion
177
178    // region OIDS 1700 - 1799
179
180    public static int Numeric = 1700;
181
182    // endregion
183
184    // region UUID
185
186    public static int Uuid = 2950;
187
188    // endregion
189
190    // region Pseudo-Types
191
192    public static int Record = 2249;
193
194    // endregion
195
196    private static Map<DataType, Integer> mapping = buildLookupTable();
197
198    private static Map<DataType, Integer> buildLookupTable() {
199
200        final Map<DataType, Integer> mapping = new HashMap<>();
201
202        mapping.put(DataType.Boolean, Boolean);
203        mapping.put(DataType.Bytea, Bytea);
204        mapping.put(DataType.Char, Char);
205        mapping.put(DataType.Name, Name);
206        mapping.put(DataType.Int8, Int8);
207        mapping.put(DataType.Int2, Int2);
208        mapping.put(DataType.Int4, Int4);
209        mapping.put(DataType.Text, Text);
210        mapping.put(DataType.Oid, Oid);
211        mapping.put(DataType.Tid, Tid);
212        mapping.put(DataType.Xid, Xid);
213        mapping.put(DataType.Cid, Cid);
214        mapping.put(DataType.Jsonb, Jsonb);
215        mapping.put(DataType.Xml, Xml);
216        mapping.put(DataType.Point, Point);
217        mapping.put(DataType.LineSegment, LineSegment);
218        mapping.put(DataType.Path, Path);
219        mapping.put(DataType.Box, Box);
220        mapping.put(DataType.Polygon, Polygon);
221        mapping.put(DataType.Line, Line);
222        mapping.put(DataType.SinglePrecision, SinglePrecision);
223        mapping.put(DataType.DoublePrecision, DoublePrecision);
224        mapping.put(DataType.AbsTime, AbsTime);
225        mapping.put(DataType.RelTime, RelTime);
226        mapping.put(DataType.TInterval, TInterval);
227        mapping.put(DataType.Unknown, Unknown);
228        mapping.put(DataType.Circle, Circle);
229        mapping.put(DataType.Cash, Cash);
230        mapping.put(DataType.Money, Money);
231        mapping.put(DataType.MacAddress, MacAddress);
232        mapping.put(DataType.Inet4, Inet);
233        mapping.put(DataType.Inet6, Inet);
234        mapping.put(DataType.Cidr, Cidr);
235        mapping.put(DataType.MacAddress8, MacAddress8);
236        mapping.put(DataType.CharLength, CharLength);
237        mapping.put(DataType.VarChar, VarCharLength);
238        mapping.put(DataType.Date, Date);
239        mapping.put(DataType.Time, Time);
240        mapping.put(DataType.Timestamp, Timestamp);
241        mapping.put(DataType.TimestampTz, TimestampTz);
242        mapping.put(DataType.Interval, Interval);
243        mapping.put(DataType.TimeTz, TimeTz);
244        mapping.put(DataType.Bit, Bit);
245        mapping.put(DataType.VarBit, VarBit);
246        mapping.put(DataType.Numeric, Numeric);
247        mapping.put(DataType.Uuid, Uuid);
248        mapping.put(DataType.Record, Record);
249
250        return mapping;
251    }
252
253    public static int mapFrom(DataType type) {
254        if(mapping.containsKey(type)) {
255            return mapping.get(type);
256        }
257        return Unknown;
258    }
259}