| 1 | <?php |
||
| 2 | |||
| 3 | namespace Lagdo\DbAdmin\Driver\MySql\Db; |
||
| 4 | |||
| 5 | use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
||
| 6 | use Lagdo\DbAdmin\Driver\Entity\TableSelectEntity; |
||
| 7 | |||
| 8 | use Lagdo\DbAdmin\Driver\Db\Grammar as AbstractGrammar; |
||
| 9 | |||
| 10 | class Grammar extends AbstractGrammar |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @inheritDoc |
||
| 14 | */ |
||
| 15 | public function escapeId(string $idf) |
||
| 16 | { |
||
| 17 | return "`" . str_replace("`", "``", $idf) . "`"; |
||
| 18 | } |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @inheritDoc |
||
| 22 | */ |
||
| 23 | public function getAutoIncrementModifier() |
||
| 24 | { |
||
| 25 | $autoIncrementIndex = " PRIMARY KEY"; |
||
| 26 | // don't overwrite primary key by auto increment |
||
| 27 | $table = $this->utils->input->getTable(); |
||
| 28 | $fields = $this->utils->input->getFields(); |
||
| 29 | $autoIncrementField = $this->utils->input->getAutoIncrementField(); |
||
| 30 | if ($table != "" && $autoIncrementField) { |
||
| 31 | foreach ($this->driver->indexes($table) as $index) { |
||
| 32 | if (in_array($fields[$autoIncrementField]["orig"], $index->columns, true)) { |
||
| 33 | $autoIncrementIndex = ""; |
||
| 34 | break; |
||
| 35 | } |
||
| 36 | if ($index->type == "PRIMARY") { |
||
| 37 | $autoIncrementIndex = " UNIQUE"; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } |
||
| 41 | return " AUTO_INCREMENT$autoIncrementIndex"; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @inheritDoc |
||
| 46 | */ |
||
| 47 | public function buildSelectQuery(TableSelectEntity $select) |
||
| 48 | { |
||
| 49 | $prefix = ''; |
||
| 50 | if (($select->page) && ($select->limit) && !empty($select->group) && |
||
| 51 | count($select->group) < count($select->fields)) { |
||
| 52 | $prefix = 'SQL_CALC_FOUND_ROWS '; |
||
| 53 | } |
||
| 54 | |||
| 55 | return $prefix . parent::buildSelectQuery($select); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @inheritDoc |
||
| 60 | */ |
||
| 61 | public function getCreateTableQuery(string $table, bool $autoIncrement, string $style) |
||
| 62 | { |
||
| 63 | $query = $this->driver->result("SHOW CREATE TABLE " . $this->escapeTableName($table), 1); |
||
| 64 | if (!$autoIncrement) { |
||
| 65 | $query = preg_replace('~ AUTO_INCREMENT=\d+~', '', $query); //! skip comments |
||
| 66 | } |
||
| 67 | return $query; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritDoc |
||
| 72 | */ |
||
| 73 | public function getTruncateTableQuery(string $table) |
||
| 74 | { |
||
| 75 | return "TRUNCATE " . $this->escapeTableName($table); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @inheritDoc |
||
| 80 | */ |
||
| 81 | public function getUseDatabaseQuery(string $database) |
||
| 82 | { |
||
| 83 | return "USE " . $this->escapeId($database); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritDoc |
||
| 88 | */ |
||
| 89 | public function getCreateTriggerQuery(string $table) |
||
| 90 | { |
||
| 91 | $query = ""; |
||
| 92 | foreach ($this->driver->rows("SHOW TRIGGERS LIKE " . |
||
| 93 | $this->driver->quote(addcslashes($table, "%_\\")), null) as $row) { |
||
|
0 ignored issues
–
show
|
|||
| 94 | $query .= "\nCREATE TRIGGER " . $this->escapeId($row["Trigger"]) . |
||
| 95 | " $row[Timing] $row[Event] ON " . $this->escapeTableName($row["Table"]) . |
||
| 96 | " FOR EACH ROW\n$row[Statement];;\n"; |
||
| 97 | } |
||
| 98 | return $query; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @inheritDoc |
||
| 103 | */ |
||
| 104 | public function convertField(TableFieldEntity $field) |
||
| 105 | { |
||
| 106 | if (preg_match("~binary~", $field->type)) { |
||
| 107 | return "HEX(" . $this->escapeId($field->name) . ")"; |
||
| 108 | } |
||
| 109 | if ($field->type == "bit") { |
||
| 110 | return "BIN(" . $this->escapeId($field->name) . " + 0)"; // + 0 is required outside MySQLnd |
||
| 111 | } |
||
| 112 | if (preg_match("~geometry|point|linestring|polygon~", $field->type)) { |
||
| 113 | return ($this->driver->minVersion(8) ? "ST_" : "") . "AsWKT(" . $this->escapeId($field->name) . ")"; |
||
| 114 | } |
||
| 115 | return ''; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @inheritDoc |
||
| 120 | */ |
||
| 121 | public function unconvertField(TableFieldEntity $field, string $value) |
||
| 122 | { |
||
| 123 | if (preg_match("~binary~", $field->type)) { |
||
| 124 | $value = "UNHEX($value)"; |
||
| 125 | } |
||
| 126 | if ($field->type == "bit") { |
||
| 127 | $value = "CONV($value, 2, 10) + 0"; |
||
| 128 | } |
||
| 129 | if (preg_match("~geometry|point|linestring|polygon~", $field->type)) { |
||
| 130 | $value = ($this->driver->minVersion(8) ? "ST_" : "") . "GeomFromText($value, SRID($field[field]))"; |
||
| 131 | } |
||
| 132 | return $value; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @inheritDoc |
||
| 137 | */ |
||
| 138 | // public function connectionId() |
||
| 139 | // { |
||
| 140 | // return "SELECT CONNECTION_ID()"; |
||
| 141 | // } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @inheritDoc |
||
| 145 | */ |
||
| 146 | protected function queryRegex() |
||
| 147 | { |
||
| 148 | return '\\s*|[\'"`#]|/\*|-- |$'; |
||
| 149 | } |
||
| 150 | } |
||
| 151 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.