/** * {@inheritdoc} * * @param bool $quoted Quote name. */ public function getName($quoted = false) { if (!$quoted) { return parent::getName(); } return $this->driver->identifier(parent::getName()); }
/** * Query query identifier, if identified stated as table - table prefix must be added. * * @param string $key Identifier can include simple column operations and functions, * having "." in it will automatically force table prefix to first * value. * @param bool $table Set to true to let quote method know that identified is related * to table name. * @param bool $forceTable In some cases we have to force prefix. * @return mixed|string */ public function quote($key, $table = false, $forceTable = false) { if ($key instanceof SQLExpression) { return $key->sqlStatement($this); } elseif ($key instanceof SQLFragmentInterface) { return $key->sqlStatement(); } if (preg_match('/ as /i', $key, $matches)) { list($key, $alias) = explode($matches[0], $key); /** * We can't do looped aliases, so let's force table prefix for identifier if we aliasing * table name at this moment. */ $quoted = $this->quote($key, $table, $table) . $matches[0] . $this->driver->identifier($alias); if ($table && strpos($key, '.') === false) { //We have to apply operation post factum to prevent self aliasing (name AS name //when db has prefix, expected: prefix_name as name) $this->aliases[$alias] = $key; } return $quoted; } if (strpos($key, '(') || strpos($key, ' ')) { return preg_replace_callback('/([a-z][0-9_a-z\\.]*\\(?)/i', function ($identifier) use(&$table) { $identifier = $identifier[1]; if (substr($identifier, -1) == '(') { //Function name return $identifier; } if ($table) { $table = false; //Only first table has to be escaped return $this->quote($identifier, true); } return $this->quote($identifier); }, $key); } if (strpos($key, '.') === false) { if ($table && !isset($this->aliases[$key]) || $forceTable) { if (!isset($this->aliases[$this->tablePrefix . $key])) { $this->aliases[$this->tablePrefix . $key] = $key; } $key = $this->tablePrefix . $key; } return $this->driver->identifier($key); } $key = explode('.', $key); //Expecting first element be table name if (!isset($this->aliases[$key[0]])) { $key[0] = $this->tablePrefix . $key[0]; } //No aliases can be collected there $key = array_map([$this->driver, 'identifier'], $key); return join('.', $key); }
/** * {@inheritdoc} * * @return Table[] */ public function getTables() { $result = []; foreach ($this->driver->tableNames() as $table) { if ($this->tablePrefix && strpos($table, $this->tablePrefix) !== 0) { //Logical partitioning continue; } $result[] = $this->table(substr($table, strlen($this->tablePrefix))); } return $result; }
/** * Check if column is enum. * * @param array $schema * @param Driver $tableDriver */ private function resolveEnum(array $schema, $tableDriver) { $query = "SELECT object_definition(o.object_id) AS [definition],\n OBJECT_NAME(o.OBJECT_ID) AS [name]\n FROM sys.objects AS o\n JOIN sys.sysconstraints AS [c]\n ON o.object_id = [c].constid\n WHERE type_desc = 'CHECK_CONSTRAINT' AND parent_object_id = ? AND [c].colid = ?"; $constraints = $tableDriver->query($query, [$schema['object_id'], $schema['column_id']]); foreach ($constraints as $checkConstraint) { $this->enumConstraint = $checkConstraint['name']; $name = preg_quote($this->getName(true)); //We made some assumptions here... if (preg_match_all('/' . $name . '=[\']?([^\']+)[\']?/i', $checkConstraint['definition'], $matches)) { $this->enumValues = $matches[1]; sort($this->enumValues); } } }
/** * Drop column constraint using it's name. * * @param string $constraint */ protected function doConstraintDrop($constraint) { $this->driver->statement("ALTER TABLE {$this->getName(true)} DROP CONSTRAINT " . $this->driver->identifier($constraint)); }
/** * Quote identifier. * * @param string $identifier * @return string */ protected function quote($identifier) { return $this->driver->identifier($identifier); }
/** * {@inheritdoc} */ protected function createPDO() { //Spiral is purely UTF-8 $pdo = parent::createPDO(); $pdo->exec("SET NAMES 'UTF-8'"); return $pdo; }