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