Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
69ef76a6
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
69ef76a6
编写于
3月 15, 2013
作者:
A
Aaron Patterson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove knowledge of SQL from the column definition object
上级
a03ab8ce
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
47 addition
and
24 deletion
+47
-24
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
...record/connection_adapters/abstract/schema_definitions.rb
+2
-21
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
...lib/active_record/connection_adapters/abstract_adapter.rb
+41
-0
activerecord/test/cases/column_definition_test.rb
activerecord/test/cases/column_definition_test.rb
+4
-3
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
浏览文件 @
69ef76a6
...
@@ -20,29 +20,9 @@ def string_to_binary(value)
...
@@ -20,29 +20,9 @@ def string_to_binary(value)
value
value
end
end
def
sql_type
base
.
type_to_sql
(
type
.
to_sym
,
limit
,
precision
,
scale
)
end
def
primary_key?
def
primary_key?
type
.
to_sym
==
:primary_key
type
.
to_sym
==
:primary_key
end
end
def
to_sql
column_sql
=
"
#{
base
.
quote_column_name
(
name
)
}
#{
sql_type
}
"
column_options
=
{}
column_options
[
:null
]
=
null
unless
null
.
nil?
column_options
[
:default
]
=
default
unless
default
.
nil?
column_options
[
:column
]
=
self
add_column_options!
(
column_sql
,
column_options
)
unless
primary_key?
column_sql
end
private
def
add_column_options!
(
sql
,
options
)
base
.
add_column_options!
(
sql
,
options
)
end
end
end
# Represents the schema of an SQL table in an abstract way. This class
# Represents the schema of an SQL table in an abstract way. This class
...
@@ -287,7 +267,8 @@ def references(*args)
...
@@ -287,7 +267,8 @@ def references(*args)
# concatenated together. This string can then be prepended and appended to
# concatenated together. This string can then be prepended and appended to
# to generate the final SQL to create the table.
# to generate the final SQL to create the table.
def
to_sql
def
to_sql
columns
.
map
{
|
c
|
c
.
to_sql
}
*
', '
viz
=
@base
.
schema_creation
columns
.
map
{
|
c
|
viz
.
accept
c
}.
join
', '
end
end
private
private
...
...
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
浏览文件 @
69ef76a6
...
@@ -100,6 +100,47 @@ def initialize(connection, logger = nil, pool = nil) #:nodoc:
...
@@ -100,6 +100,47 @@ def initialize(connection, logger = nil, pool = nil) #:nodoc:
@visitor
=
nil
@visitor
=
nil
end
end
class
SchemaCreation
def
initialize
(
conn
)
@conn
=
conn
@cache
=
{}
end
def
accept
(
o
)
m
=
@cache
[
o
.
class
]
||=
"visit_
#{
o
.
class
.
name
.
split
(
'::'
).
last
}
"
send
m
,
o
end
private
def
visit_ColumnDefinition
(
o
)
sql_type
=
type_to_sql
(
o
.
type
.
to_sym
,
o
.
limit
,
o
.
precision
,
o
.
scale
)
column_sql
=
"
#{
quote_column_name
(
o
.
name
)
}
#{
sql_type
}
"
column_options
=
{}
column_options
[
:null
]
=
o
.
null
unless
o
.
null
.
nil?
column_options
[
:default
]
=
o
.
default
unless
o
.
default
.
nil?
column_options
[
:column
]
=
o
add_column_options!
(
column_sql
,
column_options
)
unless
o
.
primary_key?
column_sql
end
def
quote_column_name
(
name
)
@conn
.
quote_column_name
name
end
def
type_to_sql
(
type
,
limit
,
precision
,
scale
)
@conn
.
type_to_sql
type
.
to_sym
,
limit
,
precision
,
scale
end
def
add_column_options!
(
column_sql
,
column_options
)
@conn
.
add_column_options!
column_sql
,
column_options
end
end
def
schema_creation
SchemaCreation
.
new
self
end
def
lease
def
lease
synchronize
do
synchronize
do
unless
in_use
unless
in_use
...
...
activerecord/test/cases/column_definition_test.rb
浏览文件 @
69ef76a6
...
@@ -8,6 +8,7 @@ def setup
...
@@ -8,6 +8,7 @@ def setup
def
@adapter
.
native_database_types
def
@adapter
.
native_database_types
{
:string
=>
"varchar"
}
{
:string
=>
"varchar"
}
end
end
@viz
=
@adapter
.
schema_creation
end
end
def
test_can_set_coder
def
test_can_set_coder
...
@@ -37,7 +38,7 @@ def test_should_not_include_default_clause_when_default_is_null
...
@@ -37,7 +38,7 @@ def test_should_not_include_default_clause_when_default_is_null
column_def
=
ColumnDefinition
.
new
(
column_def
=
ColumnDefinition
.
new
(
@adapter
,
column
.
name
,
"string"
,
@adapter
,
column
.
name
,
"string"
,
column
.
limit
,
column
.
precision
,
column
.
scale
,
column
.
default
,
column
.
null
)
column
.
limit
,
column
.
precision
,
column
.
scale
,
column
.
default
,
column
.
null
)
assert_equal
"title varchar(20)"
,
column_def
.
to_sql
assert_equal
"title varchar(20)"
,
@viz
.
accept
(
column_def
)
end
end
def
test_should_include_default_clause_when_default_is_present
def
test_should_include_default_clause_when_default_is_present
...
@@ -45,7 +46,7 @@ def test_should_include_default_clause_when_default_is_present
...
@@ -45,7 +46,7 @@ def test_should_include_default_clause_when_default_is_present
column_def
=
ColumnDefinition
.
new
(
column_def
=
ColumnDefinition
.
new
(
@adapter
,
column
.
name
,
"string"
,
@adapter
,
column
.
name
,
"string"
,
column
.
limit
,
column
.
precision
,
column
.
scale
,
column
.
default
,
column
.
null
)
column
.
limit
,
column
.
precision
,
column
.
scale
,
column
.
default
,
column
.
null
)
assert_equal
%Q{title varchar(20) DEFAULT 'Hello'}
,
column_def
.
to_sql
assert_equal
%Q{title varchar(20) DEFAULT 'Hello'}
,
@viz
.
accept
(
column_def
)
end
end
def
test_should_specify_not_null_if_null_option_is_false
def
test_should_specify_not_null_if_null_option_is_false
...
@@ -53,7 +54,7 @@ def test_should_specify_not_null_if_null_option_is_false
...
@@ -53,7 +54,7 @@ def test_should_specify_not_null_if_null_option_is_false
column_def
=
ColumnDefinition
.
new
(
column_def
=
ColumnDefinition
.
new
(
@adapter
,
column
.
name
,
"string"
,
@adapter
,
column
.
name
,
"string"
,
column
.
limit
,
column
.
precision
,
column
.
scale
,
column
.
default
,
column
.
null
)
column
.
limit
,
column
.
precision
,
column
.
scale
,
column
.
default
,
column
.
null
)
assert_equal
%Q{title varchar(20) DEFAULT 'Hello' NOT NULL}
,
column_def
.
to_sql
assert_equal
%Q{title varchar(20) DEFAULT 'Hello' NOT NULL}
,
@viz
.
accept
(
column_def
)
end
end
if
current_adapter?
(
:MysqlAdapter
)
if
current_adapter?
(
:MysqlAdapter
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录