<commit_before>//
//  SQLDatabase.swift
//  LittlinkRouterPerfect
//
//  Created by <NAME> on 10/28/16.
//
//

import Foundation

// WORKAROUND: #1 Swift doesn't support `where` clauses on associated types
public struct SQLDatabase<Client: SQLClient> where Client.RowStateSequence.Iterator.Element == Client.RowState {
    public static func supports(_ url: URL) -> Bool {
        return Client.supports(url)
    }
    
    public var state: Client.DatabaseState
    public init(url: URL) {
        state = Client.makeDatabaseState(url: url)
    }
    
    public func makeConnection() throws -> SQLConnection<Client> {
        return SQLConnection(state: try Client.makeConnectionState(for: state))
    }
}

extension Pool where Resource: _SQLConnection, Resource.Client.RowStateSequence.Iterator.Element == Resource.Client.RowState {
    public convenience init(database: SQLDatabase<Resource.Client>, maximum: Int = 10, timeout: DispatchTimeInterval = .seconds(10)) {
        self.init(maximum: maximum, timeout: timeout) {
            return try database.makeConnection() as! Resource
        }
    }
}
<commit_msg>Add doc comments for SQLDatabase<commit_after>//
//  SQLDatabase.swift
//  LittlinkRouterPerfect
//
//  Created by <NAME> on 10/28/16.
//
//

import Foundation

/// An abstract representation of a potential source of SQL data. A SQLDatabase 
/// instance contains the information needed to connect to a SQL database. Use its 
/// `makeConnection()` method to actually connect.
/// 
/// A `SQLDatabase` uses a particular client—a database library backed by 
/// a specific type conforming to `SQLClient`—indicated by `SQLDatabase`'s 
/// generic parameter. That means the type of database is part of the compile-time 
/// type of the variable. If you want to support multiple database types without 
/// using generics, see the `AnySQL` client.
// 
// WORKAROUND: #1 Swift doesn't support `where` clauses on associated types
public struct SQLDatabase<Client: SQLClient> where Client.RowStateSequence.Iterator.Element == Client.RowState {
    /// Returns `true` if this type of database can connect to the indicated URL.
    public static func supports(_ url: URL) -> Bool {
        return Client.supports(url)
    }
    
    /// The state object backing this instance. State objects are client-specific, 
    /// and some clients may expose low-level data structures through the state 
    /// object.
    public var state: Client.DatabaseState
    
    /// Creates a new instance which represents the database at the indicated URL.
    ///
    /// -Precondition: `url` is valid for this database's SQL client. If you're not 
    ///                  sure a URL will be supported, use the `supports(_:)` class 
    ///                  method to check before using this initializer. 
    public init(url: URL) {
        state = Client.makeDatabaseState(url: url)
    }
    
    /// Create a connection to this database.
    /// 
    /// -SeeAlso: `Pool`, which can be used to make connection pools.
    public func makeConnection() throws -> SQLConnection<Client> {
        return SQLConnection(state: try Client.makeConnectionState(for: state))
    }
}

extension Pool where Resource: _SQLConnection, Resource.Client.RowStateSequence.Iterator.Element == Resource.Client.RowState {
    /// Creates a pool of connections to the given database.
    /// 
    /// -Parameter database: The database to connect to.
    /// -Parameter maximum: The maximum number of database connections to use.
    ///                          Default: 10.
    /// -Parameter timeout: The maximum amount of time to wait for a connection 
    ///                         to be returned to the pool. Default: 10 seconds.
    public convenience init(database: SQLDatabase<Resource.Client>, maximum: Int = 10, timeout: DispatchTimeInterval = .seconds(10)) {
        self.init(maximum: maximum, timeout: timeout) {
            return try database.makeConnection() as! Resource
        }
    }
}

{'fraction_non_alphanumeric': 0.09811794228356337, 'fraction_numerical': 0.006524466750313676, 'mean_word_length': 2.771050141911069, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4005900', 'n_tokens_mistral': 1060, 'n_tokens_neox': 1005, 'n_words': 389}