예제 #1
0
 /**
  * Initialize migrations log storage
  *
  * @param array $options Applications options
  * @throws DbException
  */
 private static function connectionSetup($options)
 {
     if (self::$_storage) {
         return;
     }
     if (isset($options['migrationsInDb']) && (bool) $options['migrationsInDb']) {
         /** @var Config $database */
         $database = $options['config']['database'];
         if (!isset($database->adapter)) {
             throw new DbException('Unspecified database Adapter in your configuration!');
         }
         $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $database->adapter;
         if (!class_exists($adapter)) {
             throw new DbException('Invalid database Adapter!');
         }
         $configArray = $database->toArray();
         unset($configArray['adapter']);
         self::$_storage = new $adapter($configArray);
         if ($database->adapter === 'Mysql') {
             self::$_storage->query('SET FOREIGN_KEY_CHECKS=0');
         }
         if (!self::$_storage->tableExists(self::MIGRATION_LOG_TABLE)) {
             self::$_storage->createTable(self::MIGRATION_LOG_TABLE, null, ['columns' => [new Column('version', ['type' => Column::TYPE_VARCHAR, 'size' => 255, 'notNull' => true]), new Column('start_time', ['type' => Column::TYPE_TIMESTAMP, 'notNull' => true, 'default' => 'CURRENT_TIMESTAMP']), new Column('end_time', ['type' => 'TIMESTAMP NOT NULL DEFAULT NOW()'])], 'indexes' => [new Index('idx_' . self::MIGRATION_LOG_TABLE . '_version', ['version'])]]);
         }
     } else {
         $path = $options['directory'];
         if (is_file($path . '.phalcon')) {
             unlink($path . '.phalcon');
             mkdir($path . '.phalcon');
             chmod($path . '.phalcon', 0775);
         } elseif (!is_dir($path . '.phalcon')) {
             mkdir($path . '.phalcon');
             chmod($path . '.phalcon', 0775);
         }
         self::$_storage = $path . '.phalcon/migration-version';
         if (!file_exists(self::$_storage)) {
             touch(self::$_storage);
         }
     }
 }
예제 #2
0
 public function query($sql, $bindParams = null, $bindTypes = null)
 {
     if (is_string($sql)) {
         //check sql server keyword
         if (!strpos($sql, '[rowcount]')) {
             $sql = str_replace('rowcount', '[rowcount]', $sql);
             //sql server keywords
         }
         //case 1. select count(query builder)
         $countString = 'SELECT COUNT(*)';
         if (strpos($sql, $countString)) {
             $sql = str_replace('"', '', $sql);
             return parent::query($sql, $bindParams, $bindTypes);
         }
         //case 2. subquery need alais name (model find)
         $countString = 'SELECT COUNT(*) "numrows"';
         if (strpos($sql, $countString) !== false) {
             $sql .= ' dt ';
             //subquery need TOP
             if (strpos($sql, 'TOP') === false) {
                 if (strpos($sql, 'ORDER') !== false) {
                     $offset = count($countString);
                     $pos = strpos($sql, 'SELECT', $offset) + 7;
                     //'SELECT ';
                     $sql = substr($sql, 0, $pos) . 'TOP 100 PERCENT ' . substr($sql, $pos);
                 }
             }
         }
         //sql server(dblib) does not accept " as escaper
         $sql = str_replace('"', '', $sql);
     }
     return parent::query($sql, $bindParams, $bindTypes);
 }