@@ -24,6 +24,7 @@ part of nebula_dart_gdbc;
2424/// ```
2525class NgConnection implements Connection {
2626 static const String timeoutKey = 'timeout' ;
27+ static const String spaceKey = 'space' ;
2728
2829 late ng.TSocketTransport socketTransport;
2930 late ng.TFramedTransport transport;
@@ -33,6 +34,7 @@ class NgConnection implements Connection {
3334 int ? _sessionId;
3435 int ? timezoneOffset;
3536 int timeout = 0 ;
37+ String ? space;
3638
3739 /// Invoked in [DriverManager.getConnection] ,
3840 /// you should not call this method directly.
@@ -67,6 +69,11 @@ class NgConnection implements Connection {
6769 /// check the version of client and server
6870 await verifyVersion ();
6971 await authencate ();
72+
73+ if (properties.containsKey (spaceKey)) {
74+ space = properties[spaceKey];
75+ await executeQuery ('USE $space ' );
76+ }
7077 }
7178
7279 /// check the version of client and server
@@ -119,13 +126,27 @@ class NgConnection implements Connection {
119126 }
120127
121128 @override
122- Future <ResultSet > executeQuery (String gql) async {
123- return await NgStatement (this ).executeQuery (gql);
129+ Future <ResultSet > executeQuery (String gql,
130+ {Map <String , dynamic >? params}) async {
131+ var sessionId = _sessionId ?? 0 ;
132+ var stmtBytes = Int8List .fromList (utf8.encode (gql));
133+ ng.ExecutionResponse resp = params == null
134+ ? await client.execute (sessionId, stmtBytes)
135+ : await client.executeWithParameter (
136+ sessionId,
137+ stmtBytes,
138+ _convertParams (params),
139+ );
140+ if (resp.error_code == ng.ErrorCode .SUCCEEDED ) {
141+ return handleResult (resp, timezoneOffset);
142+ } else {
143+ throw GdbcQueryException (message: resp.error_msg? .utf8String ());
144+ }
124145 }
125146
126147 @override
127148 Future <int > executeUpdate (String gql) async {
128- return await NgStatement (this ).executeUpdate (gql);
149+ return await NgStatement (this ).executeUpdate (gql: gql );
129150 }
130151
131152 @override
@@ -145,14 +166,17 @@ class NgConnection implements Connection {
145166 }
146167
147168 @override
148- Future <PreparedStatement > prepareStatement (String gql) async {
149- return NgPreparedStatement (this );
169+ Future <PreparedStatement > prepareStatement (
170+ String gql, {
171+ String Function (String , Map <String , dynamic >? )? render,
172+ }) async {
173+ return NgPreparedStatement (this , gql: gql, render: render);
150174 }
151175
152176 @override
153177 Future <PreparedStatement > prepareStatementWithParameters (
154178 String gql, List <ParameterMetaData > parameters) async {
155- return NgPreparedStatement (this , parameters: parameters);
179+ return NgPreparedStatement (this , parameters: parameters, gql : gql );
156180 }
157181
158182 @override
@@ -164,4 +188,49 @@ class NgConnection implements Connection {
164188 Future <void > setAutoCommit (bool autoCommit) {
165189 throw DbFeatureException ('No support for setAutoCommit' );
166190 }
191+
192+ Map <Int8List , ng.Value > _convertParams (Map <String , dynamic >? params) {
193+ if (params == null ) return < Int8List , ng.Value > {};
194+ return params.map ((key, value) {
195+ return MapEntry (key.bytes, _convertParam (value));
196+ });
197+ }
198+
199+ ng.Value _convertParam (value) {
200+ if (value == null ) {
201+ return ng.Value ()..nVal = 0 ;
202+ } else if (value is int ) {
203+ return ng.Value ()..iVal = value;
204+ } else if (value is double ) {
205+ return ng.Value ()..fVal = value;
206+ } else if (value is bool ) {
207+ return ng.Value ()..bVal = value;
208+ } else if (value is String ) {
209+ return ng.Value ()..sVal = value.utf8code;
210+ } else if (value is List ) {
211+ return ng.Value ()
212+ ..lVal = (ng.NList ()..values = value.map (_convertParam).toList ());
213+ } else if (value is Set ) {
214+ return ng.Value ()
215+ ..uVal = (ng.NSet ()..values = value.map (_convertParam).toSet ());
216+ } else if (value is Map <String , dynamic >) {
217+ return ng.Value ()..mVal = (ng.NMap ()..kvs = _convertParams (value));
218+ } else {
219+ if (value.toJson is ! Map <String , dynamic > Function ()) {
220+ throw GdbcQueryException (
221+ message:
222+ '''Unsupported type: ${value .runtimeType }, you can define a toJson method for it. such as:
223+ Map<String, dynamic> toJson() {
224+ return {
225+ 'name': name,
226+ 'age': age,
227+ };
228+ }
229+ ''' ,
230+ );
231+ }
232+ Map <String , dynamic > jsonValue = value.toJson ();
233+ return _convertParam (jsonValue);
234+ }
235+ }
167236}
0 commit comments