public static function generateSchemaFiles() { foreach (ActiveRecord::connections() as $connectionName => $cdata) { if (!isset($cdata['username']) || !isset($cdata['password']) || !isset($cdata['database'])) { continue; } else { try { ActiveRecord::setConnection($connectionName); $connection = ActiveRecord::connection(); $dbname = $connection->selectValue("SELECT DATABASE()"); $tables = $connection->selectValues(sprintf('SHOW TABLES FROM `%s`', $dbname)); } catch (Throwable $e) { continue; } catch (\Exception $e) { continue; } if (!$tables) { throw new Exception\RuntimeException(sprintf('Couldn\'t retrieve table information for connection %s', $connectionName)); } foreach ($tables as $table_name) { $class = 'Rails\\ActiveRecord\\Adapter\\' . $connection->adapterName() . '\\Table'; $data = $class::fetchSchema($connection, $table_name); $path = \Rails::root() . '/db/table_schema/' . $connection->name(); if (!is_dir($path)) { mkdir($path, 0777, true); } $file = $path . '/' . $table_name . '.php'; $contents = "<?php\nreturn "; $contents .= var_export($data, true); $contents .= "\n;"; file_put_contents($file, $contents); } } } }
public function __construct(\Rails\ActiveRecord\Connection $connection = null) { if (!$connection) { $connection = ActiveRecord::connection(Rails::env()); } $this->connection = $connection; $this->buildZfAdapter(); }
public function run() { switch ($this->mainArgv) { case 'generate': $gen = new Generators\Generator($this); $gen->parseCmd(); break; case 'assets': $rules = ['assets' => '', 'action' => '']; $opts = new Zend\Console\Getopt($rules); $argv = $opts->getArguments(); if (empty($argv[1])) { $this->terminate("Missing argument 2"); } \Rails::resetConfig('production'); switch ($argv[1]) { case 'compile:all': \Rails::assets()->setConsole($this); \Rails::assets()->compileAll(); break; case strpos($argv[1], 'compile:') === 0: $parts = explode(':', $argv[1]); if (empty($parts[1])) { $this->terminate("Missing asset name to compile"); } \Rails::assets()->setConsole($this); \Rails::assets()->compileFile($parts[1]); break; default: $this->terminate("Unknown action for assets"); break; } break; case 'routes': $routes = $this->createRoutes(); $rules = ['routes' => '', 'f-s' => '']; $opts = new Zend\Console\Getopt($rules); if ($filename = $opts->getOption('f')) { if (true === $filename) { $logFile = \Rails::config()->paths->log->concat('routes.log'); } else { $logFile = \Rails::root() . '/' . $filename; } file_put_contents($logFile, $routes); } $this->write($routes); break; /** * Install database. */ /** * Install database. */ case 'db:create': $m = new \Rails\ActiveRecord\Migration\Migrator(); $m->loadSchema(); break; /** * Run all/pending migrations. * Creates migrations table as well. */ /** * Run all/pending migrations. * Creates migrations table as well. */ case 'db:migrate': $m = new \Rails\ActiveRecord\Migration\Migrator(); $m->run(); break; /** * Runs seeds. */ /** * Runs seeds. */ case 'db:seed': $m = new \Rails\ActiveRecord\Migration\Migrator(); $m->runSeeds(); break; case 'db:schema:dump': $dumper = new \Rails\ActiveRecord\Schema\Dumper(\Rails\ActiveRecord\ActiveRecord::connection()); $dumper->export(\Rails::root() . '/db/schema.sql'); break; } }
private function _get_builder_class() { $cn = $this->model_name; $class = '\\Rails\\ActiveRecord\\Adapter\\'; $class .= ActiveRecord::proper_adapter_name($cn::connection()->adapterName()); $class .= '\\QueryBuilder'; return $class; }
public function __construct() { $this->connection = \Rails\ActiveRecord\ActiveRecord::connection(); }
private function _save_do() { $w = $wd = $q = $d = array(); $dt = static::table()->columnNames(); $indexes = static::table()->indexes() ?: []; foreach (array_keys($this->changedAttributes()) as $attrName) { # Can't update properties that don't have a column in DB, or # PRImary keys, or time columns. if (!in_array($attrName, $dt) || $attrName == 'created_at' || $attrName == 'updated_at' || $attrName == 'created_on' || $attrName == 'updated_on' || in_array($attrName, $indexes)) { continue; } else { $q[] = '`' . $attrName . '` = ?'; $d[] = $this->getAttribute($attrName); } } if (!$q) { # Nothing to update return true; } if ($indexes) { foreach ($indexes as $idx) { $w[] = '`' . $idx . '` = ?'; if ($this->attributeChanged($idx)) { $wd[] = $this->attributeWas($idx); } else { $wd[] = $this->getAttribute($idx); } } } else { foreach ($this->attributes() as $attrName => $value) { $w[] = '`' . $attrName . '` = ?'; if ($this->attributeChanged($attrName)) { $wd[] = $this->attributeWas($attrName); } elseif (($value = $this->getAttribute($attrName)) !== null) { $wd[] = $value; } } } # Update `updated_at|on` if exists. if ($this->_check_time_column('updated_at')) { $q[] = "`updated_at` = ?"; $d[] = $this->updated_at; } elseif ($this->_check_time_column('updated_on')) { $q[] = "`updated_on` = ?"; $d[] = $this->updated_on; } if ($q) { $q = "UPDATE `" . static::tableName() . "` SET " . implode(', ', $q); $w && ($q .= ' WHERE ' . implode(' AND ', $w)); $q .= ' LIMIT 1'; $d = array_merge($d, $wd); array_unshift($d, $q); static::connection()->executeSql($d); if (ActiveRecord::lastError()) { # The error is logged by Connection. return false; } } $this->_update_init_attrs(); return true; }
private function _load_active_record() { $basename = Rails::root() . '/config/database'; $database_file = $basename . '.yml'; $connections = []; /** * It's possible to not to have a database file if no database will be used. */ if (is_file($database_file)) { $connections = Rails\Yaml\Parser::readFile($database_file); } else { $database_file = Rails::root() . '/config/database.php'; if (is_file($database_file)) { $connections = (require $database_file); } } if ($connections) { foreach ($connections as $name => $config) { ActiveRecord::addConnection($config, $name); } # Set environment connection. ActiveRecord::set_environment_connection(Rails::env()); } }
public function selectValue() { $stmt = call_user_func_array([$this, 'executeSql'], func_get_args()); if (ActiveRecord::lastError()) { return false; } if ($data = $stmt->fetch()) { $data = array_shift($data); } return $data; }
protected function get_table_class() { $model = $this->_current_model; $adapter = ActiveRecord::proper_adapter_name($model::connection()->adapterName()); $cn = 'Rails\\ActiveRecord\\Adapter\\' . $adapter . '\\Table'; return $cn; }