Advertisement
Guest User

sqlalchemy startswith

a guest
Apr 3rd, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. """
  4. Illustration of behaviour of startswith on PostgreSQL caused by lack of _ escaping.
  5.  
  6. """
  7.  
  8. DB_URI="postgresql+psycopg2://marcink:qiwicudura@localhost/testdb"
  9.  
  10. import sqlalchemy
  11. from sqlalchemy import orm, schema, sql, types
  12.  
  13. engine = sqlalchemy.create_engine(DB_URI)
  14. metadata = sqlalchemy.MetaData(bind = engine)
  15.  
  16. ######################################################################
  17. # Create test table
  18. ######################################################################
  19.  
  20. test_table = sqlalchemy.Table(
  21.     "test_table", metadata,
  22.     schema.Column('value', types.Unicode(120)))
  23.  
  24. metadata.create_all(checkfirst=True)
  25.  
  26. ######################################################################
  27. # Insert test data
  28. ######################################################################
  29.  
  30. dbconn = engine.connect()
  31.  
  32. # Delete previous data if present
  33. dbconn.execute(
  34.     test_table.delete())
  35.  
  36. dbconn.execute(
  37.     test_table.insert(),
  38.     [
  39.         {'value': u"a/b/c/d"},
  40.         {'value': u"a_b/c/d"},
  41.         {'value': u"a/b_c/d"},
  42.         {'value': u"a_b_c_d"},
  43.         {'value': u"x/y/z"},
  44.         {'value': u"x%y%z"},
  45.     ]
  46. )
  47.  
  48. ######################################################################
  49. # Query
  50. ######################################################################
  51.  
  52. print u"* All items:"
  53. qry = sql.select([test_table])
  54. for row in engine.execute(qry):
  55.     print u"   ", row['value']
  56.  
  57. print u"* Items starting with a/b/c:"
  58. qry = sql.select([test_table]).where(test_table.c.value.startswith(u"a/b/c"))
  59. #print qry
  60. for row in engine.execute(qry):
  61.     print u"   ", row['value']
  62.  
  63. print u"* Items starting with a_b_c:"
  64. qry = sql.select([test_table]).where(test_table.c.value.startswith(u"a_b_c"))
  65. for row in engine.execute(qry):
  66.     print u"   ", row['value']
  67.  
  68. print u"* Items starting with a_b_c (manually escaped):"
  69. qry = sql.select([test_table]).where(test_table.c.value.startswith(u"a\\_b\\_c"))
  70. for row in engine.execute(qry):
  71.     print u"   ", row['value']
  72.  
  73. print u"* Items starting with a_b_c (SA escaped):"
  74. qry = sql.select([test_table]).where(test_table.c.value.startswith(u"a#_b#_c", escape='#'))
  75. for row in engine.execute(qry):
  76.     print u"   ", row['value']
  77.  
  78. print u"* Items starting with x/y:"
  79. qry = sql.select([test_table]).where(test_table.c.value.startswith(u"x/y"))
  80. #print qry
  81. for row in engine.execute(qry):
  82.     print u"   ", row['value']
  83.  
  84. print u"* Items starting with x%y:"
  85. qry = sql.select([test_table]).where(test_table.c.value.startswith(u"x%y"))
  86. for row in engine.execute(qry):
  87.     print u"   ", row['value']
  88.  
  89. print u"* Items starting with x%y (manually escaped):"
  90. qry = sql.select([test_table]).where(test_table.c.value.startswith(u"x\\%y"))
  91. for row in engine.execute(qry):
  92.     print u"   ", row['value']
  93.  
  94. print u"* Items starting with x%y (SA escaped):"
  95. qry = sql.select([test_table]).where(test_table.c.value.startswith(u"x#%y", escape='#'))
  96. for row in engine.execute(qry):
  97.     print u"   ", row['value']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement