ronin-sql 0.2.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. data/.document +4 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +1 -0
  4. data/.yardopts +1 -0
  5. data/COPYING.txt +623 -288
  6. data/{History.txt → ChangeLog.md} +33 -35
  7. data/Gemfile +25 -0
  8. data/README.md +110 -0
  9. data/Rakefile +30 -20
  10. data/bin/ronin-sql +18 -5
  11. data/gemspec.yml +16 -0
  12. data/lib/ronin/formatting/extensions/sql.rb +4 -3
  13. data/lib/ronin/formatting/extensions/sql/string.rb +83 -10
  14. data/lib/ronin/formatting/sql.rb +4 -3
  15. data/lib/ronin/sql.rb +5 -12
  16. data/lib/ronin/{code/sql/create_index.rb → sql/binary_expr.rb} +25 -18
  17. data/lib/ronin/sql/clause.rb +72 -0
  18. data/lib/ronin/sql/clauses.rb +297 -0
  19. data/lib/ronin/sql/emittable.rb +84 -0
  20. data/lib/ronin/sql/emitter.rb +375 -0
  21. data/lib/ronin/sql/field.rb +106 -0
  22. data/lib/ronin/{code/sql/as.rb → sql/fields.rb} +36 -17
  23. data/lib/ronin/{code/sql/binary_expr.rb → sql/function.rb} +27 -27
  24. data/lib/ronin/sql/functions.rb +989 -0
  25. data/lib/ronin/sql/injection.rb +125 -157
  26. data/lib/ronin/{code/sql/default_values_clause.rb → sql/literal.rb} +13 -11
  27. data/lib/ronin/sql/literals.rb +72 -0
  28. data/lib/ronin/sql/operators.rb +332 -0
  29. data/lib/ronin/sql/sql.rb +86 -0
  30. data/lib/ronin/sql/statement.rb +70 -0
  31. data/lib/ronin/sql/statement_list.rb +110 -0
  32. data/lib/ronin/sql/statements.rb +115 -0
  33. data/lib/ronin/{code/sql/desc.rb → sql/unary_expr.rb} +11 -11
  34. data/lib/ronin/sql/version.rb +5 -4
  35. data/ronin-sql.gemspec +61 -0
  36. data/spec/formatting/sql/string_spec.rb +172 -0
  37. data/spec/spec_helper.rb +1 -4
  38. data/spec/sql/binary_expr.rb +5 -0
  39. data/spec/sql/binary_expr_examples.rb +25 -0
  40. data/spec/sql/clause_examples.rb +43 -0
  41. data/spec/sql/clause_spec.rb +31 -0
  42. data/spec/sql/clauses_spec.rb +43 -0
  43. data/spec/sql/emittable_spec.rb +41 -0
  44. data/spec/sql/emitter_spec.rb +472 -0
  45. data/spec/sql/field_spec.rb +103 -0
  46. data/spec/sql/fields_spec.rb +40 -0
  47. data/spec/sql/function_examples.rb +30 -0
  48. data/spec/sql/function_spec.rb +25 -0
  49. data/spec/sql/functions_spec.rb +110 -0
  50. data/spec/sql/injection_spec.rb +233 -0
  51. data/spec/sql/literal_spec.rb +5 -0
  52. data/spec/sql/literals_spec.rb +46 -0
  53. data/spec/sql/operators_spec.rb +44 -0
  54. data/spec/sql/sql_spec.rb +18 -0
  55. data/spec/sql/statement_examples.rb +39 -0
  56. data/spec/sql/statement_list_spec.rb +48 -0
  57. data/spec/sql/statement_sql.rb +38 -0
  58. data/spec/sql/statements_spec.rb +22 -0
  59. data/spec/sql/unary_expr.rb +5 -0
  60. data/spec/sql/unary_expr_examples.rb +20 -0
  61. metadata +116 -217
  62. data.tar.gz.sig +0 -0
  63. data/Manifest.txt +0 -108
  64. data/README.txt +0 -112
  65. data/lib/ronin/code/sql.rb +0 -22
  66. data/lib/ronin/code/sql/add_column_clause.rb +0 -42
  67. data/lib/ronin/code/sql/alter_table.rb +0 -52
  68. data/lib/ronin/code/sql/asc.rb +0 -36
  69. data/lib/ronin/code/sql/between.rb +0 -66
  70. data/lib/ronin/code/sql/clause.rb +0 -35
  71. data/lib/ronin/code/sql/code.rb +0 -35
  72. data/lib/ronin/code/sql/common_dialect.rb +0 -66
  73. data/lib/ronin/code/sql/create.rb +0 -74
  74. data/lib/ronin/code/sql/create_table.rb +0 -44
  75. data/lib/ronin/code/sql/create_view.rb +0 -41
  76. data/lib/ronin/code/sql/delete.rb +0 -52
  77. data/lib/ronin/code/sql/dialect.rb +0 -282
  78. data/lib/ronin/code/sql/drop.rb +0 -55
  79. data/lib/ronin/code/sql/drop_index.rb +0 -41
  80. data/lib/ronin/code/sql/drop_table.rb +0 -41
  81. data/lib/ronin/code/sql/drop_view.rb +0 -41
  82. data/lib/ronin/code/sql/emittable.rb +0 -100
  83. data/lib/ronin/code/sql/exceptions.rb +0 -24
  84. data/lib/ronin/code/sql/exceptions/unknown_clause.rb +0 -29
  85. data/lib/ronin/code/sql/exceptions/unknown_dialect.rb +0 -29
  86. data/lib/ronin/code/sql/exceptions/unknown_statement.rb +0 -29
  87. data/lib/ronin/code/sql/expr.rb +0 -102
  88. data/lib/ronin/code/sql/field.rb +0 -101
  89. data/lib/ronin/code/sql/fields_clause.rb +0 -46
  90. data/lib/ronin/code/sql/from_clause.rb +0 -42
  91. data/lib/ronin/code/sql/function.rb +0 -53
  92. data/lib/ronin/code/sql/group_by_clause.rb +0 -46
  93. data/lib/ronin/code/sql/having_clause.rb +0 -46
  94. data/lib/ronin/code/sql/in.rb +0 -47
  95. data/lib/ronin/code/sql/injected_statement.rb +0 -100
  96. data/lib/ronin/code/sql/injection.rb +0 -203
  97. data/lib/ronin/code/sql/insert.rb +0 -54
  98. data/lib/ronin/code/sql/intersect_clause.rb +0 -42
  99. data/lib/ronin/code/sql/join_clause.rb +0 -123
  100. data/lib/ronin/code/sql/like.rb +0 -73
  101. data/lib/ronin/code/sql/limit_clause.rb +0 -42
  102. data/lib/ronin/code/sql/modifier.rb +0 -48
  103. data/lib/ronin/code/sql/offset_clause.rb +0 -42
  104. data/lib/ronin/code/sql/on_clause.rb +0 -55
  105. data/lib/ronin/code/sql/order_by_clause.rb +0 -42
  106. data/lib/ronin/code/sql/program.rb +0 -225
  107. data/lib/ronin/code/sql/rename_to_clause.rb +0 -42
  108. data/lib/ronin/code/sql/replace.rb +0 -54
  109. data/lib/ronin/code/sql/select.rb +0 -103
  110. data/lib/ronin/code/sql/set_clause.rb +0 -42
  111. data/lib/ronin/code/sql/statement.rb +0 -180
  112. data/lib/ronin/code/sql/token.rb +0 -62
  113. data/lib/ronin/code/sql/unary_expr.rb +0 -47
  114. data/lib/ronin/code/sql/union_all_clause.rb +0 -42
  115. data/lib/ronin/code/sql/union_clause.rb +0 -42
  116. data/lib/ronin/code/sql/update.rb +0 -52
  117. data/lib/ronin/code/sql/values_clause.rb +0 -46
  118. data/lib/ronin/code/sql/where_clause.rb +0 -42
  119. data/lib/ronin/sql/error.rb +0 -26
  120. data/lib/ronin/sql/error/error.rb +0 -62
  121. data/lib/ronin/sql/error/extensions.rb +0 -22
  122. data/lib/ronin/sql/error/extensions/string.rb +0 -77
  123. data/lib/ronin/sql/error/message.rb +0 -62
  124. data/lib/ronin/sql/error/pattern.rb +0 -104
  125. data/lib/ronin/sql/error/patterns.rb +0 -99
  126. data/lib/ronin/sql/extensions.rb +0 -22
  127. data/lib/ronin/sql/extensions/uri.rb +0 -22
  128. data/lib/ronin/sql/extensions/uri/http.rb +0 -107
  129. data/spec/code/sql/common_dialect_spec.rb +0 -205
  130. data/spec/code/sql/create_examples.rb +0 -19
  131. data/spec/code/sql/create_index_spec.rb +0 -25
  132. data/spec/code/sql/create_table_spec.rb +0 -27
  133. data/spec/code/sql/create_view_spec.rb +0 -16
  134. data/spec/code/sql/delete_spec.rb +0 -14
  135. data/spec/code/sql/drop_examples.rb +0 -10
  136. data/spec/code/sql/drop_index_spec.rb +0 -16
  137. data/spec/code/sql/drop_table_spec.rb +0 -16
  138. data/spec/code/sql/drop_view_spec.rb +0 -16
  139. data/spec/code/sql/has_default_values_clause_examples.rb +0 -10
  140. data/spec/code/sql/has_fields_clause_examples.rb +0 -15
  141. data/spec/code/sql/has_from_clause_examples.rb +0 -13
  142. data/spec/code/sql/has_values_clause_examples.rb +0 -15
  143. data/spec/code/sql/has_where_clause_examples.rb +0 -15
  144. data/spec/code/sql/insert_spec.rb +0 -21
  145. data/spec/code/sql/replace_spec.rb +0 -21
  146. data/spec/code/sql/select_spec.rb +0 -105
  147. data/spec/code/sql/update_spec.rb +0 -26
  148. data/spec/helpers/code.rb +0 -14
  149. data/spec/sql/error_spec.rb +0 -24
  150. data/spec/sql/extensions/uri/http_spec.rb +0 -34
  151. data/spec/sql_spec.rb +0 -9
  152. data/tasks/spec.rb +0 -10
  153. data/tasks/yard.rb +0 -13
  154. metadata.gz.sig +0 -0
@@ -1,35 +0,0 @@
1
- #
2
- # Ronin SQL - A Ronin library providing support for SQL related security
3
- # tasks.
4
- #
5
- # Copyright (c) 2007-2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 2 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- #
21
-
22
- require 'ronin/code/sql/emittable'
23
- require 'ronin/code/sql/token'
24
-
25
- module Ronin
26
- module Code
27
- module SQL
28
- class Clause
29
-
30
- include Emittable
31
-
32
- end
33
- end
34
- end
35
- end
@@ -1,35 +0,0 @@
1
- #
2
- # Ronin SQL - A Ronin library providing support for SQL related security
3
- # tasks.
4
- #
5
- # Copyright (c) 2007-2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 2 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- #
21
-
22
- require 'ronin/code/sql/program'
23
- require 'ronin/code/sql/injection'
24
-
25
- module Ronin
26
- module Code
27
- def Code.sql(options={},&block)
28
- SQL::Program.new(options,&block)
29
- end
30
-
31
- def Code.sql_injection(options={},&block)
32
- SQL::Injection.new(options,&block)
33
- end
34
- end
35
- end
@@ -1,66 +0,0 @@
1
- #
2
- # Ronin SQL - A Ronin library providing support for SQL related security
3
- # tasks.
4
- #
5
- # Copyright (c) 2007-2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 2 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- #
21
-
22
- require 'ronin/code/sql/dialect'
23
- require 'ronin/code/sql/create_table'
24
- require 'ronin/code/sql/create_index'
25
- require 'ronin/code/sql/create_view'
26
- require 'ronin/code/sql/alter_table'
27
- require 'ronin/code/sql/insert'
28
- require 'ronin/code/sql/select'
29
- require 'ronin/code/sql/update'
30
- require 'ronin/code/sql/delete'
31
- require 'ronin/code/sql/drop_table'
32
- require 'ronin/code/sql/drop_index'
33
- require 'ronin/code/sql/drop_view'
34
-
35
- module Ronin
36
- module Code
37
- module SQL
38
- class CommonDialect < Dialect
39
-
40
- dialect :common
41
-
42
- primitives :yes, :no, :on, :off, :null
43
-
44
- data_type :int
45
- data_type :varchar, :length => true
46
- data_type :text
47
- data_type :record
48
-
49
- aggregators :avg, :count, :group_concat, :min, :max, :sum, :total
50
-
51
- statement :create_table, CreateTable
52
- statement :create_index, CreateIndex
53
- statement :create_view, CreateView
54
- statement :alter_table, AlterTable
55
- statement :insert, Insert
56
- statement :select, Select
57
- statement :update, Update
58
- statement :delete, Delete
59
- statement :drop_table, DropTable
60
- statement :drop_index, DropIndex
61
- statement :drop_view, DropView
62
-
63
- end
64
- end
65
- end
66
- end
@@ -1,74 +0,0 @@
1
- #
2
- # Ronin SQL - A Ronin library providing support for SQL related security
3
- # tasks.
4
- #
5
- # Copyright (c) 2007-2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 2 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- #
21
-
22
- require 'ronin/code/sql/statement'
23
- require 'ronin/code/sql/fields_clause'
24
-
25
- module Ronin
26
- module Code
27
- module SQL
28
- class Create < Statement
29
-
30
- clause :fields, FieldsClause
31
-
32
- def initialize(dialect,type,name=nil,options={},&block)
33
- @type = type
34
- @name = name
35
- @temp = (options[:temp] || options[:temporary])
36
- @if_not_exists = options[:if_not_exists]
37
-
38
- super(dialect,options,&block)
39
- end
40
-
41
- def temp!
42
- @temp = true
43
- return self
44
- end
45
-
46
- def temp?
47
- @temp == true
48
- end
49
-
50
- def if_not_exists!
51
- @if_not_exists = true
52
- return self
53
- end
54
-
55
- def if_not_exists?
56
- @if_not_exists == true
57
- end
58
-
59
- def emit
60
- tokens = emit_token('CREATE')
61
- tokens += emit_token('TEMP') if @temp
62
-
63
- tokens += emit_token(@type)
64
-
65
- tokens += emit_token('IF NOT EXISTS') if @if_not_exists
66
- tokens += emit_token(@name)
67
-
68
- return tokens + super
69
- end
70
-
71
- end
72
- end
73
- end
74
- end
@@ -1,44 +0,0 @@
1
- #
2
- # Ronin SQL - A Ronin library providing support for SQL related security
3
- # tasks.
4
- #
5
- # Copyright (c) 2007-2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 2 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- #
21
-
22
- require 'ronin/code/sql/create'
23
- require 'ronin/code/sql/fields_clause'
24
-
25
- module Ronin
26
- module Code
27
- module SQL
28
- class CreateTable < Create
29
-
30
- clause :columns, FieldsClause
31
-
32
- def initialize(dialect,table=nil,options={},&block)
33
- super(dialect,'TABLE',table,options,&block)
34
- end
35
-
36
- def table(name=nil)
37
- @name = name if name
38
- return @name
39
- end
40
-
41
- end
42
- end
43
- end
44
- end
@@ -1,41 +0,0 @@
1
- #
2
- # Ronin SQL - A Ronin library providing support for SQL related security
3
- # tasks.
4
- #
5
- # Copyright (c) 2007-2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 2 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- #
21
-
22
- require 'ronin/code/sql/create'
23
-
24
- module Ronin
25
- module Code
26
- module SQL
27
- class CreateView < Create
28
-
29
- def initialize(dialect,view=nil,options={},&block)
30
- super(dialect,'VIEW',view,options,&block)
31
- end
32
-
33
- def view(name=nil)
34
- @name = name if name
35
- return @name
36
- end
37
-
38
- end
39
- end
40
- end
41
- end
@@ -1,52 +0,0 @@
1
- #
2
- # Ronin SQL - A Ronin library providing support for SQL related security
3
- # tasks.
4
- #
5
- # Copyright (c) 2007-2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 2 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- #
21
-
22
- require 'ronin/code/sql/statement'
23
- require 'ronin/code/sql/from_clause'
24
- require 'ronin/code/sql/where_clause'
25
-
26
- module Ronin
27
- module Code
28
- module SQL
29
- class Delete < Statement
30
-
31
- clause :from, FromClause
32
- clause :where, WhereClause
33
-
34
- def initialize(dialect,table=nil,options={},&block)
35
- @table = table
36
-
37
- super(dialect,options,&block)
38
- end
39
-
40
- def table(name=nil)
41
- @table = name if name
42
- return @table
43
- end
44
-
45
- def emit
46
- emit_token('DELETE FROM') + emit_value(@table)
47
- end
48
-
49
- end
50
- end
51
- end
52
- end
@@ -1,282 +0,0 @@
1
- #
2
- # Ronin SQL - A Ronin library providing support for SQL related security
3
- # tasks.
4
- #
5
- # Copyright (c) 2007-2009 Hal Brodigan (postmodern at users.sourceforge.net)
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 2 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- #
21
-
22
- require 'ronin/code/sql/exceptions/unknown_dialect'
23
- require 'ronin/code/sql/exceptions/unknown_statement'
24
- require 'ronin/code/sql/exceptions/unknown_clause'
25
- require 'ronin/code/sql/function'
26
- require 'ronin/code/symbol_table'
27
- require 'ronin/extensions/meta'
28
-
29
- module Ronin
30
- module Code
31
- module SQL
32
- class Dialect
33
-
34
- # Symbol Table for the dialect
35
- attr_reader :symbols
36
-
37
- # Statements used within the dialect
38
- attr_reader :statements
39
-
40
- #
41
- # Creates a new Dialect object connected to the specified
42
- # _program_.
43
- #
44
- def initialize(symbols={})
45
- @symbols = SymbolTable.new(symbols)
46
- @statements = []
47
- end
48
-
49
- #
50
- # Returns the Hash of defined SQL dialects.
51
- #
52
- def Dialect.dialects
53
- @@dialects ||= {}
54
- end
55
-
56
- #
57
- # Returns +true+ if there is a SQL Dialect defined with the
58
- # specified _name_, returns +false+ otherwise.
59
- #
60
- def Dialect.has_dialect?(name)
61
- Dialect.dialects.has_key?(name.to_sym)
62
- end
63
-
64
- #
65
- # Returns the SQL Dialect defined with the specified _name_. If no
66
- # such SQL Dialect exists, an UnknownDialect exception will be
67
- # raised.
68
- #
69
- def Dialect.get(name)
70
- name = name.to_sym
71
-
72
- unless Dialect.has_dialect?(name)
73
- raise(UnknownDialect,"unknown dialect #{name}",caller)
74
- end
75
-
76
- return Dialect.dialects[name]
77
- end
78
-
79
- #
80
- # Returns the Hash of defined Statements within the Dialect.
81
- #
82
- def self.statements
83
- @@statements ||= {}
84
- end
85
-
86
- def self.has_statement?(name)
87
- self.statements.has_key?(name.to_sym)
88
- end
89
-
90
- def self.clauses
91
- all_clauses = {}
92
-
93
- self.statements.each do |stmt|
94
- all_clauses.merge!(stmt.clauses)
95
- end
96
-
97
- return all_clauses
98
- end
99
-
100
- def self.has_clause?(name)
101
- self.statements.each_value do |stmt|
102
- return true if stmt.has_clause?(name)
103
- end
104
-
105
- return false
106
- end
107
-
108
- def has_statement?(name)
109
- self.class.has_statement?(name)
110
- end
111
-
112
- def statement(name,*arguments,&block)
113
- name = name.to_sym
114
-
115
- unless has_statement?(name)
116
- raise(UnknownStatement,"unknown statement #{name} in #{dialect} dialect",caller)
117
- end
118
-
119
- return self.class.statements[name].new(self,*arguments,&block)
120
- end
121
-
122
- def enqueue_statement(name,*arguments,&block)
123
- stmt = statement(name,*arguments,&block)
124
-
125
- @statements << stmt
126
- return stmt
127
- end
128
-
129
- def has_clause?(name)
130
- self.class.has_clause?(name)
131
- end
132
-
133
- def clause(name,*arguments)
134
- name = name.to_sym
135
-
136
- self.class.statements.each do |stmt|
137
- if stmt.has_cluase?(name)
138
- return stmt.clauses[name].new(*arguments)
139
- end
140
- end
141
-
142
- raise(UnknownClause,"unknown clause #{name}",caller)
143
- end
144
-
145
- def symbol(name)
146
- sym = @symbols.symbol(name)
147
- sym.value ||= name
148
-
149
- return sym
150
- end
151
-
152
- def field(name)
153
- sym = @symbols.symbol(name)
154
- sym.value ||= Field.new(@symbols,name)
155
-
156
- return sym
157
- end
158
-
159
- def all
160
- Token.new('*')
161
- end
162
-
163
- def id
164
- field('id')
165
- end
166
-
167
- def each_token(&block)
168
- @statements.each do |stmt|
169
- stmt.emit.each(&block)
170
-
171
- block.call(Token.separator)
172
- end
173
-
174
- return self
175
- end
176
-
177
- protected
178
-
179
- #
180
- # Defines a SQL Dialect with the specified _name_.
181
- #
182
- def self.dialect(name)
183
- name = name.to_sym
184
-
185
- class_def(:dialect) { name }
186
-
187
- Dialect.dialects[name.to_sym] = self
188
- return self
189
- end
190
-
191
- #
192
- # Defines various SQL primitives with the specified _names_.
193
- #
194
- def self.primitives(*names)
195
- names.each do |name|
196
- name = name.to_s.downcase
197
-
198
- class_def(name) { Token.new(name) }
199
- end
200
-
201
- return self
202
- end
203
-
204
- #
205
- # Defines a SQL data-type with the specified _name_ and given
206
- # _options_.
207
- #
208
- def self.data_type(name,options={})
209
- name = name.to_s.downcase
210
- type_name = name.upcase
211
- supports_length = options[:length]
212
-
213
- class_def(name) do |*arguments|
214
- length = arguments.first
215
-
216
- if (supports_length && length)
217
- Token.new("#{type_name}(#{length})")
218
- else
219
- Token.new(type_name)
220
- end
221
- end
222
-
223
- return self
224
- end
225
-
226
- #
227
- # Defines various SQL function with the specified _names_.
228
- #
229
- def self.functions(*names)
230
- names.each do |name|
231
- class_def(name) do |*fields|
232
- Function.new(name,*fields)
233
- end
234
- end
235
-
236
- return self
237
- end
238
-
239
- #
240
- # Defines various SQL aggregate functions with the specified
241
- # _names_.
242
- #
243
- def self.aggregators(*names)
244
- names.each do |name|
245
- class_def(name) do |field|
246
- Function.new(name,field)
247
- end
248
- end
249
-
250
- return self
251
- end
252
-
253
- #
254
- # Defines an SQL statement with the specified _name_ and _base_
255
- # class.
256
- #
257
- def self.statement(name,base)
258
- name = name.to_sym
259
-
260
- self.statements[name] = base
261
-
262
- class_eval %{
263
- def #{name}(*arguments,&block)
264
- enqueue_statement(:#{name},*arguments,&block)
265
- end
266
- }
267
-
268
- return self
269
- end
270
-
271
- def method_missing(name,*arguments,&block)
272
- if (arguments.empty? && block.nil?)
273
- return field(name)
274
- end
275
-
276
- raise(NoMethodError,name.id2name)
277
- end
278
-
279
- end
280
- end
281
- end
282
- end