Skip to content

Commit 291162a

Browse files
committed
Move SQLite3Column into its own file
- Cleans up JdbcColumn - Override new_column_from_field method to have it select the correct column class - Add auto_increment tracking to the class
1 parent 379ea1e commit 291162a

File tree

3 files changed

+127
-112
lines changed

3 files changed

+127
-112
lines changed

lib/arjdbc/jdbc/column.rb

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,11 @@ module Jdbc
1212
# specific type.
1313
# @see JdbcAdapter#jdbc_column_class
1414
class JdbcColumn < Column
15-
# @deprecated attribute writers will be removed in 1.4
16-
attr_writer :limit, :precision # unless ArJdbc::AR42
17-
18-
def initialize(config, name, *args)
19-
if self.class == JdbcColumn
20-
# NOTE: extending classes do not want this if they do they shall call
21-
call_discovered_column_callbacks(config) if config
22-
default = args.shift
23-
else # for extending classes allow ignoring first argument :
24-
if ! config.nil? && ! config.is_a?(Hash)
25-
default = name; name = config # initialize(name, default, *args)
26-
else
27-
default = args.shift
28-
end
29-
end
30-
31-
super(name, default, *args)
32-
init_column(name, default, *args)
33-
end
34-
35-
# Additional column initialization for sub-classes.
36-
def init_column(*args); end
3715

3816
# Similar to `ActiveRecord`'s `extract_value_from_default(default)`.
3917
# @return default value for a column (possibly extracted from driver value)
4018
def default_value(value); value; end
4119

42-
protected
43-
44-
# @private
45-
def call_discovered_column_callbacks(config)
46-
dialect = (config[:dialect] || config[:driver]).to_s
47-
for matcher, block in self.class.column_types
48-
block.call(config, self) if matcher === dialect
49-
end
50-
end
51-
52-
public
53-
5420
# Returns the available column types
5521
# @return [Hash] of (matcher, block) pairs
5622
def self.column_types

lib/arjdbc/sqlite3/adapter.rb

Lines changed: 24 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require "active_record/connection_adapters/sqlite3/schema_dumper"
1717
require "active_record/connection_adapters/sqlite3/schema_statements"
1818
require "active_support/core_ext/class/attribute"
19+
require "arjdbc/sqlite3/column"
1920

2021
module SQLite3
2122
module Constants
@@ -403,6 +404,26 @@ def check_version
403404
end
404405
end
405406

407+
def new_column_from_field(table_name, field, definitions)
408+
default = field["dflt_value"]
409+
410+
type_metadata = fetch_type_metadata(field["type"])
411+
default_value = extract_value_from_default(default)
412+
default_function = extract_default_function(default_value, default)
413+
rowid = is_column_the_rowid?(field, definitions)
414+
415+
ActiveRecord::ConnectionAdapters::SQLite3Column.new(
416+
field["name"],
417+
default_value,
418+
type_metadata,
419+
field["notnull"].to_i == 0,
420+
default_function,
421+
collation: field["collation"],
422+
auto_increment: field["auto_increment"],
423+
rowid: rowid
424+
)
425+
end
426+
406427
private
407428
# See https://www.sqlite.org/limits.html,
408429
# the default value is 999 when not configured.
@@ -523,10 +544,9 @@ def copy_table(from, to, options = {})
523544
primary_key: column_name == from_primary_key
524545
}
525546

526-
# FIXME: This requires changes to the Column class
527-
# unless column.auto_increment?
528-
# column_options[:default] = default
529-
# end
547+
unless column.auto_increment?
548+
column_options[:default] = default
549+
end
530550

531551
column_type = column.bigint? ? :bigint : column.type
532552
@definition.column(column_name, column_type, **column_options)
@@ -676,80 +696,6 @@ def configure_connection
676696
end
677697

678698
module ActiveRecord::ConnectionAdapters
679-
class SQLite3Column < JdbcColumn
680-
def initialize(name, *args)
681-
if Hash === name
682-
super
683-
else
684-
super(nil, name, *args)
685-
end
686-
end
687-
688-
def self.string_to_binary(value)
689-
value
690-
end
691-
692-
def self.binary_to_string(value)
693-
if value.respond_to?(:encoding) && value.encoding != Encoding::ASCII_8BIT
694-
value = value.force_encoding(Encoding::ASCII_8BIT)
695-
end
696-
value
697-
end
698-
699-
# @override {ActiveRecord::ConnectionAdapters::JdbcColumn#init_column}
700-
def init_column(name, default, *args)
701-
if default =~ /NULL/
702-
@default = nil
703-
else
704-
super
705-
end
706-
end
707-
708-
# @override {ActiveRecord::ConnectionAdapters::JdbcColumn#default_value}
709-
def default_value(value)
710-
# JDBC returns column default strings with actual single quotes :
711-
return $1 if value =~ /^'(.*)'$/
712-
713-
value
714-
end
715-
716-
# @override {ActiveRecord::ConnectionAdapters::Column#type_cast}
717-
def type_cast(value)
718-
return nil if value.nil?
719-
case type
720-
when :string then value
721-
when :primary_key
722-
value.respond_to?(:to_i) ? value.to_i : ( value ? 1 : 0 )
723-
when :float then value.to_f
724-
when :decimal then self.class.value_to_decimal(value)
725-
when :boolean then self.class.value_to_boolean(value)
726-
else super
727-
end
728-
end
729-
730-
private
731-
732-
# @override {ActiveRecord::ConnectionAdapters::Column#extract_limit}
733-
def extract_limit(sql_type)
734-
return nil if sql_type =~ /^(real)\(\d+/i
735-
super
736-
end
737-
738-
def extract_precision(sql_type)
739-
case sql_type
740-
when /^(real)\((\d+)(,\d+)?\)/i then $2.to_i
741-
else super
742-
end
743-
end
744-
745-
def extract_scale(sql_type)
746-
case sql_type
747-
when /^(real)\((\d+)\)/i then 0
748-
when /^(real)\((\d+)(,(\d+))\)/i then $4.to_i
749-
else super
750-
end
751-
end
752-
end
753699

754700
remove_const(:SQLite3Adapter) if const_defined?(:SQLite3Adapter)
755701

lib/arjdbc/sqlite3/column.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveRecord::ConnectionAdapters
4+
class SQLite3Column < JdbcColumn
5+
6+
attr_reader :rowid
7+
8+
def initialize(name, default, sql_type_metadata = nil, null = true, default_function = nil, collation: nil, comment: nil, auto_increment: nil, rowid: false, **)
9+
super
10+
@auto_increment = auto_increment
11+
@default = nil if default =~ /NULL/
12+
@rowid = rowid
13+
end
14+
15+
def self.string_to_binary(value)
16+
value
17+
end
18+
19+
def self.binary_to_string(value)
20+
if value.respond_to?(:encoding) && value.encoding != Encoding::ASCII_8BIT
21+
value = value.force_encoding(Encoding::ASCII_8BIT)
22+
end
23+
value
24+
end
25+
26+
# @override {ActiveRecord::ConnectionAdapters::JdbcColumn#default_value}
27+
def default_value(value)
28+
# JDBC returns column default strings with actual single quotes :
29+
return $1 if value =~ /^'(.*)'$/
30+
31+
value
32+
end
33+
34+
def auto_increment?
35+
@auto_increment
36+
end
37+
38+
def auto_incremented_by_db?
39+
auto_increment? || rowid
40+
end
41+
42+
def init_with(coder)
43+
@auto_increment = coder["auto_increment"]
44+
super
45+
end
46+
47+
def encode_with(coder)
48+
coder["auto_increment"] = @auto_increment
49+
super
50+
end
51+
52+
def ==(other)
53+
other.is_a?(Column) &&
54+
super &&
55+
auto_increment? == other.auto_increment?
56+
end
57+
alias :eql? :==
58+
59+
def hash
60+
Column.hash ^
61+
super.hash ^
62+
auto_increment?.hash ^
63+
rowid.hash
64+
end
65+
66+
# @override {ActiveRecord::ConnectionAdapters::Column#type_cast}
67+
def type_cast(value)
68+
return nil if value.nil?
69+
case type
70+
when :string then value
71+
when :primary_key
72+
value.respond_to?(:to_i) ? value.to_i : ( value ? 1 : 0 )
73+
when :float then value.to_f
74+
when :decimal then self.class.value_to_decimal(value)
75+
when :boolean then self.class.value_to_boolean(value)
76+
else super
77+
end
78+
end
79+
80+
private
81+
82+
# @override {ActiveRecord::ConnectionAdapters::Column#extract_limit}
83+
def extract_limit(sql_type)
84+
return nil if sql_type =~ /^(real)\(\d+/i
85+
super
86+
end
87+
88+
def extract_precision(sql_type)
89+
case sql_type
90+
when /^(real)\((\d+)(,\d+)?\)/i then $2.to_i
91+
else super
92+
end
93+
end
94+
95+
def extract_scale(sql_type)
96+
case sql_type
97+
when /^(real)\((\d+)\)/i then 0
98+
when /^(real)\((\d+)(,(\d+))\)/i then $4.to_i
99+
else super
100+
end
101+
end
102+
end
103+
end

0 commit comments

Comments
 (0)