public static function factory($data = null, $table = null) { if ($data instanceof Pix_Table_Search) { return $data; } $search = new Pix_Table_Search(); $search->_table = $table; return $search->search($data); }
/** * set the order of merged array * * @param $order or $orders for each array * @access public * @return Pix_Array_Merger */ public function order() { $orders = func_get_args(); $rs = clone $this; $rs->_array_orders = array(); foreach ($orders as $order) { $rs->_array_orders[] = Pix_Table_Search::getOrderArray($order); } return $rs; }
public function __construct($conf) { if (!isset($conf['tableClass'])) { throw new Exception('new ResultSet 時必需要指定 tableClass'); } $this->_tableClass = $conf['tableClass']; $this->_table = Pix_Table::getTable($this->_tableClass); if (isset($conf['belongs_row'])) { $this->_belongs_row = $conf['belongs_row']; } $this->_search_object = Pix_Table_Search::factory(); }
public function testFind_by() { $table = Pix_Table::getTable('Pix_Table_TableTest_Table'); $db = $this->getMock('Pix_Table_Db_Adapter_Abstract', array('fetch')); Pix_Table_TableTest_Table::setDb($db); $search = Pix_Table_Search::factory(array('value' => '999'), $table)->limit(1); $db->expects($this->once())->method('fetch')->with($this->isInstanceOf('Pix_Table_TableTest_Table'), $search, '*')->will($this->returnValue(array(array('t1_id' => 8, 'value' => 999)))); $row = Pix_Table_TableTest_Table::find_by_value(999); $this->assertEquals($row->t1_id, 8); $this->assertEquals($row->value, 999); }
public function order($order = null) { $obj = clone $this; $obj->_order = Pix_Table_Search::getOrderArray($order); return $obj; }
/** * testDelete 測試 relation 的 delete = true 要可以 work * * @access public * @return void */ public function testDelete() { $db = $this->getMock('Pix_Table_Db_Adapter_Abstract', array('deleteOne', 'fetchOne', 'fetch')); $row = new Pix_Table_Row(array('tableClass' => 'Pix_Table_TableRelationTest_Table', 'data' => array('t1_id' => 1000, 'value' => 'delete_me'))); $db->expects($this->once())->method('fetchOne')->with($this->isInstanceOf('Pix_Table_TableRelationTest_Table2'), array(1000))->will($this->returnValue(array('t2_id' => 1000, 'value' => 'foo'))); $search = Pix_Table_Search::factory(array('t3_t1id' => 1000), Pix_Table_TableRelationTest_Table3::getTable()); $db->expects($this->once())->method('fetch')->with($this->isInstanceOf('Pix_Table_TableRelationTest_Table3'), $search, '*')->will($this->returnValue(array(array('t3_id' => 9999, 't3_t1id' => 1000, 'value' => 'bar')))); $db->expects($this->exactly(3))->method('deleteOne'); Pix_Table_TableRelationTest_Table::setDb($db); Pix_Table_TableRelationTest_Table2::setDb($db); Pix_Table_TableRelationTest_Table3::setDb($db); $row->delete(); }
/** * _get_clause 從 $search 條件中,回傳 ORDER BY ... LIMIT ... * * @param Pix_Table_Search $search * @access protected * @return string */ protected function _get_clause($search) { $sql = ''; if ($order = $search->order()) { if (is_array($order)) { // 如果指定 before 的話,順序要調過來 if ($search->before()) { $order = Pix_Table_Search::reverseOrder($order); } $order_term = array(); foreach ($order as $column => $way) { $order_term[] = $this->column_quote($column) . ' ' . $way; } $sql .= ' ORDER BY ' . implode(', ', $order_term); } else { $sql .= ' ORDER BY ' . $order; } } $limit = $search->limit(); if (!is_null($limit)) { $offset = $search->offset(); if (!is_null($offset)) { $sql .= ' LIMIT ' . $offset . ', ' . $limit; } else { $sql .= ' LIMIT ' . $limit; } } return $sql; }
/** * @covers Pix_Table_Search::getOrderArray */ public function testGetOrderArray() { $this->assertEquals(Pix_Table_Search::getOrderArray("id"), array('id' => 'asc')); $this->assertEquals(Pix_Table_Search::getOrderArray("id, id2"), array('id' => 'asc', 'id2' => 'asc')); $this->assertEquals(Pix_Table_Search::getOrderArray("id,id2"), array('id' => 'asc', 'id2' => 'asc')); $this->assertEquals(Pix_Table_Search::getOrderArray("id , id2"), array('id' => 'asc', 'id2' => 'asc')); $this->assertEquals(Pix_Table_Search::getOrderArray("id DESC , id2"), array('id' => 'desc', 'id2' => 'asc')); $this->assertEquals(Pix_Table_Search::getOrderArray(array('id', 'id2')), array('id' => 'asc', 'id2' => 'asc')); $this->assertEquals(Pix_Table_Search::getOrderArray(array('id' => 'desc', 'id2' => 'asc')), array('id' => 'desc', 'id2' => 'asc')); }
/** * fetch 取得符合 $search 條件的資料 * * @param Pix_Table $table * @param Pix_Table_Search $search * @param string|array $select_columns * @access public * @return void */ public function fetch($table, $search, $select_columns = '*') { $db = $this->_getDb(); $condictions = $search->getSearchCondictions(); if (count($condictions) == 0) { // 完全沒有條件就是 scan table $options = array(); $options['TableName'] = $table->getTableName(); // 加上指定 column if ('*' != $select_columns) { $options['AttributesToGet'] = $select_columns; } $response = $db->scan($options); } elseif (count($condictions) == 1) { // 只給一個條件的話只能是 HashKey $primary_keys = $table->getPrimaryColumns(); // 只能是 map if ('map' != $condictions[0][0]) { throw new Pix_Table_Exception("不支援的 search 條件"); } // 只能是 Primary Key 的第一個 if ($primary_keys[0] != $condictions[0][1]) { throw new Pix_Table_Exception("不支援的 search 條件"); } $options = array(); $options['TableName'] = $table->getTableName(); $options['HashKeyValue'] = array($this->_getColumnType($table, $primary_keys[0]) => $condictions[0][2]); // 加上 Limit if (!is_null($limit = $search->limit())) { $options['Limit'] = $limit; } // 加上 after or before if ($row = $search->after()) { $options['RangeKeyCondition'] = array('ComparisonOperator' => AmazonDynamoDB::CONDITION_GREATER_THAN, 'AttributeValueList' => array(array($this->_getColumnType($table, $primary_keys[1]) => $row->{$primary_keys[1]}))); } elseif ($row = $search->before()) { $options['RangeKeyCondition'] = array('ComparisonOperator' => AmazonDynamoDB::CONDITION_LESS_THAN, 'AttributeValueList' => array(array($this->_getColumnType($table, $primary_keys[1]) => $row->{$primary_keys[1]}))); $options['ScanIndexForward'] = false; } // 加上指定 column if ('*' != $select_columns) { $options['AttributesToGet'] = $select_columns; } $response = $db->query($options); } else { throw new Pix_Table_Exception("不支援的 search 條件"); } if (200 != $response->status) { throw new Pix_Table_Exception("AmazonDynamoDB: " . $get_response->body->Message); } $ret = array(); foreach ($response->body->Items[0] as $item) { $row = array(); foreach ($table->_columns as $name => $col) { if ($item->{$name}) { $row[$name] = strval($item->{$name}->S); } } $ret[] = $row; } return $ret; }