sequel 0.5.0.2 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/COPYING +18 -18
  2. data/Rakefile +17 -98
  3. data/lib/sequel.rb +2 -71
  4. metadata +10 -108
  5. data/CHANGELOG +0 -989
  6. data/bin/sequel +0 -41
  7. data/lib/sequel/adapters/adapter_skeleton.rb +0 -68
  8. data/lib/sequel/adapters/ado.rb +0 -100
  9. data/lib/sequel/adapters/db2.rb +0 -158
  10. data/lib/sequel/adapters/dbi.rb +0 -126
  11. data/lib/sequel/adapters/informix.rb +0 -87
  12. data/lib/sequel/adapters/jdbc.rb +0 -108
  13. data/lib/sequel/adapters/mysql.rb +0 -269
  14. data/lib/sequel/adapters/odbc.rb +0 -145
  15. data/lib/sequel/adapters/odbc_mssql.rb +0 -93
  16. data/lib/sequel/adapters/openbase.rb +0 -90
  17. data/lib/sequel/adapters/oracle.rb +0 -99
  18. data/lib/sequel/adapters/postgres.rb +0 -519
  19. data/lib/sequel/adapters/sqlite.rb +0 -192
  20. data/lib/sequel/ado.rb +0 -6
  21. data/lib/sequel/array_keys.rb +0 -296
  22. data/lib/sequel/connection_pool.rb +0 -152
  23. data/lib/sequel/core_ext.rb +0 -59
  24. data/lib/sequel/core_sql.rb +0 -191
  25. data/lib/sequel/database.rb +0 -433
  26. data/lib/sequel/dataset.rb +0 -409
  27. data/lib/sequel/dataset/convenience.rb +0 -321
  28. data/lib/sequel/dataset/sequelizer.rb +0 -354
  29. data/lib/sequel/dataset/sql.rb +0 -586
  30. data/lib/sequel/db2.rb +0 -6
  31. data/lib/sequel/dbi.rb +0 -6
  32. data/lib/sequel/exceptions.rb +0 -45
  33. data/lib/sequel/informix.rb +0 -6
  34. data/lib/sequel/migration.rb +0 -191
  35. data/lib/sequel/model.rb +0 -8
  36. data/lib/sequel/mysql.rb +0 -6
  37. data/lib/sequel/odbc.rb +0 -6
  38. data/lib/sequel/oracle.rb +0 -6
  39. data/lib/sequel/postgres.rb +0 -6
  40. data/lib/sequel/pretty_table.rb +0 -73
  41. data/lib/sequel/schema.rb +0 -8
  42. data/lib/sequel/schema/schema_generator.rb +0 -131
  43. data/lib/sequel/schema/schema_sql.rb +0 -131
  44. data/lib/sequel/sqlite.rb +0 -6
  45. data/lib/sequel/worker.rb +0 -58
  46. data/spec/adapters/informix_spec.rb +0 -139
  47. data/spec/adapters/mysql_spec.rb +0 -330
  48. data/spec/adapters/oracle_spec.rb +0 -130
  49. data/spec/adapters/postgres_spec.rb +0 -189
  50. data/spec/adapters/sqlite_spec.rb +0 -345
  51. data/spec/array_keys_spec.rb +0 -679
  52. data/spec/connection_pool_spec.rb +0 -356
  53. data/spec/core_ext_spec.rb +0 -67
  54. data/spec/core_sql_spec.rb +0 -301
  55. data/spec/database_spec.rb +0 -811
  56. data/spec/dataset_spec.rb +0 -2381
  57. data/spec/migration_spec.rb +0 -261
  58. data/spec/pretty_table_spec.rb +0 -66
  59. data/spec/rcov.opts +0 -4
  60. data/spec/schema_generator_spec.rb +0 -86
  61. data/spec/schema_spec.rb +0 -230
  62. data/spec/sequel_spec.rb +0 -10
  63. data/spec/sequelizer_spec.rb +0 -389
  64. data/spec/spec.opts +0 -5
  65. data/spec/spec_helper.rb +0 -44
  66. data/spec/worker_spec.rb +0 -96
data/lib/sequel/db2.rb DELETED
@@ -1,6 +0,0 @@
1
- warn "Requiring 'sequel/db2' is deprecated. Please modify your code to require 'sequel' instead."
2
-
3
- if !Object.const_defined?('Sequel')
4
- require File.join(File.dirname(__FILE__), '../sequel')
5
- end
6
- require File.join(File.dirname(__FILE__), 'adapters/db2')
data/lib/sequel/dbi.rb DELETED
@@ -1,6 +0,0 @@
1
- warn "Requiring 'sequel/dbi' is deprecated. Please modify your code to require 'sequel' instead."
2
-
3
- if !Object.const_defined?('Sequel')
4
- require File.join(File.dirname(__FILE__), '../sequel')
5
- end
6
- require File.join(File.dirname(__FILE__), 'adapters/dbi')
@@ -1,45 +0,0 @@
1
- module Sequel
2
- # Represents an error raised in Sequel code.
3
- class Error < StandardError
4
-
5
- # Rollback is a special error used to rollback a transactions.
6
- # A transaction block will catch this error and wont pass further up the stack.
7
- class Rollback < Error ; end
8
-
9
- # Represents an invalid value stored in the database.
10
- class InvalidValue < Error ; end
11
-
12
- # Represents an Invalid transform.
13
- class InvalidTransform < Error ; end
14
-
15
- # Represents an Invalid filter.
16
- class InvalidFilter < Error ; end
17
-
18
- class InvalidExpression < Error; end
19
-
20
- # Represents an attempt to performing filter operations when no filter has been specified yet.
21
- class NoExistingFilter < Error ; end
22
-
23
- # Represents an invalid join type.
24
- class InvalidJoinType < Error ; end
25
-
26
- class WorkerStop < RuntimeError ; end
27
-
28
- # Raised when Sequel is unable to load a specified adapter.
29
- class AdapterNotFound < Error ; end
30
- end
31
- end
32
-
33
- # Object extensions
34
- class Object
35
- # Cancels the current transaction without an error:
36
- #
37
- # DB.tranaction do
38
- # ...
39
- # rollback! if failed_to_contact_client
40
- # ...
41
- # end
42
- def rollback!
43
- raise Sequel::Error::Rollback
44
- end
45
- end
@@ -1,6 +0,0 @@
1
- warn "Requiring 'sequel/informix' is deprecated. Please modify your code to require 'sequel' instead."
2
-
3
- if !Object.const_defined?('Sequel')
4
- require File.join(File.dirname(__FILE__), '../sequel')
5
- end
6
- require File.join(File.dirname(__FILE__), 'adapters/informix')
@@ -1,191 +0,0 @@
1
- # The migration code is based on work by Florian Aßmann:
2
- # http://code.google.com/p/ruby-sequel/issues/detail?id=23
3
-
4
- module Sequel
5
- # The Migration class describes a database migration that can be reversed.
6
- # The migration looks very similar to ActiveRecord (Rails) migrations, e.g.:
7
- #
8
- # class CreateSessions < Sequel::Migration
9
- # def up
10
- # create_table :sessions do
11
- # primary_key :id
12
- # varchar :session_id, :size => 32, :unique => true
13
- # timestamp :created_at
14
- # text :data
15
- # end
16
- # end
17
- #
18
- # def down
19
- # execute 'DROP TABLE sessions'
20
- # end
21
- # end
22
- #
23
- # To apply a migration to a database, you can invoke the #apply with
24
- # the target database instance and the direction :up or :down, e.g.:
25
- #
26
- # DB = Sequel.open ('sqlite:///mydb')
27
- # CreateSessions.apply(DB, :up)
28
- #
29
- class Migration
30
- # Creates a new instance of the migration and sets the @db attribute.
31
- def initialize(db)
32
- @db = db
33
- end
34
-
35
- # Adds the new migration class to the list of Migration descendants.
36
- def self.inherited(base)
37
- descendants << base
38
- end
39
-
40
- # Returns the list of Migration descendants.
41
- def self.descendants
42
- @descendants ||= []
43
- end
44
-
45
- def up; end #:nodoc:
46
- def down; end #:nodoc:
47
-
48
- # Applies the migration to the supplied database in the specified
49
- # direction.
50
- def self.apply(db, direction)
51
- obj = new(db)
52
- case direction
53
- when :up
54
- obj.up
55
- when :down
56
- obj.down
57
- else
58
- raise ArgumentError, "Invalid migration direction specified (#{direction.inspect})"
59
- end
60
- end
61
-
62
- # Intercepts method calls intended for the database and sends them along.
63
- def method_missing(method_sym, *args, &block)
64
- @db.send method_sym, *args, &block
65
- end
66
- end
67
-
68
- # The Migrator module performs migrations based on migration files in a
69
- # specified directory. The migration files should be named using the
70
- # following pattern (in similar fashion to ActiveRecord migrations):
71
- #
72
- # <version>_<title>.rb
73
- #
74
- # For example, the following files are considered migration files:
75
- #
76
- # 001_create_sessions.rb
77
- # 002_add_data_column.rb
78
- # ...
79
- #
80
- # The migration files should contain one or more migration classes based
81
- # on Sequel::Migration.
82
- #
83
- # To apply a migration, the #apply method must be invoked with the database
84
- # instance, the directory of migration files and the target version. If
85
- # no current version is supplied, it is read from the database. The migrator
86
- # automatically creates a schema_info table in the database to keep track
87
- # of the current migration version. If no migration version is stored in the
88
- # database, the version is considered to be 0. If no target version is
89
- # specified, the database is migrated to the latest version available in the
90
- # migration directory.
91
- #
92
- # For example, to migrate the database to the latest version:
93
- #
94
- # Sequel::Migrator.apply(DB, '.')
95
- #
96
- # To migrate the database from version 1 to version 5:
97
- #
98
- # Sequel::Migrator.apply(DB, '.', 5, 1)
99
- #
100
- module Migrator
101
- # Migrates the supplied database in the specified directory from the
102
- # current version to the target version. If no current version is
103
- # supplied, it is extracted from a schema_info table. The schema_info
104
- # table is automatically created and maintained by the apply function.
105
- def self.apply(db, directory, target = nil, current = nil)
106
- # determine current and target version and direction
107
- current ||= get_current_migration_version(db)
108
- target ||= latest_migration_version(directory)
109
- raise Error, "No current version available" if current.nil?
110
- raise Error, "No target version available" if target.nil?
111
-
112
- direction = current < target ? :up : :down
113
-
114
- classes = migration_classes(directory, target, current, direction)
115
-
116
- db.transaction do
117
- classes.each {|c| c.apply(db, direction)}
118
- set_current_migration_version(db, target)
119
- end
120
-
121
- target
122
- end
123
-
124
- # Returns a list of migration classes filtered for the migration range and
125
- # ordered according to the migration direction.
126
- def self.migration_classes(directory, target, current, direction)
127
- range = direction == :up ?
128
- (current + 1)..target : (target + 1)..current
129
-
130
- # Remove class definitions
131
- Migration.descendants.each do |c|
132
- Object.send(:remove_const, c.to_s) rescue nil
133
- end
134
- Migration.descendants.clear # remove any defined migration classes
135
-
136
- # load migration files
137
- migration_files(directory, range).each {|fn| load(fn)}
138
-
139
- # get migration classes
140
- classes = Migration.descendants
141
- classes.reverse! if direction == :down
142
- classes
143
- end
144
-
145
- MIGRATION_FILE_PATTERN = '[0-9][0-9][0-9]_*.rb'.freeze
146
-
147
- # Returns any found migration files in the supplied directory.
148
- def self.migration_files(directory, range = nil)
149
- pattern = File.join(directory, MIGRATION_FILE_PATTERN)
150
- files = Dir[pattern].inject([]) do |m, path|
151
- m[File.basename(path).to_i] = path
152
- m
153
- end
154
- filtered = range ? files[range] : files
155
- filtered ? filtered.compact : []
156
- end
157
-
158
- # Returns the latest version available in the specified directory.
159
- def self.latest_migration_version(directory)
160
- l = migration_files(directory).last
161
- l ? File.basename(l).to_i : nil
162
- end
163
-
164
- # Gets the current migration version stored in the database. If no version
165
- # number is stored, 0 is returned.
166
- def self.get_current_migration_version(db)
167
- r = schema_info_dataset(db).first
168
- r ? r[:version] : 0
169
- end
170
-
171
- # Sets the current migration version stored in the database.
172
- def self.set_current_migration_version(db, version)
173
- dataset = schema_info_dataset(db)
174
- if dataset.first
175
- dataset.update(:version => version)
176
- else
177
- dataset << {:version => version}
178
- end
179
- end
180
-
181
- # Returns the dataset for the schema_info table. If no such table
182
- # exists, it is automatically created.
183
- def self.schema_info_dataset(db)
184
- unless db.table_exists?(:schema_info)
185
- db.create_table(:schema_info) {integer :version}
186
- end
187
-
188
- db[:schema_info]
189
- end
190
- end
191
- end
data/lib/sequel/model.rb DELETED
@@ -1,8 +0,0 @@
1
- module Sequel
2
- class Model
3
- def self.database_opened(db)
4
- @db = db if (self == Model) && !@db
5
- end
6
- end
7
- end
8
-
data/lib/sequel/mysql.rb DELETED
@@ -1,6 +0,0 @@
1
- warn "Requiring 'sequel/mysql' is deprecated. Please modify your code to require 'sequel' instead."
2
-
3
- if !Object.const_defined?('Sequel')
4
- require File.join(File.dirname(__FILE__), '../sequel')
5
- end
6
- require File.join(File.dirname(__FILE__), 'adapters/mysql')
data/lib/sequel/odbc.rb DELETED
@@ -1,6 +0,0 @@
1
- warn "Requiring 'sequel/odbc' is deprecated. Please modify your code to require 'sequel' instead."
2
-
3
- if !Object.const_defined?('Sequel')
4
- require File.join(File.dirname(__FILE__), '../sequel')
5
- end
6
- require File.join(File.dirname(__FILE__), 'adapters/odbc')
data/lib/sequel/oracle.rb DELETED
@@ -1,6 +0,0 @@
1
- warn "Requiring 'sequel/oracle' is deprecated. Please modify your code to require 'sequel' instead."
2
-
3
- if !Object.const_defined?('Sequel')
4
- require File.join(File.dirname(__FILE__), '../sequel')
5
- end
6
- require File.join(File.dirname(__FILE__), 'adapters/oracle')
@@ -1,6 +0,0 @@
1
- warn "Requiring 'sequel/postgres' is deprecated. Please modify your code to require 'sequel' instead."
2
-
3
- if !Object.const_defined?('Sequel')
4
- require File.join(File.dirname(__FILE__), '../sequel')
5
- end
6
- require File.join(File.dirname(__FILE__), 'adapters/postgres')
@@ -1,73 +0,0 @@
1
- module Sequel
2
- # Prints nice-looking plain-text tables
3
- # +--+-------+
4
- # |id|name |
5
- # |--+-------|
6
- # |1 |fasdfas|
7
- # |2 |test |
8
- # +--+-------+
9
- module PrettyTable
10
- def self.records_columns(records)
11
- columns = []
12
- records.each do |r|
13
- if Array === r && (k = r.keys)
14
- return k
15
- elsif Hash === r
16
- r.keys.each {|k| columns << k unless columns.include?(k)}
17
- end
18
- end
19
- columns
20
- end
21
-
22
- def self.column_sizes(records, columns)
23
- sizes = Hash.new {0}
24
- columns.each do |c|
25
- s = c.to_s.size
26
- sizes[c.to_sym] = s if s > sizes[c.to_sym]
27
- end
28
- records.each do |r|
29
- columns.each do |c|
30
- s = r[c].to_s.size
31
- sizes[c.to_sym] = s if s > sizes[c.to_sym]
32
- end
33
- end
34
- sizes
35
- end
36
-
37
- def self.separator_line(columns, sizes)
38
- l = ''
39
- '+' + columns.map {|c| '-' * sizes[c]}.join('+') + '+'
40
- end
41
-
42
- def self.format_cell(size, v)
43
- case v
44
- when Bignum, Fixnum
45
- "%#{size}d" % v
46
- when Float
47
- "%#{size}g" % v
48
- else
49
- "%-#{size}s" % v.to_s
50
- end
51
- end
52
-
53
- def self.data_line(columns, sizes, record)
54
- '|' + columns.map {|c| format_cell(sizes[c], record[c])}.join('|') + '|'
55
- end
56
-
57
- def self.header_line(columns, sizes)
58
- '|' + columns.map {|c| "%-#{sizes[c]}s" % c.to_s}.join('|') + '|'
59
- end
60
-
61
- def self.print(records, columns = nil) # records is an array of hashes
62
- columns ||= records_columns(records)
63
- sizes = column_sizes(records, columns)
64
-
65
- puts separator_line(columns, sizes)
66
- puts header_line(columns, sizes)
67
- puts separator_line(columns, sizes)
68
- records.each {|r| puts data_line(columns, sizes, r)}
69
- puts separator_line(columns, sizes)
70
- end
71
- end
72
- end
73
-
data/lib/sequel/schema.rb DELETED
@@ -1,8 +0,0 @@
1
- module Sequel
2
- module Schema
3
- end
4
- end
5
-
6
- require File.join(File.dirname(__FILE__), 'schema/schema_sql')
7
- require File.join(File.dirname(__FILE__), 'schema/schema_generator')
8
-
@@ -1,131 +0,0 @@
1
- module Sequel
2
- module Schema
3
- class Generator
4
- def initialize(db, &block)
5
- @db = db
6
- @columns = []
7
- @indexes = []
8
- @primary_key = nil
9
- instance_eval(&block) if block
10
- end
11
-
12
- def method_missing(type, name = nil, opts = {})
13
- name ? column(name, type, opts) : super
14
- end
15
-
16
- def primary_key_name
17
- @primary_key ? @primary_key[:name] : nil
18
- end
19
-
20
- def primary_key(name, *args)
21
- @primary_key = @db.serial_primary_key_options.merge({:name => name})
22
-
23
- if opts = args.pop
24
- opts = {:type => opts} unless opts.is_a?(Hash)
25
- if type = args.pop
26
- opts.merge!(:type => type)
27
- end
28
- @primary_key.merge!(opts)
29
- end
30
- @primary_key
31
- end
32
-
33
- def column(name, type, opts = {})
34
- @columns << {:name => name, :type => type}.merge(opts)
35
- index(name) if opts[:index]
36
- end
37
-
38
- def foreign_key(name, opts = {})
39
- @columns << {:name => name, :type => :integer}.merge(opts)
40
- index(name) if opts[:index]
41
- end
42
-
43
- def has_column?(name)
44
- @columns.each {|c| return true if c[:name] == name}
45
- false
46
- end
47
-
48
- def index(columns, opts = {})
49
- columns = [columns] unless columns.is_a?(Array)
50
- @indexes << {:columns => columns}.merge(opts)
51
- end
52
-
53
- def unique(columns, opts = {})
54
- index(columns, {:unique => true}.merge(opts))
55
- end
56
-
57
- def create_info
58
- if @primary_key && !has_column?(@primary_key[:name])
59
- @columns.unshift(@primary_key)
60
- end
61
- [@columns, @indexes]
62
- end
63
- end
64
-
65
- class AlterTableGenerator
66
- attr_reader :operations
67
-
68
- def initialize(db, &block)
69
- @db = db
70
- @operations = []
71
- instance_eval(&block) if block
72
- end
73
-
74
- def add_column(name, type, opts = {})
75
- @operations << { \
76
- :op => :add_column, \
77
- :name => name, \
78
- :type => type \
79
- }.merge(opts)
80
- end
81
-
82
- def drop_column(name)
83
- @operations << { \
84
- :op => :drop_column, \
85
- :name => name \
86
- }
87
- end
88
-
89
- def rename_column(name, new_name, opts = {})
90
- @operations << { \
91
- :op => :rename_column, \
92
- :name => name, \
93
- :new_name => new_name \
94
- }.merge(opts)
95
- end
96
-
97
- def set_column_type(name, type)
98
- @operations << { \
99
- :op => :set_column_type, \
100
- :name => name, \
101
- :type => type \
102
- }
103
- end
104
-
105
- def set_column_default(name, default)
106
- @operations << { \
107
- :op => :set_column_default, \
108
- :name => name, \
109
- :default => default \
110
- }
111
- end
112
-
113
- def add_index(columns, opts = {})
114
- columns = [columns] unless columns.is_a?(Array)
115
- @operations << { \
116
- :op => :add_index, \
117
- :columns => columns \
118
- }.merge(opts)
119
- end
120
-
121
- def drop_index(columns)
122
- columns = [columns] unless columns.is_a?(Array)
123
- @operations << { \
124
- :op => :drop_index, \
125
- :columns => columns \
126
- }
127
- end
128
- end
129
- end
130
- end
131
-