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.impl.SqlConnectionBase;
19 
20 import hunt.database.base.impl.Connection;
21 import hunt.database.base.impl.NamedQueryDesc;
22 import hunt.database.base.impl.NamedQueryImpl;
23 import hunt.database.base.impl.PreparedQueryImpl;
24 import hunt.database.base.impl.PreparedStatement;
25 import hunt.database.base.impl.SqlClientBase;
26 import hunt.database.base.impl.command.CommandResponse;
27 import hunt.database.base.impl.command.PrepareStatementCommand;
28 
29 import hunt.database.base.AsyncResult;
30 import hunt.database.base.PreparedQuery;
31 
32 import hunt.concurrency.Future;
33 import hunt.concurrency.FuturePromise;
34 import hunt.Exceptions;
35 import hunt.logging;
36 import hunt.net.AbstractConnection;
37 
38 /**
39  * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
40  */
41 abstract class SqlConnectionBase(C) : SqlClientBase!(C) { 
42     // if(is(C : SqlConnectionBase!(C))) 
43 
44     protected DbConnection conn;
45 
46     protected this(DbConnection conn) {
47         this.conn = conn;
48     }
49 
50     C prepare(string sql, PreparedQueryHandler handler) {
51         version(HUNT_DB_DEBUG) trace(sql);
52         scheduleThen!(PreparedStatement)(new PrepareStatementCommand(sql), 
53             (CommandResponse!PreparedStatement cr) {
54                 if(handler !is null) {
55                     if (cr.succeeded()) {
56                         handler(succeededResult!(PreparedQuery)(new PreparedQueryImpl(conn, cr.result())));
57                     } else {
58                         handler(failedResult!(PreparedQuery)(cr.cause()));
59                     }
60                 }
61             }
62         );
63         return cast(C) this;
64     }
65 
66     Future!PreparedQuery prepareAsync(string sql) {
67         version(HUNT_DB_DEBUG) trace(sql);
68         auto f = new FuturePromise!PreparedQuery();
69 
70         scheduleThen!(PreparedStatement)(new PrepareStatementCommand(sql), 
71             (CommandResponse!PreparedStatement ar) {
72                 if (ar.succeeded()) {
73                     f.succeeded(new PreparedQueryImpl(conn, ar.result()));
74                 } else {
75                     f.failed(ar.cause()); 
76                 }
77             }
78         );
79         
80         return f;
81     }
82 
83     PreparedQuery prepare(string sql) {
84         auto f = prepareAsync(sql);
85         version(HUNT_DB_DEBUG) warning("try to get a prepare result");
86         return f.get(awaittingTimeout);
87     }
88 
89     // protected AbstractNamedQueryDesc getNamedQueryDesc(string sql) {
90     //     throw new NotImplementedException("getNamedQueryDesc");
91     // }
92 
93     // Future!NamedQuery prepareNamedQueryAsync(string sql) {
94     //     version(HUNT_DB_DEBUG) trace(sql);
95     //     auto f = new FuturePromise!NamedQuery();
96     //     AbstractNamedQueryDesc queryDesc = getNamedQueryDesc(sql);
97 
98     //     scheduleThen!(PreparedStatement)(new PrepareStatementCommand(queryDesc.getSql()), 
99     //         (CommandResponse!PreparedStatement ar) {
100     //             if (ar.succeeded()) {
101     //                 NamedQueryImpl queryImpl = new NamedQueryImpl(conn, ar.result(), queryDesc);
102     //                 f.succeeded(queryImpl);
103     //             } else {
104     //                 f.failed(ar.cause()); 
105     //             }
106     //         }
107     //     );
108         
109     //     return f;
110     // }
111 
112     // NamedQuery prepareNamedQuery(string sql) {
113     //     auto f = prepareNamedQueryAsync(sql);
114     //     return f.get();
115     // }    
116 }
117