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 
18 module hunt.database.base.Tuple;
19 
20 import hunt.database.base.impl.ArrayTuple;
21 // import io.vertx.codegen.annotations.Fluent;
22 // import io.vertx.codegen.annotations.GenIgnore;
23 // import io.vertx.codegen.annotations.VertxGen;
24 // import io.vertx.core.buffer.Buffer;
25 
26 import hunt.math.BigDecimal;
27 // import java.time.*;
28 // import java.time.temporal.Temporal;
29 // import java.util.UUID;
30 
31 import std.variant;
32 
33 import std.typecons;
34 
35 /**
36  * A general purpose tuple.
37  */
38 interface Tuple {
39 
40     /**
41      * The JSON null literal value.
42      * <br/>
43      * It is used to distinguish a JSON null literal value from the Java {@code null} value. This is only
44      * used when the database supports JSON types.
45      */
46     // Object JSON_NULL = new Object();
47 
48     /**
49      * @return a new empty tuple
50      */
51     static Tuple tuple() {
52         return new ArrayTuple(10);
53     }
54 
55     /**
56      * Create a tuple of an arbitrary number of elements.
57      *
58      * @param elts the elements
59      * @return the tuple
60      */
61     static Tuple of(Args...)(Args elts) {
62         ArrayTuple tuple = new ArrayTuple(cast(int)Args.length);
63         foreach (elt; elts) {
64             static if(is(T == Variant)) {
65                 tuple.addValue(elt);
66             } else {
67                 Variant v = Variant(elt);
68                 tuple.addValue(v);
69             }
70         }
71         return tuple;
72     }    
73 
74     /**
75      * Get an object value at {@code pos}.
76      *
77      * @param pos the position
78      * @return the value or {@code null}
79      */
80     Variant getValue(int pos);
81 
82     /// ditto
83     alias opIndex = getValue;
84 
85     /**
86      * Get a boolean value at {@code pos}.
87      *
88      * @param pos the position
89      * @return the value or {@code null}
90      */
91     bool getBoolean(int pos);
92 
93     /**
94      * Get a short value at {@code pos}.
95      *
96      * @param pos the position
97      * @return the value or {@code null}
98      */
99     short getShort(int pos);
100 
101     /**
102      * Get an integer value at {@code pos}.
103      *
104      * @param pos the position
105      * @return the value or {@code null}
106      */
107     int getInteger(int pos);
108 
109     /**
110      * Get a long value at {@code pos}.
111      *
112      * @param pos the position
113      * @return the value or {@code null}
114      */
115     long getLong(int pos);
116 
117     /**
118      * Get a float value at {@code pos}.
119      *
120      * @param pos the position
121      * @return the value or {@code null}
122      */
123     float getFloat(int pos);
124 
125     /**
126      * Get a double value at {@code pos}.
127      *
128      * @param pos the position
129      * @return the value or {@code null}
130      */
131     double getDouble(int pos);
132 
133     /**
134      * Get a string value at {@code pos}.
135      *
136      * @param pos the position
137      * @return the value or {@code null}
138      */
139     string getString(int pos);
140 
141     // /**
142     //  * Get a {@link java.time.temporal.Temporal} value at {@code pos}.
143     //  *
144     //  * @param pos the position
145     //  * @return the value or {@code null}
146     //  */
147     // Temporal getTemporal(int pos);
148 
149     // /**
150     //  * Get {@link java.time.LocalDate} value at {@code pos}.
151     //  *
152     //  * @param pos the position
153     //  * @return the value or {@code null}
154     //  */
155     // LocalDate getLocalDate(int pos);
156 
157     // /**
158     //  * Get {@link java.time.LocalTime} value at {@code pos}.
159     //  *
160     //  * @param pos the position
161     //  * @return the value or {@code null}
162     //  */
163     // LocalTime getLocalTime(int pos);
164 
165     // /**
166     //  * Get {@link java.time.LocalDateTime} value at {@code pos}.
167     //  *
168     //  * @param pos the position
169     //  * @return the value or {@code null}
170     //  */
171     // LocalDateTime getLocalDateTime(int pos);
172 
173     // /**
174     //  * Get {@link java.time.OffsetTime} value at {@code pos}.
175     //  *
176     //  * @param pos the position
177     //  * @return the value or {@code null}
178     //  */
179     // OffsetTime getOffsetTime(int pos);
180 
181     // /**
182     //  * Get {@link java.time.OffsetDateTime} value at {@code pos}.
183     //  *
184     //  * @param pos the position
185     //  * @return the value or {@code null}
186     //  */
187     // OffsetDateTime getOffsetDateTime(int pos);
188 
189     // /**
190     //  * Get {@link java.util.UUID} value at {@code pos}.
191     //  *
192     //  * @param pos the position
193     //  * @return the value or {@code null}
194     //  */
195     // UUID getUUID(int pos);
196 
197     // /**
198     //  * Get {@link BigDecimal} value at {@code pos}.
199     //  *
200     //  * @param pos the position
201     //  * @return the value or {@code null}
202     //  */
203     // BigDecimal getBigDecimal(int pos);
204 
205     // /**
206     //  * Get an array of {@link Integer} value at {@code pos}.
207     //  *
208     //  * @param pos the position
209     //  * @return the value or {@code null}
210     //  */
211     // Integer[] getIntegerArray(int pos);
212 
213     // /**
214     //  * Get an array of {@link Boolean} value at {@code pos}.
215     //  *
216     //  * @param pos the position
217     //  * @return the value or {@code null}
218     //  */
219     // Boolean[] getBooleanArray(int pos);
220 
221     // /**
222     //  * Get an array of  {@link Short} value at {@code pos}.
223     //  *
224     //  * @param pos the position
225     //  * @return the value or {@code null}
226     //  */
227     // Short[] getShortArray(int pos);
228 
229     // /**
230     //  * Get an array of {@link Long} value at {@code pos}.
231     //  *
232     //  * @param pos the position
233     //  * @return the value or {@code null}
234     //  */
235     // Long[] getLongArray(int pos);
236 
237     // /**
238     //  * Get an array of  {@link Float} value at {@code pos}.
239     //  *
240     //  * @param pos the position
241     //  * @return the value or {@code null}
242     //  */
243     // Float[] getFloatArray(int pos);
244 
245     // /**
246     //  * Get an array of  {@link Double} value at {@code pos}.
247     //  *
248     //  * @param pos the position
249     //  * @return the value or {@code null}
250     //  */
251     // Double[] getDoubleArray(int pos);
252 
253     // /**
254     //  * Get an array of  {@link String} value at {@code pos}.
255     //  *
256     //  * @param pos the position
257     //  * @return the value or {@code null}
258     //  */
259     // String[] getStringArray(int pos);
260 
261     // /**
262     //  * Get an array of  {@link LocalDate} value at {@code pos}.
263     //  *
264     //  * @param pos the position
265     //  * @return the value or {@code null}
266     //  */
267     // LocalDate[] getLocalDateArray(int pos);
268 
269     // /**
270     //  * Get an array of  {@link LocalTime} value at {@code pos}.
271     //  *
272     //  * @param pos the position
273     //  * @return the value or {@code null}
274     //  */
275     // LocalTime[] getLocalTimeArray(int pos);
276 
277     // /**
278     //  * Get an array of  {@link OffsetTime} value at {@code pos}.
279     //  *
280     //  * @param pos the position
281     //  * @return the value or {@code null}
282     //  */
283     // OffsetTime[] getOffsetTimeArray(int pos);
284 
285     // /**
286     //  * Get an array of  {@link LocalDateTime} value at {@code pos}.
287     //  *
288     //  * @param pos the position
289     //  * @return the value or {@code null}
290     //  */
291     // LocalDateTime[] getLocalDateTimeArray(int pos);
292 
293     // /**
294     //  * Get an array of  {@link OffsetDateTime} value at {@code pos}.
295     //  *
296     //  * @param pos the position
297     //  * @return the value or {@code null}
298     //  */
299     // OffsetDateTime[] getOffsetDateTimeArray(int pos);
300 
301     // /**
302     //  * Get an array of  {@link Buffer} value at {@code pos}.
303     //  *
304     //  * @param pos the position
305     //  * @return the value or {@code null}
306     //  */
307     // Buffer[] getBufferArray(int pos);
308 
309     // /**
310     //  * Get an array of {@link UUID} value at {@code pos}.
311     //  *
312     //  * @param pos the column
313     //  * @return the value or {@code null}
314     //  */
315     // UUID[] getUUIDArray(int pos);
316 
317     /**
318      * Get a buffer value at {@code pos}.
319      *
320      * @param pos the position
321      * @return the value or {@code null}
322      */
323     byte[] getBuffer(int pos);
324 
325     // /**
326     //  * Add a boolean value at the end of the tuple.
327     //  *
328     //  * @param value the value
329     //  * @return a reference to this, so the API can be used fluently
330     //  */
331     // Tuple addBoolean(Boolean value);
332 
333     /**
334      * Add an object value at the end of the tuple.
335      *
336      * @param value the value
337      * @return a reference to this, so the API can be used fluently
338      */
339     Tuple addValue(ref Variant value);
340 
341     // /**
342     //  * Add a short value at the end of the tuple.
343     //  *
344     //  * @param value the value
345     //  * @return a reference to this, so the API can be used fluently
346     //  */
347     // Tuple addShort(Short value);
348 
349     // /**
350     //  * Add an integer value at the end of the tuple.
351     //  *
352     //  * @param value the value
353     //  * @return a reference to this, so the API can be used fluently
354     //  */
355     // Tuple addInteger(Integer value);
356 
357     // /**
358     //  * Add a long value at the end of the tuple.
359     //  *
360     //  * @param value the value
361     //  * @return a reference to this, so the API can be used fluently
362     //  */
363     // Tuple addLong(Long value);
364 
365     // /**
366     //  * Add a float value at the end of the tuple.
367     //  *
368     //  * @param value the value
369     //  * @return a reference to this, so the API can be used fluently
370     //  */
371     // Tuple addFloat(Float value);
372 
373     // /**
374     //  * Add a double value at the end of the tuple.
375     //  *
376     //  * @param value the value
377     //  * @return a reference to this, so the API can be used fluently
378     //  */
379     // Tuple addDouble(Double value);
380 
381     // /**
382     //  * Add a string value at the end of the tuple.
383     //  *
384     //  * @param value the value
385     //  * @return a reference to this, so the API can be used fluently
386     //  */
387     // Tuple addString(String value);
388 
389     // /**
390     //  * Add a buffer value at the end of the tuple.
391     //  *
392     //  * @param value the value
393     //  * @return a reference to this, so the API can be used fluently
394     //  */
395     // Tuple addBuffer(Buffer value);
396 
397     // /**
398     //  * Add a {@link java.time.temporal.Temporal} value at the end of the tuple.
399     //  *
400     //  * @param value the value
401     //  * @return a reference to this, so the API can be used fluently
402     //  */
403     // Tuple addTemporal(Temporal value);
404 
405     // /**
406     //  * Add a {@link java.time.LocalDate} value at the end of the tuple.
407     //  *
408     //  * @param value the value
409     //  * @return a reference to this, so the API can be used fluently
410     //  */
411     // Tuple addLocalDate(LocalDate value);
412 
413     // /**
414     //  * Add a {@link java.time.LocalTime} value at the end of the tuple.
415     //  *
416     //  * @param value the value
417     //  * @return a reference to this, so the API can be used fluently
418     //  */
419     // Tuple addLocalTime(LocalTime value);
420 
421     // /**
422     //  * Add a {@link java.time.LocalDateTime} value at the end of the tuple.
423     //  *
424     //  * @param value the value
425     //  * @return a reference to this, so the API can be used fluently
426     //  */
427     // Tuple addLocalDateTime(LocalDateTime value);
428 
429     // /**
430     //  * Add a {@link java.time.OffsetTime} value at the end of the tuple.
431     //  *
432     //  * @param value the value
433     //  * @return a reference to this, so the API can be used fluently
434     //  */
435     // Tuple addOffsetTime(OffsetTime value);
436 
437     // /**
438     //  * Add a {@link java.time.OffsetDateTime} value at the end of the tuple.
439     //  *
440     //  * @param value the value
441     //  * @return a reference to this, so the API can be used fluently
442     //  */
443     // Tuple addOffsetDateTime(OffsetDateTime value);
444 
445     // /**
446     //  * Add a {@link java.util.UUID} value at the end of the tuple.
447     //  *
448     //  * @param value the value
449     //  * @return a reference to this, so the API can be used fluently
450     //  */
451     // Tuple addUUID(UUID value);
452 
453     // /**
454     //  * Add a {@link BigDecimal} value at the end of the tuple.
455     //  *
456     //  * @param value the value
457     //  * @return a reference to this, so the API can be used fluently
458     //  */
459     // Tuple addBigDecimal(BigDecimal value);
460 
461     // /**
462     //  * Add an array of {@code Integer} value at the end of the tuple.
463     //  *
464     //  * @param value the value
465     //  * @return a reference to this, so the API can be used fluently
466     //  */
467     // Tuple addIntegerArray(Integer[] value);
468 
469     // /**
470     //  * Add an array of {@code Boolean} value at the end of the tuple.
471     //  *
472     //  * @param value the value
473     //  * @return a reference to this, so the API can be used fluently
474     //  */
475     // Tuple addBooleanArray(Boolean[] value);
476 
477     // /**
478     //  * Add an array of {@link Short} value at the end of the tuple.
479     //  *
480     //  * @param value the value
481     //  * @return a reference to this, so the API can be used fluently
482     //  */
483     // Tuple addShortArray(Short[] value);
484 
485     // /**
486     //  * Add an array of {@link Long} value at the end of the tuple.
487     //  *
488     //  * @param value the value
489     //  * @return a reference to this, so the API can be used fluently
490     //  */
491     // Tuple addLongArray(Long[] value);
492 
493     // /**
494     //  * Add an array of {@link Float} value at the end of the tuple.
495     //  *
496     //  * @param value the value
497     //  * @return a reference to this, so the API can be used fluently
498     //  */
499     // Tuple addFloatArray(Float[] value);
500 
501     // /**
502     //  * Add an array of {@link Double} value at the end of the tuple.
503     //  *
504     //  * @param value the value
505     //  * @return a reference to this, so the API can be used fluently
506     //  */
507     // Tuple addDoubleArray(Double[] value);
508 
509     // /**
510     //  * Add an array of {@link String} value at the end of the tuple.
511     //  *
512     //  * @param value the value
513     //  * @return a reference to this, so the API can be used fluently
514     //  */
515     // Tuple addStringArray(String[] value);
516 
517     // /**
518     //  * Add an array of {@link LocalDate} value at the end of the tuple.
519     //  *
520     //  * @param value the value
521     //  * @return a reference to this, so the API can be used fluently
522     //  */
523     // Tuple addLocalDateArray(LocalDate[] value);
524 
525     // /**
526     //  * Add an array of {@link LocalTime} value at the end of the tuple.
527     //  *
528     //  * @param value the value
529     //  * @return a reference to this, so the API can be used fluently
530     //  */
531     // Tuple addLocalTimeArray(LocalTime[] value);
532 
533     // /**
534     //  * Add an array of {@link OffsetTime} value at the end of the tuple.
535     //  *
536     //  * @param value the value
537     //  * @return a reference to this, so the API can be used fluently
538     //  */
539     // Tuple addOffsetTimeArray(OffsetTime[] value);
540 
541     // /**
542     //  * Add an array of {@link LocalDateTime} value at the end of the tuple.
543     //  *
544     //  * @param value the value
545     //  * @return a reference to this, so the API can be used fluently
546     //  */
547     // Tuple addLocalDateTimeArray(LocalDateTime[] value);
548 
549     // /**
550     //  * Add an array of {@link OffsetDateTime} value at the end of the tuple.
551     //  *
552     //  * @param value the value
553     //  * @return a reference to this, so the API can be used fluently
554     //  */
555     // Tuple addOffsetDateTimeArray(OffsetDateTime[] value);
556 
557     // /**
558     //  * Add an array of {@link Buffer} value at the end of the tuple.
559     //  *
560     //  * @param value the value
561     //  * @return a reference to this, so the API can be used fluently
562     //  */
563     // Tuple addBufferArray(Buffer[] value);
564 
565     // /**
566     //  * Add an array of {@link UUID} value at the end of the tuple.
567     //  *
568     //  * @param value the value
569     //  * @return a reference to this, so the API can be used fluently
570     //  */
571     // Tuple addUUIDArray(UUID[] value);
572 
573     // <T> T get(Class!(T) type, int pos);
574 
575     // <T> T[] getValues(Class!(T) type, int pos);
576 
577     // <T> Tuple addValues(T[] value);
578 
579     /**
580      * @return the tuple size
581      */
582     int size();
583 
584     void clear();
585 
586 }