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.RowSet;
19 
20 import hunt.database.base.AsyncResult;
21 import hunt.database.base.Row;
22 import hunt.database.base.RowIterator;
23 import hunt.database.base.SqlResult;
24 
25 import hunt.util.Common;
26 
27 alias RowSetHandler = AsyncResultHandler!RowSet;
28 alias RowSetAsyncResult = AsyncResult!RowSet;
29 
30 import std.variant;
31 
32 /**
33  * A set of rows.
34  */
35 interface RowSet : Iterable!(Row), SqlResult!(RowSet) {
36 
37     RowIterator iterator();
38 
39     // override RowSet next();
40 
41     alias getAs = bind;
42 
43     final T[] bind(T, alias getColumnNameFun="b")() if(is(T == struct)) {
44         T[] r = new T[this.rowCount()];
45         size_t index = 0;
46         foreach(Row row; this) {
47             r[index] = row.bind!(T, getColumnNameFun)();
48             index++;
49         }
50 
51         return r;
52     }
53 
54     final T[] bind(T, bool traverseBase=true, alias getColumnNameFun="b")() if(is(T == class)) {
55         T[] r = new T[this.rowCount()];
56         size_t index = 0;
57         foreach(Row row; this) {
58             r[index] = row.bind!(T, traverseBase, getColumnNameFun)();
59             index++;
60         }
61 
62         return r;
63     }
64 
65     Row firstRow();
66     
67     Row lastRow();
68     
69     final T columnInLastRow(T = int)(string name) {
70         T r = T.init;
71         foreach(Row row; this) {
72             Variant v = row.getValue(name);
73             r = v.get!T();
74         }
75 
76         return r;
77     }
78 }