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.base.Transaction;
18 
19 import hunt.database.base.AsyncResult;
20 import hunt.database.base.Common;
21 import hunt.database.base.PreparedQuery;
22 import hunt.database.base.RowSet;
23 import hunt.database.base.SqlClient;
24 
25 import hunt.collection.List;
26 
27 alias AsyncTransactionHandler = AsyncResultHandler!Transaction;
28 alias TransactionAsyncResult = AsyncResult!Transaction;
29 
30 /**
31  * A transaction that allows to control the transaction and receive events.
32  */
33 interface Transaction : SqlClient {
34 
35     /**
36      * Create a prepared query.
37      *
38      * @param sql the sql
39      * @param handler the handler notified with the prepared query asynchronously
40      */
41     Transaction prepare(string sql, PreparedQueryHandler handler);
42 
43     /**
44      * Commit the current transaction.
45      */
46     void commit();
47 
48     /**
49      * Like {@link #commit} with an handler to be notified when the transaction commit has completed
50      */
51     void commit(AsyncVoidHandler handler);
52 
53     /**
54      * Rollback the current transaction.
55      */
56     void rollback();
57 
58     /**
59      * Like {@link #rollback} with an handler to be notified when the transaction rollback has completed
60      */
61     void rollback(AsyncVoidHandler handler);
62 
63     /**
64      * Set an handler to be called when the transaction is aborted.
65      *
66      * @param handler the handler
67      */
68     Transaction abortHandler(AsyncVoidHandler handler);
69 
70     Transaction query(string sql, RowSetHandler handler);
71 
72     // override
73     // <R> Transaction query(string sql, Collector<Row, ?, R> collector, Handler!(AsyncResult!(SqlResult!(R))) handler);
74 
75     // override
76     // Transaction preparedQuery(string sql, RowSetHandler handler);
77 
78     // override
79     // <R> Transaction preparedQuery(string sql, Collector<Row, ?, R> collector, Handler!(AsyncResult!(SqlResult!(R))) handler);
80 
81     // override
82     // Transaction preparedQuery(string sql, Tuple arguments, RowSetHandler handler);
83 
84     // override
85     // <R> Transaction preparedQuery(string sql, Tuple arguments, Collector<Row, ?, R> collector, Handler!(AsyncResult!(SqlResult!(R))) handler);
86 
87     // override
88     // Transaction preparedBatch(string sql, List!(Tuple) batch, RowSetHandler handler);
89 
90     // override
91     // <R> Transaction preparedBatch(string sql, List!(Tuple) batch, Collector<Row, ?, R> collector, Handler!(AsyncResult!(SqlResult!(R))) handler);
92 
93     /**
94      * Rollback the transaction and release the associated resources.
95      */
96     void close();
97 
98     int status();
99 }