Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
50 / 50
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractGateway
100.00% covered (success)
100.00%
50 / 50
100.00% covered (success)
100.00%
3 / 3
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTableInfo
100.00% covered (success)
100.00%
48 / 48
100.00% covered (success)
100.00%
1 / 1
14
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Db\Gateway;
15
16use Pop\Db\Db;
17use Pop\Db\Sql;
18use Pop\Db\Adapter\AbstractAdapter;
19
20/**
21 * Db abstract gateway class
22 *
23 * @category   Pop
24 * @package    Pop\Db
25 * @author     Nick Sagona, III <dev@nolainteractive.com>
26 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
27 * @license    http://www.popphp.org/license     New BSD License
28 * @version    6.5.0
29 */
30abstract class AbstractGateway implements GatewayInterface
31{
32
33    /**
34     * Table
35     * @var ?string
36     */
37    protected ?string $table = null;
38
39    /**
40     * Constructor
41     *
42     * Instantiate the AbstractGateway object.
43     *
44     * @param  string $table
45     */
46    public function __construct(string $table)
47    {
48        $this->table = $table;
49    }
50
51    /**
52     * Get the table name
53     *
54     * @return string
55     */
56    public function getTable(): string
57    {
58        return $this->table;
59    }
60
61    /**
62     * Get table info
63     *
64     * @param  ?AbstractAdapter $db
65     * @return array
66     */
67    public function getTableInfo(?AbstractAdapter $db = null): array
68    {
69        if ($db === null) {
70            $db = Db::getDb($this->table);
71        }
72
73        $tables = $db->getTables();
74        $sql    = $db->createSql();
75        $info   = [
76            'tableName' => $this->table,
77            'columns'   => []
78        ];
79
80        if (in_array($this->table, $tables)) {
81            $sqlString = null;
82            $field     = 'column_name';
83            $type      = 'data_type';
84            $nullField = 'is_nullable';
85            $keyField  = 'constraint_type';
86
87            switch ($sql->getDbType()) {
88                case Sql::PGSQL:
89                case Sql::SQLSRV:
90                    $sqlString = 'SELECT * FROM information_schema.columns ' .
91                        'LEFT JOIN information_schema.table_constraints ' .
92                        'ON information_schema.table_constraints.table_name = information_schema.columns.table_name ' .
93                        'WHERE information_schema.columns.table_name = \'' . $this->table . '\'';
94                    break;
95                case Sql::SQLITE:
96                    $sqlString = 'PRAGMA table_info(\'' . $this->table . '\')';
97                    $field     = 'name';
98                    $type      = 'type';
99                    $nullField = 'notnull';
100                    $keyField  = 'pk';
101                    break;
102                default:
103                    $sqlString = 'SHOW COLUMNS FROM `' . $this->table . '`';
104                    $field     = 'Field';
105                    $type      = 'Type';
106                    $nullField = 'Null';
107                    $keyField  = 'Key';
108            }
109
110            $db->query($sqlString);
111
112            while (($row = $db->fetch()) != false) {
113                switch ($sql->getDbType()) {
114                    case Sql::SQLITE:
115                        $nullResult    = !($row[$nullField]);
116                        $primaryResult = ($row[$keyField] == 1);
117                        break;
118                    case Sql::MYSQL:
119                        $nullResult    = (!empty($row[$nullField]) && (strtoupper($row[$nullField]) != 'NO'));
120                        $primaryResult = (!empty($row[$keyField]) && (strtoupper($row[$keyField]) == 'PRI'));
121                        break;
122                    default:
123                        $nullResult    = $row[$nullField];
124                        $primaryResult = (!empty($row[$keyField]) && (strtoupper($row[$keyField]) == 'PRIMARY KEY'));
125
126                }
127
128                $info['columns'][$row[$field]] = [
129                    'type'    => $row[$type],
130                    'primary' => $primaryResult,
131                    'null'    => $nullResult
132                ];
133            }
134        }
135
136        return $info;
137    }
138
139}