示例#1
0
 public function fire()
 {
     $db = new Db(Db::PGSQL, env('DB_DATABASE'), env('DB_USERNAME'), env('DB_PASSWORD'), env('DB_HOST', 'localhost'));
     $args = $this->input->getArguments();
     $email = strtolower(trim($args['email']));
     $password = \Hash::make($args['password']);
     $table = "`{$args['schema']}`.`{$args['table']}`";
     $exists = $db->processRecords($db->query(DbExpr::create("SELECT 1 FROM {$table} WHERE `email`=``{$email}``")), $db::FETCH_VALUE);
     if ($exists > 0) {
         $query = "UPDATE {$table} SET `password`=``{$password}``, `role`=``{$args['role']}`` WHERE `email`=``{$email}``";
     } else {
         $query = "INSERT INTO {$table} (`email`, `password`, `role`) VALUES (``{$email}``,``{$password}``, ``{$args['role']}``)";
     }
     try {
         $result = $db->exec(DbExpr::create($query));
         if ($result > 0) {
             $this->line($exists ? 'Admin updated' : 'Admin created');
         } else {
             $this->line('Fail. DB returned "0 rows updated"');
         }
     } catch (\Exception $exc) {
         $this->line('Fail. DB Exception:');
         $this->line($exc->getMessage());
         $this->line($exc->getTraceAsString());
     }
 }
示例#2
0
 public function fire()
 {
     $driver = config('database.default');
     $db = new Db($driver, config("database.connections.{$driver}.database"), config("database.connections.{$driver}.username"), config("database.connections.{$driver}.password"), config("database.connections.{$driver}.host") ?: 'localhost');
     $args = $this->input->getArguments();
     $emailOrLogin = strtolower(trim($args['email_or_login']));
     $authField = $this->input->getOption('login') ? 'login' : 'email';
     $password = \Hash::make($args['password']);
     $table = "`{$args['schema']}`.`{$args['table']}`";
     $exists = $db::processRecords($db->query(DbExpr::create("SELECT 1 FROM {$table} WHERE `{$authField}`=``{$emailOrLogin}``")), $db::FETCH_VALUE);
     if ($exists > 0) {
         $query = "UPDATE {$table} SET `password`=``{$password}``, `role`=``{$args['role']}`` WHERE `{$authField}`=``{$emailOrLogin}``";
     } else {
         $query = "INSERT INTO {$table} (`{$authField}`, `password`, `role`) VALUES (``{$emailOrLogin}``,``{$password}``, ``{$args['role']}``)";
     }
     try {
         $result = $db->exec(DbExpr::create($query));
         if ($result > 0) {
             $this->line($exists ? 'Admin updated' : 'Admin created');
         } else {
             $this->line('Fail. DB returned "0 rows updated"');
         }
     } catch (\Exception $exc) {
         $this->line('Fail. DB Exception:');
         $this->line($exc->getMessage());
         $this->line($exc->getTraceAsString());
     }
 }
 /**
  * Bootstrap any application services.
  *
  * @return void
  * @throws \DebugBar\DebugBarException
  * @throws \PeskyORM\Exception\DbConnectionConfigException
  */
 public function boot()
 {
     $driver = config('database.default');
     CmfDbModel::setDbConnectionConfig(DbConnectionConfig::create()->setDriver($driver)->setHost(config("database.connections.{$driver}.host"))->setDbName(config("database.connections.{$driver}.database"))->setUserName(config("database.connections.{$driver}.username"))->setPassword(config("database.connections.{$driver}.password")));
     DbColumnConfig::registerType('password', DbColumnConfig::DB_TYPE_VARCHAR, PasswordField::class);
     if (config('app.debug', false) && app()->offsetExists('debugbar') && debugbar()->isEnabled()) {
         $timeCollector = debugbar()->hasCollector('time') ? debugbar()->getCollector('time') : null;
         $pdoCollector = new PDOCollector(null, $timeCollector);
         $pdoCollector->setRenderSqlWithParams(true);
         debugbar()->addCollector($pdoCollector);
         Db::setConnectionWrapper(function (Db $db, \PDO $pdo) {
             $pdoTracer = new PeskyOrmPdoTracer($pdo);
             if (debugbar()->hasCollector('pdo')) {
                 debugbar()->getCollector('pdo')->addConnection($pdoTracer, $db->getDbName());
             }
             return $pdoTracer;
         });
     }
 }
示例#4
0
 public static function getTimezonesList($asOptions = false)
 {
     if (self::$timeZonesList === null) {
         $ds = self::_getDataSource('default');
         $query = $ds->replaceQuotes(DbExpr::create('SELECT * from `pg_timezone_names` ORDER BY `utc_offset` ASC')->get());
         self::$timeZonesList = Db::processRecords($ds->query($query), Db::FETCH_ALL);
     }
     if ($asOptions) {
         if (self::$timeZonesOptions === null) {
             self::$timeZonesOptions = [];
             foreach (self::$timeZonesList as $tzInfo) {
                 $offset = preg_replace('%:\\d\\d$%', '', $tzInfo['utc_offset']);
                 $offsetPrefix = $offset[0] === '-' ? '' : '+';
                 self::$timeZonesOptions[$tzInfo['name']] = "({$offsetPrefix}{$offset}) {$tzInfo['name']}";
             }
         }
         return self::$timeZonesOptions;
     } else {
         return self::$timeZonesList;
     }
 }
示例#5
0
 /**
  * @param string $schema
  * @param string $queryTpl
  * @param array $tables - empty value: just execute $queryTpl
  * @param null|string $testQueryTpl
  * @throws \PeskyORM\Exception\DbException
  */
 public function executeQueryOnSchema($schema, $queryTpl, array $tables = [], $testQueryTpl = null)
 {
     $this->out('DB Schema: ' . $schema);
     $ds = $this->getConnection();
     if (!empty($tables)) {
         foreach ($tables as $tableName) {
             $this->out('Update table: ' . $tableName);
             $query = StringUtils::insert($queryTpl, ['table' => $tableName, 'schema' => $schema]);
             if (!empty($testQueryTpl)) {
                 $test = StringUtils::insert($testQueryTpl, ['table' => $tableName, 'schema' => $schema]);
                 $stmnt = $ds->query(DbExpr::create($test));
                 if ($stmnt) {
                     $exists = Db::processRecords($stmnt, DB::FETCH_VALUE);
                     if (!empty($exists)) {
                         $this->out('- Object already exists');
                     } else {
                         $ds->exec(DbExpr::create($query));
                         $this->out('+ Done');
                     }
                 } else {
                     $this->out('- Failed to test if Object already exists');
                 }
             } else {
                 $ds->exec(DbExpr::create($query));
                 $this->out('+ Done');
             }
         }
     } else {
         $query = $queryTpl;
         if (!empty($testQueryTpl)) {
             $test = StringUtils::insert($testQueryTpl, ['schema' => $schema]);
             $stmnt = $ds->query(DbExpr::create($test));
             if ($stmnt) {
                 $exists = Db::processRecords($stmnt, DB::FETCH_VALUE);
                 if (!empty($exists)) {
                     $this->out('- Object already exists');
                 } else {
                     $ds->exec(DbExpr::create($query));
                     $this->out('+ Done');
                 }
             } else {
                 $this->out('- Failed to test if Object already exists');
             }
         } else {
             $ds->exec(DbExpr::create($query));
             $this->out('+ Done');
         }
     }
     $ds->disconnect();
 }