|
| 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. |
0 commit comments