Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
/spec/reports/
/tmp/
*.gem

*.sublime-*
Binary file modified db/example.fdb
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
module ActiveRecord::ConnectionAdapters::Firebird::DatabaseStatements

def native_database_types
{
primary_key: 'integer not null primary key',
string: { name: 'varchar', limit: 255 },
text: { name: 'blob sub_type text' },
integer: { name: 'integer' },
float: { name: 'float' },
decimal: { name: 'decimal' },
datetime: { name: 'timestamp' },
timestamp: { name: 'timestamp' },
date: { name: 'date' },
binary: { name: 'blob' },
boolean: { name: 'smallint' }
}
end
delegate :boolean_domain, to: 'ActiveRecord::ConnectionAdapters::FirebirdAdapter'

def execute(sql, name = nil)
sql = sql.encode(encoding, 'UTF-8')
Expand Down
24 changes: 24 additions & 0 deletions lib/active_record/connection_adapters/firebird/fb_column.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module ActiveRecord
module ConnectionAdapters
module Firebird
class FbColumn < ActiveRecord::ConnectionAdapters::Column # :nodoc:

attr_reader :domain
def initialize(
name,
default,
sql_type_metadata = nil,
null = true,
table_name = nil,
default_function = nil,
collation = nil,
comment = nil,
firebird_options = {}
)
@domain = firebird_options.domain
super(name, default, sql_type_metadata, null, table_name, default_function, collation, comment: comment)
end
end
end
end
end
36 changes: 33 additions & 3 deletions lib/active_record/connection_adapters/firebird/quoting.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
module ActiveRecord::ConnectionAdapters::Firebird::Quoting
def quoted_date(value)
super.sub(/(\.\d{6})\z/, $1.to_s.first(5))
# frozen_string_literal: true

module ActiveRecord
module ConnectionAdapters
module Firebird
module Quoting

def unquoted_true
boolean_domain[:true]
end

def quoted_true # :nodoc:
quote unquoted_true
end

def unquoted_false
boolean_domain[:false]
end

def quoted_false # :nodoc:
quote unquoted_false
end

def lookup_cast_type_from_column(column) # :nodoc:
sql_type = (column.domain == boolean_domain[:name]) ? 'BOOLEAN' : column.sql_type
type_map.lookup(sql_type)
end

def quoted_date(value)
super.sub(/(\.\d{6})\z/, $1.to_s.first(5))
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,34 @@ def column_definitions(table_name)
end

def new_column_from_field(table_name, field)
type_metadata = fetch_type_metadata(field["sql_type"])
ActiveRecord::ConnectionAdapters::Column.new(field["name"], field["default"], type_metadata, field["nullable"], table_name)
type_metadata = fetch_type_metadata(field["sql_type"], field)
ActiveRecord::ConnectionAdapters::Firebird::FbColumn.new(
field["name"],
field["default"],
type_metadata,
field["nullable"],
table_name,
nil,
nil,
nil,
field
)
end

def fetch_type_metadata(sql_type, field = "")
if field['domain'] == ActiveRecord::ConnectionAdapters::FirebirdAdapter.boolean_domain[:name]
cast_type = lookup_cast_type("boolean")
else
cast_type = lookup_cast_type(sql_type)
end
ActiveRecord::ConnectionAdapters::Firebird::SqlTypeMetadata.new(
sql_type: sql_type,
type: cast_type.type,
precision: cast_type.precision,
scale: cast_type.scale,
limit: cast_type.limit,
field: field
)
end

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module ActiveRecord
module ConnectionAdapters
module Firebird
class SqlTypeMetadata < ActiveRecord::ConnectionAdapters::SqlTypeMetadata

def initialize(sql_type: nil, type: nil, limit: nil, precision: nil, scale: nil, **firebird_options)
@sql_type = sql_type
@type = (firebird_options[:field].domain) ? :boolean : sql_type
@limit = limit
@precision = precision
@scale = scale
@firebird_options = firebird_options
end

protected

def attributes_for_hash
super + [@firebird_options]
end

end
end
end
end
29 changes: 29 additions & 0 deletions lib/active_record/connection_adapters/firebird_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'active_record/connection_adapters/firebird/database_limits'
require 'active_record/connection_adapters/firebird/database_statements'
require 'active_record/connection_adapters/firebird/schema_statements'
require 'active_record/connection_adapters/firebird/sql_type_metadata'
require 'active_record/connection_adapters/firebird/fb_column'
require 'active_record/connection_adapters/firebird/quoting'

require 'arel/visitors/firebird'
Expand All @@ -13,11 +15,38 @@ class ActiveRecord::ConnectionAdapters::FirebirdAdapter < ActiveRecord::Connecti
ADAPTER_NAME = "Firebird".freeze
DEFAULT_ENCODING = "Windows-1252".freeze

include ActiveRecord::ConnectionAdapters::Firebird::Quoting
include ActiveRecord::ConnectionAdapters::Firebird::DatabaseLimits
include ActiveRecord::ConnectionAdapters::Firebird::DatabaseStatements
include ActiveRecord::ConnectionAdapters::Firebird::SchemaStatements
include ActiveRecord::ConnectionAdapters::Firebird::Quoting



@boolean_domain = { name: "smallint", limit: 1, type: "smallint", true: 1, false: 0}

class << self
attr_accessor :boolean_domain
end

NATIVE_DATABASE_TYPES = {
primary_key: 'integer not null primary key',
string: { name: 'varchar', limit: 255 },
text: { name: 'blob sub_type text' },
integer: { name: 'integer' },
float: { name: 'float' },
decimal: { name: 'decimal' },
datetime: { name: 'timestamp' },
timestamp: { name: 'timestamp' },
date: { name: 'date' },
binary: { name: 'blob' },
boolean: { name: ActiveRecord::ConnectionAdapters::FirebirdAdapter.boolean_domain[:name] }
}

def native_database_types
NATIVE_DATABASE_TYPES
end

def arel_visitor
@arel_visitor ||= Arel::Visitors::Firebird.new(self)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/queries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,12 @@
expect(SisTest.where(field_varchar: value).count).to eq 1
end

it 'where with boolean' do
value = false
SisTest.create!(field_boolean: value)

expect(SisTest.where(SisTest.arel_table[:field_boolean].eq(value)))
expect(SisTest.where(field_boolean: value).count).to eq 1
end

end