function from($t) { do { $stmt = _ns(array('quote_char' => '', 'name_sep' => '.', 'new_line' => ' ')); $stmt->add_from('foo'); $t->is($stmt->as_sql(), "FROM foo"); } while (false); do { $stmt = _ns(array('quote_char' => '`', 'name_sep' => '.', 'new_line' => ' ')); $stmt->add_from('foo'); $stmt->add_from('bar'); $t->is($stmt->as_sql(), "FROM `foo`, `bar`"); } while (false); do { $stmt = _ns(array('quote_char' => '`', 'name_sep' => '.', 'new_line' => ' ')); $stmt->add_from(array('foo' => 'f')); $stmt->add_from('bar', 'b'); $t->is($stmt->as_sql(), "FROM `foo` `f`, `bar` `b`"); } while (false); do { $stmt = _ns(array('quote_char' => '', 'name_sep' => '.', 'new_line' => ' ')); $stmt->add_from(array('foo', 'f')); $stmt->add_from('bar', 'b'); $t->is($stmt->as_sql(), "FROM foo f, bar b"); } while (false); }
#!/usr/bin/env php <?php require_once dirname(__FILE__) . '/lib/setup.php'; require_once 'SQLMaker/Select.php'; require_once 'SQLMaker/SelectSet.php'; $s1 = _ns()->add_from('table1')->add_select('id')->add_where('foo', 100); $s2 = _ns()->add_from('table2')->add_select('id')->add_where('bar', 200); $s3 = _ns()->add_from('table3')->add_select('id')->add_where('baz', 300); function union($t) { global $s1, $s2, $s3; $set = SQLMakerSelectSet::union($s1, $s2); $t->is($set->as_sql(), 'SELECT id FROM table1 WHERE (foo = ?) UNION SELECT id FROM table2 WHERE (bar = ?)'); $t->is(implode(',', $set->bind()), '100,200'); $set = SQLMakerSelectSet::union($set, $s3); $t->is($set->as_sql(), 'SELECT id FROM table1 WHERE (foo = ?) UNION SELECT id FROM table2 WHERE (bar = ?) UNION SELECT id FROM table3 WHERE (baz = ?)'); $t->is(implode(',', $set->bind()), '100,200,300'); $set = SQLMakerSelectSet::union($s3, SQLMakerSelectSet::union($s1, $s2)); $t->is($set->as_sql(), 'SELECT id FROM table3 WHERE (baz = ?) UNION SELECT id FROM table1 WHERE (foo = ?) UNION SELECT id FROM table2 WHERE (bar = ?)'); $t->is(implode(',', $set->bind()), '300,100,200'); $set = SQLMakerSelectSet::union_all($s1, $s2); $t->is($set->as_sql(), 'SELECT id FROM table1 WHERE (foo = ?) UNION ALL SELECT id FROM table2 WHERE (bar = ?)'); $t->is(implode(',', $set->bind()), '100,200'); $set->add_order_by('id'); $t->is($set->as_sql(), 'SELECT id FROM table1 WHERE (foo = ?) UNION ALL SELECT id FROM table2 WHERE (bar = ?) ORDER BY id'); $t->is(implode(',', $set->bind()), '100,200'); $set = SQLMakerSelectSet::union(SQLMakerSelectSet::union($s3, $s1), $s2); $t->is($set->as_sql(), 'SELECT id FROM table3 WHERE (baz = ?) UNION SELECT id FROM table1 WHERE (foo = ?) UNION SELECT id FROM table2 WHERE (bar = ?)'); $t->is(implode(',', $set->bind()), '300,100,200'); $set = SQLMakerSelectSet::union(SQLMakerSelectSet::union($s1, $s2), SQLMakerSelectSet::union($s2, $s3)); $t->is($set->as_sql(), 'SELECT id FROM table1 WHERE (foo = ?) UNION SELECT id FROM table2 WHERE (bar = ?) UNION SELECT id FROM table2 WHERE (bar = ?) UNION SELECT id FROM table3 WHERE (baz = ?)');
function add_having($t) { $sql = _ns()->add_from('foo')->add_select(array('COUNT(*)' => 'cnt'))->add_having(array('cnt' => 2))->as_sql(); $t->is($sql, 'SELECT COUNT(*) AS cnt FROM foo HAVING (COUNT(*) = ?)'); }