Skip to content

Commit 126c4ed

Browse files
committed
Update DocC documentation with new features (RPC, BCP, Transactions)
1 parent b36e7a9 commit 126c4ed

4 files changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Bulk Insert (BCP)
2+
3+
High-performance insertion of large datasets using the Bulk Copy (BCP) protocol.
4+
5+
## Overview
6+
7+
The Bulk Copy (BCP) interface is significantly faster than standard `INSERT` statements when loading thousands of rows. SQLClient provides a simple `bulkInsert` method that leverages the underlying FreeTDS BCP implementation.
8+
9+
### Usage
10+
11+
To perform a bulk insert, prepare an array of ``SQLRow`` objects and specify the target table name.
12+
13+
```swift
14+
var rows: [SQLRow] = []
15+
for i in 1...1000 {
16+
let storage: [(key: String, value: Sendable)] = [
17+
(key: "Id", value: i),
18+
(key: "Name", value: "User \(i)")
19+
]
20+
rows.append(SQLRow(storage, columnTypes: [:]))
21+
}
22+
23+
let insertedCount = try await client.bulkInsert(table: "Users", rows: rows)
24+
print("Successfully inserted \(insertedCount) rows.")
25+
```
26+
27+
### Considerations
28+
29+
- **Encoding**: Currently, BCP works best with standard `VARCHAR` columns.
30+
- **Table Existence**: The target table must exist in the database before calling `bulkInsert`.
31+
- **Permissions**: The database user must have the necessary permissions to perform bulk operations.
32+
33+
## Methods
34+
35+
- ``SQLClient/bulkInsert(table:rows:)``
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Stored Procedures (RPC)
2+
3+
Execute stored procedures efficiently using Remote Procedure Calls (RPC).
4+
5+
## Overview
6+
7+
Remote Procedure Calls are the native way to execute stored procedures in SQL Server. Unlike building an `EXEC` string, RPC correctly handles input and output parameters, return statuses, and is generally more efficient.
8+
9+
### Usage
10+
11+
To call a stored procedure, use the `executeRPC` method along with an array of ``SQLParameter`` objects.
12+
13+
```swift
14+
let params = [
15+
SQLParameter(name: "@InVal", value: 42),
16+
SQLParameter(name: "@OutVal", value: 0, isOutput: true)
17+
]
18+
19+
let result = try await client.executeRPC("MyProcedure", parameters: params)
20+
21+
// Access output parameters
22+
if let resultVal = result.outputParameters["@OutVal"] as? NSNumber {
23+
print("Result from procedure: \(resultVal.intValue)")
24+
}
25+
26+
// Access return status
27+
if let status = result.returnStatus {
28+
print("Procedure returned status: \(status)")
29+
}
30+
```
31+
32+
### Advantages of RPC
33+
34+
- **Type Safety**: Parameters are passed with their native types.
35+
- **Output Parameters**: Built-in support for capturing values from `OUTPUT` parameters.
36+
- **Performance**: Skips some of the parsing overhead associated with ad-hoc T-SQL.
37+
38+
## Related Types
39+
40+
- ``SQLParameter``
41+
- ``SQLClientResult/outputParameters``
42+
- ``SQLClientResult/returnStatus``
43+
44+
## Methods
45+
46+
- ``SQLClient/executeRPC(_:parameters:)``

Sources/SQLClientSwift/SQLClientSwift.docc/SQLClientSwift.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ SQLClient-Swift provides a clean, asynchronous API for interacting with SQL Serv
1313
- **Type Safety**: Automatic mapping of database rows to Swift `Decodable` types.
1414
- **Thread Safety**: Uses Swift actors to ensure safe concurrent access to database connections.
1515
- **Full Encryption Support**: Supports TLS/SSL for Azure SQL and modern SQL Server instances.
16+
- **Stored Procedures**: Full support for Remote Procedure Calls (RPC) with output parameters.
17+
- **Bulk Copy (BCP)**: High-performance data ingestion for large datasets.
18+
- **Connection Pooling**: Built-in support for managing reusable connection pools.
1619

1720
## Topics
1821

@@ -22,3 +25,11 @@ SQLClient-Swift provides a clean, asynchronous API for interacting with SQL Serv
2225
- ``SQLClientConnectionOptions``
2326
- ``SQLClientResult``
2427
- ``SQLRow``
28+
- ``SQLParameter``
29+
30+
### Advanced Features
31+
32+
- ``SQLClientPool``
33+
- <doc:Transactions>
34+
- <doc:BulkInsert>
35+
- <doc:RPC>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Transactions
2+
3+
Execute multiple SQL statements within a single atomic transaction.
4+
5+
## Overview
6+
7+
SQLClient allows you to explicitly manage database transactions using the `beginTransaction`, `commitTransaction`, and `rollbackTransaction` methods. This ensures that a group of operations either all succeed or all fail together, maintaining data integrity.
8+
9+
### Usage
10+
11+
```swift
12+
try await client.beginTransaction()
13+
do {
14+
try await client.run("INSERT INTO Orders (Id, Total) VALUES (1, 100)")
15+
try await client.run("UPDATE Inventory SET Stock = Stock - 1 WHERE ProductId = 5")
16+
17+
// Commit the changes to the database
18+
try await client.commitTransaction()
19+
} catch {
20+
// If any operation fails, roll back the entire transaction
21+
try await client.rollbackTransaction()
22+
throw error
23+
}
24+
```
25+
26+
## Methods
27+
28+
- ``SQLClient/beginTransaction()``
29+
- ``SQLClient/commitTransaction()``
30+
- ``SQLClient/rollbackTransaction()``

0 commit comments

Comments
 (0)