def test_equal(self): for equal in [Equal(self.table.c1, self.table.c2), self.table.c1 == self.table.c2, ~NotEqual(self.table.c1, self.table.c2)]: self.assertEqual(str(equal), '("c1" = "c2")') self.assertEqual(equal.params, ()) equal = Equal(Literal('foo'), Literal('bar')) self.assertEqual(str(equal), '(%s = %s)') self.assertEqual(equal.params, ('foo', 'bar')) equal = Equal(self.table.c1, Null) self.assertEqual(str(equal), '("c1" IS NULL)') self.assertEqual(equal.params, ()) equal = Equal(Literal('test'), Null) self.assertEqual(str(equal), '(%s IS NULL)') self.assertEqual(equal.params, ('test',)) equal = Equal(Null, self.table.c1) self.assertEqual(str(equal), '("c1" IS NULL)') self.assertEqual(equal.params, ()) equal = Equal(Null, Literal('test')) self.assertEqual(str(equal), '(%s IS NULL)') self.assertEqual(equal.params, ('test',))
def test_not_equal(self): for equal in [NotEqual(self.table.c1, self.table.c2), self.table.c1 != self.table.c2, ~Equal(self.table.c1, self.table.c2)]: self.assertEqual(str(equal), '("c1" != "c2")') self.assertEqual(equal.params, ()) equal = NotEqual(self.table.c1, Null) self.assertEqual(str(equal), '("c1" IS NOT NULL)') self.assertEqual(equal.params, ()) equal = NotEqual(Null, self.table.c1) self.assertEqual(str(equal), '("c1" IS NOT NULL)') self.assertEqual(equal.params, ())
def parse_join(self, fields_join, join_type): table = self.table self.join_path = [] for field_join in fields_join: self.join_path.append(field_join) fk = self.foreign_key(table._name, field_join) table_join = Table(fk['foreign_table_name']) join = Join(self.join_on, table_join, type_=join_type) column = getattr(table, fk['column_name']) fk_col = getattr(join.right, fk['foreign_column_name']) join.condition = Equal(column, fk_col) dotted_path = '.'.join(self.join_path) join = self.get_join(dotted_path) if not join: join = self.join_on.join(table_join, type_=join_type) join.condition = Equal(column, fk_col) self.joins_map[dotted_path] = join self.joins.append(join) table = table_join else: if join not in self.joins: self.joins.append(join) table = join.right
def test_equal(self): equal = Equal(self.table.c1, self.table.c2) self.assertEqual(str(equal), '("c1" = "c2")') self.assertEqual(equal.params, ()) equal = Equal(Literal('foo'), Literal('bar')) self.assertEqual(str(equal), '(%s = %s)') self.assertEqual(equal.params, ('foo', 'bar')) equal = Equal(self.table.c1, None) self.assertEqual(str(equal), '("c1" IS NULL)') self.assertEqual(equal.params, ()) equal = Equal(Literal('test'), None) self.assertEqual(str(equal), '(%s IS NULL)') self.assertEqual(equal.params, ('test', )) equal = Equal(None, self.table.c1) self.assertEqual(str(equal), '("c1" IS NULL)') self.assertEqual(equal.params, ()) equal = Equal(None, Literal('test')) self.assertEqual(str(equal), '(%s IS NULL)') self.assertEqual(equal.params, ('test', ))
def __eq__(self, other): from sql.operators import Equal return Equal(self, other)