sqldsl 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +91 -0
- data/README_TEMPLATE +39 -0
- data/lib/array.rb +20 -0
- data/lib/delete.rb +22 -0
- data/lib/insert.rb +55 -0
- data/lib/numeric.rb +10 -0
- data/lib/object.rb +34 -0
- data/lib/select.rb +33 -0
- data/lib/sql_statement.rb +19 -0
- data/lib/sqldsl.rb +12 -0
- data/lib/string.rb +12 -0
- data/lib/symbol.rb +12 -0
- data/lib/time.rb +20 -0
- data/lib/update.rb +37 -0
- data/lib/where_builder.rb +58 -0
- data/test/all_tests.rb +1 -0
- data/test/array_test.rb +11 -0
- data/test/delete_acceptance_test.rb +10 -0
- data/test/delete_test.rb +36 -0
- data/test/insert_acceptance_test.rb +15 -0
- data/test/insert_test.rb +37 -0
- data/test/object_test.rb +29 -0
- data/test/select_acceptance_test.rb +20 -0
- data/test/select_test.rb +29 -0
- data/test/string_test.rb +11 -0
- data/test/symbol_test.rb +7 -0
- data/test/test_helper.rb +2 -0
- data/test/time_test.rb +8 -0
- data/test/update_acceptance_test.rb +10 -0
- data/test/update_test.rb +13 -0
- data/test/where_builder_test.rb +50 -0
- metadata +79 -0
data/README
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
= SQL DSL
|
2
|
+
|
3
|
+
SQL DSL is a library for creating SQL statements using ruby code.
|
4
|
+
|
5
|
+
by Jay[http://jayfields.blogspot.com] Fields[http://jayfields.blogspot.com]
|
6
|
+
|
7
|
+
== Download and Installation
|
8
|
+
|
9
|
+
You can download SQL DSL from here[http://rubyforge.org/projects/sqldsl] or install it with the following command.
|
10
|
+
|
11
|
+
$ gem install sqldsl
|
12
|
+
|
13
|
+
== License
|
14
|
+
|
15
|
+
You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt).
|
16
|
+
|
17
|
+
== Examples
|
18
|
+
|
19
|
+
See the tests for more examples
|
20
|
+
|
21
|
+
=== Test Helper
|
22
|
+
|
23
|
+
require 'test/unit'
|
24
|
+
require File.dirname(__FILE__) + '/../lib/sqldsl'
|
25
|
+
|
26
|
+
=== Insert Example
|
27
|
+
|
28
|
+
require File.dirname(__FILE__) + '/test_helper'
|
29
|
+
|
30
|
+
class InsertAcceptanceTest < Test::Unit::TestCase
|
31
|
+
def test_insert_select
|
32
|
+
statement = Insert.into[:table1][:column1, :column2, :column3].values do
|
33
|
+
Select[1]
|
34
|
+
end
|
35
|
+
assert_equal 'insert into table1 (column1, column2, column3) select 1', statement.to_sql
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_insert_with_values
|
39
|
+
statement = Insert.into[:table1][:column1, :column2, :column3].values(10, 'book', :column4)
|
40
|
+
assert_equal "insert into table1 (column1, column2, column3) values (10, 'book', column4)", statement.to_sql
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
=== Update Example
|
45
|
+
|
46
|
+
require File.dirname(__FILE__) + '/test_helper'
|
47
|
+
|
48
|
+
class UpdateAcceptanceTest < Test::Unit::TestCase
|
49
|
+
def test_insert_select
|
50
|
+
statement = Update[:table1].set[:column1=>10, :column2=>'book'].where do
|
51
|
+
not_exists(Select[1].from[:table2])
|
52
|
+
end
|
53
|
+
assert_equal "update table1 set column1=10, column2='book' where not exists (select 1 from table2)", statement.to_sql
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
=== Delete Example
|
58
|
+
|
59
|
+
require File.dirname(__FILE__) + '/test_helper'
|
60
|
+
|
61
|
+
class DeleteAcceptanceTest < Test::Unit::TestCase
|
62
|
+
def test_insert_select
|
63
|
+
statement = Delete.from[:table1].where do
|
64
|
+
exists(Select[1].from[:table2])
|
65
|
+
end
|
66
|
+
assert_equal "delete from table1 where exists (select 1 from table2)", statement.to_sql
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
=== Select Example
|
71
|
+
|
72
|
+
require File.dirname(__FILE__) + '/test_helper'
|
73
|
+
|
74
|
+
class SelectAcceptanceTest < Test::Unit::TestCase
|
75
|
+
def test_simple_select
|
76
|
+
statement = Select[:column1, 'book', 10].from[:table1].where do
|
77
|
+
equal :column1, 99
|
78
|
+
equal :column2, 'star'
|
79
|
+
end
|
80
|
+
expected = "select column1, 'book', 10 from table1 where column1 = 99 and column2 = 'star'"
|
81
|
+
assert_equal expected, statement.to_sql
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_select_with_join
|
85
|
+
statement = Select[:column1, :column2].from[:table1, :table2].where do
|
86
|
+
equal :'table1.id', :'table2.table1_id'
|
87
|
+
end
|
88
|
+
expected = "select column1, column2 from table1, table2 where table1.id = table2.table1_id"
|
89
|
+
assert_equal expected, statement.to_sql
|
90
|
+
end
|
91
|
+
end
|
data/README_TEMPLATE
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= SQL DSL
|
2
|
+
|
3
|
+
SQL DSL is a library for creating SQL statements using ruby code.
|
4
|
+
|
5
|
+
by Jay[http://jayfields.blogspot.com] Fields[http://jayfields.blogspot.com]
|
6
|
+
|
7
|
+
== Download and Installation
|
8
|
+
|
9
|
+
You can download SQL DSL from here[http://rubyforge.org/projects/sqldsl] or install it with the following command.
|
10
|
+
|
11
|
+
$ gem install sqldsl
|
12
|
+
|
13
|
+
== License
|
14
|
+
|
15
|
+
You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt).
|
16
|
+
|
17
|
+
== Examples
|
18
|
+
|
19
|
+
See the tests for more examples
|
20
|
+
|
21
|
+
=== Test Helper
|
22
|
+
|
23
|
+
<%= File.readlines("test/test_helper.rb").collect { |line| " " + line } %>
|
24
|
+
|
25
|
+
=== Insert Example
|
26
|
+
|
27
|
+
<%= File.readlines("test/insert_acceptance_test.rb").collect { |line| " " + line } %>
|
28
|
+
|
29
|
+
=== Update Example
|
30
|
+
|
31
|
+
<%= File.readlines("test/update_acceptance_test.rb").collect { |line| " " + line } %>
|
32
|
+
|
33
|
+
=== Delete Example
|
34
|
+
|
35
|
+
<%= File.readlines("test/delete_acceptance_test.rb").collect { |line| " " + line } %>
|
36
|
+
|
37
|
+
=== Select Example
|
38
|
+
|
39
|
+
<%= File.readlines("test/select_acceptance_test.rb").collect { |line| " " + line } %>
|
data/lib/array.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class Array
|
2
|
+
# call-seq: array.to_sql -> a_string
|
3
|
+
#
|
4
|
+
# Returns a string by collecting all elements, calling +to_sql+ on each one, and
|
5
|
+
# then joins them with ', '.
|
6
|
+
#
|
7
|
+
# [10, 'book', :column2].to_sql #=> "10, 'book', column2"
|
8
|
+
def to_sql
|
9
|
+
self.collect { |element| element.to_sql }.join(', ')
|
10
|
+
end
|
11
|
+
|
12
|
+
# call-seq: array.to_sql_equal(any) -> a_string
|
13
|
+
#
|
14
|
+
# Returns a string containing the sql in expression
|
15
|
+
#
|
16
|
+
# [10, :column2, 'book'].to_sql_equal(:column1) #=> "column1 in (10, column2, 'book')"
|
17
|
+
def to_sql_equal(lval)
|
18
|
+
"#{lval.to_sql } in (#{ self.to_sql })"
|
19
|
+
end
|
20
|
+
end
|
data/lib/delete.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class Delete < SqlStatement
|
2
|
+
class << self
|
3
|
+
# call-seq: Delete.from -> a_delete
|
4
|
+
#
|
5
|
+
# Returns a Delete instance with the SQL initialized to 'delete from '
|
6
|
+
#
|
7
|
+
# Delete.from.to_sql #=> "delete from "
|
8
|
+
def from
|
9
|
+
self.new('delete from ')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# call-seq: delete[table] -> a_delete
|
14
|
+
#
|
15
|
+
# Returns a Delete instance with the table appended to the SQL statement.
|
16
|
+
#
|
17
|
+
# Delete.from[:table1].to_sql #=> "delete from table1"
|
18
|
+
def [](table)
|
19
|
+
@to_sql += table.to_sql
|
20
|
+
self
|
21
|
+
end
|
22
|
+
end
|
data/lib/insert.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
class Insert < SqlStatement
|
2
|
+
class << self
|
3
|
+
# call-seq: Insert.into -> Insert
|
4
|
+
#
|
5
|
+
# Returns the Insert class. Unnecessary and only available to mimic SQL statements.
|
6
|
+
#
|
7
|
+
# Insert.into #=> Insert
|
8
|
+
def into
|
9
|
+
self
|
10
|
+
end
|
11
|
+
|
12
|
+
# call-seq: Insert[table] -> an_insert
|
13
|
+
#
|
14
|
+
# Returns an Insert instance with the SQL initialized to 'insert into [table] '
|
15
|
+
#
|
16
|
+
# Insert[:table1].to_sql #=> "insert into table1 "
|
17
|
+
def [](table)
|
18
|
+
self.new("insert into #{table.to_sql}")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# call-seq: insert[column1,...] -> an_insert
|
23
|
+
#
|
24
|
+
# Returns an Insert instance with the columns appended to the SQL statement.
|
25
|
+
#
|
26
|
+
# Insert.into[:table1][:column1, :column2].to_sql #=> "insert into table1 (column1, column2)"
|
27
|
+
def [](*columns)
|
28
|
+
@to_sql += " (#{columns.join(', ')})"
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
# call-seq: insert.values { block } -> an_insert
|
33
|
+
# insert.values(arg,...)
|
34
|
+
#
|
35
|
+
# If a block is given:
|
36
|
+
# Ignores any parameters given to the method.
|
37
|
+
# Executes the block then calls +to_sql+ on the result.
|
38
|
+
# Returns an Insert instance with the result of the block's execution appended to the SQL statement.
|
39
|
+
#
|
40
|
+
# insert = Insert.into[:table1][:column1].values { Select['book'] }
|
41
|
+
# insert.to_sql #=> "insert into table1 (column1) select 'book'"
|
42
|
+
#
|
43
|
+
# If no block is given:
|
44
|
+
# Returns an Insert instance with the args appended to the SQL statement as values
|
45
|
+
#
|
46
|
+
# insert = Insert.into[:table1][:column1, :column2].values(10, 'book')
|
47
|
+
# insert.to_sql #=> "insert into table1 (column1, column2) values (10, 'book')"
|
48
|
+
def values(*args)
|
49
|
+
@to_sql += case
|
50
|
+
when block_given? then " #{yield.to_sql}"
|
51
|
+
else " values (#{args.to_sql})"
|
52
|
+
end
|
53
|
+
self
|
54
|
+
end
|
55
|
+
end
|
data/lib/numeric.rb
ADDED
data/lib/object.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
# call-seq: obj.to_sql_equal(any) -> a_string
|
4
|
+
#
|
5
|
+
# Returns a string containing the sql equality expression
|
6
|
+
#
|
7
|
+
# 10.to_sql_equal(:column1) #=> "column1 = 10"
|
8
|
+
def to_sql_equal(lval)
|
9
|
+
sql_expression(lval, "=")
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_sql_more_than(lval) #:nodoc:
|
13
|
+
sql_expression(lval, ">")
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_sql_less_than(lval) #:nodoc:
|
17
|
+
sql_expression(lval, "<")
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_sql_less_than_or_equal_to(lval) #:nodoc:
|
21
|
+
sql_expression(lval, "<=")
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_sql_more_than_or_equal_to(lval) #:nodoc:
|
25
|
+
sql_expression(lval, ">=")
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def sql_expression(lval, operator) #:nodoc:
|
31
|
+
"#{ lval.to_sql } #{operator} #{ self.to_sql }"
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/lib/select.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
class Select < SqlStatement
|
2
|
+
class << self
|
3
|
+
# call-seq: Select[arg,...] -> a_select
|
4
|
+
#
|
5
|
+
# Returns a Select instance with the SQL initialized to 'select ' plus the args joined by ', '
|
6
|
+
#
|
7
|
+
# Select[1, :column1, 'book'].to_sql #=> "select 1, column1, 'book'"
|
8
|
+
def [](*columns)
|
9
|
+
self.new("select #{columns.collect{ |column| column.to_sql }.join(', ')}")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# call-seq: select.from -> a_select
|
14
|
+
#
|
15
|
+
# Returns a Select instance with ' from ' appended to the SQL statement.
|
16
|
+
#
|
17
|
+
# Select[1, :column1, 'book'].from.to_sql #=> "select 1, column1, 'book' from "
|
18
|
+
def from
|
19
|
+
@to_sql += " from "
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
# call-seq: select[table,...] -> a_select
|
24
|
+
#
|
25
|
+
# Returns a Select instance with the table names, joined by ', ' appended to the SQL statement.
|
26
|
+
#
|
27
|
+
# Select[1, :column1, 'book'].from[:table1, :table2].to_sql #=> "select 1, column1, 'book' from table1, table2"
|
28
|
+
def [](*table_names)
|
29
|
+
@to_sql += table_names.collect{ |table| table.to_s }.sort.join(', ')
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class SqlStatement
|
2
|
+
attr_reader :to_sql
|
3
|
+
|
4
|
+
def initialize(sql) #:nodoc:
|
5
|
+
@to_sql = sql
|
6
|
+
end
|
7
|
+
|
8
|
+
# call-seq: sql_statement.where { block } -> a_sql_statement
|
9
|
+
#
|
10
|
+
# Creates a new WhereBuilder instance, passing the block as a parameter, then executes to_sql on the WhereBuilder instance.
|
11
|
+
# The resulting string from the WhereBuilder instance is appended to the SQL statement.
|
12
|
+
# Returns self.
|
13
|
+
#
|
14
|
+
# Select[1].where { equal :column1, 1 }.to_sql #=> "select 1 where column1 = 1"
|
15
|
+
def where(&block)
|
16
|
+
@to_sql += WhereBuilder.new(&block).to_sql
|
17
|
+
self
|
18
|
+
end
|
19
|
+
end
|
data/lib/sqldsl.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/object.rb'
|
2
|
+
require File.dirname(__FILE__) + '/symbol.rb'
|
3
|
+
require File.dirname(__FILE__) + '/array.rb'
|
4
|
+
require File.dirname(__FILE__) + '/string.rb'
|
5
|
+
require File.dirname(__FILE__) + '/numeric.rb'
|
6
|
+
require File.dirname(__FILE__) + '/time.rb'
|
7
|
+
require File.dirname(__FILE__) + '/sql_statement.rb'
|
8
|
+
require File.dirname(__FILE__) + '/where_builder.rb'
|
9
|
+
require File.dirname(__FILE__) + '/select.rb'
|
10
|
+
require File.dirname(__FILE__) + '/insert.rb'
|
11
|
+
require File.dirname(__FILE__) + '/update.rb'
|
12
|
+
require File.dirname(__FILE__) + '/delete.rb'
|
data/lib/string.rb
ADDED
data/lib/symbol.rb
ADDED
data/lib/time.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class Time
|
2
|
+
# call-seq: time.to_sql -> a_string
|
3
|
+
#
|
4
|
+
# Returns a string that represents a database agnostic time.
|
5
|
+
#
|
6
|
+
# Time.at(946702800).to_sql #=> "to_timestamp('2000-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')"
|
7
|
+
def to_sql
|
8
|
+
"to_timestamp('" + formatted + "', 'YYYY-MM-DD HH24:MI:SS')"
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
def formatted #:nodoc:
|
14
|
+
"#{year.to_s}-#{pad(month)}-#{pad(day)} #{pad(hour)}:#{pad(min)}:#{pad(sec)}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def pad(num) #:nodoc:
|
18
|
+
num.to_s.rjust(2,'0')
|
19
|
+
end
|
20
|
+
end
|
data/lib/update.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
class Update < SqlStatement
|
2
|
+
class << self
|
3
|
+
# call-seq: Update[table] -> an_update
|
4
|
+
#
|
5
|
+
# Returns an Update instance with the SQL initialized to 'update [table] '
|
6
|
+
#
|
7
|
+
# Update[:table1].to_sql #=> "update table1"
|
8
|
+
def [](table)
|
9
|
+
self.new("update #{table.to_sql}")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# call-seq: update.set -> update
|
14
|
+
#
|
15
|
+
# Returns self. Unnecessary and only available to mimic SQL statements.
|
16
|
+
#
|
17
|
+
# Update[:table1].set.to_sql #=> "update table1"
|
18
|
+
def set
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
# call-seq: update[column1=>'book',...] -> an_update
|
23
|
+
#
|
24
|
+
# Returns an Update instance with the set values appended to the SQL statement.
|
25
|
+
#
|
26
|
+
# update = Update[:table1].set[:column1=>'book', :column2=>10]
|
27
|
+
# update.to_sql #=> "update table1 set column1 = 'book, column2 = 10"
|
28
|
+
def [](hash)
|
29
|
+
@to_sql += " set "
|
30
|
+
set_args = []
|
31
|
+
hash.each_pair do |col, val|
|
32
|
+
set_args << "#{col.to_sql}=#{val.to_sql}"
|
33
|
+
end
|
34
|
+
@to_sql += set_args.sort.join(', ')
|
35
|
+
self
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
class WhereBuilder
|
2
|
+
|
3
|
+
# call-seq: WhereBuilder.new(&block) -> a_where_builder
|
4
|
+
#
|
5
|
+
# Returns a new WhereBuilder. At initialization time the block is instance evaled on the
|
6
|
+
# new WhereBuilder instance.
|
7
|
+
#
|
8
|
+
# WhereBuilder.new { equal :column1, 10 }.to_sql #=> " where column1 = 10"
|
9
|
+
def initialize(&block)
|
10
|
+
instance_eval(&block)
|
11
|
+
end
|
12
|
+
|
13
|
+
# call-seq: where.equal(arg1, arg2)
|
14
|
+
#
|
15
|
+
# Appends an equality condition to the where SQL clause.
|
16
|
+
#
|
17
|
+
# WhereBuilder.new { equal :column1, 10 }.to_sql #=> " where column1 = 10"
|
18
|
+
def equal(lval, rval)
|
19
|
+
sql_parts << rval.to_sql_equal(lval)
|
20
|
+
end
|
21
|
+
|
22
|
+
# call-seq: where.exists(clause)
|
23
|
+
#
|
24
|
+
# Appends an exists condition to the where SQL clause.
|
25
|
+
#
|
26
|
+
# WhereBuilder.new { exists 'select id from table1' }.to_sql #=> " where exists (select id from table1)"
|
27
|
+
def exists(clause)
|
28
|
+
sql_parts << "exists (#{clause.to_sql})"
|
29
|
+
end
|
30
|
+
|
31
|
+
# call-seq: where.not_exists(clause)
|
32
|
+
#
|
33
|
+
# Appends an exists condition to the where SQL clause.
|
34
|
+
#
|
35
|
+
# WhereBuilder.new { not_exists 'select id from table1' }.to_sql #=> " where not exists (select id from table1)"
|
36
|
+
def not_exists(clause)
|
37
|
+
sql_parts << "not exists (#{clause.to_sql})"
|
38
|
+
end
|
39
|
+
|
40
|
+
# call-seq: where.to_sql -> a_string
|
41
|
+
#
|
42
|
+
# Returns a string by collecting all the conditions and joins them with ' and '.
|
43
|
+
#
|
44
|
+
# WhereBuilder.new do
|
45
|
+
# equal :column1, 10
|
46
|
+
# equal :column2, 'book'
|
47
|
+
# end.to_sql #=> " where column1 = 10 and column2 = 'book'"
|
48
|
+
def to_sql
|
49
|
+
" where #{sql_parts.join(' and ')}"
|
50
|
+
end
|
51
|
+
|
52
|
+
protected
|
53
|
+
|
54
|
+
def sql_parts #:nodoc:
|
55
|
+
@sql_parts ||= []
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/test/all_tests.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Dir['**/*_test.rb'].each { |testCase| require testCase }
|
data/test/array_test.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class ArrayTest < Test::Unit::TestCase
|
4
|
+
def test_to_sql_gives_of_to_sql_objects_comma_delimited
|
5
|
+
assert_equal "too, 'the', 2", [:too, 'the', 2].to_sql
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_sql_equal_uses_in
|
9
|
+
assert_equal "column1 in (too, 'the', 2)", [:too, 'the', 2].to_sql_equal(:column1)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class DeleteAcceptanceTest < Test::Unit::TestCase
|
4
|
+
def test_insert_select
|
5
|
+
statement = Delete.from[:table1].where do
|
6
|
+
exists(Select[1].from[:table2])
|
7
|
+
end
|
8
|
+
assert_equal "delete from table1 where exists (select 1 from table2)", statement.to_sql
|
9
|
+
end
|
10
|
+
end
|
data/test/delete_test.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class DeleteTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_delete_without_where_clause
|
6
|
+
assert_equal 'delete from foo', Delete.from[:foo].to_sql
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_delete_with_where_clause
|
10
|
+
statement = Delete.from[:table].where do
|
11
|
+
equal :'table.column1', :'table.column2'
|
12
|
+
end
|
13
|
+
assert_equal 'delete from table where table.column1 = table.column2', statement.to_sql
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_delete_with_select_statement_including_tables_from_delete_context
|
17
|
+
statement = Delete.from[:potential_account_offers].where do
|
18
|
+
exists(Select[:'potential_account_offers.id'].from[:foo].where do
|
19
|
+
equal :'potential_account_offers.id', :'foo.bar'
|
20
|
+
end)
|
21
|
+
end
|
22
|
+
expected = 'delete from potential_account_offers where exists (select potential_account_offers.id from foo where potential_account_offers.id = foo.bar)'
|
23
|
+
assert_equal expected, statement.to_sql
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_if_where_is_called_with_no_block_gives_reminder_to_use_parents
|
27
|
+
assert_raise(ArgumentError) do
|
28
|
+
statement = Delete.from[:potential_account_offers].where do
|
29
|
+
exists Select[:'potential_account_offers.id'].from[:foo].where do
|
30
|
+
equal :'foo.id', :'foo.bar'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class InsertAcceptanceTest < Test::Unit::TestCase
|
4
|
+
def test_insert_select
|
5
|
+
statement = Insert.into[:table1][:column1, :column2, :column3].values do
|
6
|
+
Select[1]
|
7
|
+
end
|
8
|
+
assert_equal 'insert into table1 (column1, column2, column3) select 1', statement.to_sql
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_insert_with_values
|
12
|
+
statement = Insert.into[:table1][:column1, :column2, :column3].values(10, 'book', :column4)
|
13
|
+
assert_equal "insert into table1 (column1, column2, column3) values (10, 'book', column4)", statement.to_sql
|
14
|
+
end
|
15
|
+
end
|
data/test/insert_test.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class InsertTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_table_name_saved
|
6
|
+
assert_equal "insert into table", Insert.into[:table].to_sql
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_insert_with_statement_block
|
10
|
+
statement = Insert.into[:foo].values do
|
11
|
+
Struct.new(:to_sql).new('bar')
|
12
|
+
end
|
13
|
+
assert_equal "insert into foo bar", statement.to_sql
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_insert_with_explicit_column_order
|
17
|
+
statement = Insert.into[:foo][:column1, :column2].values do
|
18
|
+
Struct.new(:to_sql).new('more')
|
19
|
+
end
|
20
|
+
assert_equal "insert into foo (column1, column2) more", statement.to_sql
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_insert_with_multiple_sql_statements_in_block_uses_last
|
24
|
+
statement = Insert.into[:foo].values do
|
25
|
+
Struct.new(:to_sql).new('woo')
|
26
|
+
Struct.new(:to_sql).new('hoo')
|
27
|
+
end
|
28
|
+
assert_equal "insert into foo hoo", statement.to_sql
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def test_values_WITH_no_block
|
33
|
+
statement = Insert.into[:foo].values(10, :book)
|
34
|
+
assert_equal "insert into foo values (10, book)", statement.to_sql
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
data/test/object_test.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class ObjectTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_to_sql_equal
|
6
|
+
assert_equal "column1 = foo", foo.to_sql_equal(:column1)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_to_sql_more_than
|
10
|
+
assert_equal "column1 > foo", foo.to_sql_more_than(:column1)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_to_sql_less_than
|
14
|
+
assert_equal "column1 < foo", foo.to_sql_less_than(:column1)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_to_sql_less_than_equal
|
18
|
+
assert_equal "column1 <= foo", foo.to_sql_less_than_or_equal_to(:column1)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_to_sql_more_than_equal
|
22
|
+
assert_equal "column1 >= foo", foo.to_sql_more_than_or_equal_to(:column1)
|
23
|
+
end
|
24
|
+
|
25
|
+
def foo
|
26
|
+
Struct.new(:to_sql).new(:foo)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class SelectAcceptanceTest < Test::Unit::TestCase
|
4
|
+
def test_simple_select
|
5
|
+
statement = Select[:column1, 'book', 10].from[:table1].where do
|
6
|
+
equal :column1, 99
|
7
|
+
equal :column2, 'star'
|
8
|
+
end
|
9
|
+
expected = "select column1, 'book', 10 from table1 where column1 = 99 and column2 = 'star'"
|
10
|
+
assert_equal expected, statement.to_sql
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_select_with_join
|
14
|
+
statement = Select[:column1, :column2].from[:table1, :table2].where do
|
15
|
+
equal :'table1.id', :'table2.table1_id'
|
16
|
+
end
|
17
|
+
expected = "select column1, column2 from table1, table2 where table1.id = table2.table1_id"
|
18
|
+
assert_equal expected, statement.to_sql
|
19
|
+
end
|
20
|
+
end
|
data/test/select_test.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class SelectTest < Test::Unit::TestCase
|
4
|
+
def test_select_with_single_column
|
5
|
+
assert_equal 'select foo', Select[:foo].to_sql
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_select_with_multiple_columns
|
9
|
+
assert_equal 'select foo, bar', Select[:foo, :bar].to_sql
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_time_literals_quoted
|
13
|
+
time = Time.at(946702800)
|
14
|
+
assert_equal "select to_timestamp('2000-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')", Select[time].to_sql
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_select_with_symbol_and_literal_columns
|
18
|
+
assert_equal "select symbol, 'literal'", Select[:symbol, 'literal'].to_sql
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_select_with_single_table
|
22
|
+
assert_equal 'select foo from foo', Select[:foo].from[:foo].to_sql
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_select_with_multiple_tables
|
26
|
+
assert_equal 'select column from bar, foo',
|
27
|
+
Select[:column].from[:foo, :bar].to_sql
|
28
|
+
end
|
29
|
+
end
|
data/test/string_test.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class StringTest < Test::Unit::TestCase
|
4
|
+
def test_to_sql_gives_quoted
|
5
|
+
assert_equal "'123'", "123".to_sql
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_to_sql_gives_quoted_and_escapes_single_quotes
|
9
|
+
assert_equal "'it''s'", "it's".to_sql
|
10
|
+
end
|
11
|
+
end
|
data/test/symbol_test.rb
ADDED
data/test/test_helper.rb
ADDED
data/test/time_test.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class UpdateAcceptanceTest < Test::Unit::TestCase
|
4
|
+
def test_insert_select
|
5
|
+
statement = Update[:table1].set[:column1=>10, :column2=>'book'].where do
|
6
|
+
not_exists(Select[1].from[:table2])
|
7
|
+
end
|
8
|
+
assert_equal "update table1 set column1=10, column2='book' where not exists (select 1 from table2)", statement.to_sql
|
9
|
+
end
|
10
|
+
end
|
data/test/update_test.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class UpdateTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_update_takes_a_table_name
|
6
|
+
assert_equal "update account_info", Update[:account_info].to_sql
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_update_set_appends_hash_values
|
10
|
+
assert_equal "update account_info set bar='cat', foo=1", Update[:account_info].set[:foo=>1, :bar=> 'cat'].to_sql
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class WhereBuilderTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_single_equal_where_criteria
|
6
|
+
statement = WhereBuilder.new do
|
7
|
+
equal :'foo.column1', :'bar.column2'
|
8
|
+
end
|
9
|
+
assert_equal ' where foo.column1 = bar.column2', statement.to_sql
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_single_equal_with_array_where_criteria
|
13
|
+
statement = WhereBuilder.new do
|
14
|
+
equal :'foo.column1', ['foo','bar']
|
15
|
+
end
|
16
|
+
assert_equal " where foo.column1 in ('foo', 'bar')", statement.to_sql
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_multiple_equal_with_more_than_one_where_criteria
|
20
|
+
statement = WhereBuilder.new do
|
21
|
+
equal :'foo.column1', :'bar.column2'
|
22
|
+
equal :'foo.column3', :'bar.column5'
|
23
|
+
end
|
24
|
+
expected = " where foo.column1 = bar.column2 and foo.column3 = bar.column5"
|
25
|
+
assert_equal expected, statement.to_sql
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_with_literal_where_criteria
|
29
|
+
statement = WhereBuilder.new do
|
30
|
+
equal 'foo.column1', :'bar.column2'
|
31
|
+
equal :'foo.column3', 'bar.column5'
|
32
|
+
end
|
33
|
+
expected = " where 'foo.column1' = bar.column2 and foo.column3 = 'bar.column5'"
|
34
|
+
assert_equal expected, statement.to_sql
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_exists_evaluates_sql_statement_followed
|
38
|
+
statement = WhereBuilder.new do
|
39
|
+
exists Struct.new(:to_sql).new('select foo')
|
40
|
+
end
|
41
|
+
assert_equal ' where exists (select foo)', statement.to_sql
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_not_exists_evaluates_sql_statement_followed
|
45
|
+
statement = WhereBuilder.new do
|
46
|
+
not_exists Struct.new(:to_sql).new('select foo')
|
47
|
+
end
|
48
|
+
assert_equal ' where not exists (select foo)', statement.to_sql
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: sqldsl
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.2
|
7
|
+
date: 2006-09-16 00:00:00 -04:00
|
8
|
+
summary: A DSL for creating SQL Statements
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: sqldsl-developer@rubyforge.org
|
12
|
+
homepage: http://sqldsl.rubyforge.org
|
13
|
+
rubyforge_project: sqldsl
|
14
|
+
description: A DSL for creating SQL Statements
|
15
|
+
autorequire: sqldsl
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- Jay Fields
|
30
|
+
files:
|
31
|
+
- lib/array.rb
|
32
|
+
- lib/delete.rb
|
33
|
+
- lib/insert.rb
|
34
|
+
- lib/numeric.rb
|
35
|
+
- lib/object.rb
|
36
|
+
- lib/select.rb
|
37
|
+
- lib/sql_statement.rb
|
38
|
+
- lib/sqldsl.rb
|
39
|
+
- lib/string.rb
|
40
|
+
- lib/symbol.rb
|
41
|
+
- lib/time.rb
|
42
|
+
- lib/update.rb
|
43
|
+
- lib/where_builder.rb
|
44
|
+
- test/all_tests.rb
|
45
|
+
- test/array_test.rb
|
46
|
+
- test/delete_acceptance_test.rb
|
47
|
+
- test/delete_test.rb
|
48
|
+
- test/insert_acceptance_test.rb
|
49
|
+
- test/insert_test.rb
|
50
|
+
- test/object_test.rb
|
51
|
+
- test/select_acceptance_test.rb
|
52
|
+
- test/select_test.rb
|
53
|
+
- test/string_test.rb
|
54
|
+
- test/symbol_test.rb
|
55
|
+
- test/test_helper.rb
|
56
|
+
- test/time_test.rb
|
57
|
+
- test/update_acceptance_test.rb
|
58
|
+
- test/update_test.rb
|
59
|
+
- test/where_builder_test.rb
|
60
|
+
- README
|
61
|
+
- README_TEMPLATE
|
62
|
+
test_files:
|
63
|
+
- test/all_tests.rb
|
64
|
+
rdoc_options:
|
65
|
+
- --title
|
66
|
+
- SQL DSL
|
67
|
+
- --main
|
68
|
+
- README
|
69
|
+
- --line-numbers
|
70
|
+
extra_rdoc_files:
|
71
|
+
- README
|
72
|
+
executables: []
|
73
|
+
|
74
|
+
extensions: []
|
75
|
+
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
dependencies: []
|
79
|
+
|