Issues (17)

lib/Query/InsertBase.php (2 issues)

1
<?php declare(strict_types=1);
2
3
/** 
4
 *  ___      _        _
5
 * | _ \__ _| |_ __ _| |__  __ _ ___ ___
6
 * |  _/ _` |  _/ _` | '_ \/ _` (_-</ -_)
7
 * |_| \__,_|\__\__,_|_.__/\__,_/__/\___|
8
 * 
9
 * This file is part of Kristuff\Patabase.
10
 * (c) Kristuff <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 *
15
 * @version    1.0.1
16
 * @copyright  2017-2022 Christophe Buliard
17
 */
18
19
namespace Kristuff\Patabase\Query;
20
21
use Kristuff\Patabase\Query\QueryBuilder;
22
use Kristuff\Patabase\Driver\DatabaseDriver;
23
24
/**
25
 * Class InsertBase
26
 *
27
 * Abstract base class for Insert or update
28
 */
29
abstract class InsertBase extends QueryBuilder
30
{
31
32
    /**
33
     * Table name
34
     *
35
     * @access private
36
     * @var    string
37
     */
38
    protected $tableName = null;
39
40
    /**
41
     * Constructor
42
     *
43
     * @access public
44
     * @param DatabaseDriver    $driver         The driver instance
45
     * @param string            $tableName      The table name
46
     */
47
    public function __construct(DatabaseDriver $driver, $tableName)
48
    {
49
        parent::__construct($driver);
50
        $this->tableName = $tableName;
51
    }    
52
    
53
    /**
54
     * Get argument name
55
     *
56
     * @access public
57
     * @return string
58
     */
59
    protected function getArgName(string $column): string
60
    {
61
         return '_' . str_replace('.', '_', $column);
62
    }
63
64
    /**
65
     * Prepare the INSERT query
66
     * 
67
     * @access public
68
     * @param string    column names
0 ignored issues
show
The type Kristuff\Patabase\Query\column was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
69
     *
70
     * @return $this
71
     */
72
    public function prepare()
73
    {
74
        // Define column parameters
75
        $columns = func_get_args();
76
        if (!empty($columns)){
77
78
            // clear current parameters
79
            unset($this->parameters);
80
            $this->parameters = array();
81
            foreach ($columns as $column) {
82
                $this->parameters[$column] = null;
83
            }        
84
        }
85
86
        // build query
87
        parent::prepare();       
88
        return $this;
89
    }
90
    
91
    /**
92
     * Bind values parameters
93
     *
94
     * @access public
95
     * @return void
96
     */
97
    public function bindValues(): void
98
    {
99
        foreach ($this->parameters as $key => $val) {
100
            $arg = $this->getArgName($key);
101
            $this->pdoParameters[$arg] = $val;
102
        }
103
        parent::bindValues();
104
    }   
105
106
    /**
107
     * Set a Name/Value Parameter
108
     *
109
     * @access public
110
     * @param string        $columName          The column name
111
     * @param mixed         $value              The column value
112
     *
113
     * @return $this
114
     */
115
    public function setValue(string $columName, $value)
116
    {
117
        $this->parameters[$columName] = $value;
118
        return $this;
119
    }
120
121
    /**
122
     * Set a list of Name/Value Parameters
123
     *
124
     * @access public
125
     * @param array       $values              The key/values array
126
     * 
127
     * @return $this
128
     */
129
    public function values(array $values)
130
    {
131
        foreach ($values as $key => $val) {
132
            $this->SetValue($key, $val);
133
        }
134
        return $this;    
135
    }
136
137
    /**
138
     * Returns the number of rows affected by the last SQL statement
139
     * 
140
     * This function returns false if there is no executed query.
141
     *
142
     * @access public
143
     * @return int|false     The number of affected rows if any, otherwise false.
144
     */
145
    public function lastId()
146
    {
147
        return (empty(!$this->pdoStatement)) ? $this->driver->lastInsertedId() : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return empty(! $this->pd...astInsertedId() : false also could return the type string which is incompatible with the documented return type false|integer.
Loading history...
148
    }
149
    
150
}