sequel 0.1.3 → 0.1.4

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.
data/CHANGELOG CHANGED
@@ -1,3 +1,18 @@
1
+ *0.1.4*
2
+
3
+ * Added String#split_sql.
4
+
5
+ * Implemented Array#to_sql and String#to_sql. Database#to_sql can now take an array of strings and convert into an SQL string. Comments and excessive white-space are removed.
6
+
7
+ * Improved Schema generator to support data types as method names:
8
+ DB.create_table :test do
9
+ integer :abc
10
+ text :def
11
+ ...
12
+ end
13
+
14
+ * Implemented ODBC adapter.
15
+
1
16
  *0.1.3*
2
17
 
3
18
  * Implemented DBI adapter.
data/README CHANGED
@@ -31,6 +31,7 @@ Sequel currently supports:
31
31
  * Postgresql
32
32
  * SQLite 3
33
33
  * DBI
34
+ * ODBC
34
35
  * MySQL (basic support)
35
36
 
36
37
  == The Sequel Console
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'fileutils'
6
6
  include FileUtils
7
7
 
8
8
  NAME = "sequel"
9
- VERS = "0.1.3"
9
+ VERS = "0.1.4"
10
10
  CLEAN.include ['**/.*.sw?', 'pkg/*', '.config', 'doc/*', 'coverage/*']
11
11
  RDOC_OPTS = ['--quiet', '--title', "Sequel: Concise ORM for Ruby",
12
12
  "--opname", "index.html",
@@ -3,4 +3,29 @@ module Enumerable
3
3
  def send_each(sym, *args)
4
4
  each {|i| i.send(sym, *args)}
5
5
  end
6
- end
6
+ end
7
+
8
+ # Array extensions
9
+ class Array
10
+ # Concatenates an array of strings into an SQL string. ANSI SQL and C-style
11
+ # comments are removed, as well as excessive white-space.
12
+ def to_sql
13
+ map {|l| (l =~ /^(.*)--/ ? $1 : l).chomp}.join(' '). \
14
+ gsub(/\/\*.*\*\//, '').gsub(/\s+/, ' ').strip
15
+ end
16
+ end
17
+
18
+ # String extensions
19
+ class String
20
+ # Converts a string into an SQL string by removing comments.
21
+ # See also Array#to_sql.
22
+ def to_sql
23
+ split($/).to_sql
24
+ end
25
+
26
+ # Splits a string into separate SQL statements, removing comments
27
+ # and excessive white-space.
28
+ def split_sql
29
+ to_sql.split(';').map {|s| s.strip}
30
+ end
31
+ end
@@ -62,7 +62,10 @@ module Sequel
62
62
  raise NotImplementedError
63
63
  end
64
64
 
65
- def <<(sql); execute(sql); end
65
+ # Executes the supplied SQL. The SQL can be supplied as a string or as an
66
+ # array of strings. Comments and excessive white space are removed. See
67
+ # also Array#to_sql.
68
+ def <<(sql); execute(sql.to_sql); end
66
69
 
67
70
  # Acquires a database connection, yielding it to the passed block.
68
71
  def synchronize(&block)
@@ -0,0 +1,108 @@
1
+ if !Object.const_defined?('Sequel')
2
+ require File.join(File.dirname(__FILE__), '../sequel')
3
+ end
4
+
5
+ require 'odbc'
6
+
7
+ module Sequel
8
+ module ODBC
9
+ class Database < Sequel::Database
10
+ set_adapter_scheme :odbc
11
+
12
+ def connect
13
+ conn = ::ODBC::connect(@opts[:database], @opts[:user], @opts[:password])
14
+ conn.autocommit = true
15
+ conn
16
+ end
17
+
18
+ def dataset(opts = nil)
19
+ ODBC::Dataset.new(self, opts)
20
+ end
21
+
22
+ def execute(sql)
23
+ @logger.info(sql) if @logger
24
+ @pool.hold do |conn|
25
+ conn.run(sql)
26
+ end
27
+ end
28
+
29
+ def do(sql)
30
+ @logger.info(sql) if @logger
31
+ @pool.hold do |conn|
32
+ conn.do(sql)
33
+ end
34
+ end
35
+ end
36
+
37
+ class Dataset < Sequel::Dataset
38
+ def literal(v)
39
+ case v
40
+ when true: '1'
41
+ when false: '0'
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ def each(opts = nil, &block)
48
+ @db.synchronize do
49
+ s = @db.execute select_sql(opts)
50
+ begin
51
+ fetch_rows(s, &block)
52
+ s.fetch {|r| yield hash_row(s, r)}
53
+ ensure
54
+ s.drop unless s.nil? rescue nil
55
+ end
56
+ end
57
+ self
58
+ end
59
+
60
+ def fetch_rows(stmt)
61
+ columns = stmt.columns(true).map {|c| c.name.to_sym}
62
+ rows = stmt.fetch_all
63
+ rows.each {|row| yield hash_row(stmt, columns, row)}
64
+ end
65
+
66
+ def hash_row(stmt, columns, row)
67
+ hash = {}
68
+ row.each_with_index do |v, idx|
69
+ hash[columns[idx]] = convert_odbc_value(v)
70
+ end
71
+ hash
72
+ end
73
+
74
+ def convert_odbc_value(v)
75
+ # When fetching a result set, the Ruby ODBC driver converts all ODBC
76
+ # SQL types to an equivalent Ruby type; with the exception of
77
+ # SQL_TYPE_DATE, SQL_TYPE_TIME and SQL_TYPE_TIMESTAMP.
78
+ #
79
+ # The conversions below are consistent with the mappings in
80
+ # ODBCColumn#mapSqlTypeToGenericType and Column#klass.
81
+ case v
82
+ when ::ODBC::TimeStamp
83
+ Time.gm(v.year, v.month, v.day, v.hour, v.minute, v.second)
84
+ when ::ODBC::Time
85
+ DateTime.now
86
+ Time.gm(now.year, now.month, now.day, v.hour, v.minute, v.second)
87
+ when ::ODBC::Date
88
+ Date.new(v.year, v.month, v.day)
89
+ else
90
+ v
91
+ end
92
+ end
93
+
94
+ def insert(*values)
95
+ @db.do insert_sql(*values)
96
+ end
97
+
98
+ def update(values, opts = nil)
99
+ @db.do update_sql(values, opts)
100
+ self
101
+ end
102
+
103
+ def delete(opts = nil)
104
+ @db.do delete_sql(opts)
105
+ end
106
+ end
107
+ end
108
+ end
data/lib/sequel/schema.rb CHANGED
@@ -88,6 +88,11 @@ module Sequel
88
88
  instance_eval(&block)
89
89
  end
90
90
 
91
+ def method_missing(type, name = nil, opts = nil)
92
+ return super unless name
93
+ column(name, type, opts)
94
+ end
95
+
91
96
  def primary_key(name, type = nil, opts = nil)
92
97
  @primary_key = {
93
98
  :name => name,
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: sequel
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.3
7
- date: 2007-05-20 00:00:00 +03:00
6
+ version: 0.1.4
7
+ date: 2007-05-25 00:00:00 +03:00
8
8
  summary: Concise ORM for Ruby.
9
9
  require_paths:
10
10
  - lib
@@ -33,21 +33,23 @@ files:
33
33
  - README
34
34
  - Rakefile
35
35
  - bin/sequel
36
+ - doc/rdoc
36
37
  - lib/sequel
37
- - lib/sequel/pretty_table.rb
38
- - lib/sequel/model.rb
39
- - lib/sequel/schema.rb
40
- - lib/sequel/sqlite.rb
41
- - lib/sequel/database.rb
42
- - lib/sequel/dataset.rb
43
- - lib/sequel/mysql.rb
44
- - lib/sequel/postgres.rb
38
+ - lib/sequel.rb
45
39
  - lib/sequel/connection_pool.rb
46
40
  - lib/sequel/core_ext.rb
41
+ - lib/sequel/database.rb
42
+ - lib/sequel/dataset.rb
43
+ - lib/sequel/dbi.rb
47
44
  - lib/sequel/error.rb
48
45
  - lib/sequel/expressions.rb
49
- - lib/sequel/dbi.rb
50
- - lib/sequel.rb
46
+ - lib/sequel/model.rb
47
+ - lib/sequel/mysql.rb
48
+ - lib/sequel/odbc.rb
49
+ - lib/sequel/postgres.rb
50
+ - lib/sequel/pretty_table.rb
51
+ - lib/sequel/schema.rb
52
+ - lib/sequel/sqlite.rb
51
53
  - CHANGELOG
52
54
  test_files: []
53
55