Ejemplo n.º 1
0
 /**
  * Log de notice
  * @param  string $pathLog caminho do erro
  * @param  string $message mensagem de erro
  * @return boolean
  */
 public static function notice($pathLog, $message)
 {
     self::createLogger();
     self::$logger->addWriter(new \Zend\Log\Writer\Stream($pathLog . DIRECTORY_SEPARATOR . "notice.log"));
     self::$logger->notice($message);
     self::destroyLogger();
     return TRUE;
 }
 public function transactionReceived(string $address, float $amount)
 {
     $this->logger->notice("Received transaction to address {$address}");
     $stmt = $this->connection->prepare('UPDATE addresses SET last_used = NULL WHERE address = :address');
     $stmt->execute([':address' => $address]);
     $addressMaxAge = new \DateTime($this->addressLockTime);
     $stmt = $this->connection->prepare('SELECT id, ordered_genre_id, price FROM orders WHERE address = :address AND ordered > :maxAge AND paid = FALSE');
     $stmt->execute([':address' => $address, ':maxAge' => $addressMaxAge->format("Y-m-d H:i:s")]);
     $result = $stmt->fetch(PDO::FETCH_ASSOC);
     $orderId = $result['id'];
     $orderedGenreId = $result['ordered_genre_id'];
     $price = $result['price'];
     if ($price > $amount) {
         //if paid amount is smaller than ordered price
         return;
     }
     $stmt = $this->connection->prepare('UPDATE orders SET paid = TRUE WHERE address = :address AND ordered > :maxAge AND paid = FALSE');
     $stmt->execute([':address' => $address, ':maxAge' => $addressMaxAge->format("Y-m-d H:i:s")]);
     if ($orderedGenreId != null) {
         file_put_contents($this->currentGenreFile, $orderedGenreId);
     }
     $this->logger->info("{$orderId} has been paid");
     $stmt = $this->connection->prepare('UPDATE queue SET paid = TRUE WHERE order_id = :id');
     $stmt->execute([':id' => $orderId]);
 }
Ejemplo n.º 3
0
 /**
  * Create or update table according to schema
  *
  * @param \Zend\Log\Logger $logger Logger instance
  * @param array $schema Parsed table schema
  * @param \Nada\Database\AbstractDatabase $database Database object
  * @param string[] $obsoleteColumns List of obsolete columns to prune or warn about
  * @param bool $prune Drop obsolete tables/columns
  */
 public static function setSchema($logger, $schema, $database, array $obsoleteColumns = array(), $prune = false)
 {
     $tableName = $schema['name'];
     if (in_array($tableName, $database->getTableNames())) {
         // Table exists
         // Update table and column comments
         $table = $database->getTable($tableName);
         if ($schema['comment'] != $table->getComment()) {
             $table->setComment($schema['comment']);
         }
         $columns = $table->getColumns();
         foreach ($schema['columns'] as $column) {
             if (isset($columns[$column['name']])) {
                 // Column exists. Set comment.
                 $columnObj = $table->getColumn($column['name']);
                 $columnObj->setComment($column['comment']);
                 // Change datatype if different.
                 if ($columnObj->getDatatype() != $column['type'] or $columnObj->getLength() != $column['length']) {
                     $logger->info("Setting column {$tableName}.{$column['name']} type to {$column['type']}({$column['length']})...");
                     $columnObj->setDatatype($column['type'], $column['length']);
                     $logger->info('done.');
                 }
                 // Change constraints if different.
                 if ($columnObj->getNotNull() != $column['notnull']) {
                     $logger->info(($column['notnull'] ? 'Setting' : 'Removing') . " NOT NULL constraint on column {$tableName}.{$column['name']}...");
                     $columnObj->setNotNull($column['notnull']);
                     $logger->info('done.');
                 }
                 // Change default if different.
                 // Since SQL types cannot be completely mapped to PHP
                 // types, a loose comparision is required, but changes
                 // to/from NULL must be taken into account.
                 if ($columnObj->getDefault() === null and $column['default'] !== null or $columnObj->getDefault() !== null and $column['default'] === null or $columnObj->getDefault() != $column['default']) {
                     $logger->info(sprintf("Setting default value of column {$tableName}.{$column['name']} to %s...", $column['default'] === null ? 'NULL' : "'{$column['default']}'"));
                     $columnObj->setDefault($column['default']);
                     $logger->info('done.');
                 }
             } else {
                 $logger->info("Creating column {$tableName}.{$column['name']}...");
                 $table->addColumnObject($database->createColumnFromArray($column));
                 $logger->info('done.');
             }
         }
         // Check for altered PK definition
         $primaryKey = $table->getPrimaryKey();
         if ($primaryKey) {
             foreach ($primaryKey as &$column) {
                 $column = $column->getName();
             }
             unset($column);
         } else {
             $primaryKey = array();
         }
         if ($schema['primary_key'] != $primaryKey) {
             $logger->info(sprintf('Changing PK of %s from (%s) to (%s)...', $tableName, implode(', ', $primaryKey), implode(', ', $schema['primary_key'])));
             $table->setPrimaryKey($schema['primary_key']);
             $logger->info('done.');
         }
     } else {
         // Table does not exist, create it
         $logger->info("Creating table '{$tableName}'...");
         $table = $database->createTable($tableName, $schema['columns'], $schema['primary_key']);
         $table->setComment($schema['comment']);
         if ($database->isMySql()) {
             $table->setEngine($schema['mysql']['engine']);
             $table->setCharset('utf8');
         }
         $logger->info('done.');
     }
     // Create missing indexes. Ignore name for comparision with existing indexes.
     if (isset($schema['indexes'])) {
         foreach ($schema['indexes'] as $index) {
             if (!$table->hasIndex($index['columns'], $index['unique'])) {
                 $logger->info("Creating index '{$index['name']}'...");
                 $table->createIndex($index['name'], $index['columns'], $index['unique']);
                 $logger->info('done.');
             }
         }
     }
     // Detect obsolete columns that are present in the database but not in
     // the current schema.
     foreach ($obsoleteColumns as $column) {
         if ($prune) {
             $logger->notice("Dropping column {$tableName}.{$column}...");
             $table->dropColumn($column);
             $logger->notice('done.');
         } else {
             $logger->warn("Obsolete column {$tableName}.{$column} detected.");
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * Drop a column if it exists
  *
  * @param \Zend\Log\Logger $logger Logger instance
  * @param \Nada\Database\AbstractDatabase $database Database object
  * @param string $column column name
  * @codeCoverageIgnore
  */
 protected function _dropColumnIfExists($logger, $database, $column)
 {
     $tables = $database->getTables();
     if (isset($tables[$this->table])) {
         $table = $tables[$this->table];
         $columns = $table->getColumns();
         if (isset($columns[$column])) {
             $logger->notice("Dropping column {$this->table}.{$column}...");
             $table->dropColumn($column);
             $logger->notice('done.');
         }
     }
 }