protected function db()
 {
     if ($this->db instanceof \Closure) {
         $mapper = $this->db;
         $db = $mapper();
     } elseif (is_scalar($this->db)) {
         $db = DB\Connection::instance($this->db);
     } else {
         $db = $this->db;
     }
     if (!$db instanceof DB\Iface) {
         throw new Exception('invalid db');
     }
     if (!$db->isa('mysql')) {
         throw new Exception('invalid db');
     }
     if (!$db->isa('gaia\\db\\extendediface')) {
         throw new Exception('invalid db');
     }
     if (!$db->isa('Gaia\\DB\\Except')) {
         $db = new DB\Except($db);
     }
     if (DB\Transaction::inProgress()) {
         DB\Transaction::add($db);
     }
     return $db;
 }
Пример #2
0
 /**
  * Utility function used mainly by other functions to derive values, but can be used by
  * the application if you know what you are doing.
  * Returns a count of how many entries are in each shard.
  */
 public function shardSequences()
 {
     $table = $this->table('index');
     $db = $this->db($table);
     if (DB\Transaction::inProgress()) {
         DB\Transaction::add($db);
     }
     $sql = "SELECT `shard`, `sequence` FROM {$table} WHERE `thread` = %s ORDER BY `shard` DESC";
     $rs = $db->execute($sql, $this->thread);
     $result = array();
     while ($row = $rs->fetch()) {
         $result[$row['shard']] = $row['sequence'];
     }
     $rs->free();
     return $result;
 }
Пример #3
0
 public function handle(\Exception $e)
 {
     if (Transaction::inProgress()) {
         Transaction::rollback();
     }
     return $e;
 }
Пример #4
0
 public function query(array $params = array())
 {
     $search = NULL;
     $min = NULL;
     $max = NULL;
     $sort = 'ASC';
     $limit = NULL;
     $result = array();
     if (isset($params['search'])) {
         $search = $params['search'];
     }
     if (isset($params['min'])) {
         $min = $params['min'];
     }
     if (isset($params['max'])) {
         $max = $params['max'];
     }
     if (isset($params['sort'])) {
         $sort = $params['sort'];
     }
     if (isset($params['limit'])) {
         $limit = $params['limit'];
     }
     if ($limit !== NULL) {
         $limit = str_replace(' ', '', $limit);
     }
     $sort = strtoupper($sort);
     if ($sort != 'DESC') {
         $sort = 'ASC';
     }
     $db = $this->db();
     $table = $this->table();
     if (DB\Transaction::inProgress()) {
         DB\Transaction::add($db);
     }
     $where = array();
     if ($search !== NULL) {
         $where[] = $db->prep_args("`stratum` IN( %s )", array($search));
     }
     if ($min !== NULL) {
         $where[] = $db->prep_args("`stratum` >= %i", array($min));
     }
     if ($max !== NULL) {
         $where[] = $db->prep_args("`stratum` <= %i", array($max));
     }
     $where = $where ? 'WHERE ' . implode(' AND ', $where) : '';
     $sql = "SELECT `constraint`, `stratum` FROM `{$table}` {$where} ORDER BY `stratum` {$sort}";
     if ($limit !== NULL && preg_match("#^([0-9]+)((,[0-9]+)?)\$#", $limit)) {
         $sql .= " LIMIT " . $limit;
     }
     //print "\n$sql\n";
     $rs = $db->execute($sql);
     while ($row = $rs->fetch()) {
         $result[$row['constraint']] = $row['stratum'];
     }
     $rs->free();
     return $result;
 }
Пример #5
0
 /**
  * @see Stockpile_Interface::subtract();
  */
 public function subtract($item_id, $quantity = 1, array $data = NULL)
 {
     Transaction::start();
     try {
         if (!$quantity instanceof Stockpile_HybridQuantity) {
             $quantity = $this->get($item_id)->grab($quantity);
         }
         if ($quantity->value() < 1) {
             throw $this->handle(new Stockpile_Exception('cannot subtract: invalid quantity', $quantity));
         }
         if ($quantity->tally() > 0) {
             $tally = $this->core->subtract($item_id, $quantity->tally(), $data);
         } else {
             $tally = $this->core->get($item_id, $with_lock = TRUE);
         }
         if (count($quantity->all()) > 0) {
             $serial = $this->serial->subtract($item_id, $quantity->all(), $data);
         } else {
             $serial = $this->serial->get($item_id);
         }
         if (!Transaction::commit()) {
             throw new Exception('database error');
         }
         return $this->quantity(array('tally' => $tally, 'serial' => $serial));
     } catch (\Exception $e) {
         if (Transaction::inProgress()) {
             Transaction::rollback();
         }
         $e = new Exception('cannot subtract: ' . $e->getMessage(), $e->__toString());
         throw $e;
     }
 }