|
| 1 | +part of nebula_dart_gdbc; |
| 2 | + |
| 3 | +/// Create by [DriverManager.getConnection] |
| 4 | +/// |
| 5 | +/// 通过 [DriverManager.getConnection] 创建 |
| 6 | +/// |
| 7 | +/// For example: |
| 8 | +/// ```dart |
| 9 | +/// DriverManager.getConnection( |
| 10 | +/// 'gdbc.nebula://127.0.0.1:9669/?username=root&password=nebula&space=nb', |
| 11 | +/// properties: {// properties is optional |
| 12 | +/// DriverManager.usrKey: 'root', |
| 13 | +/// DriverManager.pwdKey: 'nebula', |
| 14 | +/// 'timeout': '1000', |
| 15 | +/// 'space': 'nb', |
| 16 | +/// }, |
| 17 | +/// username: 'root', // username is optional |
| 18 | +/// password: 'nebula', // password is optional |
| 19 | +/// ); |
| 20 | +/// ``` |
| 21 | +class NgConnection implements Connection { |
| 22 | + static const String timeoutKey = 'timeout'; |
| 23 | + |
| 24 | + NgConnection._(); |
| 25 | + |
| 26 | + late TSocketTransport socketTransport; |
| 27 | + late TFramedTransport transport; |
| 28 | + late THeaderProtocol protocol; |
| 29 | + late GraphServiceClient client; |
| 30 | + late Map<String, dynamic> properties; |
| 31 | + int? _sessionId; |
| 32 | + int? timezoneOffset; |
| 33 | + int timeout = 0; |
| 34 | + |
| 35 | + /// Invoked in [DriverManager.getConnection], |
| 36 | + /// you should not call this method directly. |
| 37 | + /// Because connection is not open yet. |
| 38 | + /// |
| 39 | + /// 通过 [DriverManager.getConnection] 调用, |
| 40 | + /// 不应该直接调用这个方法。 |
| 41 | + /// 因为连接还没有打开。 |
| 42 | + NgConnection._create(Uri address, {Map<String, dynamic>? properties}) { |
| 43 | + this.properties = properties ?? <String, dynamic>{}; |
| 44 | + socketTransport = TSocketTransport( |
| 45 | + host: address.host, |
| 46 | + port: address.port, |
| 47 | + connectionTimeout: properties?[timeoutKey] ?? 6000, |
| 48 | + ); |
| 49 | + |
| 50 | + transport = THeaderTransport( |
| 51 | + transport: socketTransport, |
| 52 | + clientTypes: [ClientTypes.HEADERS], |
| 53 | + supportedClients: [false]); |
| 54 | + |
| 55 | + protocol = THeaderProtocol(transport); |
| 56 | + client = GraphServiceClient(protocol); |
| 57 | + } |
| 58 | + |
| 59 | + /// Invoked in [DriverManager.getConnection], after the connection created. |
| 60 | + /// |
| 61 | + /// 在 [DriverManager.getConnection] 内创建了连接之后调用。 |
| 62 | + Future<void> _open() async { |
| 63 | + if (!socketTransport.isOpen) await socketTransport.open(); |
| 64 | + |
| 65 | + /// check the version of client and server |
| 66 | + await verifyVersion(); |
| 67 | + await authencate(); |
| 68 | + } |
| 69 | + |
| 70 | + /// check the version of client and server |
| 71 | + /// if the version is not compatible, throw an exception |
| 72 | + /// |
| 73 | + /// 检查客户端和服务端的版本是否兼容 |
| 74 | + /// 如果不兼容,抛出异常 |
| 75 | + Future<void> verifyVersion() async { |
| 76 | + VerifyClientVersionResp resp = |
| 77 | + await client.verifyClientVersion(VerifyClientVersionReq()); |
| 78 | + |
| 79 | + if (resp.error_code != ErrorCode.SUCCEEDED) { |
| 80 | + throw VersionException( |
| 81 | + message: String.fromCharCodes(resp.error_msg ?? [])); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// authenticate the user |
| 86 | + /// |
| 87 | + /// 认证用户 |
| 88 | + Future<void> authencate() async { |
| 89 | + AuthResponse resp = await client.authenticate( |
| 90 | + Int8List.fromList(utf8.encode(properties[DriverManager.usrKey] ?? '')), |
| 91 | + Int8List.fromList(utf8.encode(properties[DriverManager.pwdKey] ?? '')), |
| 92 | + ); |
| 93 | + |
| 94 | + if (resp.error_code != ErrorCode.SUCCEEDED) { |
| 95 | + if (resp.error_msg != null) { |
| 96 | + throw ConnectException(message: String.fromCharCodes(resp.error_msg!)); |
| 97 | + } else { |
| 98 | + throw ConnectException(message: "Auth failed"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + _sessionId = resp.session_id; |
| 103 | + timezoneOffset = resp.time_zone_offset_seconds ?? 0; |
| 104 | + } |
| 105 | + |
| 106 | + @override |
| 107 | + Future<void> close() async { |
| 108 | + await transport.close(); |
| 109 | + } |
| 110 | + |
| 111 | + @override |
| 112 | + Future<void> commit() async {} |
| 113 | + |
| 114 | + @override |
| 115 | + Future<Statement> createStatement() async { |
| 116 | + return NgStatement(this); |
| 117 | + } |
| 118 | + |
| 119 | + @override |
| 120 | + Future<ResultSet> executeQuery(String gql) async { |
| 121 | + return await NgStatement(this).executeQuery(gql); |
| 122 | + } |
| 123 | + |
| 124 | + @override |
| 125 | + Future<int> executeUpdate(String gql) async { |
| 126 | + return await NgStatement(this).executeUpdate(gql); |
| 127 | + } |
| 128 | + |
| 129 | + @override |
| 130 | + Future<bool> getAutoCommit() async { |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + @override |
| 135 | + Future<ResultSetMetaData> getMetaData() { |
| 136 | + // TODO: implement getMetaData |
| 137 | + throw UnimplementedError(); |
| 138 | + } |
| 139 | + |
| 140 | + @override |
| 141 | + Future<bool> isClosed() async { |
| 142 | + return !transport.isOpen; |
| 143 | + } |
| 144 | + |
| 145 | + @override |
| 146 | + Future<PreparedStatement> prepareStatement(String gql) async { |
| 147 | + return NgPreparedStatement(this); |
| 148 | + } |
| 149 | + |
| 150 | + @override |
| 151 | + Future<PreparedStatement> prepareStatementWithParameters( |
| 152 | + String gql, List<ParameterMetaData> parameters) async { |
| 153 | + return NgPreparedStatement(this, parameters: parameters); |
| 154 | + } |
| 155 | + |
| 156 | + @override |
| 157 | + Future<void> rollback() { |
| 158 | + throw DbFeatureException('No support for rollback'); |
| 159 | + } |
| 160 | + |
| 161 | + @override |
| 162 | + Future<void> setAutoCommit(bool autoCommit) { |
| 163 | + throw DbFeatureException('No support for setAutoCommit'); |
| 164 | + } |
| 165 | +} |
0 commit comments