/src/lobos/ast.clj

http://github.com/budu/lobos · Clojure · 195 lines · 131 code · 45 blank · 19 comment · 0 complexity · 7211fefbdfbd65e5dd7b2f1c96b17365 MD5 · raw file

  1. ; Copyright (c) Nicolas Buduroi. All rights reserved.
  2. ; The use and distribution terms for this software are covered by the
  3. ; Eclipse Public License 1.0 which can be found in the file
  4. ; epl-v10.html at the root of this distribution. By using this software
  5. ; in any fashion, you are agreeing to be bound by the terms of this
  6. ; license.
  7. ; You must not remove this notice, or any other, from this software.
  8. (ns lobos.ast
  9. "Abstract SQL syntax tree for the DDL part of the language."
  10. (:use clojure.template)
  11. (:import (java.io Writer)))
  12. ;; -----------------------------------------------------------------------------
  13. ;; ## AST Records
  14. ;; ### Special Records
  15. (defrecord Mode
  16. [db-spec])
  17. ;; ### Expression Records
  18. (defrecord ScalarExpression
  19. [db-spec scalar])
  20. (defrecord IdentifierExpression
  21. [db-spec name qualifiers])
  22. (defrecord FunctionExpression
  23. [db-spec name args])
  24. (defrecord OperatorExpression
  25. [db-spec op left right])
  26. ;; ### Clause Records
  27. (defrecord AutoIncClause
  28. [db-spec])
  29. (defrecord DataTypeClause
  30. [db-spec dtype args options])
  31. ;; ### Definition Records
  32. (defrecord ColumnDefinition
  33. [db-spec cname data-type default auto-inc not-null others])
  34. (defrecord ConstraintDefinition
  35. [db-spec cname])
  36. (defrecord UniqueConstraintDefinition
  37. [db-spec cname ctype columns])
  38. (defrecord ForeignKeyConstraintDefinition
  39. [db-spec cname columns parent-table parent-columns match triggered-actions])
  40. (defrecord CheckConstraintDefinition
  41. [db-spec cname condition])
  42. ;; ### Statement Records
  43. (defrecord CreateSchemaStatement
  44. [db-spec sname elements])
  45. (defrecord CreateTableStatement
  46. [db-spec tname elements])
  47. (defrecord CreateIndexStatement
  48. [db-spec iname tname columns options])
  49. (defrecord DropStatement
  50. [db-spec otype oname behavior options])
  51. (defrecord AlterTableStatement
  52. [db-spec tname action element])
  53. ;; ### Alter Action Records
  54. (defrecord AlterAddAction
  55. [db-spec element])
  56. (defrecord AlterDropAction
  57. [db-spec element])
  58. (defrecord AlterModifyAction
  59. [db-spec element])
  60. (defrecord AlterModifyDataTypeAndOptionsAction
  61. [db-spec element])
  62. (defrecord AlterModifyDefaultAction
  63. [db-spec element])
  64. (defrecord AlterRenameAction
  65. [db-spec element])
  66. ;; -----------------------------------------------------------------------------
  67. ;; ## Definations Print Method
  68. (defn print-without-db-spec [r, ^Writer w]
  69. (.write w "#")
  70. (.write w (.getName (class r)))
  71. (print-method (dissoc r :db-spec) w))
  72. (do-template [record]
  73. (defmethod print-method record [r, ^Writer w]
  74. (print-without-db-spec r w))
  75. Mode
  76. ScalarExpression
  77. IdentifierExpression
  78. FunctionExpression
  79. OperatorExpression
  80. AutoIncClause
  81. DataTypeClause
  82. ColumnDefinition
  83. ConstraintDefinition
  84. UniqueConstraintDefinition
  85. ForeignKeyConstraintDefinition
  86. CheckConstraintDefinition
  87. CreateSchemaStatement
  88. CreateTableStatement
  89. CreateIndexStatement
  90. DropStatement
  91. AlterTableStatement
  92. AlterAddAction
  93. AlterDropAction
  94. AlterModifyAction
  95. AlterModifyDataTypeAndOptionsAction
  96. AlterModifyDefaultAction
  97. AlterRenameAction)
  98. ;; -----------------------------------------------------------------------------
  99. ;; ## Import Helpers
  100. (defn import-expressions
  101. "Import all expression AST records into the calling namespace."
  102. []
  103. (import
  104. '(lobos.ast ScalarExpression
  105. IdentifierExpression
  106. FunctionExpression
  107. OperatorExpression)))
  108. (defn import-clauses
  109. "Import all clause AST records into the calling namespace."
  110. []
  111. (import
  112. '(lobos.ast AutoIncClause
  113. DataTypeClause)))
  114. (defn import-definitions
  115. "Import all definition AST records into the calling namespace."
  116. []
  117. (import
  118. '(lobos.ast ColumnDefinition
  119. ConstraintDefinition
  120. UniqueConstraintDefinition
  121. ForeignKeyConstraintDefinition
  122. CheckConstraintDefinition)))
  123. (defn import-statements
  124. "Import all statement AST records into the calling namespace."
  125. []
  126. (import
  127. '(lobos.ast CreateSchemaStatement
  128. CreateTableStatement
  129. CreateIndexStatement
  130. DropStatement
  131. AlterTableStatement)))
  132. (defn import-actions
  133. "Import all action AST records into the calling namespace."
  134. []
  135. (import
  136. '(lobos.ast AlterAddAction
  137. AlterDropAction
  138. AlterModifyAction
  139. AlterModifyDataTypeAndOptionsAction
  140. AlterModifyDefaultAction
  141. AlterRenameAction)))
  142. (defn import-all
  143. "Import all AST records into the calling namespace."
  144. []
  145. (import '(lobos.ast Mode))
  146. (import-expressions)
  147. (import-clauses)
  148. (import-definitions)
  149. (import-statements)
  150. (import-actions))