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
5 changes: 3 additions & 2 deletions docs/tableclass.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,11 @@ Parameters:
- operation type - `add` or `drop`
- name of the attribute
- type of the attribute (only for `add`)
- options - optional column options (only for `add`), can be a string or an array of strings


```php
$table->alter($operation,$name,$type);
$table->alter($operation,$name,$type,$options);
```


Expand All @@ -474,7 +475,7 @@ $table->keywords($query, $options);
```

```php
$table->alter($operation,$name,$type);
$table->alter($operation,$name,$type,$options);
```


Expand Down
41 changes: 39 additions & 2 deletions docs/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ The `body` parameters:
* `column` - for add, drop operations, the column is an array of
* `name` - column name
* `type` - data type
* `options` - optional, additional column options (only for `add`). Can be a string or an array of strings


Example of adding a column without options:

$params = [
'table' => 'testrt',
'body' => [
Expand All @@ -97,11 +100,45 @@ The `body` parameters:
'name' => 'tag',
'type'=> 'string'
]

]
];
$response = $client->tables()->alter($params);


Example of adding a column with a single option:

$params = [
'table' => 'testrt',
'body' => [
'operation' => 'add',
'column' => [
'name' => 'tag',
'type'=> 'string',
'options' => 'indexed'
]

]
];
$response = $client->tables()->alter($params);

Example of adding a column with multiple options:

$params = [
'table' => 'testrt',
'body' => [
'operation' => 'add',
'column' => [
'name' => 'content',
'type'=> 'text',
'options' => ['indexed', 'stored']
]

]
];
$response = $client->tables()->alter($params);

Example of dropping a column:

$params = [
'table' => 'testrt',
'body' => [
Expand Down
18 changes: 14 additions & 4 deletions src/Manticoresearch/Endpoints/Tables/Alter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ public function setBody($params = null) {
if (isset($this->table)) {
if (isset($params['operation'])) {
if ($params['operation'] === 'add' && isset($params['column'])) {
return parent::setBody(
['query' => 'ALTER TABLE ' . $this->table . ' ADD COLUMN ' .
$params['column']['name'] . ' ' . strtoupper($params['column']['type'])]
);
$columnDef = $params['column']['name'] . ' ' . strtoupper($params['column']['type']);

if (isset($params['column']['options'])) {
$options = $params['column']['options'];
if (is_string($options)) {
$columnDef .= ' ' . $options;
} elseif (is_array($options)) {
$columnDef .= ' ' . implode(' ', $options);
}
}

return parent::setBody(
['query' => 'ALTER TABLE ' . $this->table . ' ADD COLUMN ' . $columnDef]
);
}
if ($params['operation'] === 'drop') {
return parent::setBody(
Expand Down
6 changes: 5 additions & 1 deletion src/Manticoresearch/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function flushramchunk() {
$this->client->tables()->flushramchunk($params);
}

public function alter($operation, $name, ?string $type = null) {
public function alter($operation, $name, ?string $type = null, $options = null) {
if ($operation === 'add') {
$params = [
'table' => $this->table,
Expand All @@ -338,6 +338,10 @@ public function alter($operation, $name, ?string $type = null) {
'column' => ['name' => $name, 'type' => $type],
],
];

if ($options !== null) {
$params['body']['column']['options'] = $options;
}
} elseif ($operation === 'drop') {
$params = [
'table' => $this->table,
Expand Down
45 changes: 45 additions & 0 deletions test/Manticoresearch/Endpoints/Tables/AlterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,51 @@ public function testTableAddColumn() {
$this->assertEquals(array_keys($expectedResponse), array_keys($response));
}

public function testTableAddColumnWithSingleOption() {
$params = [
'table' => 'products',
'body' => [
'operation' => 'add',
'column' => [
'name' => 'content_single',
'type' => 'text',
'options' => 'indexed',
],
],
];
$response = static::$client->tables()->alter($params);
$this->assertEquals(['total' => 0, 'error' => '', 'warning' => ''], $response);

$response = static::$client->tables()->describe(['table' => 'products']);
$this->assertTrue(isset($response['content_single']), 'Column content_single is missing');

$properties = (string)$response['content_single']['Properties'];
$this->assertStringContainsString('indexed', $properties);
}

public function testTableAddColumnWithMultipleOptions() {
$params = [
'table' => 'products',
'body' => [
'operation' => 'add',
'column' => [
'name' => 'content_multiple',
'type' => 'text',
'options' => ['indexed', 'stored'],
],
],
];
$response = static::$client->tables()->alter($params);
$this->assertEquals(['total' => 0, 'error' => '', 'warning' => ''], $response);

$response = static::$client->tables()->describe(['table' => 'products']);
$this->assertTrue(isset($response['content_multiple']), 'Column content_multiple is missing');

$properties = (string)$response['content_multiple']['Properties'];
$this->assertStringContainsString('indexed', $properties);
$this->assertStringContainsString('stored', $properties);
}

public function testSetGetTable() {
$alter = new Alter();
$alter->setTable('testName');
Expand Down
40 changes: 40 additions & 0 deletions test/Manticoresearch/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,46 @@ public function testAlterAdd() {
$this->assertEquals(['Type' => 'string', 'Properties' => ''], $description['example']);
}

public function testAlterAddWithSingleOption() {
$table = $this->getTable();
$response = $table->alter('add', 'content_single', 'text', 'indexed');
$this->assertEquals(['total' => 0, 'error' => '', 'warning' => ''], $response);

$description = $table->describe();
$keys = array_keys($description);
sort($keys);
$this->assertEquals(
[
'content_single',
'gid',
'id',
'label',
'props',
'tags',
'title',
], $keys
);

$properties = (string)$description['content_single']['Properties'];
$this->assertStringContainsString('indexed', $properties);
}

public function testAlterAddWithMultipleOptions() {
$table = $this->getTable();
$response = $table->alter('add', 'content_multiple', 'text', ['indexed', 'stored']);
$this->assertEquals(['total' => 0, 'error' => '', 'warning' => ''], $response);

$description = $table->describe();
$this->assertTrue(
isset($description['content_multiple']),
'Column content_multiple is missing'
);

$properties = (string)$description['content_multiple']['Properties'];
$this->assertStringContainsString('indexed', $properties);
$this->assertStringContainsString('stored', $properties);
}

public function testAlterInvalidOperation() {
$table = $this->getTable();
$this->expectException(RuntimeException::class);
Expand Down
Loading