Skip to content

Commit fb44cc5

Browse files
committed
v0.0.1+1
1 parent bac08cf commit fb44cc5

15 files changed

+243
-51
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 0.0.1+1
2+
- 支持在创建连接时指定Space(可选)
3+
- 适配dart_gdbc v0.0.1+3
4+
- 修改了Statement的接口
5+
- PreparedStatement支持gql参数渲染
6+
- 完善PreparedStatement的带参实现
7+
- 拓展方法
8+
- string.bytes 获取 Int8List
9+
- string.utf8code 获取Utf8编码的Int8List
10+
- int8List.utf8string 获取Utf8编码的String
11+
112
## 0.0.1
213

314
- 提供nebulagraph的驱动

bin/nebula_dart_gdbc.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
// import 'package:nebula_dart_gdbc/nebula_dart_gdbc.dart' as nebula_dart_gdbc;
1+
import 'package:nebula_dart_gdbc/nebula_dart_gdbc.dart';
22

3-
void main(List<String> arguments) {}
3+
void main() async {
4+
// 注册驱动
5+
DriverManager.registerDriver(NgDriver());
6+
// 获取连接
7+
var conn = await DriverManager.getConnection(
8+
'gdbc.nebula://127.0.0.1:9669/?space=test',
9+
username: 'root', // username is optional
10+
password: 'nebula', // password is optional
11+
);
12+
13+
// 创建语句
14+
var stmt = await conn.createStatement();
15+
// 执行语句
16+
var rs = await stmt.executeQuery(gql: 'SHOW SPACES;');
17+
// 打印结果
18+
print(rs);
19+
// 关闭连接
20+
conn.close();
21+
}

example/lib/example.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
import 'package:nebula_dart_gdbc/nebula_dart_gdbc.dart';
44

55
void main(List<String> args) async {
6+
// 注册驱动
67
DriverManager.registerDriver(NgDriver());
7-
8+
// 获取连接
89
var conn = await DriverManager.getConnection(
9-
'gdbc.nebula://127.0.0.1:9669/?username=root&password=nebula&space=test&timeout=1000',
10+
'gdbc.nebula://127.0.0.1:9669/?space=test',
11+
username: 'root', // username is optional
12+
password: 'nebula', // password is optional
1013
);
1114

15+
// 创建语句
1216
var stmt = await conn.createStatement();
13-
var rs = await stmt.executeQuery('SHOW SPACES;');
14-
17+
// 执行语句
18+
var rs = await stmt.executeQuery(gql: 'SHOW SPACES;');
19+
// 打印结果
1520
print(rs);
16-
await conn.close();
21+
// 关闭连接
22+
conn.close();
1723
}

lib/gen/nebula_graph/src/graph_service.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ class GraphServiceClient implements GraphService {
5555

5656
Future<AuthResponse> authenticate(
5757
Int8List username, Int8List password) async {
58-
print('authenticate');
5958
oprot.writeMessageBegin(
6059
new TMessage("authenticate", TMessageType.CALL, nextSeqid()));
6160
authenticate_args args = new authenticate_args();
@@ -96,7 +95,6 @@ class GraphServiceClient implements GraphService {
9695
}
9796

9897
Future<ExecutionResponse> execute(int sessionId, Int8List stmt) async {
99-
print('execute');
10098
oprot.writeMessageBegin(
10199
new TMessage("execute", TMessageType.CALL, nextSeqid()));
102100
execute_args args = new execute_args();
@@ -219,7 +217,6 @@ class GraphServiceClient implements GraphService {
219217

220218
Future<VerifyClientVersionResp> verifyClientVersion(
221219
VerifyClientVersionReq? req) async {
222-
print('verifyClientVersion');
223220
oprot.writeMessageBegin(
224221
new TMessage("verifyClientVersion", TMessageType.CALL, nextSeqid()));
225222
verifyClientVersion_args args = new verifyClientVersion_args();

lib/nebula_dart_gdbc.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ part 'src/ng_prepared_statement.dart';
1515
part 'src/ng_result_set.dart';
1616
part 'src/ng_result_set_meta_data.dart';
1717
part 'src/ng_result_handler.dart';
18+
19+
part 'src/ext/string_ext.dart';
20+
part 'src/ext/int8list_ext.dart';

lib/src/ext/int8list_ext.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
part of nebula_dart_gdbc;
2+
3+
extension Int8ListToString on Int8List {
4+
String utf8String() {
5+
return utf8.decode(Uint8List.fromList(this));
6+
}
7+
}

lib/src/ext/string_ext.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
part of nebula_dart_gdbc;
2+
3+
extension StringExt on String {
4+
Int8List get bytes => Int8List.fromList(codeUnits);
5+
Int8List get utf8code => Int8List.fromList(utf8.encode(this));
6+
}

lib/src/ng_connection.dart

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ part of nebula_dart_gdbc;
2424
/// ```
2525
class 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
}

lib/src/ng_prepared_statement.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,29 @@ part of nebula_dart_gdbc;
66

77
class NgPreparedStatement extends NgStatement implements PreparedStatement {
88
List<ParameterMetaData>? parameters;
9-
NgPreparedStatement(super.conn, {this.parameters});
9+
String gql;
10+
String Function(String, Map<String, dynamic>?)? render;
11+
12+
NgPreparedStatement(super.conn,
13+
{this.parameters, required this.gql, this.render});
1014

1115
@override
12-
Future<bool> execute([String? gql, Map<String, dynamic>? params]) async {
13-
var rs = await executeQuery(gql, params);
16+
Future<bool> execute({String? gql, Map<String, dynamic>? params}) async {
17+
var rs = await executeQuery(gql: gql, params: params);
1418
return rs.success;
1519
}
1620

1721
@override
1822
Future<ResultSet> executeQuery(
19-
[String? gql, Map<String, dynamic>? params]) async {
20-
return super.executeQuery(gql);
23+
{String? gql, Map<String, dynamic>? params}) async {
24+
if (params != null && render != null) {
25+
gql = render?.call(gql ?? this.gql, params);
26+
}
27+
return super.executeQuery(gql: gql ?? this.gql, params: params);
2128
}
2229

2330
@override
24-
Future<int> executeUpdate(String? gql, [Map<String, dynamic>? params]) async {
25-
return super.executeUpdate(gql);
31+
Future<int> executeUpdate({String? gql, Map<String, dynamic>? params}) async {
32+
return super.executeUpdate(gql: gql, params: params);
2633
}
2734
}

lib/src/ng_result_handler.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
part of nebula_dart_gdbc;
66

7-
extension Int8ListToString on Int8List {
8-
String utf8String() {
9-
return utf8.decode(Uint8List.fromList(this));
10-
}
11-
}
12-
137
NgResultSet handleResult(ng.ExecutionResponse rs, int? timezoneOffset) {
148
ValueMetaData? meta = ValueMetaData();
159
var rows = _handleValue(rs.data, meta, timezoneOffset);

0 commit comments

Comments
 (0)