activerecord-fb-adapter 0.8.9 → 0.9.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.
@@ -0,0 +1,73 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class FbColumn < Column # :nodoc:
4
+ def initialize(name, domain, type, sub_type, length, precision, scale, default_source, null_flag)
5
+ @firebird_type = ::Fb::SqlType.from_code(type, sub_type || 0)
6
+ super(name.downcase, nil, @firebird_type, !null_flag)
7
+ @default = parse_default(default_source) if default_source
8
+ case @firebird_type
9
+ when 'VARCHAR', 'CHAR'
10
+ @limit = length
11
+ when 'DECIMAL', 'NUMERIC'
12
+ @precision, @scale = precision, scale.abs
13
+ end
14
+ @domain, @sub_type = domain, sub_type
15
+ end
16
+
17
+ def type
18
+ if @domain =~ /BOOLEAN/
19
+ :boolean
20
+ elsif @type == :binary and @sub_type == 1
21
+ :text
22
+ else
23
+ @type
24
+ end
25
+ end
26
+
27
+ # Submits a _CAST_ query to the database, casting the default value to the specified SQL type.
28
+ # This enables Firebird to provide an actual value when context variables are used as column
29
+ # defaults (such as CURRENT_TIMESTAMP).
30
+ def default
31
+ if @default
32
+ sql = "SELECT CAST(#{@default} AS #{column_def}) FROM RDB$DATABASE"
33
+ connection = ActiveRecord::Base.connection
34
+ if connection
35
+ value = connection.raw_connection.query(:hash, sql)[0]['cast']
36
+ return nil if value.acts_like?(:date) || value.acts_like?(:time)
37
+ type_cast(value)
38
+ else
39
+ raise ConnectionNotEstablished, "No Firebird connections established."
40
+ end
41
+ end
42
+ end
43
+
44
+ def self.value_to_boolean(value)
45
+ %W(#{FbAdapter.boolean_domain[:true]} true t 1).include? value.to_s.downcase
46
+ end
47
+
48
+ private
49
+
50
+ def parse_default(default_source)
51
+ default_source =~ /^\s*DEFAULT\s+(.*)\s*$/i
52
+ return $1 unless $1.upcase == "NULL"
53
+ end
54
+
55
+ def column_def
56
+ case @firebird_type
57
+ when 'CHAR', 'VARCHAR' then "#{@firebird_type}(#{@limit})"
58
+ when 'NUMERIC', 'DECIMAL' then "#{@firebird_type}(#{@precision},#{@scale.abs})"
59
+ #when 'DOUBLE' then "DOUBLE PRECISION"
60
+ else @firebird_type
61
+ end
62
+ end
63
+
64
+ def simplified_type(field_type)
65
+ if field_type == 'TIMESTAMP'
66
+ :datetime
67
+ else
68
+ super
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveRecord
2
+ class Base
3
+ def self.fb_connection(config) # :nodoc:
4
+ config = config.symbolize_keys.reverse_merge(:downcase_names => true)
5
+ fail ArgumentError, 'No database specified. Missing argument: database.' unless config[:database]
6
+ if db_host = config[:host]
7
+ config[:database] = File.expand_path(config[:database]) if db_host =~ /localhost/i
8
+ config[:database].prepend "#{db_host}/#{config[:port] || 3050}:"
9
+ end
10
+ require 'fb'
11
+ db = ::Fb::Database.new(config)
12
+ begin
13
+ connection = db.connect
14
+ rescue
15
+ unless config[:create]
16
+ require 'pp'
17
+ pp config
18
+ raise ConnectionNotEstablished, "No Firebird connections established."
19
+ end
20
+ connection = db.create.connect
21
+ end
22
+ ConnectionAdapters::FbAdapter.new(connection, logger, config)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,43 @@
1
+ module Arel
2
+ module Visitors
3
+ class Fb < Arel::Visitors::ToSql
4
+ private
5
+
6
+ def visit_Arel_Nodes_SelectStatement o, *a
7
+ select_core = o.cores.map { |x| visit_Arel_Nodes_SelectCore(x, *a) }.join
8
+ select_core.sub!(/^\s*SELECT/i, "SELECT #{visit(o.offset)}") if o.offset && !o.limit
9
+ [
10
+ select_core,
11
+ ("ORDER BY #{o.orders.map { |x| visit(x) }.join(', ')}" unless o.orders.empty?),
12
+ (limit_offset(o) if o.limit && o.offset),
13
+ (visit(o.limit) if o.limit && !o.offset),
14
+ ].compact.join ' '
15
+ end
16
+
17
+ def visit_Arel_Nodes_UpdateStatement o, *a
18
+ [
19
+ "UPDATE #{visit o.relation}",
20
+ ("SET #{o.values.map { |value| visit(value) }.join ', '}" unless o.values.empty?),
21
+ ("WHERE #{o.wheres.map { |x| visit(x) }.join ' AND '}" unless o.wheres.empty?),
22
+ (visit(o.limit) if o.limit),
23
+ ].compact.join ' '
24
+ end
25
+
26
+ def visit_Arel_Nodes_Limit o, *a
27
+ "ROWS #{visit(o.expr)}"
28
+ end
29
+
30
+ def visit_Arel_Nodes_Offset o, *a
31
+ "SKIP #{visit(o.expr)}"
32
+ end
33
+
34
+ # Firebird helpers
35
+
36
+ def limit_offset(o)
37
+ "ROWS #{visit(o.offset.expr) + 1} TO #{visit(o.offset.expr) + visit(o.limit.expr)}"
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ Arel::Visitors::VISITORS['fb'] = Arel::Visitors::Fb
metadata CHANGED
@@ -1,63 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-fb-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.9
5
- prerelease:
4
+ version: 0.9.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Brent Rowland
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-08-07 00:00:00.000000000 Z
11
+ date: 2014-12-01 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: fb
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
- version: 0.7.1
19
+ version: 0.7.4
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
- version: 0.7.1
26
+ version: 0.7.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.0
30
41
  description:
31
42
  email: rowland@rowlandresearch.com
32
43
  executables: []
33
44
  extensions: []
34
45
  extra_rdoc_files: []
35
46
  files:
47
+ - README.md
48
+ - lib/active_record/connection_adapters/fb/database_limits.rb
49
+ - lib/active_record/connection_adapters/fb/database_statements.rb
50
+ - lib/active_record/connection_adapters/fb/quoting.rb
51
+ - lib/active_record/connection_adapters/fb/schema_statements.rb
52
+ - lib/active_record/connection_adapters/fb/table_definition.rb
36
53
  - lib/active_record/connection_adapters/fb_adapter.rb
54
+ - lib/active_record/connection_adapters/fb_column.rb
55
+ - lib/active_record/fb_base.rb
56
+ - lib/arel/visitors/fb.rb
37
57
  homepage: http://github.com/rowland/activerecord-fb-adapter
38
58
  licenses:
39
59
  - MIT
60
+ metadata: {}
40
61
  post_install_message:
41
62
  rdoc_options: []
42
63
  require_paths:
43
64
  - lib
44
65
  required_ruby_version: !ruby/object:Gem::Requirement
45
- none: false
46
66
  requirements:
47
- - - ! '>='
67
+ - - '>='
48
68
  - !ruby/object:Gem::Version
49
69
  version: '0'
50
70
  required_rubygems_version: !ruby/object:Gem::Requirement
51
- none: false
52
71
  requirements:
53
- - - ! '>='
72
+ - - '>='
54
73
  - !ruby/object:Gem::Version
55
74
  version: '0'
56
75
  requirements:
57
76
  - Firebird library fb
58
77
  rubyforge_project:
59
- rubygems_version: 1.8.23
78
+ rubygems_version: 2.2.2
60
79
  signing_key:
61
- specification_version: 3
80
+ specification_version: 4
62
81
  summary: ActiveRecord Firebird Adapter for Rails 3 and 4 with support for migrations.
63
82
  test_files: []
83
+ has_rdoc: false