### Initialize configuration from connection string Source: https://github.com/aaronjedwards/tds-nio/blob/main/Sources/TDSNIO/Documentation.docc/connecting-to-sql-server.md Creates a configuration instance by parsing a standard SQL Server-style connection string. ```swift let configuration = try TDSConnection.Configuration( connectionString: "Server=127.0.0.1,1433;User Id=sa;Password=your-strong-password;Database=app_database" ) ``` -------------------------------- ### Connect to SQL Server using configuration object Source: https://github.com/aaronjedwards/tds-nio/blob/main/Sources/TDSNIO/Documentation.docc/connecting-to-sql-server.md Initializes a connection using a structured configuration object and performs basic lifecycle management. ```swift import Logging import TDSNIO let configuration = TDSConnection.Configuration( host: "127.0.0.1", port: 1433, username: "sa", password: "your-strong-password", database: "app_database" ) let connection = try await TDSConnection.connect( configuration: configuration, id: 1, logger: Logger(label: "tds-nio") ) try await connection.close() ``` -------------------------------- ### Create a TDS Connection Source: https://github.com/aaronjedwards/tds-nio/blob/main/README.md Establish a connection to SQL Server using configuration objects or connection strings. ```swift import Logging import TDSNIO let configuration = TDSConnection.Configuration( host: "127.0.0.1", port: 1433, username: "sa", password: "your-strong-password", database: "app_database" ) let logger = Logger(label: "tds-nio") let connection = try await TDSConnection.connect( configuration: configuration, id: 1, logger: logger ) try await connection.close() ``` ```swift let configuration = try TDSConnection.Configuration( connectionString: "Server=127.0.0.1,1433;User Id=sa;Password=your-strong-password;Database=app_database" ) ``` ```swift import NIOSSL import TDSNIO let sslContext = try NIOSSLContext(configuration: .makeClientConfiguration()) let configuration = TDSConnection.Configuration( host: "sql.example.com", username: "sa", password: "your-strong-password", database: "app_database", tls: .require(sslContext) ) ``` -------------------------------- ### Configure TLS for SQL Server connections Source: https://github.com/aaronjedwards/tds-nio/blob/main/Sources/TDSNIO/Documentation.docc/connecting-to-sql-server.md Enables encrypted connections by providing a NIOSSLContext to the configuration. ```swift import NIOSSL import TDSNIO let sslContext = try NIOSSLContext(configuration: .makeClientConfiguration()) let configuration = TDSConnection.Configuration( host: "sql.example.com", username: "sa", password: "your-strong-password", database: "app_database", tls: .require(sslContext) ) ``` -------------------------------- ### Run SQL Statements Source: https://github.com/aaronjedwards/tds-nio/blob/main/README.md Execute queries and commands against the database, with support for row decoding. ```swift let rows = try await connection.query(""" SELECT id, username, created_at FROM users ORDER BY id """) for try await row in rows { let id = try row.decode(column: "id", as: Int.self) let username = try row.decode(column: "username", as: String.self) let createdAt = try row.decode(column: "created_at", as: Date.self) print(id, username, createdAt) } ``` ```swift let rows = try await connection.query("SELECT id, username FROM users") for try await (id, username) in rows.decode((Int, String).self) { print(id, username) } ``` ```swift let result = try await connection.execute(""" UPDATE users SET is_active = 1 WHERE last_login_at IS NOT NULL """) print(result.rowsAffected ?? 0) ``` -------------------------------- ### Using parameterized queries Source: https://github.com/aaronjedwards/tds-nio/blob/main/Sources/TDSNIO/Documentation.docc/querying-and-pooling.md Supports string interpolation for bind parameters, which are sent to SQL Server via sp_executesql. ```swift let id = 42 let username = "fancyuser" try await connection.execute(""" INSERT INTO users (id, username) VALUES (\(id), \(username)) """) ``` -------------------------------- ### Managing connections with TDSClient Source: https://github.com/aaronjedwards/tds-nio/blob/main/Sources/TDSNIO/Documentation.docc/querying-and-pooling.md Configures and runs a connection pool in a long-lived task for reusable connections. ```swift import Logging import TDSNIO var options = TDSClient.Options() options.maximumConnections = 10 let client = TDSClient( configuration: configuration, options: options, backgroundLogger: Logger(label: "tds-nio.pool") ) Task { await client.run() } try await client.withConnection { connection in let rows = try await connection.query("SELECT id, username FROM users") for try await (id, username) in rows.decode((Int, String).self) { print(id, username) } } ``` -------------------------------- ### Add TDSNIO Dependency Source: https://github.com/aaronjedwards/tds-nio/blob/main/README.md Include the package in your Swift Package Manager dependencies. ```swift dependencies: [ .package(url: "https://github.com/aaronjedwards/tds-nio.git", exact: "0.1.0-alpha.1"), ] ``` ```swift targets: [ .target(name: "MyTarget", dependencies: [ .product(name: "TDSNIO", package: "tds-nio"), ]) ] ``` -------------------------------- ### Executing commands with TDSConnection Source: https://github.com/aaronjedwards/tds-nio/blob/main/Sources/TDSNIO/Documentation.docc/querying-and-pooling.md Uses execute to perform operations like updates and retrieve the full TDSQueryResult. ```swift let result = try await connection.execute(""" UPDATE users SET is_active = 1 WHERE last_login_at IS NOT NULL """) print(result.rowsAffected ?? 0) ``` -------------------------------- ### Querying rows with TDSConnection Source: https://github.com/aaronjedwards/tds-nio/blob/main/Sources/TDSNIO/Documentation.docc/querying-and-pooling.md Executes a query and iterates through the result set, decoding columns by name. ```swift let rows = try await connection.query(""" SELECT id, username FROM users ORDER BY id """) for try await row in rows { let id = try row.decode(column: "id", as: Int.self) let username = try row.decode(column: "username", as: String.self) print(id, username) } ``` -------------------------------- ### Decoding rows positionally Source: https://github.com/aaronjedwards/tds-nio/blob/main/Sources/TDSNIO/Documentation.docc/querying-and-pooling.md Decodes result rows directly into a tuple format for simplified access. ```swift let rows = try await connection.query("SELECT id, username FROM users") for try await (id, username) in rows.decode((Int, String).self) { print(id, username) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.