/** * Migrates the database. * * @return Schema */ public function migrate() { $diff = Comparator::compareSchemas($this->manager->createSchema(), $this->schema); foreach ($diff->toSaveSql($this->connection->getDatabasePlatform()) as $query) { $this->connection->executeQuery($query); } }
/** * Returns a merge/upsert (i.e. insert or update) SQL query when supported by the database. * * @return string|null The SQL string or null when not supported */ protected function getMergeSql() { $platform = $this->connection->getDatabasePlatform(); if ($platform instanceof MySqlPlatform) { return "INSERT INTO {$this->table} (id, data, time) VALUES (:id, :data, :time) " . "ON DUPLICATE KEY UPDATE data = VALUES(data), time = CASE WHEN time = :time THEN (VALUES(time) + INTERVAL 1 SECOND) ELSE VALUES(time) END"; } elseif ($platform instanceof SqlitePlatform) { return "INSERT OR REPLACE INTO {$this->table} (id, data, time) VALUES (:id, :data, :time)"; } }
/** * Creates the "select" SQL string from the query parts. * * @return string */ protected function getSQLForSelect() { extract($this->parts); $query = sprintf('SELECT %s FROM ' . $from, $select ? implode(', ', $select) : '*'); foreach ($join as $j) { $query .= sprintf(' %s JOIN %s ON %s', strtoupper($j['type']), $j['table'], (string) $j['condition']); } if ($where) { $query .= ' WHERE ' . $where; } if ($group) { $query .= ' GROUP BY ' . implode(', ', $group); } if ($having) { $query .= ' HAVING ' . $having; } if ($order) { $query .= ' ORDER BY ' . implode(', ', $order); } return $limit === null && $offset === null ? $query : $this->connection->getDatabasePlatform()->modifyLimitQuery($query, $limit, $offset); }