Completed
Push — master ( 4f50ab...5be735 )
by Ondřej
03:27
created

pg_async_query_burst.php ➔ process_result()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 53
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 46
nc 6
nop 1
dl 0
loc 53
rs 8.7155
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Test of asynchronous queries, results collected only after sending the whole batch.
4
 */
5
6
namespace Ivory\Sandpit;
7
8
$conn = pg_connect('dbname=ivory_scratch user=ivory password=ivory', PGSQL_CONNECT_FORCE_NEW);
9
if ($conn === false) {
10
    fprintf(STDERR, "Error connecting to the database\n");
11
    exit(1);
12
}
13
14
$queries = [
15
    'START TRANSACTION',
16
    'CREATE TABLE foo (i int)',
17
    'SAVEPOINT abc',
18
    'INSERT INTO foo VALUES (0),(1),(2),(3)',
19
    'DELETE FROM foo WHERE i > 1',
20
    'SAVEPOINT def',
21
    'SELECT *, i % 2 FROM foo WHERE i <= 0',
22
    'TRUNCATE foo',
23
    'COMMIT',
24
    'DROP TABLE foo',
25
];
26
$maxI = 10000;
27
for ($i = 0; $i < $maxI; $i++) {
28
    echo "Starting batch $i\n";
29
30
    if (pg_connection_busy($conn)) {
31
        fprintf(STDERR, "Connection is busy\n");
32
        exit(1);
33
    }
34
35
    foreach ($queries as $sql) {
36
        while (pg_connection_busy($conn));
37
        $sent = pg_send_query_params($conn, $sql, []);
38
        if (!$sent) {
39
            fprintf(STDERR, "Query send failed\n");
40
            exit(1);
41
        }
42
    }
43
44
    while (($res = pg_get_result($conn)) !== false) {
45
        process_result($res);
46
    }
47
48
    echo "---------------------\n";
49
}
50
51
function process_result($res)
52
{
53
    $statCodes = [
54
        PGSQL_EMPTY_QUERY => 'empty query',
55
        PGSQL_COMMAND_OK => 'command ok',
56
        PGSQL_TUPLES_OK => 'tuples ok',
57
        PGSQL_COPY_IN => 'copy in',
58
        PGSQL_COPY_OUT => 'copy out',
59
        PGSQL_BAD_RESPONSE => 'bad response',
60
        PGSQL_NONFATAL_ERROR => 'non-fatal error', // reported as impossible to get this status returned from php pgsql
61
        PGSQL_FATAL_ERROR => 'fatal error',
62
    ];
63
    $statCode = pg_result_status($res);
64
    $statStr = pg_result_status($res, PGSQL_STATUS_STRING);
65
    echo "Result status: $statCode ({$statCodes[$statCode]}); $statStr\n";
66
67
    switch ($statCode){
68
        case PGSQL_TUPLES_OK:
69
            $numRows = pg_num_rows($res);
70
            $numFields = pg_num_fields($res);
71
            echo "The result was fetched, having $numRows rows, each having $numFields fields\n";
72
            break;
73
        case PGSQL_COMMAND_OK:
74
            echo "Command OK\n";
75
            break;
76
        case PGSQL_COPY_IN:
77
            echo "COPY IN started\n";
78
            break;
79
        case PGSQL_COPY_OUT:
80
            echo "COPY OUT started\n";
81
            break;
82
        default:
83
            echo "Error occurred.\n";
84
            $fields = [
85
                'SQL state' => PGSQL_DIAG_SQLSTATE,
86
                'Severity' => PGSQL_DIAG_SEVERITY,
87
                'Message' => PGSQL_DIAG_MESSAGE_PRIMARY,
88
                'Detail' => PGSQL_DIAG_MESSAGE_DETAIL,
89
                'Hint' => PGSQL_DIAG_MESSAGE_HINT,
90
                'Statement position' => PGSQL_DIAG_STATEMENT_POSITION,
91
                'Internal position' => PGSQL_DIAG_INTERNAL_POSITION,
92
                'Internal query' => PGSQL_DIAG_INTERNAL_QUERY,
93
                'Context' => PGSQL_DIAG_CONTEXT,
94
                'Source file' => PGSQL_DIAG_SOURCE_FILE,
95
                'Source line' => PGSQL_DIAG_SOURCE_LINE,
96
                'Source function' => PGSQL_DIAG_SOURCE_FUNCTION,
97
            ];
98
            foreach ($fields as $desc => $field) {
99
                echo "$desc: " . pg_result_error_field($res, $field) . "\n";
100
            }
101
            echo "\n";
102
    }
103
}
104