Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
fc7129d2
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,发现更多精彩内容 >>
提交
fc7129d2
编写于
11月 11, 2013
作者:
V
Vipul A M
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move `SchemaCreation` to its own file instead of `AbstractAdapter`.
上级
3cc64df9
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
84 addition
and
78 deletion
+84
-78
activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb
...ve_record/connection_adapters/abstract/schema_creation.rb
+83
-0
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
...lib/active_record/connection_adapters/abstract_adapter.rb
+1
-78
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb
0 → 100644
浏览文件 @
fc7129d2
module
ActiveRecord
module
ConnectionAdapters
class
AbstractAdapter
class
SchemaCreation
# :nodoc:
def
initialize
(
conn
)
@conn
=
conn
@cache
=
{}
end
def
accept
(
o
)
m
=
@cache
[
o
.
class
]
||=
"visit_
#{
o
.
class
.
name
.
split
(
'::'
).
last
}
"
send
m
,
o
end
def
visit_AddColumn
(
o
)
sql_type
=
type_to_sql
(
o
.
type
.
to_sym
,
o
.
limit
,
o
.
precision
,
o
.
scale
)
sql
=
"ADD
#{
quote_column_name
(
o
.
name
)
}
#{
sql_type
}
"
add_column_options!
(
sql
,
column_options
(
o
))
end
private
def
visit_AlterTable
(
o
)
sql
=
"ALTER TABLE
#{
quote_table_name
(
o
.
name
)
}
"
sql
<<
o
.
adds
.
map
{
|
col
|
visit_AddColumn
col
}.
join
(
' '
)
end
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
}
"
add_column_options!
(
column_sql
,
column_options
(
o
))
unless
o
.
primary_key?
column_sql
end
def
visit_TableDefinition
(
o
)
create_sql
=
"CREATE
#{
' TEMPORARY'
if
o
.
temporary
}
TABLE "
create_sql
<<
"
#{
quote_table_name
(
o
.
name
)
}
("
create_sql
<<
o
.
columns
.
map
{
|
c
|
accept
c
}.
join
(
', '
)
create_sql
<<
")
#{
o
.
options
}
"
create_sql
end
def
column_options
(
o
)
column_options
=
{}
column_options
[
:null
]
=
o
.
null
unless
o
.
null
.
nil?
column_options
[
:default
]
=
o
.
default
unless
o
.
default
.
nil?
column_options
[
:column
]
=
o
column_options
[
:first
]
=
o
.
first
column_options
[
:after
]
=
o
.
after
column_options
end
def
quote_column_name
(
name
)
@conn
.
quote_column_name
name
end
def
quote_table_name
(
name
)
@conn
.
quote_table_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!
(
sql
,
options
)
sql
<<
" DEFAULT
#{
@conn
.
quote
(
options
[
:default
],
options
[
:column
])
}
"
if
options_include_default?
(
options
)
# must explicitly check for :null to allow change_column to work on migrations
if
options
[
:null
]
==
false
sql
<<
" NOT NULL"
end
if
options
[
:auto_increment
]
==
true
sql
<<
" AUTO_INCREMENT"
end
sql
end
def
options_include_default?
(
options
)
options
.
include?
(
:default
)
&&
!
(
options
[
:null
]
==
false
&&
options
[
:default
].
nil?
)
end
end
end
end
end
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
浏览文件 @
fc7129d2
...
...
@@ -4,6 +4,7 @@
require
'active_support/core_ext/benchmark'
require
'active_record/connection_adapters/schema_cache'
require
'active_record/connection_adapters/abstract/schema_dumper'
require
'active_record/connection_adapters/abstract/schema_creation'
require
'monitor'
module
ActiveRecord
...
...
@@ -106,84 +107,6 @@ def valid_type?(type)
true
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
def
visit_AddColumn
(
o
)
sql_type
=
type_to_sql
(
o
.
type
.
to_sym
,
o
.
limit
,
o
.
precision
,
o
.
scale
)
sql
=
"ADD
#{
quote_column_name
(
o
.
name
)
}
#{
sql_type
}
"
add_column_options!
(
sql
,
column_options
(
o
))
end
private
def
visit_AlterTable
(
o
)
sql
=
"ALTER TABLE
#{
quote_table_name
(
o
.
name
)
}
"
sql
<<
o
.
adds
.
map
{
|
col
|
visit_AddColumn
col
}.
join
(
' '
)
end
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
}
"
add_column_options!
(
column_sql
,
column_options
(
o
))
unless
o
.
primary_key?
column_sql
end
def
visit_TableDefinition
(
o
)
create_sql
=
"CREATE
#{
' TEMPORARY'
if
o
.
temporary
}
TABLE "
create_sql
<<
"
#{
quote_table_name
(
o
.
name
)
}
("
create_sql
<<
o
.
columns
.
map
{
|
c
|
accept
c
}.
join
(
', '
)
create_sql
<<
")
#{
o
.
options
}
"
create_sql
end
def
column_options
(
o
)
column_options
=
{}
column_options
[
:null
]
=
o
.
null
unless
o
.
null
.
nil?
column_options
[
:default
]
=
o
.
default
unless
o
.
default
.
nil?
column_options
[
:column
]
=
o
column_options
[
:first
]
=
o
.
first
column_options
[
:after
]
=
o
.
after
column_options
end
def
quote_column_name
(
name
)
@conn
.
quote_column_name
name
end
def
quote_table_name
(
name
)
@conn
.
quote_table_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!
(
sql
,
options
)
sql
<<
" DEFAULT
#{
@conn
.
quote
(
options
[
:default
],
options
[
:column
])
}
"
if
options_include_default?
(
options
)
# must explicitly check for :null to allow change_column to work on migrations
if
options
[
:null
]
==
false
sql
<<
" NOT NULL"
end
if
options
[
:auto_increment
]
==
true
sql
<<
" AUTO_INCREMENT"
end
sql
end
def
options_include_default?
(
options
)
options
.
include?
(
:default
)
&&
!
(
options
[
:null
]
==
false
&&
options
[
:default
].
nil?
)
end
end
def
schema_creation
SchemaCreation
.
new
self
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录