Completed
Push — master ( 1b7d94...99c6f2 )
by Hong
02:08
created

Result   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 19
c 3
b 0
f 1
lcom 1
cbo 1
dl 0
loc 149
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 5 1
A fieldCount() 0 4 1
A rowCount() 0 4 1
A affectedRows() 0 4 1
A close() 0 6 2
A realFetchAll() 0 4 1
A realFetchRow() 0 14 3
A getOneRow() 0 14 4
B bindResult() 0 24 5
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db\Driver\Mysqli;
16
17
use Phossa2\Db\Driver\ResultAbstract;
18
19
/**
20
 * Result
21
 *
22
 * @package Phossa2\Db
23
 * @author  Hong Zhang <[email protected]>
24
 * @see     ResultAbstract
25
 * @version 2.0.0
26
 * @since   2.0.0 added
27
 */
28
class Result extends ResultAbstract
29
{
30
    /**
31
     * the column names
32
     *
33
     * @var    array
34
     * @access protected
35
     */
36
    protected $cols;
37
38
    /**
39
     * the column values
40
     *
41
     * @var    array
42
     * @access protected
43
     */
44
    protected $vals;
45
46
    /**
47
     * @var    \mysqli_stmt
48
     * @access protected
49
     */
50
    protected $statement;
51
52
    /**
53
     * Invoke to set statement
54
     *
55
     * @param  \mysqli_stmt $statement
56
     * @return $this
57
     * @access public
58
     */
59
    public function __invoke(\mysqli_stmt $statement)
60
    {
61
        $this->statement = $statement;
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function fieldCount()/*# : int */
69
    {
70
        return $this->statement->field_count;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function rowCount()/*# : int */
77
    {
78
        return $this->statement->num_rows;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function affectedRows()/*# : int */
85
    {
86
        return $this->statement->affected_rows;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function close()
93
    {
94
        if ($this->statement) {
95
            $this->statement->free_result();
96
        }
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    protected function realFetchAll()/*# : array */
103
    {
104
        return $this->realFetchRow(100000);
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110
    protected function realFetchRow($rowCount)/*# : array */
111
    {
112
        $count = 0;
113
        $result = [];
114
        while ($count++ < $rowCount) {
115
            $row = $this->getOneRow();
116
            if (is_array($row)) {
117
                $result[] = $row;
118
            } else {
119
                break;
120
            }
121
        }
122
        return $result;
123
    }
124
125
    /**
126
     * Get one row of data
127
     *
128
     * @return array|false
129
     * @access protected
130
     */
131
    protected function getOneRow()
132
    {
133
        if ($this->bindResult()) {
134
            if ($this->statement->fetch()) {
135
                $row = [];
136
                foreach($this->cols as $i => $col) {
137
                    $row[$col] = $this->vals[$i];
138
                }
139
                return $row;
140
            }
141
        }
142
143
        return false;
144
    }
145
146
    /**
147
     * Bind result
148
     *
149
     * @return bool
150
     * @access protected
151
     */
152
    protected function bindResult()/*# : bool */
153
    {
154
        if (null === $this->cols) {
155
            $result = $this->statement->result_metadata();
156
            if (false === $result) return false;
157
158
            $this->cols = [];
159
160
            // set column name
161
            foreach ($result->fetch_fields() as $col) {
162
                $this->cols[] = $col->name;
163
            }
164
165
            // bind values
166
            $this->vals = array_fill(0, count($this->cols), null);
167
168
            $refs = [];
169
            foreach ($this->vals as $i => &$f) {
170
                $refs[$i] = &$f;
171
            }
172
            call_user_func_array([$this->statement, 'bind_result'], $refs);
173
        }
174
        return true;
175
    }
176
}
177