method getCreate documentation in bbn\Db\Languages\Pgsql

Return SQL string for table creation.

function(string $table, array $model = NULL) { $st = ''; if (!$model) { $model = $this->modelize($table); } if ($st = $this->getCreateTable($table, $model)) { if (empty($model['keys'])) { return $st; } $lines = X::split($st, PHP_EOL); $end = array_pop($lines); $st = X::join($lines, PHP_EOL); $indexes = []; foreach ($model['keys'] as $name => $key) { $separator = ',' . PHP_EOL . ' '; if ( !empty($key['unique']) && (count($key['columns']) === 1) && isset($model['fields'][$key['columns'][0]]) && isset($model['fields'][$key['columns'][0]]['key']) && $model['fields'][$key['columns'][0]]['key'] === 'PRI' ) { $st .= $separator . 'PRIMARY KEY'; } elseif (!empty($key['unique'])) { $st .= $separator . 'CONSTRAINT ' . $this->escape($name) . ' UNIQUE'; } elseif (!empty($key['ref_table']) && !empty($key['ref_column'])) { continue; } else { // Pgsql does not support creating normal indexes in the create table statement // so will return it as another sql $indexes[$name] = $key; continue; } $st .= ' (' . implode( ',', array_map( function ($a) { return $this->escape($a); }, $key['columns'] ) ) . ')'; } // For avoiding constraint names conflicts $keybase = strtolower(Str::genpwd(8, 4)); $i = 1; foreach ($model['keys'] as $name => $key) { if (!empty($key['ref_table']) && !empty($key['ref_column'])) { $st .= ',' . PHP_EOL . ' ' . 'CONSTRAINT ' . $this->escape($keybase.$i) . ' FOREIGN KEY (' . $this->escape($key['columns'][0]) . ') ' . 'REFERENCES ' . $this->escape($key['ref_table']) . ' (' . $this->escape($key['ref_column']) . ')' . (!empty($key['delete']) ? ' ON DELETE ' . $key['delete'] : '') . (!empty($key['update']) ? ' ON UPDATE ' . $key['update'] : ''); $i++; } } $st .= PHP_EOL . $end; if (!empty($indexes)) { $st .= ';' . PHP_EOL; foreach ($indexes as $name => $index) { $st .= 'CREATE INDEX ' . $this->escape($name) . " ON $table"; $st .= ' (' . implode( ',', array_map( function ($a) { return $this->escape($a); }, $index['columns'] ) ) . ')'; $st .= ";" . PHP_EOL; } } } return $st; }

Return SQL string for table creation. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way

This website uses cookies to ensure you get the best experience on our website.