<commit_before><?php

/**
 * Standard DB Connector class that wraps PDO functions.
 *
 * Init with:
 *  DB::instance([
 *          'host' => 'localhost', // optional - will default to localhost
 *          'port' => 3306,        // optional - will default to 3306
 *          'user' => '<<< DB USER >>>',
 *          'pass' => '<<< <PASSWORD> >>>',
 *          'db'   => '<<< DB NAME >>>',
 *          'key'  => '<<< DB AES KEY >>>', // preferred method would be to file_get_contents a file OUTSIDE of public_html here
 *      ]);
 */
class DB {
    const INSERT_SIMPLE  = 0;
    const INSERT_REPLACE = 1;
    const INSERT_UPDATE  = 2;

    protected static $conn_opts = [
        PDO::ATTR_PERSISTENT         => true,
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_WARNING,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];

    /**
     * Configuration array for server settings; each key is a name that can be passed to DB::instance.
     * Server configurations can be pre-loaded here, or configured on-the-fly by passing the connection options to DB::instance
     * Connection options for each connection:
     *  - host (optional, default=localhost)
     *  - port (optional, default=3306)
     *  - user (required)
     *  - pass (optional if user has no password)
     *  - db (optional, no default)
     *  - key (optional, no default, but the encrypt/decrypts function won't work without it)
     *    if key is provided, "SET @key=UNHEX('...');" will be run upon connection
     */
    protected static $config = [];

    /**
     * @var DB[] initialized instances of this class, keyed by configuration name
     */
    protected static $instances = [];

    /**
     * Initialize a connection (but don't actually connect) for the given configuration name
     * This function does a few different things based on the arguments:
     *  - no arguments: get the connection instance for the "default" configuration
     *  - string: get the connection instance for the specified configuration name
     *  - configuration options array: set the configuration for the "default" configuration, and get the "default" connection instance
     *  - string and conf opts array: set the configuration for the specified connection, and get that connection instance
     *
     * @param string|array|null $conn_name configuration name, or connection options array for "default" connection, or null to get an instance of the default connectin
     * @param array|null $config connection options array to set if configuration name was specified, or null to get and instance of the specified connection
     * @return DB instance of this class corresponding to the connection
     */
    public static function instance($conn_name = null, $config = null) {
        if ($conn_name === null) {
            $conn_name = 'default';
        } else if (is_array($conn_name)) {
            $conn_name = 'default';
            $config = $conn_name;
        } else {
            $conn_name = strval($conn_name);
        }

        if (!is_array($config) || (isset(static::$config[$conn_name]) && static::$config[$conn_name] == $config)) {
            $config = null;
        }

        if ($config !== null) {
            static::$config[$conn_name] = $config;
            unset(static::$instances[$conn_name]);
        }

        if (isset(static::$instances[$conn_name])) {
            return static::$instances[$conn_name];
        }

        $db = new self($conn_name);
        static::$instances[$conn_name] = $db;
        return $db;<commit_msg>Fix big derp. Derp derp derp.<commit_after><?php

/**
 * Standard DB Connector class that wraps PDO functions.
 *
 * Init with:
 *  DB::instance([
 *          'host' => 'localhost', // optional - will default to localhost
 *          'port' => 3306,        // optional - will default to 3306
 *          'user' => '<<< DB USER >>>',
 *          'pass' => '<<< <PASSWORD> >>>',
 *          'db'   => '<<< DB NAME >>>',
 *          'key'  => '<<< DB AES KEY >>>', // preferred method would be to file_get_contents a file OUTSIDE of public_html here
 *      ]);
 */
class DB {
    const INSERT_SIMPLE  = 0;
    const INSERT_REPLACE = 1;
    const INSERT_UPDATE  = 2;

    protected static $conn_opts = [
        PDO::ATTR_PERSISTENT         => true,
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_WARNING,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];

    /**
     * Configuration array for server settings; each key is a name that can be passed to DB::instance.
     * Server configurations can be pre-loaded here, or configured on-the-fly by passing the connection options to DB::instance
     * Connection options for each connection:
     *  - host (optional, default=localhost)
     *  - port (optional, default=3306)
     *  - user (required)
     *  - pass (optional if user has no password)
     *  - db (optional, no default)
     *  - key (optional, no default, but the encrypt/decrypts function won't work without it)
     *    if key is provided, "SET @key=UNHEX('...');" will be run upon connection
     */
    protected static $config = [];

    /**
     * @var DB[] initialized instances of this class, keyed by configuration name
     */
    protected static $instances = [];

    /**
     * Initialize a connection (but don't actually connect) for the given configuration name
     * This function does a few different things based on the arguments:
     *  - no arguments: get the connection instance for the "default" configuration
     *  - string: get the connection instance for the specified configuration name
     *  - configuration options array: set the configuration for the "default" configuration, and get the "default" connection instance
     *  - string and conf opts array: set the configuration for the specified connection, and get that connection instance
     *
     * @param string|array|null $conn_name configuration name, or connection options array for "default" connection, or null to get an instance of the default connectin
     * @param array|null $config connection options array to set if configuration name was specified, or null to get and instance of the specified connection
     * @return DB instance of this class corresponding to the connection
     */
    public static function instance($conn_name = null, $config = null) {
        if ($conn_name === null) {
            $conn_name = 'default';
        } else if (is_array($conn_name)) {
            $config = $conn_name;
            $conn_name = 'default';
        } else {
            $conn_name = strval($conn_name);
        }

        if (!is_array($config) || (isset(static::$config[$conn_name]) && static::$config[$conn_name] == $config)) {
            $config = null;
        }

        if ($config !== null) {
            static::$config[$conn_name] = $config;
            unset(static::$instances[$conn_name]);
        }

        if (isset(static::$instances[$conn_name])) {
            return static::$instances[$conn_name];
        }

        $db = new self($conn_name);
        static::$instances[$conn_name] = $db;
        return $db;
{'fraction_non_alphanumeric': 0.11504424778761062, 'fraction_numerical': 0.004148230088495576, 'mean_word_length': 2.5490677134445536, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 49, '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': '383797', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1957, 'n_words': 694}