|
1 |
| -import 'dart:convert'; |
2 | 1 | import 'dart:core';
|
3 | 2 | import 'dart:core' as core;
|
4 | 3 | import 'dart:typed_data';
|
@@ -139,206 +138,156 @@ class Point {
|
139 | 138 | /// Supported data types.
|
140 | 139 | abstract class Type<T extends Object> {
|
141 | 140 | /// Used to represent value without any type representation.
|
142 |
| - static const unspecified = GenericType<Object>(null); |
| 141 | + static final unspecified = unspecifiedType(); |
143 | 142 |
|
144 | 143 | /// Must be a [String].
|
145 |
| - static const text = |
146 |
| - GenericType<String>(TypeOid.text, nameForSubstitution: 'text'); |
| 144 | + static final text = |
| 145 | + _genericType<String>(TypeOid.text, nameForSubstitution: 'text'); |
147 | 146 |
|
148 | 147 | /// Must be an [int] (4-byte integer)
|
149 |
| - static const integer = |
150 |
| - GenericType<int>(TypeOid.integer, nameForSubstitution: 'int4'); |
| 148 | + static final integer = |
| 149 | + _genericType<int>(TypeOid.integer, nameForSubstitution: 'int4'); |
151 | 150 |
|
152 | 151 | /// Must be an [int] (2-byte integer)
|
153 |
| - static const smallInteger = |
154 |
| - GenericType<int>(TypeOid.smallInteger, nameForSubstitution: 'int2'); |
| 152 | + static final smallInteger = |
| 153 | + _genericType<int>(TypeOid.smallInteger, nameForSubstitution: 'int2'); |
155 | 154 |
|
156 | 155 | /// Must be an [int] (8-byte integer)
|
157 |
| - static const bigInteger = |
158 |
| - GenericType<int>(TypeOid.bigInteger, nameForSubstitution: 'int8'); |
| 156 | + static final bigInteger = |
| 157 | + _genericType<int>(TypeOid.bigInteger, nameForSubstitution: 'int8'); |
159 | 158 |
|
160 | 159 | /// Must be an [int] (autoincrementing 4-byte integer)
|
161 |
| - static const serial = GenericType<int>(null, nameForSubstitution: 'int4'); |
| 160 | + static final serial = _genericType<int>(null, nameForSubstitution: 'int4'); |
162 | 161 |
|
163 | 162 | /// Must be an [int] (autoincrementing 8-byte integer)
|
164 |
| - static const bigSerial = GenericType<int>(null, nameForSubstitution: 'int8'); |
| 163 | + static final bigSerial = _genericType<int>(null, nameForSubstitution: 'int8'); |
165 | 164 |
|
166 | 165 | /// Must be a [double] (32-bit floating point value)
|
167 |
| - static const real = |
168 |
| - GenericType<core.double>(TypeOid.real, nameForSubstitution: 'float4'); |
| 166 | + static final real = |
| 167 | + _genericType<core.double>(TypeOid.real, nameForSubstitution: 'float4'); |
169 | 168 |
|
170 | 169 | /// Must be a [double] (64-bit floating point value)
|
171 |
| - static const double = |
172 |
| - GenericType<core.double>(TypeOid.double, nameForSubstitution: 'float8'); |
| 170 | + static final double = |
| 171 | + _genericType<core.double>(TypeOid.double, nameForSubstitution: 'float8'); |
173 | 172 |
|
174 | 173 | /// Must be a [bool]
|
175 |
| - static const boolean = |
176 |
| - GenericType<bool>(TypeOid.boolean, nameForSubstitution: 'boolean'); |
| 174 | + static final boolean = |
| 175 | + _genericType<bool>(TypeOid.boolean, nameForSubstitution: 'boolean'); |
177 | 176 |
|
178 | 177 | /// Must be a [DateTime] (microsecond date and time precision)
|
179 |
| - static const timestampWithoutTimezone = GenericType<DateTime>( |
| 178 | + static final timestampWithoutTimezone = _genericType<DateTime>( |
180 | 179 | TypeOid.timestampWithoutTimezone,
|
181 | 180 | nameForSubstitution: 'timestamp');
|
182 | 181 |
|
183 | 182 | /// Must be a [DateTime] (microsecond date and time precision)
|
184 |
| - static const timestampWithTimezone = GenericType<DateTime>( |
| 183 | + static final timestampWithTimezone = _genericType<DateTime>( |
185 | 184 | TypeOid.timestampWithTimezone,
|
186 | 185 | nameForSubstitution: 'timestamptz');
|
187 | 186 |
|
188 | 187 | /// Must be a [Interval]
|
189 |
| - static const interval = |
190 |
| - GenericType<Interval>(TypeOid.interval, nameForSubstitution: 'interval'); |
| 188 | + static final interval = |
| 189 | + _genericType<Interval>(TypeOid.interval, nameForSubstitution: 'interval'); |
191 | 190 |
|
192 | 191 | /// An arbitrary-precision number.
|
193 | 192 | ///
|
194 | 193 | /// This library supports encoding numbers in a textual format, or when
|
195 | 194 | /// passed as [int] or [double]. When decoding values, numeric types are
|
196 | 195 | /// always returned as string.
|
197 |
| - static const numeric = |
198 |
| - GenericType<Object>(TypeOid.numeric, nameForSubstitution: 'numeric'); |
| 196 | + static final numeric = |
| 197 | + _genericType<Object>(TypeOid.numeric, nameForSubstitution: 'numeric'); |
199 | 198 |
|
200 | 199 | /// Must be a [DateTime] (contains year, month and day only)
|
201 |
| - static const date = |
202 |
| - GenericType<DateTime>(TypeOid.date, nameForSubstitution: 'date'); |
| 200 | + static final date = |
| 201 | + _genericType<DateTime>(TypeOid.date, nameForSubstitution: 'date'); |
203 | 202 |
|
204 | 203 | /// Must be encodable via [json.encode].
|
205 | 204 | ///
|
206 | 205 | /// Values will be encoded via [json.encode] before being sent to the database.
|
207 |
| - static const jsonb = GenericType(TypeOid.jsonb, nameForSubstitution: 'jsonb'); |
| 206 | + static final jsonb = GenericType(TypeOid.jsonb, nameForSubstitution: 'jsonb'); |
208 | 207 |
|
209 | 208 | /// Must be encodable via [core.json.encode].
|
210 | 209 | ///
|
211 | 210 | /// Values will be encoded via [core.json.encode] before being sent to the database.
|
212 |
| - static const json = GenericType(TypeOid.json, nameForSubstitution: 'json'); |
| 211 | + static final json = GenericType(TypeOid.json, nameForSubstitution: 'json'); |
213 | 212 |
|
214 | 213 | /// Must be a [List] of [int].
|
215 | 214 | ///
|
216 | 215 | /// Each element of the list must fit into a byte (0-255).
|
217 |
| - static const byteArray = |
218 |
| - GenericType<List<int>>(TypeOid.byteArray, nameForSubstitution: 'bytea'); |
| 216 | + static final byteArray = |
| 217 | + _genericType<List<int>>(TypeOid.byteArray, nameForSubstitution: 'bytea'); |
219 | 218 |
|
220 | 219 | /// Must be a [String]
|
221 | 220 | ///
|
222 | 221 | /// Used for internal pg structure names
|
223 |
| - static const name = |
224 |
| - GenericType<String>(TypeOid.name, nameForSubstitution: 'name'); |
| 222 | + static final name = |
| 223 | + _genericType<String>(TypeOid.name, nameForSubstitution: 'name'); |
225 | 224 |
|
226 | 225 | /// Must be a [String].
|
227 | 226 | ///
|
228 | 227 | /// Must contain 32 hexadecimal characters. May contain any number of '-' characters.
|
229 | 228 | /// When returned from database, format will be xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
|
230 |
| - static const uuid = |
231 |
| - GenericType<String>(TypeOid.uuid, nameForSubstitution: 'uuid'); |
| 229 | + static final uuid = |
| 230 | + _genericType<String>(TypeOid.uuid, nameForSubstitution: 'uuid'); |
232 | 231 |
|
233 | 232 | /// Must be a [Point]
|
234 |
| - static const point = |
235 |
| - GenericType<Point>(TypeOid.point, nameForSubstitution: 'point'); |
| 233 | + static final point = |
| 234 | + _genericType<Point>(TypeOid.point, nameForSubstitution: 'point'); |
236 | 235 |
|
237 | 236 | /// Must be a [List<bool>]
|
238 |
| - static const booleanArray = GenericType<List<bool>>(TypeOid.booleanArray, |
| 237 | + static final booleanArray = _genericType<List<bool>>(TypeOid.booleanArray, |
239 | 238 | nameForSubstitution: '_bool');
|
240 | 239 |
|
241 | 240 | /// Must be a [List<int>]
|
242 |
| - static const integerArray = GenericType<List<int>>(TypeOid.integerArray, |
| 241 | + static final integerArray = _genericType<List<int>>(TypeOid.integerArray, |
243 | 242 | nameForSubstitution: '_int4');
|
244 | 243 |
|
245 | 244 | /// Must be a [List<int>]
|
246 |
| - static const bigIntegerArray = GenericType<List<int>>(TypeOid.bigIntegerArray, |
| 245 | + static final bigIntegerArray = _genericType<List<int>>( |
| 246 | + TypeOid.bigIntegerArray, |
247 | 247 | nameForSubstitution: '_int8');
|
248 | 248 |
|
249 | 249 | /// Must be a [List<String>]
|
250 |
| - static const textArray = GenericType<List<String>>(TypeOid.textArray, |
| 250 | + static final textArray = _genericType<List<String>>(TypeOid.textArray, |
251 | 251 | nameForSubstitution: '_text');
|
252 | 252 |
|
253 | 253 | /// Must be a [List<double>]
|
254 |
| - static const doubleArray = GenericType<List<core.double>>(TypeOid.doubleArray, |
| 254 | + static final doubleArray = _genericType<List<core.double>>( |
| 255 | + TypeOid.doubleArray, |
255 | 256 | nameForSubstitution: '_float8');
|
256 | 257 |
|
257 | 258 | /// Must be a [String]
|
258 |
| - static const varChar = |
259 |
| - GenericType<String>(TypeOid.varChar, nameForSubstitution: 'varchar'); |
| 259 | + static final varChar = |
| 260 | + _genericType<String>(TypeOid.varChar, nameForSubstitution: 'varchar'); |
260 | 261 |
|
261 | 262 | /// Must be a [List<String>]
|
262 |
| - static const varCharArray = GenericType<List<String>>(TypeOid.varCharArray, |
| 263 | + static final varCharArray = _genericType<List<String>>(TypeOid.varCharArray, |
263 | 264 | nameForSubstitution: '_varchar');
|
264 | 265 |
|
265 | 266 | /// Must be a [List] of encodable objects
|
266 |
| - static const jsonbArray = |
267 |
| - GenericType<List>(TypeOid.jsonbArray, nameForSubstitution: '_jsonb'); |
| 267 | + static final jsonbArray = |
| 268 | + _genericType<List>(TypeOid.jsonbArray, nameForSubstitution: '_jsonb'); |
268 | 269 |
|
269 | 270 | /// Must be a [Type].
|
270 |
| - static const regtype = |
271 |
| - GenericType<Type>(TypeOid.regtype, nameForSubstitution: 'regtype'); |
| 271 | + static final regtype = |
| 272 | + _genericType<Type>(TypeOid.regtype, nameForSubstitution: 'regtype'); |
272 | 273 |
|
273 | 274 | /// Impossible to bind to, always null when read.
|
274 |
| - static const voidType = GenericType<Object>(TypeOid.voidType); |
| 275 | + static final voidType = _genericType<Object>(TypeOid.voidType); |
275 | 276 |
|
276 | 277 | /// The object ID of this data type.
|
277 | 278 | final int? oid;
|
278 | 279 |
|
279 |
| - /// The name of this type as considered by [Sql.named]. |
280 |
| - /// |
281 |
| - /// To declare an explicit type for a substituted parameter in a query, this |
282 |
| - /// name can be used. |
283 |
| - final String? nameForSubstitution; |
284 |
| - |
285 |
| - const Type( |
286 |
| - this.oid, { |
287 |
| - this.nameForSubstitution, |
288 |
| - }); |
289 |
| - |
290 |
| - bool get hasOid => oid != null && oid! > 0; |
| 280 | + const Type(this.oid); |
291 | 281 |
|
292 | 282 | TypedValue<T> value(T value) => TypedValue<T>(this, value);
|
293 | 283 |
|
294 |
| - EncodeOutput encode(EncodeInput<T> input); |
295 |
| - |
296 |
| - T? decode(DecodeInput input); |
297 |
| - |
298 | 284 | @override
|
299 |
| - String toString() => '$runtimeType(oid:$oid)'; |
| 285 | + String toString() => 'Type(oid:$oid)'; |
300 | 286 | }
|
301 | 287 |
|
302 |
| -class EncodeInput<T extends Object> { |
303 |
| - final T value; |
304 |
| - final Encoding encoding; |
305 |
| - |
306 |
| - EncodeInput({ |
307 |
| - required this.value, |
308 |
| - required this.encoding, |
309 |
| - }); |
310 |
| -} |
311 |
| - |
312 |
| -class EncodeOutput { |
313 |
| - final Uint8List? bytes; |
314 |
| - final String? text; |
315 |
| - |
316 |
| - EncodeOutput.bytes(Uint8List value) |
317 |
| - : bytes = value, |
318 |
| - text = null; |
319 |
| - |
320 |
| - EncodeOutput.text(String value) |
321 |
| - : bytes = null, |
322 |
| - text = value; |
323 |
| - |
324 |
| - bool get isBinary => bytes != null; |
325 |
| -} |
326 |
| - |
327 |
| -class DecodeInput { |
328 |
| - final Uint8List bytes; |
329 |
| - final bool isBinary; |
330 |
| - final Encoding encoding; |
331 |
| - final TypeRegistry typeRegistry; |
332 |
| - |
333 |
| - DecodeInput({ |
334 |
| - required this.bytes, |
335 |
| - required this.isBinary, |
336 |
| - required this.encoding, |
337 |
| - required this.typeRegistry, |
338 |
| - }); |
339 |
| - |
340 |
| - late final asText = encoding.decode(bytes); |
341 |
| -} |
| 288 | +Type<T> _genericType<T extends Object>(int? typeOid, |
| 289 | + {String? nameForSubstitution}) => |
| 290 | + GenericType<T>(typeOid, nameForSubstitution: nameForSubstitution); |
342 | 291 |
|
343 | 292 | class TypedValue<T extends Object> {
|
344 | 293 | final Type<T> type;
|
|
0 commit comments