File: config.inc

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (71 lines) | stat: -rw-r--r-- 1,895 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

// bug #72969 reflects a bug with FreeTDS, not with pdo_dblib
// this function will version detect so the relevant tests can XFAILIF
// assume this bug isn't present if not using FreeTDS
// otherwise require FreeTDS>=1.1
function driver_supports_batch_statements_without_select($db) {
    $version = $db->getAttribute(PDO::DBLIB_ATTR_VERSION);
    return !strstartswith($version, 'freetds ') || !strstartswith($version, 'freetds v1.0');
}

function strstartswith($haystack, $needle) {
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
}

function getCredentials() {
    if (false !== getenv('PDO_DBLIB_TEST_DSN')) {
        $dsn = getenv('PDO_DBLIB_TEST_DSN');
    } else {
        $dsn = 'dblib:host=localhost;dbname=test';
    }

    if (false !== getenv('PDO_DBLIB_TEST_USER')) {
        $user = getenv('PDO_DBLIB_TEST_USER');
    } else {
        $user = 'php';
    }

    if (false !== getenv('PDO_DBLIB_TEST_PASS')) {
        $pass = getenv('PDO_DBLIB_TEST_PASS');
    } else {
        $pass = 'password';
    }

    return [$dsn, $user, $pass];
}

function setAttributes(PDO $db) {
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
}

function getDbConnection(string $class = PDO::class, ?array $attributes = null) {
    [$dsn, $user, $pass] = getCredentials();

    try {
        $db = new $class($dsn, $user, $pass, $attributes);
        if ($attributes === null) {
            setAttributes($db);
        }
    } catch (PDOException $e) {
        die('skip ' . $e->getMessage());
    }

    return $db;
}

function connectToDb() {
    [$dsn, $user, $pass] = getCredentials();

    try {
        $db = PDO::connect($dsn, $user, $pass);
        setAttributes($db);
    } catch (PDOException $e) {
        die('skip ' . $e->getMessage());
    }

    return $db;
}

?>