/** * indicates FROM clause and switches to from context * * @param Charcoal_String $model_name model name for table * @param Charcoal_String $alias alias name for table * * @return Charcoal_FromContext from context */ public function from($model_name, $alias = NULL) { Charcoal_ParamTrait::validateString(1, $model_name); Charcoal_ParamTrait::validateString(2, $alias, TRUE); $query_target = new Charcoal_QueryTarget($model_name); if ($alias) { $query_target->setAlias($alias); } $this->getContext()->setQueryTarget($query_target); return new Charcoal_FromContext($this->getContext()); }
/** * テスト */ public function test($action, $context) { $action = us($action); switch ($action) { case "qt_table_alias": $target = new Charcoal_QueryTarget(s("model_a as a")); //echo "qtlist:" . print_r($qtlist,true) . eol(); $this->assertEquals('model_a', $target->getModelName()); $this->assertEquals('a', $target->getAlias()); return TRUE; case "qt_inner_join": $target = new Charcoal_QueryTarget(s("model_a + model_b")); //echo "qtlist:" . print_r($qtlist,true) . eol(); $this->assertEquals('model_a', $target->getModelName()); $this->assertEquals('', $target->getAlias()); $joins = $target->getJoins(); $join_head = array_shift($joins); $this->assertEquals(Charcoal_EnumSQLJoinType::INNER_JOIN, $join_head->getJoinType()); $this->assertEquals('model_b', $join_head->getModelName()); $this->assertEquals('', $join_head->getAlias()); return TRUE; case "qt_inner_join_alias": $target = new Charcoal_QueryTarget(s("model_a as a + model_b as b")); //echo "qtlist:" . print_r($qtlist,true) . eol(); $this->assertEquals('model_a', $target->getModelName()); $this->assertEquals('a', $target->getAlias()); $joins = $target->getJoins(); $join_head = array_shift($joins); $this->assertEquals(Charcoal_EnumSQLJoinType::INNER_JOIN, $join_head->getJoinType()); $this->assertEquals('model_b', $join_head->getModelName()); $this->assertEquals('b', $join_head->getAlias()); return TRUE; case "qt_left_join": $target = new Charcoal_QueryTarget(s("model_a (+ model_b")); //echo "qtlist:" . print_r($qtlist,true) . eol(); $this->assertEquals('model_a', $target->getModelName()); $this->assertEquals('', $target->getAlias()); $joins = $target->getJoins(); $join_head = array_shift($joins); $this->assertEquals(Charcoal_EnumSQLJoinType::LEFT_JOIN, $join_head->getJoinType()); $this->assertEquals('model_b', $join_head->getModelName()); $this->assertEquals('', $join_head->getAlias()); return TRUE; case "qt_left_join_alias": $target = new Charcoal_QueryTarget(s("model_a as a (+ model_b as b")); //echo "qtlist:" . print_r($qtlist,true) . eol(); $this->assertEquals('model_a', $target->getModelName()); $this->assertEquals('a', $target->getAlias()); $joins = $target->getJoins(); $join_head = array_shift($joins); $this->assertEquals(Charcoal_EnumSQLJoinType::LEFT_JOIN, $join_head->getJoinType()); $this->assertEquals('model_b', $join_head->getModelName()); $this->assertEquals('b', $join_head->getAlias()); return TRUE; case "qt_right_join": $target = new Charcoal_QueryTarget(s("model_a +) model_b")); //echo "qtlist:" . print_r($qtlist,true) . eol(); $this->assertEquals('model_a', $target->getModelName()); $this->assertEquals('', $target->getAlias()); $joins = $target->getJoins(); $join_head = array_shift($joins); $this->assertEquals(Charcoal_EnumSQLJoinType::RIGHT_JOIN, $join_head->getJoinType()); $this->assertEquals('model_b', $join_head->getModelName()); $this->assertEquals('', $join_head->getAlias()); return TRUE; case "qt_right_join_alias": $target = new Charcoal_QueryTarget(s("model_a as a +) model_b as b")); //echo "qtlist:" . print_r($qtlist,true) . eol(); $this->assertEquals('model_a', $target->getModelName()); $this->assertEquals('a', $target->getAlias()); $joins = $target->getJoins(); $join_head = array_shift($joins); $this->assertEquals(Charcoal_EnumSQLJoinType::RIGHT_JOIN, $join_head->getJoinType()); $this->assertEquals('model_b', $join_head->getModelName()); $this->assertEquals('b', $join_head->getAlias()); return TRUE; case "qt_complex_join": $target = new Charcoal_QueryTarget(s("model_a + model_b + model_c")); //echo "qtlist:" . print_r($qtlist,true) . eol(); $this->assertEquals('model_a', $target->getModelName()); $this->assertEquals('', $target->getAlias()); $joins = $target->getJoins(); $this->assertEquals(Charcoal_EnumSQLJoinType::INNER_JOIN, $joins[0]->getJoinType()); $this->assertEquals('model_b', $joins[0]->getModelName()); $this->assertEquals('', $joins[0]->getAlias()); $this->assertEquals(Charcoal_EnumSQLJoinType::INNER_JOIN, $joins[1]->getJoinType()); $this->assertEquals('model_c', $joins[1]->getModelName()); $this->assertEquals('', $joins[1]->getAlias()); return TRUE; case "qt_complex_join_alias": $target = new Charcoal_QueryTarget(s("model_a as a (+ model_b as b +) model_c as c")); //echo "qtlist:" . print_r($qtlist,true) . eol(); $this->assertEquals('model_a', $target->getModelName()); $this->assertEquals('a', $target->getAlias()); $joins = $target->getJoins(); $this->assertEquals(Charcoal_EnumSQLJoinType::LEFT_JOIN, $joins[0]->getJoinType()); $this->assertEquals('model_b', $joins[0]->getModelName()); $this->assertEquals('b', $joins[0]->getAlias()); $this->assertEquals(Charcoal_EnumSQLJoinType::RIGHT_JOIN, $joins[1]->getJoinType()); $this->assertEquals('model_c', $joins[1]->getModelName()); $this->assertEquals('c', $joins[1]->getAlias()); return TRUE; } return FALSE; }