DboSource::buildIndex PHP Method

buildIndex() public method

Format indexes for create table.
public buildIndex ( array $indexes, string $table = null ) : array
$indexes array The indexes to build
$table string The table name.
return array
    public function buildIndex($indexes, $table = null)
    {
        $join = array();
        foreach ($indexes as $name => $value) {
            $out = '';
            if ($name === 'PRIMARY') {
                $out .= 'PRIMARY ';
                $name = null;
            } else {
                if (!empty($value['unique'])) {
                    $out .= 'UNIQUE ';
                }
                $name = $this->startQuote . $name . $this->endQuote;
            }
            if (is_array($value['column'])) {
                $out .= 'KEY ' . $name . ' (' . implode(', ', array_map(array(&$this, 'name'), $value['column'])) . ')';
            } else {
                $out .= 'KEY ' . $name . ' (' . $this->name($value['column']) . ')';
            }
            $join[] = $out;
        }
        return $join;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * testBuildIndex method
  *
  * @return void
  */
 public function testBuildIndex()
 {
     $data = array('PRIMARY' => array('column' => 'id'));
     $result = $this->Dbo->buildIndex($data);
     $expected = array('PRIMARY KEY  (`id`)');
     $this->assertSame($expected, $result);
     $data = array('MyIndex' => array('column' => 'id', 'unique' => true));
     $result = $this->Dbo->buildIndex($data);
     $expected = array('UNIQUE KEY `MyIndex` (`id`)');
     $this->assertEquals($expected, $result);
     $data = array('MyIndex' => array('column' => array('id', 'name'), 'unique' => true));
     $result = $this->Dbo->buildIndex($data);
     $expected = array('UNIQUE KEY `MyIndex` (`id`, `name`)');
     $this->assertEquals($expected, $result);
     $data = array('MyFtIndex' => array('column' => array('name', 'description'), 'type' => 'fulltext'));
     $result = $this->Dbo->buildIndex($data);
     $expected = array('FULLTEXT KEY `MyFtIndex` (`name`, `description`)');
     $this->assertEquals($expected, $result);
     $data = array('MyTextIndex' => array('column' => 'text_field', 'length' => array('text_field' => 20)));
     $result = $this->Dbo->buildIndex($data);
     $expected = array('KEY `MyTextIndex` (`text_field`(20))');
     $this->assertEquals($expected, $result);
     $data = array('MyMultiTextIndex' => array('column' => array('text_field1', 'text_field2'), 'length' => array('text_field1' => 20, 'text_field2' => 20)));
     $result = $this->Dbo->buildIndex($data);
     $expected = array('KEY `MyMultiTextIndex` (`text_field1`(20), `text_field2`(20))');
     $this->assertEquals($expected, $result);
 }
All Usage Examples Of DboSource::buildIndex
DboSource