Skip to content

Commit 00374e5

Browse files
committed
feat: ResultSet meta/value getter
1 parent b7f44d0 commit 00374e5

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

Diff for: lib/src/driver.dart

+19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
part of dart_gdbc;
22

3+
/// A [Driver] is a software component enabling a [Connection] to a database.
4+
/// ---
5+
/// 數據庫驅動接口,提供兩個方法:
6+
/// * 給定 url 的格式,通過 url 格式來確定使用的驅動
7+
/// * 通過 url 和 properties 來創建一個 [Connection]
8+
///---
9+
/// 数据库驱动接口,提供两个方法:
10+
/// * 给定 url 的格式,通过 url 格式来确定使用的驱动
11+
/// * 通过 url 和 properties 来创建一个 [Connection]
312
abstract class Driver {
13+
/// Attempts to make a database connection to the given URL.
14+
/// ---
15+
/// 嘗試通過給定的 url 來創建一個數據庫連接。
16+
/// ---
17+
/// 尝试通过给定的 url 来创建一个数据库连接。
418
Future<Connection> connect(String url, {Map<String, dynamic> properties});
519

20+
/// Retrieves whether the driver thinks that it can open a connection to the given URL.
21+
/// ---
22+
/// 獲取驅動程序是否認為它可以打開到給定URL的連接。
23+
/// ---
24+
/// 获取驱动程序是否认为它可以打开到给定URL的连接。
625
bool acceptsURL(String url);
726
}

Diff for: lib/src/driver_manager.dart

+29
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
11
part of dart_gdbc;
22

3+
/// The basic service for managing a set of GDBC drivers.
4+
/// Two things are supported in DriverManager:
5+
/// * You can register a driver with the [DriverManager.registerDriver(driver)].
6+
/// * You can request a connection using the [DriverManager.getConnection] method.
7+
/// ---
8+
/// 驅動程序管理器的基本服務。
9+
/// DriverManager支持兩件事:
10+
/// * 您可以使用[DriverManager.registerDriver(driver)]註冊驅動程序。
11+
/// * 您可以使用[DriverManager.getConnection]方法請求連接。
12+
///---
13+
/// 驱动程序管理器的基本服务。
14+
/// DriverManager支持两件事:
15+
/// * 您可以使用[DriverManager.registerDriver(driver)]注册驱动程序。
16+
/// * 您可以使用[DriverManager.getConnection]方法请求连接。
317
class DriverManager {
418
static const String usrKey = 'username';
519
static const String pwdKey = 'password';
620

721
static final Map<String, Driver> _drivers = {};
822

23+
/// Register a driver with the driver manager.
24+
///---
25+
/// 使用驱动程序管理器注册驱动程序。
26+
///---
27+
/// 使用驅動程序管理器註冊驅動程序。
928
static void registerDriver(Driver driver, [String? driverId]) {
1029
_drivers[driverId ?? driver.runtimeType.toString()] = driver;
1130
}
1231

32+
/// Get a connection from the first driver that recognizes the given URL.
33+
/// username and password are optional, if you don't provide them in the url, you can provide them in the properties.
34+
/// ---
35+
/// 通過url獲取第一個格式相符的驅動程序的連接。
36+
/// username和password是可選的,可以通過 properties 傳入,可以通過 url 傳入。
37+
/// 不過通過 url 傳入的方式是不可靠的,依托具體的數據庫驅動包。
38+
/// ---
39+
/// 通过url获取第一个格式相符的驱动程序的连接。
40+
/// username和password是可选的,可以通过 properties 传入,可以通过 url 传入,
41+
/// 不过通过 url 传入的方式是不可靠的,依托具体的数据库驱动包。
1342
static Future<Connection> getConnection(
1443
String url, {
1544
Map<String, dynamic>? properties,

Diff for: lib/src/result_set.dart

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
part of dart_gdbc;
22

33
abstract class ResultSet {
4-
bool success = false;
4+
bool success = true;
55

66
List<ValueMetaData> get metas;
77

@@ -13,4 +13,35 @@ abstract class ResultSet {
1313
String toString() {
1414
return '$runtimeType{\n\tsuccess: $success,\n\tmetas: ${metas.map((e) => e.toJson()).toList()},\n\tcolumns: $columns,\n\trows: ${jsonEncode(rows)}\n}';
1515
}
16+
17+
ValueMetaData? meta(List<int> indexes, [List<ValueMetaData>? metas]) {
18+
indexes = [...indexes];
19+
metas ??= this.metas;
20+
if (indexes.isEmpty) {
21+
return null;
22+
}
23+
24+
var currentLevelColumnIndex = indexes.removeAt(0);
25+
if (indexes.isEmpty) {
26+
return metas[currentLevelColumnIndex];
27+
} else {
28+
return meta(indexes, metas[currentLevelColumnIndex].submetas);
29+
}
30+
}
31+
32+
dynamic value(List<int> colIndexes,
33+
[int rowIndex = 0, List<dynamic>? values]) {
34+
colIndexes = [...colIndexes];
35+
values ??= rows[rowIndex];
36+
if (colIndexes.isEmpty) {
37+
return null;
38+
}
39+
40+
var c = colIndexes.removeAt(0);
41+
if (colIndexes.isEmpty) {
42+
return values[c];
43+
} else {
44+
return value(colIndexes, values[c]);
45+
}
46+
}
1647
}

0 commit comments

Comments
 (0)