No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlalter.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sqlalchemy as sqla
  4. from sqlalchemy.ext.compiler import compiles
  5. ## @file sqlalter.py
  6. # This file defines all DDL (data definition langage) for the ALTER TABLE instructions
  7. #
  8. # It uses the SqlAlchemy compilation and quoting methos to generate SQL
  9. class AddColumn(sqla.schema.DDLElement):
  10. """ Defines the ddl for adding a column to a table """
  11. def __init__(self,table, column):
  12. """ Instanciate the DDL
  13. @param table sqlalchemy.Table: A sqlalchemy table object
  14. @param column sqlalchemy.Column: A sqlalchemy column object
  15. """
  16. self.col = column
  17. self.table = table
  18. @compiles(AddColumn, 'mysql')
  19. @compiles(AddColumn, 'postgresql')
  20. @compiles(AddColumn, 'sqlite')
  21. def visit_add_column(element, ddlcompiler, **kw):
  22. """ Compiles the AddColumn DDL for mysql, postgresql and sqlite"""
  23. prep = ddlcompiler.sql_compiler.preparer
  24. tname = prep.format_table(element.table)
  25. colname = prep.format_column(element.col)
  26. return 'ALTER TABLE %s ADD COLUMN %s %s'%(tname, colname, element.col.type)
  27. @compiles(AddColumn)
  28. def visit_add_column(element, ddlcompiler, **kw):
  29. raise NotImplementedError('Add column not yet implemented for '+str(ddlcompiler.dialect.name))
  30. class DropColumn(sqla.schema.DDLElement):
  31. """ Defines the DDL for droping a column from a table """
  32. def __init__(self, table, column):
  33. """ Instanciate the DDL
  34. @param table sqlalchemy.Table: A sqlalchemy table object
  35. @param column sqlalchemy.Column: A sqlalchemy column object representing the column to drop
  36. """
  37. self.col = column
  38. self.table = table
  39. @compiles(DropColumn,'mysql')
  40. @compiles(DropColumn, 'postgresql')
  41. def visit_drop_column(element, ddlcompiler, **kw):
  42. """ Compiles the DropColumn DDL for mysql & postgresql """
  43. prep = ddlcompiler.sql_compiler.preparer
  44. tname = prep.format_table(element.table)
  45. colname = prep.format_column(element.col)
  46. return 'ALTER TABLE %s DROP COLUMN %s'%(tname, colname)
  47. @compiles(DropColumn)
  48. def visit_drop_column(element, ddlcompiler, **kw):
  49. raise NotImplementedError('Drop column not yet implemented for '+str(ddlcompiler.dialect.name))
  50. class AlterColumn(sqla.schema.DDLElement):
  51. """ Defines the DDL for changing the type of a column """
  52. def __init__(self, table, column):
  53. """ Instanciate the DDL
  54. @param table sqlalchemy.Table: A sqlalchemy Table object
  55. @param column sqlalchemy.Column: A sqlalchemy Column object representing the new column
  56. """
  57. self.col = column
  58. self.table = table
  59. @compiles(AlterColumn, 'mysql')
  60. def visit_alter_column(element, ddlcompiler, **kw):
  61. """ Compiles the AlterColumn DDL for mysql """
  62. prep = ddlcompiler.sql_compiler.preparer
  63. tname = prep.format_table(element.table)
  64. colname = prep.format_column(element.col)
  65. return 'ALTER TABLE %s ALTER COLUMN %s %s'%(tname, colname, element.col.type)
  66. @compiles(AlterColumn, 'postgresql')
  67. def visit_alter_column(element, ddlcompiler, **kw):
  68. """ Compiles the AlterColumn DDL for postgresql """
  69. prep = ddlcompiler.sql_compiler.preparer
  70. tname = prep.format_table(element.table)
  71. colname = prep.format_column(element.col)
  72. return 'ALTER TABLE %s ALTER COLUMN %s TYPE %s'%(tname, colname, element.col.type)
  73. @compiles(AlterColumn)
  74. def visit_alter_column(element, ddlcompiler, **kw):
  75. raise NotImplementedError('Alter column not yet implemented for '+str(ddlcompiler.dialect.name))