def test_sqlexpr(self): m = MetaData() t = Table( "t", m, Column( "x", Integer, server_default=literal_column("a") + literal_column("b"), ), ) self.assert_compile(CreateTable(t), "CREATE TABLE t (x INTEGER DEFAULT a + b)")
def test_literal_column_label_alias_diffname(self): col = sql.literal_column("NEEDS QUOTES").label("NEEDS QUOTES_") self.assert_compile( select([col]).alias().select(), 'SELECT anon_1."NEEDS QUOTES_" FROM (SELECT NEEDS QUOTES AS ' '"NEEDS QUOTES_") AS anon_1', )
def test_literal_column_label_alias_diffname_explcit_quote(self): col = sql.literal_column("NEEDS QUOTES").label( quoted_name("NEEDS QUOTES_", True) ) self.assert_compile( select([col]).alias().select(), 'SELECT anon_1."NEEDS QUOTES_" FROM ' '(SELECT NEEDS QUOTES AS "NEEDS QUOTES_") AS anon_1', )
def test_literal_column_label_embedded_select_diffname(self): col = sql.literal_column("NEEDS QUOTES").label("NEEDS QUOTES_") # embedded SELECT use case, going away in 1.4 however use a # SelectStatementGrouping here when that merges self.assert_compile( select([col]).select(), 'SELECT "NEEDS QUOTES_" FROM (SELECT NEEDS QUOTES AS ' '"NEEDS QUOTES_")', )
def test_literal_column_label_embedded_select_samename_explcit_quote(self): col = sql.literal_column("NEEDS QUOTES").label( quoted_name("NEEDS QUOTES", True) ) # embedded SELECT use case, going away in 1.4 however use a # SelectStatementGrouping here when that merges self.assert_compile( select([col]).select(), 'SELECT "NEEDS QUOTES" FROM ' '(SELECT NEEDS QUOTES AS "NEEDS QUOTES")', )
def test_literal_column_wo_label_currently_maintained(self): # test related to [ticket:4730] where we are maintaining that # literal_column() proxied outwards *without* a label is maintained # as is; in most cases literal_column would need proxying however # at least if the column is being used to generate quoting in some # way, it's maintined as given col = sql.literal_column('"NEEDS QUOTES"') self.assert_compile( select([col]).alias().select(), 'SELECT anon_1."NEEDS QUOTES" FROM ' '(SELECT "NEEDS QUOTES") AS anon_1', )
def test_literal_column_already_with_quotes(self): # Lower case names metadata = MetaData() table = Table("t1", metadata, Column("col1", Integer)) # Note that 'col1' is already quoted (literal_column) columns = [sql.literal_column("'col1'").label("label1")] x = select(columns, from_obj=[table]).alias("alias1") x = x.select() self.assert_compile( x, "SELECT " "alias1.label1 " "FROM (" "SELECT " "'col1' AS label1 " "FROM t1" ") AS alias1", ) # Not lower case names metadata = MetaData() table = Table("T1", metadata, Column("Col1", Integer)) # Note that 'Col1' is already quoted (literal_column) columns = [sql.literal_column("'Col1'").label("Label1")] x = select(columns, from_obj=[table]).alias("Alias1") x = x.select() self.assert_compile( x, "SELECT " '"Alias1"."Label1" ' "FROM (" "SELECT " "'Col1' AS \"Label1\" " 'FROM "T1"' ') AS "Alias1"', )
def test_literal_column_default_no_label(self): self._run_test(default=literal_column("1", type_=self.MyInteger))