Skip to content

Commit

Permalink
Merge 33c0a53 into e02b70d
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 17, 2020
2 parents e02b70d + 33c0a53 commit daa276a
Show file tree
Hide file tree
Showing 17 changed files with 571 additions and 261 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -31,7 +31,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
"dev-master": "4.0-dev"
}
}
}
18 changes: 18 additions & 0 deletions howto-install-sqlserver.md
@@ -0,0 +1,18 @@
How to install SQL Server
=========================

1. Download & Install SQL server with "basic" option: https://www.microsoft.com/sql-server/sql-server-2019
2. After installation, open console using `sqlcmd -s localhost\SQLEXPRESS -E`
3. Use this commands to enable `sa` account:
```
USE [master]
GO
ALTER LOGIN [sa] WITH PASSWORD = 'your-password', CHECK_POLICY=OFF
GO
ALTER LOGIN [sa] ENABLE
GO
CREATE DATABSE nextras_dbal_test
GO
```
4. Enable username login in registry: in `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQLServer` set `LoginMode` to `2`, where `MSSQL12.SQLEXPRESS` is a name of your db instance.
5. Restart server.
10 changes: 6 additions & 4 deletions src/Platforms/CachedPlatform.php
Expand Up @@ -13,6 +13,8 @@

class CachedPlatform implements IPlatform
{
private const CACHE_VERSION = 'v2';

/** @var IPlatform */
private $platform;

Expand All @@ -35,31 +37,31 @@ public function getName(): string

public function getTables(): array
{
return $this->cache->load('tables', function () {
return $this->cache->load(self::CACHE_VERSION . '.tables', function () {
return $this->platform->getTables();
});
}


public function getColumns(string $table): array
{
return $this->cache->load('columns.' . $table, function () use ($table) {
return $this->cache->load(self::CACHE_VERSION . '.columns.' . $table, function () use ($table) {
return $this->platform->getColumns($table);
});
}


public function getForeignKeys(string $table): array
{
return $this->cache->load('foreign_keys.' . $table, function () use ($table) {
return $this->cache->load(self::CACHE_VERSION . '.foreign_keys.' . $table, function () use ($table) {
return $this->platform->getForeignKeys($table);
});
}


public function getPrimarySequenceName(string $table): ?string
{
return $this->cache->load('sequence.' . $table, function () use ($table) {
return $this->cache->load(self::CACHE_VERSION . '.sequence.' . $table, function () use ($table) {
return [$this->platform->getPrimarySequenceName($table)];
})[0];
}
Expand Down
43 changes: 43 additions & 0 deletions src/Platforms/Data/Column.php
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

/**
* This file is part of the Nextras\Orm library.
* @license MIT
* @link https://github.com/nextras/orm
*/

namespace Nextras\Dbal\Platforms\Data;


class Column
{
/** @var string */
public $name;

/** @var string */
public $type;

/** @var int|null */
public $size;

/** @var string|null */
public $default;

/** @var bool */
public $isPrimary;

/** @var bool */
public $isAutoincrement;

/** @var bool */
public $isUnsigned;

/** @var bool */
public $isNullable;

/**
* @var mixed[]
* @phpstan-var array<string, mixed>
*/
public $meta;
}
43 changes: 43 additions & 0 deletions src/Platforms/Data/ForeignKey.php
@@ -0,0 +1,43 @@
<?php declare(strict_types = 1);

/**
* This file is part of the Nextras\Orm library.
* @license MIT
* @link https://github.com/nextras/orm
*/

namespace Nextras\Dbal\Platforms\Data;


class ForeignKey
{
/** @var string */
public $name;

/** @var string */
public $schema;

/** @var string */
public $column;

/** @var string */
public $refTable;

/** @var string */
public $refTableSchema;

/** @var string */
public $refColumn;


public function getNameFqn(): string
{
return "$this->schema.$this->name";
}


public function getRefTableFqn(): string
{
return "$this->refTableSchema.$this->refTable";
}
}
28 changes: 28 additions & 0 deletions src/Platforms/Data/Table.php
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

/**
* This file is part of the Nextras\Orm library.
* @license MIT
* @link https://github.com/nextras/orm
*/

namespace Nextras\Dbal\Platforms\Data;


class Table
{
/** @var string */
public $name;

/** @var string */
public $schema;

/** @var bool */
public $isView;


public function getNameFqn(): string
{
return "$this->schema.$this->name";
}
}
12 changes: 11 additions & 1 deletion src/Platforms/IPlatform.php
Expand Up @@ -8,6 +8,10 @@

namespace Nextras\Dbal\Platforms;

use Nextras\Dbal\Platforms\Data\Column;
use Nextras\Dbal\Platforms\Data\ForeignKey;
use Nextras\Dbal\Platforms\Data\Table;


interface IPlatform
{
Expand All @@ -22,19 +26,25 @@ public function getName(): string;


/**
* Returns list of tables names indexed by table name.
* Returns list of tables names indexed by FQN table name.
* @return Table[]
* @phpstan-return array<string, Table>
*/
public function getTables(): array;


/**
* Returns list of table columns metadata, indexed by column name.
* @return Column[]
* @phpstan-return array<string, Column>
*/
public function getColumns(string $table): array;


/**
* Returns list of table foreign keys, indexed by column name.
* @return ForeignKey[]
* @phpstan-return array<string, ForeignKey>
*/
public function getForeignKeys(string $table): array;

Expand Down
78 changes: 52 additions & 26 deletions src/Platforms/MySqlPlatform.php
Expand Up @@ -9,6 +9,9 @@
namespace Nextras\Dbal\Platforms;

use Nextras\Dbal\Connection;
use Nextras\Dbal\Platforms\Data\Column;
use Nextras\Dbal\Platforms\Data\ForeignKey;
use Nextras\Dbal\Platforms\Data\Table;


class MySqlPlatform implements IPlatform
Expand All @@ -29,44 +32,60 @@ public function getName(): string
}


/** @inheritDoc */
public function getTables(): array
{
$result = $this->connection->query('
SELECT
TABLE_SCHEMA,
TABLE_NAME,
TABLE_TYPE
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
');

$tables = [];
foreach ($this->connection->query('SHOW FULL TABLES') as $row) {
$row = array_values($row->toArray());
$tables[$row[0]] = [
'name' => $row[0],
'is_view' => isset($row[1]) && $row[1] === 'VIEW',
];
foreach ($result as $row) {
$table = new Table();
$table->name = $row->TABLE_NAME;
$table->schema = $row->TABLE_SCHEMA;
$table->isView = $row->TABLE_TYPE === 'VIEW';

$tables[$table->getNameFqn()] = $table;
}
return $tables;
}


/** @inheritDoc */
public function getColumns(string $table): array
{
$columns = [];
foreach ($this->connection->query('SHOW FULL COLUMNS FROM %table', $table) as $row) {
$type = explode('(', $row->Type);
$columns[$row->Field] = [
'name' => $row->Field,
'type' => strtoupper($type[0]),
'size' => isset($type[1]) ? (int) $type[1] : null,
'default' => $row->Default,
'is_primary' => $row->Key === 'PRI',
'is_autoincrement' => $row->Extra === 'auto_increment',
'is_unsigned' => (bool) strstr($row->Type, 'unsigned'),
'is_nullable' => $row->Null === 'YES',
];

$column = new Column();
$column->name = $row->Field;
$column->type = \strtoupper($type[0]);
$column->size = isset($type[1]) ? (int) $type[1] : null;
$column->default = $row->Default;
$column->isPrimary = $row->Key === 'PRI';
$column->isAutoincrement = $row->Extra === 'auto_increment';
$column->isUnsigned = (bool) \strstr($row->Type, 'unsigned');
$column->isNullable = $row->Null === 'YES';
$column->meta = [];

$columns[$column->name] = $column;
}
return $columns;
}


/** @inheritDoc */
public function getForeignKeys(string $table): array
{
$parts = explode('.', $table);
if (count($parts) === 2) {
$parts = \explode('.', $table);
if (\count($parts) === 2) {
$db = $parts[0];
$table = $parts[1];
} else {
Expand All @@ -75,7 +94,12 @@ public function getForeignKeys(string $table): array

$result = $this->connection->query('
SELECT
CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_SCHEMA
CONSTRAINT_NAME,
CONSTRAINT_SCHEMA,
COLUMN_NAME,
REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME,
REFERENCED_TABLE_SCHEMA
FROM
information_schema.KEY_COLUMN_USAGE
WHERE
Expand All @@ -88,13 +112,15 @@ public function getForeignKeys(string $table): array

$keys = [];
foreach ($result as $row) {
$keys[$row->COLUMN_NAME] = [
'name' => $row->CONSTRAINT_NAME,
'column' => $row->COLUMN_NAME,
'ref_table' => $row->REFERENCED_TABLE_NAME,
'ref_table_fqn' => "$row->REFERENCED_TABLE_SCHEMA.$row->REFERENCED_TABLE_NAME",
'ref_column' => $row->REFERENCED_COLUMN_NAME,
];
$foreignKey = new ForeignKey();
$foreignKey->name = $row->CONSTRAINT_NAME;
$foreignKey->schema = $row->CONSTRAINT_SCHEMA;
$foreignKey->column = $row->COLUMN_NAME;
$foreignKey->refTable = $row->REFERENCED_TABLE_NAME;
$foreignKey->refTableSchema = $row->REFERENCED_TABLE_SCHEMA;
$foreignKey->refColumn = $row->REFERENCED_COLUMN_NAME;

$keys[$foreignKey->column] = $foreignKey;
}
return $keys;
}
Expand Down

0 comments on commit daa276a

Please sign in to comment.