Skip to content

Commit

Permalink
Add rest of the functions to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
back-2-95 committed Aug 22, 2022
1 parent 72acc60 commit bd3d8c4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/TypeAdapter/TypeAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@ public function addDropDatabase(string $databaseName): string;
public function addDropTrigger(string $triggerName): string;
public function backupParameters(): string;
public function commitTransaction(): string;
public function createEvent(array $row): string;
public function createFunction(array $row): string;
public function createProcedure(array $row): string;
public function createTable(array $row): string;
public function createTrigger(array $row): string;
public function createView(array $row): string;
public function databases(string $databaseName): string;
public function dropTable(string $tableName): string;
public function dropView(string $viewName): string;
public function endAddDisableKeys(string $tableName): string;
public function endAddLockTable(string $tableName): string;
public function endDisableAutocommit(): string;
public function getDatabaseHeader(string $databaseName): string;
public function getVersion(): string;
public function lockTable(string $tableName): string;
public function parseColumnType(array $colType): array;
public function restoreParameters(): string;
public function setupTransaction(): string;
public function showColumns(string $tableName): string;
public function showCreateEvent(string $eventName): string;
public function showCreateFunction(string $functionName): string;
public function showCreateProcedure(string $procedureName): string;
public function showCreateTable(string $tableName): string;
public function showCreateTrigger(string $triggerName): string;
public function showCreateView(string $viewName): string;
Expand All @@ -35,6 +41,7 @@ public function showTriggers(string $databaseName): string;
public function showViews(string $databaseName): string;
public function startAddDisableKeys(string $tableName): string;
public function startAddLockTable(string $tableName): string;
public function startDisableAutocommit(): string;
public function startTransaction(): string;
public function unlockTable(string $tableName): string;
}
53 changes: 29 additions & 24 deletions src/TypeAdapter/TypeAdapterMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TypeAdapterMysql implements TypeAdapterInterface
{
const DEFINER_RE = 'DEFINER=`(?:[^`]|``)*`@`(?:[^`]|``)*`';

protected ?PDO $conn = null;
protected PDO $db;
protected array $dumpSettings = [];

// Numerical Mysql types
Expand All @@ -36,7 +36,7 @@ class TypeAdapterMysql implements TypeAdapterInterface
'binary',
'varbinary',
'bit',
'geometry', /* http://bugs.mysql.com/bug.php?id=43544 */
'geometry', /* https://bugs.mysql.com/bug.php?id=43544 */
'point',
'linestring',
'polygon',
Expand All @@ -47,35 +47,37 @@ class TypeAdapterMysql implements TypeAdapterInterface
]
];

public function __construct(?PDO $conn = null, array $dumpSettings = [])
public function __construct(PDO $conn, array $dumpSettings = [])
{
$this->conn = $conn;
$this->db = $conn;
$this->dumpSettings = $dumpSettings;
$this->init();
}

protected function init()
{
// Execute init commands once connected
foreach ($this->dumpSettings['init_commands'] as $stmt) {
$this->conn->exec($stmt);
$this->db->exec($stmt);
}
}

public function databases(string $databaseName): string
{
$resultSet = $this->conn->query("SHOW VARIABLES LIKE 'character_set_database';");
$characterSet = $resultSet->fetchColumn(1);
$resultSet->closeCursor();
$stmt = $this->db->query("SHOW VARIABLES LIKE 'character_set_database';");
$characterSet = $stmt->fetchColumn(1);
$stmt->closeCursor();

$resultSet = $this->conn->query("SHOW VARIABLES LIKE 'collation_database';");
$collationDb = $resultSet->fetchColumn(1);
$resultSet->closeCursor();
$stmt = $this->db->query("SHOW VARIABLES LIKE 'collation_database';");
$collation = $stmt->fetchColumn(1);
$stmt->closeCursor();

return "CREATE DATABASE /*!32312 IF NOT EXISTS*/ `${databaseName}`" .
" /*!40100 DEFAULT CHARACTER SET ${characterSet} " .
" COLLATE ${collationDb} */;" . PHP_EOL . PHP_EOL .
"USE `${databaseName}`;" . PHP_EOL . PHP_EOL;
return sprintf(
"CREATE DATABASE /*!32312 IF NOT EXISTS*/ `%s`" .
" /*!40100 DEFAULT CHARACTER SET %s " .
" COLLATE %s */;" . PHP_EOL . PHP_EOL .
"USE `%s`;" . PHP_EOL . PHP_EOL,
$databaseName,
$characterSet,
$collation,
$databaseName
);
}

public function showCreateTable(string $tableName): string
Expand Down Expand Up @@ -108,7 +110,10 @@ public function showCreateEvent(string $eventName): string
return "SHOW CREATE EVENT `$eventName`";
}

public function createTable($row): string
/**
* @throws Exception
*/
public function createTable(array $row): string
{
if (!isset($row['Create Table'])) {
throw new Exception("Error getting table code, unknown output");
Expand Down Expand Up @@ -283,7 +288,7 @@ public function createEvent(array $row): string
$ret = "";
if (!isset($row['Create Event'])) {
throw new Exception("Error getting event code, unknown output. ".
"Please check 'http://stackoverflow.com/questions/10853826/mysql-5-5-create-event-gives-syntax-error'");
"Please check 'https://stackoverflow.com/questions/10853826/mysql-5-5-create-event-gives-syntax-error'");
}
$eventName = $row['Event'];
$eventStmt = $row['Create Event'];
Expand Down Expand Up @@ -407,12 +412,12 @@ public function commitTransaction(): string

public function lockTable(string $tableName): string
{
return $this->conn->exec(sprintf("LOCK TABLES `%s` READ LOCAL", $tableName));
return $this->db->exec(sprintf("LOCK TABLES `%s` READ LOCAL", $tableName));
}

public function unlockTable(string $tableName): string
{
return $this->conn->exec("UNLOCK TABLES");
return $this->db->exec("UNLOCK TABLES");
}

public function startAddLockTable(string $tableName): string
Expand Down Expand Up @@ -559,6 +564,6 @@ public function restoreParameters(): string

public function getVersion(): string
{
return $this->conn->getAttribute(PDO::ATTR_SERVER_VERSION);
return $this->db->getAttribute(PDO::ATTR_SERVER_VERSION);
}
}

0 comments on commit bd3d8c4

Please sign in to comment.