PageRenderTime 20ms CodeModel.GetById 7ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/src/lobos/ast.clj

http://github.com/budu/lobos
Clojure | 195 lines | 131 code | 45 blank | 19 comment | 1 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
  9(ns lobos.ast
 10  "Abstract SQL syntax tree for the DDL part of the language."
 11  (:use clojure.template)
 12  (:import (java.io Writer)))
 13
 14;; -----------------------------------------------------------------------------
 15
 16;; ## AST Records
 17
 18;; ### Special Records
 19
 20(defrecord Mode
 21  [db-spec])
 22
 23;; ### Expression Records
 24
 25(defrecord ScalarExpression
 26  [db-spec scalar])
 27
 28(defrecord IdentifierExpression
 29  [db-spec name qualifiers])
 30
 31(defrecord FunctionExpression
 32  [db-spec name args])
 33
 34(defrecord OperatorExpression
 35  [db-spec op left right])
 36
 37;; ### Clause Records
 38
 39(defrecord AutoIncClause
 40  [db-spec])
 41
 42(defrecord DataTypeClause
 43  [db-spec dtype args options])
 44
 45;; ### Definition Records
 46
 47(defrecord ColumnDefinition
 48  [db-spec cname data-type default auto-inc not-null others])
 49
 50(defrecord ConstraintDefinition
 51  [db-spec cname])
 52
 53(defrecord UniqueConstraintDefinition
 54  [db-spec cname ctype columns])
 55
 56(defrecord ForeignKeyConstraintDefinition
 57  [db-spec cname columns parent-table parent-columns match triggered-actions])
 58
 59(defrecord CheckConstraintDefinition
 60  [db-spec cname condition])
 61
 62;; ### Statement Records
 63
 64(defrecord CreateSchemaStatement
 65  [db-spec sname elements])
 66
 67(defrecord CreateTableStatement
 68  [db-spec tname elements])
 69
 70(defrecord CreateIndexStatement
 71  [db-spec iname tname columns options])
 72
 73(defrecord DropStatement
 74  [db-spec otype oname behavior options])
 75
 76(defrecord AlterTableStatement
 77  [db-spec tname action element])
 78
 79;; ### Alter Action Records
 80
 81(defrecord AlterAddAction
 82  [db-spec element])
 83
 84(defrecord AlterDropAction
 85  [db-spec element])
 86
 87(defrecord AlterModifyAction
 88  [db-spec element])
 89
 90(defrecord AlterModifyDataTypeAndOptionsAction
 91  [db-spec element])
 92
 93(defrecord AlterModifyDefaultAction
 94  [db-spec element])
 95
 96(defrecord AlterRenameAction
 97  [db-spec element])
 98
 99
100;; -----------------------------------------------------------------------------
101
102;; ## Definations Print Method
103
104(defn print-without-db-spec [r, ^Writer w]
105  (.write w "#")
106  (.write w (.getName (class r)))
107  (print-method (dissoc r :db-spec) w))
108
109(do-template [record]
110  (defmethod print-method record [r, ^Writer w]
111    (print-without-db-spec r w))
112  Mode
113  ScalarExpression
114  IdentifierExpression
115  FunctionExpression
116  OperatorExpression
117  AutoIncClause
118  DataTypeClause
119  ColumnDefinition
120  ConstraintDefinition
121  UniqueConstraintDefinition
122  ForeignKeyConstraintDefinition
123  CheckConstraintDefinition
124  CreateSchemaStatement
125  CreateTableStatement
126  CreateIndexStatement
127  DropStatement
128  AlterTableStatement
129  AlterAddAction
130  AlterDropAction
131  AlterModifyAction
132  AlterModifyDataTypeAndOptionsAction
133  AlterModifyDefaultAction
134  AlterRenameAction)
135
136;; -----------------------------------------------------------------------------
137
138;; ## Import Helpers
139
140(defn import-expressions
141  "Import all expression AST records into the calling namespace."
142  []
143  (import
144   '(lobos.ast ScalarExpression
145               IdentifierExpression
146               FunctionExpression
147               OperatorExpression)))
148
149(defn import-clauses
150  "Import all clause AST records into the calling namespace."
151  []
152  (import
153   '(lobos.ast AutoIncClause
154               DataTypeClause)))
155
156(defn import-definitions
157  "Import all definition AST records into the calling namespace."
158  []
159  (import
160   '(lobos.ast ColumnDefinition
161               ConstraintDefinition
162               UniqueConstraintDefinition
163               ForeignKeyConstraintDefinition
164               CheckConstraintDefinition)))
165
166(defn import-statements
167  "Import all statement AST records into the calling namespace."
168  []
169  (import
170   '(lobos.ast CreateSchemaStatement
171               CreateTableStatement
172               CreateIndexStatement
173               DropStatement
174               AlterTableStatement)))
175
176(defn import-actions
177  "Import all action AST records into the calling namespace."
178  []
179  (import
180   '(lobos.ast AlterAddAction
181               AlterDropAction
182               AlterModifyAction
183               AlterModifyDataTypeAndOptionsAction
184               AlterModifyDefaultAction
185               AlterRenameAction)))
186
187(defn import-all
188  "Import all AST records into the calling namespace."
189  []
190  (import '(lobos.ast Mode))
191  (import-expressions)
192  (import-clauses)
193  (import-definitions)
194  (import-statements)
195  (import-actions))