Skip to content

Commit 70e1871

Browse files
committed
new updates by
1 parent 72e773d commit 70e1871

File tree

10 files changed

+984
-522
lines changed

10 files changed

+984
-522
lines changed

LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

Package.swift

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,62 @@
1-
// swift-tools-version:5.9
1+
// swift-tools-version: 5.9
2+
// Package.swift for vkuttyp/SQLClient-Swift
3+
24
import PackageDescription
35

46
let package = Package(
5-
name: "SQLClient-Swift",
7+
name: "SQLClientSwift",
8+
9+
// Explicit platform minimums — required for async/await and modern Swift Concurrency.
610
platforms: [
11+
.iOS(.v16),
712
.macOS(.v13),
8-
.iOS(.v13)
13+
.tvOS(.v16),
914
],
15+
1016
products: [
11-
.library(name: "SQLClient", targets: ["SQLClient"]),
17+
.library(
18+
name: "SQLClientSwift",
19+
targets: ["SQLClientSwift"]
20+
),
1221
],
22+
1323
targets: [
24+
// ── System library target for FreeTDS ─────────────────────────────
25+
// Enables `import CFreeTDS` in Swift without a bridging header,
26+
// which is required for Linux / Swift Package Manager builds.
27+
// On macOS/iOS you still need to link libsybdb.a manually.
1428
.systemLibrary(
15-
name: "CSybdb",
16-
pkgConfig: "freetds",
29+
name: "CFreeTDS",
30+
pkgConfig: "freetds", // resolved via `pkg-config freetds`
1731
providers: [
18-
.brew(["freetds"]),
19-
.apt(["freetds-dev"])
32+
.brew(["freetds"]), // macOS: brew install freetds
33+
.apt(["freetds-dev"]), // Linux: apt install freetds-dev
2034
]
2135
),
36+
37+
// ── Main library target ───────────────────────────────────────────
2238
.target(
23-
name: "SQLClient",
24-
dependencies: ["CSybdb"],
39+
name: "SQLClientSwift",
40+
dependencies: ["CFreeTDS"],
41+
path: "Sources/SQLClientSwift",
42+
swiftSettings: [
43+
// Enable strict concurrency checking (Swift 5.9+)
44+
.enableExperimentalFeature("StrictConcurrency=complete"),
45+
],
2546
linkerSettings: [
2647
.unsafeFlags(["-L/opt/homebrew/opt/freetds/lib"], .when(platforms: [.macOS])),
2748
.linkedLibrary("sybdb"),
2849
.linkedLibrary("iconv", .when(platforms: [.macOS]))
2950
]
30-
)
51+
),
52+
53+
// ── Unit & integration test target ───────────────────────────────
54+
// Integration tests require a live SQL Server; controlled via
55+
// environment variables HOST, DATABASE, USERNAME, PASSWORD.
56+
.testTarget(
57+
name: "SQLClientSwiftTests",
58+
dependencies: ["SQLClientSwift"],
59+
path: "Tests/SQLClientSwiftTests"
60+
),
3161
]
3262
)

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# SQLClient-Swift
2+
3+
A modern, high-performance Swift client for **Microsoft SQL Server**, built for macOS, iOS, and Linux.
4+
5+
This library is a lightweight Swift wrapper around the **FreeTDS (db-lib)** C library. It provides a clean, native Swift interface with full support for modern Swift features like `Async/Await`.
6+
7+
## Features
8+
9+
- **Modern Concurrency**: Full `Async/Await` support for non-blocking database operations.
10+
- **Cross-Platform**: Works seamlessly on macOS (via Homebrew) and Linux (via APT).
11+
- **FreeTDS 1.x Optimized**:
12+
- Supports modern SQL Server types: `DATETIME2`, `NVARCHAR(MAX)`, `UNIQUEIDENTIFIER` (UUID), etc.
13+
- Handles affected-row counts (`dbcount`) for DML operations.
14+
- Support for named encryption modes (`off`, `request`, `require`, `strict` for TDS 8.0).
15+
- **Robust & Compatible**:
16+
- Automatically detects and handles different FreeTDS build modes (Sybase vs. Microsoft).
17+
- Gracefully falls back on advanced features for older FreeTDS versions.
18+
- Fixes the legacy "MONEY truncation" bug in older libraries.
19+
- **Thread-Safe**: Uses internal serial dispatch queues to safely manage the underlying C state.
20+
21+
## Installation
22+
23+
### 1. Install FreeTDS
24+
25+
#### macOS (Homebrew)
26+
```bash
27+
brew install freetds
28+
```
29+
30+
#### Linux (Ubuntu/Debian)
31+
```bash
32+
sudo apt-get update
33+
sudo apt-get install freetds-dev freetds-bin
34+
```
35+
36+
### 2. Add to Swift Package Manager
37+
38+
Add the dependency to your `Package.swift`:
39+
40+
```swift
41+
dependencies: [
42+
.package(url: "https://github.com/vkuttyp/SQLClient-Swift.git", from: "1.0.0")
43+
]
44+
```
45+
46+
And add it to your target:
47+
48+
```swift
49+
targets: [
50+
.target(
51+
name: "YourApp",
52+
dependencies: [
53+
.product(name: "SQLClient", package: "SQLClient-Swift")
54+
]
55+
)
56+
]
57+
```
58+
59+
## Usage
60+
61+
### Simple Connection (Async/Await)
62+
63+
```swift
64+
import SQLClient
65+
66+
let client = SQLClient.shared
67+
68+
// Note: For some Linux/Sybase-mode builds, include the port in the host string
69+
let connected = await client.connect(
70+
server: "sql.marivil.com:1433",
71+
username: "sa",
72+
password: "your-password",
73+
database: "TestDB"
74+
)
75+
76+
if connected {
77+
let results = await client.execute("SELECT * FROM Products")
78+
for table in results {
79+
for row in table {
80+
print(row["title"] ?? "No Title")
81+
}
82+
}
83+
}
84+
```
85+
86+
### Advanced Configuration
87+
88+
Use `SQLClientConnectionOptions` to tune timeouts, encryption, and more:
89+
90+
```swift
91+
var options = SQLClientConnectionOptions(
92+
server: "vps.marivil.com",
93+
username: "sa",
94+
password: "password"
95+
)
96+
options.port = 1430
97+
options.database = "SwiftTestDb"
98+
options.encryption = .require
99+
options.loginTimeout = 10
100+
options.queryTimeout = 30
101+
102+
let connected = await client.connect(options: options)
103+
```
104+
105+
### Handling Affected Rows
106+
107+
```swift
108+
let result = await client.executeWithResult("UPDATE Products SET price = 19.99 WHERE id = 1")
109+
print("Rows changed: \(result.rowsAffected)")
110+
```
111+
112+
## Environment Variables
113+
114+
The library defaults to **TDS version 7.4** (compatible with SQL Server 2012-2022). You can override this via the `TDSVER` environment variable:
115+
116+
```bash
117+
export TDSVER=7.4
118+
```
119+
120+
## License
121+
122+
MIT License. See [LICENSE](LICENSE) for details.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef CSYBDB_H
2-
#define CSYBDB_H
1+
#ifndef CFREETDS_H
2+
#define CFREETDS_H
33

44
#ifdef __APPLE__
55
#include "/opt/homebrew/opt/freetds/include/sybdb.h"
@@ -9,4 +9,4 @@
99
#include <sybfront.h>
1010
#endif
1111

12-
#endif /* CSYBDB_H */
12+
#endif /* CFREETDS_H */

Sources/CFreeTDS/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module CFreeTDS [system] {
2+
header "include/CFreeTDS.h"
3+
export *
4+
}

Sources/CSybdb/module.modulemap

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)