Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
MysqlDriverMetas.php
Go to the documentation of this file.
1<?php
2
4
6
16
17 public function __construct($dbInstance) {
18 parent::__construct($dbInstance);
19 $this->operations[DbOperations::CREATE_TABLE]='CREATE TABLE {name} ({fields}) ENGINE=InnoDB DEFAULT CHARSET=utf8';
20 $this->operations[DbOperations::AUTO_INC]='ALTER TABLE {tableName} MODIFY {fieldInfos} AUTO_INCREMENT, AUTO_INCREMENT={value}';
21 }
22
23 public function getTablesName(): array {
24 $query = $this->dbInstance->query ( 'SHOW TABLES' );
25 return $query->fetchAll ( \PDO::FETCH_COLUMN );
26 }
27
28 public function getPrimaryKeys(string $tableName): array {
29 $fieldkeys = array ();
30 $recordset = $this->dbInstance->query ( "SHOW KEYS FROM `{$tableName}` WHERE Key_name = 'PRIMARY'" );
31 $keys = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
32 foreach ( $keys as $key ) {
33 $fieldkeys [] = $key ['Column_name'];
34 }
35 return $fieldkeys;
36 }
37
38 public function getForeignKeys(string $tableName, string $pkName, ?string $dbName = null): array {
39 $recordset = $this->dbInstance->query ( "SELECT *
40 FROM
41 information_schema.KEY_COLUMN_USAGE
42 WHERE
43 REFERENCED_TABLE_NAME = '" . $tableName . "'
44 AND REFERENCED_COLUMN_NAME = '" . $pkName . "'
45 AND TABLE_SCHEMA = '" . $dbName . "';" );
46 return $recordset->fetchAll ( \PDO::FETCH_ASSOC );
47 }
48
49 public function getFieldsInfos(string $tableName): array {
50 $fieldsInfos = array ();
51 $recordset = $this->dbInstance->query ( "SHOW COLUMNS FROM `{$tableName}`" );
52 $fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
53 foreach ( $fields as $field ) {
54 $fieldsInfos [$field ['Field']] = [ "Type" => $field ['Type'],"Nullable" => $field ["Null"] ];
55 }
56 return $fieldsInfos;
57 }
58
59 public function getRowNum(string $tableName, string $pkName, string $condition): int {
60 $query = $this->dbInstance->query ( "SELECT num FROM (SELECT *, @rownum:=@rownum + 1 AS num FROM `{$tableName}`, (SELECT @rownum:=0) r ORDER BY {$pkName}) d WHERE " . $condition );
61 if ($query) {
62 return $query->fetchColumn ( 0 );
63 }
64 return 0;
65 }
66
67 public function groupConcat(string $fields, string $separator): string {
68 return "GROUP_CONCAT({$fields} SEPARATOR '{$separator}')";
69 }
70
71 public function setIsolationLevel($isolationLevel) {
72 return $this->dbInstance->exec("SET TRANSACTION ISOLATION LEVEL $isolationLevel");
73 }
74}
Ubiquity\db\providers$DriverMetaDatas This class is part of Ubiquity.
Ubiquity\db\providers\pdo\drivers$MysqlDriverMetas This class is part of Ubiquity.
getForeignKeys(string $tableName, string $pkName, ?string $dbName=null)
Returns the list of foreign keys in a table.
getFieldsInfos(string $tableName)
Returns metadata related to the fields of the specified table.
setIsolationLevel($isolationLevel)
Sets the isolation level for transactions.
getPrimaryKeys(string $tableName)
Returns an array of the primary keys field names.
groupConcat(string $fields, string $separator)
Returns the SQL callback for fields concatenation.
getRowNum(string $tableName, string $pkName, string $condition)
Returns the line number of a data record.
getTablesName()
Returns all table names in the database.