diff --git a/docs/tableclass.md b/docs/tableclass.md index bc29700..8557453 100755 --- a/docs/tableclass.md +++ b/docs/tableclass.md @@ -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); ``` @@ -474,7 +475,7 @@ $table->keywords($query, $options); ``` ```php -$table->alter($operation,$name,$type); +$table->alter($operation,$name,$type,$options); ``` diff --git a/docs/tables.md b/docs/tables.md index 85c1181..31c8064 100644 --- a/docs/tables.md +++ b/docs/tables.md @@ -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' => [ @@ -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' => [ diff --git a/src/Manticoresearch/Endpoints/Tables/Alter.php b/src/Manticoresearch/Endpoints/Tables/Alter.php index 7e741b2..050e372 100644 --- a/src/Manticoresearch/Endpoints/Tables/Alter.php +++ b/src/Manticoresearch/Endpoints/Tables/Alter.php @@ -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( diff --git a/src/Manticoresearch/Table.php b/src/Manticoresearch/Table.php index 31b1f73..f7c9440 100755 --- a/src/Manticoresearch/Table.php +++ b/src/Manticoresearch/Table.php @@ -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, @@ -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, diff --git a/test/Manticoresearch/Endpoints/Tables/AlterTest.php b/test/Manticoresearch/Endpoints/Tables/AlterTest.php index 9f2d60d..aadcab3 100644 --- a/test/Manticoresearch/Endpoints/Tables/AlterTest.php +++ b/test/Manticoresearch/Endpoints/Tables/AlterTest.php @@ -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'); diff --git a/test/Manticoresearch/TableTest.php b/test/Manticoresearch/TableTest.php index e02c3df..693a49b 100644 --- a/test/Manticoresearch/TableTest.php +++ b/test/Manticoresearch/TableTest.php @@ -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);