Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ RUN pecl_mt_install() { \
&& apk add --no-cache bash gmp libxml2 libstdc++ \
&& apk add --no-cache --virtual=.build-deps autoconf curl-dev gcc gmp-dev g++ libxml2-dev linux-headers make pcre-dev tzdata \
&& docker-php-ext-install -j$(nproc) bcmath gmp \
&& pecl_mt_install protobuf \
&& pecl_mt_install protobuf-4.30.2 \
&& pecl_mt_install grpc \
&& pecl_mt_install pcov \
&& docker-php-ext-enable grpc opcache protobuf \
Expand Down
27 changes: 11 additions & 16 deletions src/Query/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,25 @@ public function processTables($results)
* Process the results of a columns query.
*
* {@inheritDoc}
* @param array<array-key, array<array-key, mixed>> $results
* @return array<array-key, array{
* name: string,
* type_name: string,
* type: string,
* collation: null,
* nullable: bool,
* default: scalar,
* auto_increment: false,
* comment: null
* }>
* @param list<array<string, mixed>> $results
* @return list<array{name: string, type: string, type_name: string, nullable: bool, collation: null, default: mixed, auto_increment: false, comment: null, generation: null}>
*/
public function processColumns($results)
{
return array_map(static function (array $result) {
$result = (object) $result;

return [
'name' => $result['COLUMN_NAME'],
'type_name' => preg_replace("/\([^)]+\)/", "", $result['SPANNER_TYPE']),
'type' => $result['SPANNER_TYPE'],
'name' => $result->name,
'type_name' => (string) preg_replace("/\([^)]+\)/", "", $result->type),
'type' => $result->type,
'collation' => null,
'nullable' => $result['IS_NULLABLE'] !== 'NO',
'default' => $result['COLUMN_DEFAULT'],
'nullable' => $result->nullable === 'YES',
'default' => $result->default,
// TODO check IS_IDENTITY and set auto_increment accordingly
'auto_increment' => false,
'comment' => null,
'generation' => null,
];
}, $results);
}
Expand Down
35 changes: 21 additions & 14 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ public function compileTables()
]);
}

/**
* Compile the query to determine the columns.
*
* @param string $table
* @return string
*/
public function compileColumns($table)
{
return implode(' ', [
'select',
implode(', ', [
'column_name as `name`',
'spanner_type as `type`',
'is_nullable as `nullable`',
'column_default as `default`',
]),
'from information_schema.columns',
'where table_name = ' . $this->quoteString($table),
]);
}

/**
* Compile the query to determine the list of indexes.
*
Expand Down Expand Up @@ -111,20 +132,6 @@ public function compileForeignKeys($table)
]);
}

/**
* Compile the query to determine the columns.
*
* @param string $table
* @return string
*/
public function compileColumns($table)
{
return sprintf(
'select * from information_schema.columns where table_schema = \'\' and table_name = %s',
$this->quoteString($table),
);
}

/**
* Compile a create table command.
*
Expand Down
33 changes: 28 additions & 5 deletions tests/Schema/BuilderTestLast.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,26 +226,49 @@ public function test_getTables(): void
], $row);
}

public function test_getColumns(): void
public function test_getColumns_with_nullable(): void
{
$conn = $this->getDefaultConnection();
$sb = $conn->getSchemaBuilder();
$table = $this->generateTableName(class_basename(__CLASS__));

$sb->create($table, function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
$table->integer('id')->nullable()->primary();
});

$this->assertSame([
'name' => 'id',
'type_name' => 'INT64',
'type' => 'INT64',
'collation' => null,
'nullable' => true,
'default' => null,
'auto_increment' => false,
'comment' => null,
'generation' => null,
], Arr::first($sb->getColumns($table)));
}

public function test_getColumns_with_default(): void
{
$conn = $this->getDefaultConnection();
$sb = $conn->getSchemaBuilder();
$table = $this->generateTableName(class_basename(__CLASS__));

$sb->create($table, function (Blueprint $table) {
$table->string('id', 1)->default('a')->primary();
});

$this->assertSame([
'name' => 'id',
'type_name' => 'STRING',
'type' => 'STRING(36)',
'type' => 'STRING(1)',
'collation' => null,
'nullable' => false,
'default' => null,
'default' => '"a"',
'auto_increment' => false,
'comment' => null,
'generation' => null,
], Arr::first($sb->getColumns($table)));
}

Expand Down