1: <?php
2:
3: namespace Alchemy\tests;
4:
5: use Alchemy\dialect\ANSICompiler;
6: use Alchemy\core\schema\Column;
7: use Alchemy\core\schema\Table;
8: use Alchemy\core\query\Join;
9: use Alchemy\core\query\Predicate;
10: use Alchemy\core\query\Scalar;
11: use Alchemy\core\query\Query;
12:
13:
14: class ANSICompilerTest extends BaseTest {
15:
16: public function testBigInt() {
17: $ansi = new ANSICompiler();
18: $col = Column::BigInt(array(20, 'null' => false, 'auto_increment' => false), null, 'Col');
19:
20: $this->assertEquals("Col BIGINT(20) NOT NULL",
21: $ansi->Create_Column($col));
22: }
23:
24:
25: public function testBinary() {
26: $ansi = new ANSICompiler();
27: $col = Column::Binary(array(100, 'null' => false), null, 'Col');
28:
29: $this->assertEquals("Col BINARY(100) NOT NULL", $ansi->Create_Column($col));
30: }
31:
32:
33: public function testPredicate() {
34: $ansi = new ANSICompiler();
35:
36: $exprA = Predicate::lt(new Scalar(3), new Scalar(5));
37: $this->assertEquals(":p0 < :p1", $ansi->compile($exprA));
38:
39: $col = Column::Integer(null, null, 'Col');
40: $exprB = Predicate::isNull($col);
41: $this->assertEquals("NOT (Col IS NULL)", $ansi->compile($exprB->not()));
42:
43: $exprC = Predicate::in($col, new Scalar(3), new Scalar(5));
44: $this->assertEquals("Col IN (:p2, :p3)", $ansi->compile($exprC));
45:
46: $exprD = Predicate::and_($exprA->not(), $exprB, $exprC);
47: $this->assertEquals("NOT ((NOT (:p0 < :p1) AND Col IS NULL AND Col IN (:p2, :p3)))", $ansi->compile($exprD->not()));
48: }
49:
50:
51: public function testBlob() {
52: $ansi = new ANSICompiler();
53: $col = Column::Blob(null, null, 'Col');
54:
55: $this->assertEquals("Col BLOB NOT NULL", $ansi->Create_Column($col));
56: }
57:
58:
59: public function testBool() {
60: $ansi = new ANSICompiler();
61: $col = Column::Bool(array(11, 'null' => false), null, 'Col');
62:
63: $this->assertEquals("Col", $ansi->compile($col));
64: $this->assertEquals("Col BOOL NOT NULL",
65: $ansi->Create_Column($col));
66: }
67:
68:
69: public function testColumnRef() {
70: $ansi = new ANSICompiler();
71: $table = Table::Core('Tbl', array(
72: 'columns' => array('Col' => Column::Bool())));
73: $table = $table->getRef();
74:
75: $this->assertEquals("tb1.Col",
76: $ansi->compile($table->Col, array('alias_tables' => true)));
77: }
78:
79:
80: public function testChar() {
81: $ansi = new ANSICompiler();
82: $col = Column::Char(array(200, 'null' => false), null, 'Col');
83:
84: $this->assertEquals("Col CHAR(200) NOT NULL", $ansi->Create_Column($col));
85: }
86:
87:
88: public function testCreate() {
89: $ansi = new ANSICompiler();
90: $expr = Query::Create(
91: Table::Core('Tbl', array('columns' => array(
92: 'Col' => Column::Integer(array(11, 'null' => false, 'auto_increment' => false)),
93: 'Key' => Column::Foreign(array('self.Col', 'null' => true)) ))));
94:
95: $this->assertEquals(
96: "CREATE TABLE IF NOT EXISTS Tbl (Col INT(11) NOT NULL, Key INT(11) NULL, FOREIGN KEY (Key) REFERENCES Tbl (Col))",
97: $ansi->compile($expr));
98: }
99:
100:
101: public function testDate() {
102: $ansi = new ANSICompiler();
103: $col = Column::Date(array('null' => false), null, 'Col');
104:
105: $this->assertEquals("Col DATE NOT NULL", $ansi->Create_Column($col));
106: }
107:
108:
109: public function testDatetime() {
110: $ansi = new ANSICompiler();
111: $col = Column::Datetime(array('null' => true), null, 'Col');
112:
113: $this->assertEquals("Col DATETIME NULL", $ansi->Create_Column($col));
114: }
115:
116:
117: public function testDecimal() {
118: $ansi = new ANSICompiler();
119: $col = Column::Decimal(array(5, 3), null, 'Col');
120:
121: $this->assertEquals("Col DECIMAL(5, 3) NOT NULL",
122: $ansi->Create_Column($col));
123: }
124:
125:
126: public function testDrop() {
127: $ansi = new ANSICompiler();
128: $expr = Query::Drop(Table::Core('Tbl'));
129:
130: $this->assertEquals("DROP TABLE IF EXISTS Tbl", $ansi->compile($expr));
131: }
132:
133:
134: public function testFloat() {
135: $ansi = new ANSICompiler();
136: $col = Column::Float(array(23, 'null' => false), null, 'Col');
137:
138: $this->assertEquals("Col FLOAT(23) NOT NULL",
139: $ansi->Create_Column($col));
140: }
141:
142:
143: public function testInteger() {
144: $ansi = new ANSICompiler();
145: $col = Column::Integer(array(11, 'null' => false, 'auto_increment' => false), null, 'Col');
146:
147: $this->assertEquals("Col INT(11) NOT NULL",
148: $ansi->Create_Column($col));
149: }
150:
151:
152: public function testJoin() {
153: $ansi = new ANSICompiler();
154: $table = Table::Core('Tbl', array('columns' => array(
155: 'Col' => Column::Bool() )));
156: $table = $table->getRef();
157:
158: $expr = Predicate::lt($table->Col, $table->Col);
159: $join = new Join(Join::LEFT, Join::INNER, $table, $expr);
160:
161: $this->assertEquals("LEFT INNER JOIN Tbl ON Col < Col", $ansi->compile($join));
162: }
163:
164:
165: public function testMediumInt() {
166: $ansi = new ANSICompiler();
167: $col = Column::MediumInt(array(8, 'null' => false, 'auto_increment' => false), null, 'Col');
168:
169: $this->assertEquals("Col MEDIUMINT(8) NOT NULL",
170: $ansi->Create_Column($col));
171: }
172:
173:
174: public function testScalar() {
175: $ansi = new ANSICompiler();
176: $scalar = new Scalar(3);
177:
178: $this->assertEquals(":p0", $ansi->compile($scalar));
179: }
180:
181:
182: public function testSmallInt() {
183: $ansi = new ANSICompiler();
184: $col = Column::SmallInt(array(6, 'null' => false, 'auto_increment' => false), null, 'Col');
185:
186: $this->assertEquals("Col SMALLINT(6) NOT NULL",
187: $ansi->Create_Column($col));
188: }
189:
190:
191: public function testString() {
192: $ansi = new ANSICompiler();
193: $col = Column::String(array(200, 'null' => false), null, 'Col');
194:
195: $this->assertEquals("Col VARCHAR(200) NOT NULL", $ansi->Create_Column($col));
196: }
197:
198:
199: public function testTableRef() {
200: $ansi = new ANSICompiler();
201: $table = Table::Core('Tbl');
202: $table = $table->getRef();
203:
204: $this->assertEquals("Tbl", $table->name());
205: $this->assertEquals("Tbl tb1", $ansi->compile($table, array('alias_tables' => true)));
206: }
207:
208:
209: public function testTime() {
210: $ansi = new ANSICompiler();
211: $col = Column::Time(array('null' => false), null, 'Col');
212:
213: $this->assertEquals("Col TIME NOT NULL", $ansi->Create_Column($col));
214: }
215:
216:
217: public function testTimestamp() {
218: $ansi = new ANSICompiler();
219: $col = Column::Timestamp(array('null' => true), null, 'Col');
220:
221: $this->assertEquals("Col TIMESTAMP NULL", $ansi->Create_Column($col));
222: }
223:
224:
225: public function testTinyInt() {
226: $ansi = new ANSICompiler();
227: $col = Column::TinyInt(array(4, 'null' => false, 'auto_increment' => false), null, 'Col');
228:
229: $this->assertEquals("Col TINYINT(4) NOT NULL",
230: $ansi->Create_Column($col));
231: }
232: }
233: