activerecord-rdb-adapter 0.9.4 → 0.9.6.beta2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,152 +1,152 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- module Rdb
4
- module Quoting # :nodoc:
5
- QUOTED_FALSE = "'false'".freeze
6
- QUOTED_TRUE = "'true'".freeze
7
-
8
- QUOTED_POSITION = '"POSITION"'.freeze
9
- QUOTED_VALUE = '"VALUE"'.freeze
10
-
11
- def quote_string(string) # :nodoc:
12
- string.gsub(/'/, "''")
13
- end
14
-
15
- def quoted_date(time)
16
- if time.is_a?(Time) || time.is_a?(DateTime)
17
- time.localtime.strftime('%d.%m.%Y %H:%M:%S')
18
- else
19
- time.strftime('%d.%m.%Y')
20
- end
21
- end
22
-
23
- def quote_column_name(column_name) # :nodoc:
24
- column = column_name.dup.to_s
25
- column.gsub!(/(?<=[^"\w]|^)position(?=[^"\w]|$)/i, QUOTED_POSITION)
26
- column.gsub!(/(?<=[^"\w]|^)value(?=[^"\w]|$)/i, QUOTED_VALUE)
27
- column.delete!('"')
28
- column.upcase!
29
- @connection.dialect == 1 ? column.to_s : %("#{column}")
30
- end
31
-
32
- def quote_table_name_for_assignment(_table, attr)
33
- quote_column_name(attr)
34
- end
35
-
36
- def unquoted_true
37
- true
38
- end
39
-
40
- def quoted_true # :nodoc:
41
- QUOTED_TRUE
42
- end
43
-
44
- def unquoted_false
45
- false
46
- end
47
-
48
- def quoted_false # :nodoc:
49
- QUOTED_FALSE
50
- end
51
-
52
- def type_cast_from_column(column, value) # :nodoc:
53
- if column
54
- type = column.type || lookup_cast_type_from_column(column)
55
- if type.is_a?(ActiveRecord::Type::Serialized)
56
- value
57
- else
58
- type.serialize(value)
59
- end
60
- else
61
- value
62
- end
63
- end
64
-
65
- def lookup_cast_type_from_column(column) # :nodoc:
66
- type = column.try(:sql_type) || column.try(:type)
67
- lookup_cast_type(type)
68
- end
69
-
70
- def type_casted_binds(binds) # :nodoc:
71
- if binds.first.is_a?(Array)
72
- binds.map { |column, value| type_cast(value, column) }
73
- else
74
- binds.map { |attr| type_cast(attr.value_for_database, attr) }
75
- end
76
- end
77
-
78
- private
79
-
80
- def id_value_for_database(value)
81
- primary_key = value.class.primary_key
82
- if primary_key
83
- value.instance_variable_get(:@attributes)[primary_key].value_for_database
84
- end
85
- end
86
-
87
- def _quote(value)
88
- case value
89
- when Time, DateTime
90
- "'#{value.strftime('%d.%m.%Y %H:%M')}'"
91
- when Date
92
- "'#{value.strftime('%d.%m.%Y')}'"
93
- else
94
- super
95
- end
96
- end
97
-
98
- def _type_cast(value)
99
- case value
100
- when Symbol, ActiveSupport::Multibyte::Chars, Type::Binary::Data
101
- value.to_s
102
- when Array
103
- value.to_yaml
104
- when Hash then
105
- encode_hash(value)
106
- when true then
107
- unquoted_true
108
- when false then
109
- unquoted_false
110
- # BigDecimals need to be put in a non-normalized form and quoted.
111
- when BigDecimal then
112
- value.to_s('F')
113
- when Type::Time::Value then
114
- quoted_time(value)
115
- when Date, Time, DateTime then
116
- quoted_date(value)
117
- when *types_which_need_no_typecasting
118
- value
119
- else
120
- raise TypeError
121
- end
122
- end
123
-
124
- def rdb_to_ar_case(column_name)
125
- /[[:lower:]]/.match?(column_name) ? column_name : column_name.downcase
126
- end
127
-
128
- def ar_to_rdb_case(column_name)
129
- /[[:upper:]]/.match?(column_name) ? column_name : column_name.upcase
130
- end
131
-
132
- def encode_hash(value)
133
- if value.is_a?(Hash)
134
- value.to_yaml
135
- else
136
- value
137
- end
138
- end
139
-
140
- if defined? Encoding
141
- def decode(str)
142
- Base64.decode64(str).force_encoding(@connection.encoding)
143
- end
144
- else
145
- def decode(str)
146
- Base64.decode64(str)
147
- end
148
- end
149
- end
150
- end
151
- end
152
- end
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module Rdb
4
+ module Quoting # :nodoc:
5
+ QUOTED_FALSE = "'false'".freeze
6
+ QUOTED_TRUE = "'true'".freeze
7
+
8
+ QUOTED_POSITION = '"POSITION"'.freeze
9
+ QUOTED_VALUE = '"VALUE"'.freeze
10
+
11
+ def quote_string(string) # :nodoc:
12
+ string.gsub(/'/, "''")
13
+ end
14
+
15
+ def quoted_date(time)
16
+ if time.is_a?(Time) || time.is_a?(DateTime)
17
+ time.localtime.strftime('%d.%m.%Y %H:%M:%S')
18
+ else
19
+ time.strftime('%d.%m.%Y')
20
+ end
21
+ end
22
+
23
+ def quote_column_name(column_name) # :nodoc:
24
+ column = column_name.dup.to_s
25
+ column.gsub!(/(?<=[^"\w]|^)position(?=[^"\w]|$)/i, QUOTED_POSITION)
26
+ column.gsub!(/(?<=[^"\w]|^)value(?=[^"\w]|$)/i, QUOTED_VALUE)
27
+ column.delete!('"')
28
+ column.upcase!
29
+ @connection.dialect == 1 ? column.to_s : %("#{column}")
30
+ end
31
+
32
+ def quote_table_name_for_assignment(_table, attr)
33
+ quote_column_name(attr)
34
+ end
35
+
36
+ def unquoted_true
37
+ true
38
+ end
39
+
40
+ def quoted_true # :nodoc:
41
+ QUOTED_TRUE
42
+ end
43
+
44
+ def unquoted_false
45
+ false
46
+ end
47
+
48
+ def quoted_false # :nodoc:
49
+ QUOTED_FALSE
50
+ end
51
+
52
+ def type_cast_from_column(column, value) # :nodoc:
53
+ if column
54
+ type = column.type || lookup_cast_type_from_column(column)
55
+ if type.is_a?(ActiveRecord::Type::Serialized)
56
+ value
57
+ else
58
+ type.serialize(value)
59
+ end
60
+ else
61
+ value
62
+ end
63
+ end
64
+
65
+ def lookup_cast_type_from_column(column) # :nodoc:
66
+ type = column.try(:sql_type) || column.try(:type)
67
+ lookup_cast_type(type)
68
+ end
69
+
70
+ def type_casted_binds(binds) # :nodoc:
71
+ if binds.first.is_a?(Array)
72
+ binds.map { |column, value| type_cast(value, column) }
73
+ else
74
+ binds.map { |attr| type_cast(attr.value_for_database, attr) }
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ def id_value_for_database(value)
81
+ primary_key = value.class.primary_key
82
+ if primary_key
83
+ value.instance_variable_get(:@attributes)[primary_key].value_for_database
84
+ end
85
+ end
86
+
87
+ def _quote(value)
88
+ case value
89
+ when Time, DateTime
90
+ "'#{value.strftime('%d.%m.%Y %H:%M:%S')}'"
91
+ when Date
92
+ "'#{value.strftime('%d.%m.%Y')}'"
93
+ else
94
+ super
95
+ end
96
+ end
97
+
98
+ def _type_cast(value)
99
+ case value
100
+ when Symbol, ActiveSupport::Multibyte::Chars, Type::Binary::Data
101
+ value.to_s
102
+ when Array
103
+ value.to_yaml
104
+ when Hash then
105
+ encode_hash(value)
106
+ when true then
107
+ unquoted_true
108
+ when false then
109
+ unquoted_false
110
+ # BigDecimals need to be put in a non-normalized form and quoted.
111
+ when BigDecimal then
112
+ value.to_s('F')
113
+ when Type::Time::Value then
114
+ quoted_time(value)
115
+ when Date, Time, DateTime then
116
+ quoted_date(value)
117
+ when *types_which_need_no_typecasting
118
+ value
119
+ else
120
+ raise TypeError
121
+ end
122
+ end
123
+
124
+ def rdb_to_ar_case(column_name)
125
+ /[[:lower:]]/.match?(column_name) ? column_name : column_name.downcase
126
+ end
127
+
128
+ def ar_to_rdb_case(column_name)
129
+ /[[:upper:]]/.match?(column_name) ? column_name : column_name.upcase
130
+ end
131
+
132
+ def encode_hash(value)
133
+ if value.is_a?(Hash)
134
+ value.to_yaml
135
+ else
136
+ value
137
+ end
138
+ end
139
+
140
+ if defined? Encoding
141
+ def decode(str)
142
+ Base64.decode64(str).force_encoding(@connection.encoding)
143
+ end
144
+ else
145
+ def decode(str)
146
+ Base64.decode64(str)
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -1,53 +1,53 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- module Rdb
4
- class SchemaCreation < AbstractAdapter::SchemaCreation # :nodoc:
5
-
6
- private
7
-
8
- def visit_ColumnDefinition(o)
9
- o.sql_type = type_to_sql(o.type, o.options)
10
- column_sql = "#{quote_column_name(o.name)} #{o.sql_type}"
11
- add_column_options!(column_sql, column_options(o)) unless o.type == :primary_key
12
- column_sql
13
- end
14
-
15
- def add_column_options!(sql, options)
16
- sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
17
- # must explicitly check for :null to allow change_column to work on migrations
18
- if !options[:null].nil? && !options[:null]
19
- sql << " NOT NULL"
20
- end
21
- if options[:auto_increment]
22
- sql << " AUTO_INCREMENT"
23
- end
24
- if options[:primary_key]
25
- sql << " PRIMARY KEY"
26
- end
27
- sql
28
- end
29
-
30
- def visit_TableDefinition(o)
31
- create_sql = "CREATE#{' TEMPORARY' if o.temporary} TABLE #{quote_table_name(o.name)} "
32
-
33
- statements = o.columns.map(&method(:accept))
34
- statements << accept(o.primary_keys) if o.primary_keys
35
-
36
- if supports_indexes_in_create?
37
- statements.concat(o.indexes.map { |column_name, options| index_in_create(o.name, column_name, options) })
38
- end
39
-
40
- if supports_foreign_keys_in_create?
41
- statements.concat(o.foreign_keys.map { |to_table, options| foreign_key_in_create(o.name, to_table, options) })
42
- end
43
-
44
- create_sql << "(#{statements.join(', ')})" if statements.present?
45
- add_table_options!(create_sql, table_options(o))
46
- create_sql << " AS #{@conn.to_sql(o.as)}" if o.as
47
- create_sql
48
- end
49
-
50
- end
51
- end
52
- end
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module Rdb
4
+ class SchemaCreation < AbstractAdapter::SchemaCreation # :nodoc:
5
+
6
+ private
7
+
8
+ def visit_ColumnDefinition(o)
9
+ o.sql_type = type_to_sql(o.type, o.options)
10
+ column_sql = "#{quote_column_name(o.name)} #{o.sql_type}"
11
+ add_column_options!(column_sql, column_options(o)) unless o.type == :primary_key
12
+ column_sql
13
+ end
14
+
15
+ def add_column_options!(sql, options)
16
+ sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
17
+ # must explicitly check for :null to allow change_column to work on migrations
18
+ if !options[:null].nil? && !options[:null]
19
+ sql << " NOT NULL"
20
+ end
21
+ if options[:auto_increment]
22
+ sql << " AUTO_INCREMENT"
23
+ end
24
+ if options[:primary_key]
25
+ sql << " PRIMARY KEY"
26
+ end
27
+ sql
28
+ end
29
+
30
+ def visit_TableDefinition(o)
31
+ create_sql = "CREATE#{' TEMPORARY' if o.temporary} TABLE #{quote_table_name(o.name)} "
32
+
33
+ statements = o.columns.map(&method(:accept))
34
+ statements << accept(o.primary_keys) if o.primary_keys
35
+
36
+ if supports_indexes_in_create?
37
+ statements.concat(o.indexes.map { |column_name, options| index_in_create(o.name, column_name, options) })
38
+ end
39
+
40
+ if supports_foreign_keys_in_create?
41
+ statements.concat(o.foreign_keys.map { |to_table, options| foreign_key_in_create(o.name, to_table, options) })
42
+ end
43
+
44
+ create_sql << "(#{statements.join(', ')})" if statements.present?
45
+ add_table_options!(create_sql, table_options(o))
46
+ create_sql << " AS #{@conn.to_sql(o.as)}" if o.as
47
+ create_sql
48
+ end
49
+
50
+ end
51
+ end
52
+ end
53
53
  end
@@ -1,23 +1,23 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- module Rdb
4
- class SchemaDumper < ConnectionAdapters::SchemaDumper # :nodoc:
5
- private
6
-
7
- def column_spec_for_primary_key(column)
8
- spec = super
9
- spec.delete(:auto_increment) if column.type == :integer && column.auto_increment?
10
- spec
11
- end
12
-
13
- def schema_type(column)
14
- if column.bigint?
15
- :bigint
16
- else
17
- column.type.type
18
- end
19
- end
20
- end
21
- end
22
- end
23
- end
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module Rdb
4
+ class SchemaDumper < ConnectionAdapters::SchemaDumper # :nodoc:
5
+ private
6
+
7
+ def column_spec_for_primary_key(column)
8
+ spec = super
9
+ spec.delete(:auto_increment) if column.type == :integer && column.auto_increment?
10
+ spec
11
+ end
12
+
13
+ def schema_type(column)
14
+ if column.bigint?
15
+ :bigint
16
+ else
17
+ column.type.type
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end