Table   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 13
eloc 26
c 3
b 1
f 0
dl 0
loc 146
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A insert() 0 7 2
A create() 0 3 1
A __construct() 0 4 1
A exists() 0 3 1
A delete() 0 3 1
A rename() 0 7 2
A select() 0 6 1
A drop() 0 3 1
A name() 0 3 1
A update() 0 7 2
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;
20
21
use Kristuff\Patabase\Database;
22
use Kristuff\Patabase\Query;
23
use Kristuff\Patabase\Query\CreateTable;
24
use Kristuff\Patabase\Query\Delete;
25
use Kristuff\Patabase\Query\Insert;
26
use Kristuff\Patabase\Query\Select;
27
use Kristuff\Patabase\Query\Update;
28
29
/**
30
 * Class Table 
31
 */
32
class Table
33
{
34
    /**
35
     * @access protected
36
     * @var Database        $database           The Database parent
37
     */
38
    protected $database;
39
40
    /**
41
     * @access protected
42
     * @var string          $name               The Table Name
43
     */
44
    protected $name;
45
46
    /**
47
     * Constructor
48
     *
49
     * @access public
50
     * @param Database      $database           The Database instance
51
     * @param string        $tableName          The table name
52
     */
53
    public function __construct(Database $database, string $tableName)
54
    {
55
        $this->database = $database;                 
56
        $this->name = $tableName;                 
57
    }
58
59
    /**
60
     * Gets/Returns the table name
61
     *
62
     * @access public
63
     * @return string
64
     */
65
    public function name(): string
66
    {
67
        return $this->name;        
68
    }
69
70
    /**
71
     * Get a new Select query instance
72
     *
73
     * @access public
74
     * @param array|string(s) $columns      (optional) Column name(s), array of columns name / alias
75
     *    
76
     * @return Select
77
     */
78
    public function select(): Select
79
    {
80
        $args = func_get_args();
81
        $query = new Query\Select($this->database->getDriver(), null, $args);
82
        $query->from($this->name);
83
        return $query;
84
    }
85
86
    /**
87
     * Get a new Update query instance
88
     *
89
     * @access public
90
     * @param array    $parameters (optional)     Array of columns name / values
91
     * @return Update
92
     */
93
    public function update(array $parameters = array()): Update
94
    {
95
        $query = new Query\Update($this->database->getDriver(), $this->name);
96
        foreach ($parameters as $key => $val) {
97
            $query->setValue($key,  $val);
98
        }
99
        return $query;
100
    }
101
102
    /**
103
     * Get a new Delete query instance
104
     *
105
     * @access public
106
     * @return Delete
107
     */
108
    public function delete(): Delete
109
    {
110
        return new Query\Delete($this->database->getDriver(), $this->name);
111
    }
112
    
113
    /**
114
     * Get a new Insert query instance
115
     *
116
     * @access public
117
     * @param array    $parameters (optional)     Array of columns name / values
118
     *
119
     * @return Insert
120
     */
121
    public function insert(array $parameters = array()): Insert
122
    {
123
        $query = new Query\Insert($this->database->getDriver(), $this->name);
124
        foreach ($parameters as $key => $val) {
125
            $query->setValue($key,  $val);
126
        }
127
        return $query;
128
    }
129
130
    /**
131
     * Get a new CreateTable query instance
132
     *
133
     * @access public
134
     * @return CreateTable
135
     */
136
    public function create(): CreateTable
137
    {
138
        return new Query\CreateTable($this->database->getDriver(), $this->name);
139
    }
140
141
    /**
142
     * Checks if the table exists
143
     * 
144
     * @access public
145
     * @return bool     True if the table exists, otherwise false.
146
     */
147
    public function exists(): bool
148
    {
149
        return $this->database->tableExists($this->name);
150
    }
151
    
152
    /**
153
     * Drop the table
154
     *
155
     * @access public
156
     * @return bool     True if the table has been dropped, otherwise false.
157
     */
158
    public function drop(): bool
159
    {
160
        return $this->database->dropTable($this->name);
161
    }
162
 
163
    /**
164
     * Rename the table
165
     *
166
     * @access public
167
     * @param string    $newName        The new table name
168
     *
169
     * @return bool     True if the table has been renamed, otherwise false.
170
     */
171
    public function rename(string $newName): bool
172
    {
173
       $result = $this->database->renameTable($this->name, $newName);
174
       if ($result){
175
            $this->name = $newName;
176
        }
177
        return $result;
178
    }
179
}