/sqlautocode/constants.py

https://code.google.com/p/sqlautocode/ · Python · 98 lines · 81 code · 14 blank · 3 comment · 0 complexity · c6bafed248bd43d091178fb2cfd61b0f MD5 · raw file

  1. # Nice print stuff
  2. TAB = 4*' '
  3. NLTAB = ',\n'+TAB
  4. HEADER = """\
  5. # -*- coding: %(encoding)s -*-
  6. ## File autogenerated by SQLAutoCode
  7. ## see http://code.google.com/p/sqlautocode/
  8. from sqlalchemy import *
  9. %(dialect)s
  10. metadata = MetaData()
  11. """
  12. HEADER_Z3C = """\
  13. # -*- coding: %(encoding)s -*-
  14. ## File autogenerated by SQLAutoCode
  15. ## see http://code.google.com/p/sqlautocode/
  16. ## Export type: z3c.sqlalchemy
  17. from sqlalchemy import *
  18. %(dialect)s
  19. from z3c.sqlalchemy import Model
  20. from z3c.sqlalchemy.mapper import MappedClassBase
  21. def getModel(metadata):
  22. model = Model()
  23. """
  24. PG_IMPORT = """\
  25. try:
  26. from sqlalchemy.dialects.postgresql import *
  27. except ImportError:
  28. from sqlalchemy.databases.postgres import *
  29. """
  30. FOOTER_Z3C = """
  31. return model
  32. """
  33. FOOTER_EXAMPLE = """
  34. # some example usage
  35. if __name__ == '__main__':
  36. db = create_engine(%(url)r)
  37. metadata.bind = db
  38. # fetch first 10 items from %(tablename)s
  39. s = %(tablename)s.select().limit(10)
  40. rs = s.execute()
  41. for row in rs:
  42. print row
  43. """
  44. #I suppose you wondering why here we use list for columns and constraints
  45. #python have a maximum number of arguments set to 255 ;(
  46. TABLE = """Table('%(name)s', metadata,*[%(columns)s,%(constraints)s]%(schema)s)"""
  47. COLUMN = """Column(%(name)r, %(type)s%(constraints)s%(args)s)"""
  48. FOREIGN_KEY = """ForeignKeyConstraint(%(names)s, %(specs)s, name=%(name)s)"""
  49. INDEX = """Index(%(name)s, %(columns)s, unique=%(unique)s)"""
  50. HEADER_DECL = """#autogenerated by sqlautocode
  51. from sqlalchemy import *
  52. from sqlalchemy.ext.declarative import declarative_base
  53. from sqlalchemy.orm import relation
  54. engine = create_engine('%s')
  55. DeclarativeBase = declarative_base()
  56. metadata = DeclarativeBase.metadata
  57. metadata.bind = engine
  58. """
  59. EXAMPLE_DECL = """#example on how to query your Schema
  60. from sqlalchemy.orm import sessionmaker
  61. session = sessionmaker(bind=engine)()
  62. objs = session.query(%s).all()
  63. print 'All %s objects: %%s'%%objs
  64. """
  65. INTERACTIVE = """
  66. print 'Trying to start IPython shell...',
  67. try:
  68. from IPython.Shell import IPShellEmbed
  69. print 'Success! Press <ctrl-d> to exit.'
  70. print 'Available models:%%s'%%%s
  71. print '\\nTry something like: session.query(%s).all()'
  72. ipshell = IPShellEmbed()
  73. ipshell()
  74. except:
  75. 'Failed. please easy_install ipython'
  76. """
  77. CHECK_CONSTRAINT = """CheckConstraint('%(sqltext)s')"""