Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
dcd39949
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,发现更多精彩内容 >>
提交
dcd39949
编写于
9月 13, 2015
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add `#views` and `#view_exists?` methods on connection adapters
上级
602ec7a4
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
88 addition
and
0 deletion
+88
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+4
-0
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
..._record/connection_adapters/abstract/schema_statements.rb
+13
-0
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
...tive_record/connection_adapters/abstract_mysql_adapter.rb
+16
-0
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
...ecord/connection_adapters/postgresql/schema_statements.rb
+24
-0
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
.../lib/active_record/connection_adapters/sqlite3_adapter.rb
+13
-0
activerecord/test/cases/view_test.rb
activerecord/test/cases/view_test.rb
+18
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
dcd39949
*
Add
`#views`
and
`#view_exists?`
methods on connection adapters.
*Ryuta Kamizono*
*
Correct query for PostgreSQL 8.2 compatibility.
*Ben Murphy*, *Matthew Draper*
...
...
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
浏览文件 @
dcd39949
...
...
@@ -36,6 +36,19 @@ def table_exists?(table_name)
tables
.
include?
(
table_name
.
to_s
)
end
# Returns an array of view names defined in the database.
def
views
raise
NotImplementedError
,
"#views is not implemented"
end
# Checks to see if the view +view_name+ exists on the database.
#
# view_exists?(:ebooks)
#
def
view_exists?
(
view_name
)
views
.
include?
(
view_name
.
to_s
)
end
# Returns an array of indexes for the given table.
# def indexes(table_name, name = nil) end
...
...
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
浏览文件 @
dcd39949
...
...
@@ -549,6 +549,22 @@ def table_exists?(name)
tables
(
nil
,
schema
,
table
).
any?
end
def
views
# :nodoc:
select_values
(
"SHOW FULL TABLES WHERE table_type = 'VIEW'"
,
'SCHEMA'
)
end
def
view_exists?
(
view_name
)
# :nodoc:
return
false
unless
view_name
.
present?
schema
,
name
=
view_name
.
to_s
.
split
(
'.'
,
2
)
schema
,
name
=
@config
[
:database
],
schema
unless
name
# A view was provided without a schema
sql
=
"SELECT table_name FROM information_schema.tables WHERE table_type = 'VIEW'"
sql
<<
" AND table_schema =
#{
quote
(
schema
)
}
AND table_name =
#{
quote
(
name
)
}
"
select_values
(
sql
,
'SCHEMA'
).
any?
end
# Returns an array of indexes for the given table.
def
indexes
(
table_name
,
name
=
nil
)
#:nodoc:
indexes
=
[]
...
...
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
浏览文件 @
dcd39949
...
...
@@ -90,6 +90,30 @@ def table_exists?(name)
SQL
end
def
views
# :nodoc:
select_values
(
<<-
SQL
,
'SCHEMA'
)
SELECT c.relname
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('v','m') -- (v)iew, (m)aterialized view
AND n.nspname = ANY (current_schemas(false))
SQL
end
def
view_exists?
(
view_name
)
# :nodoc:
name
=
Utils
.
extract_schema_qualified_name
(
view_name
.
to_s
)
return
false
unless
name
.
identifier
select_values
(
<<-
SQL
,
'SCHEMA'
).
any?
SELECT c.relname
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('v','m') -- (v)iew, (m)aterialized view
AND c.relname = '
#{
name
.
identifier
}
'
AND n.nspname =
#{
name
.
schema
?
"'
#{
name
.
schema
}
'"
:
'ANY (current_schemas(false))'
}
SQL
end
def
drop_table
(
table_name
,
options
=
{})
# :nodoc:
execute
"DROP TABLE
#{
' IF EXISTS'
if
options
[
:if_exists
]
}
#{
quote_table_name
(
table_name
)
}#{
' CASCADE'
if
options
[
:force
]
==
:cascade
}
"
end
...
...
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
浏览文件 @
dcd39949
...
...
@@ -325,6 +325,19 @@ def table_exists?(table_name)
table_name
&&
tables
(
nil
,
table_name
).
any?
end
def
views
# :nodoc:
select_values
(
"SELECT name FROM sqlite_master WHERE type = 'view' AND name <> 'sqlite_sequence'"
,
'SCHEMA'
)
end
def
view_exists?
(
view_name
)
# :nodoc:
return
false
unless
view_name
.
present?
sql
=
"SELECT name FROM sqlite_master WHERE type = 'view' AND name <> 'sqlite_sequence'"
sql
<<
" AND name =
#{
quote
(
view_name
)
}
"
select_values
(
sql
,
'SCHEMA'
).
any?
end
# Returns an array of +Column+ objects for the table specified by +table_name+.
def
columns
(
table_name
)
#:nodoc:
table_structure
(
table_name
).
map
do
|
field
|
...
...
activerecord/test/cases/view_test.rb
浏览文件 @
dcd39949
...
...
@@ -31,6 +31,15 @@ def test_reading
assert_equal
[
"Ruby for Rails"
],
books
.
map
(
&
:name
)
end
def
test_views
assert_equal
[
Ebook
.
table_name
],
@connection
.
views
end
def
test_view_exists
view_name
=
Ebook
.
table_name
assert
@connection
.
view_exists?
(
view_name
),
"'
#{
view_name
}
' view should exist"
end
def
test_table_exists
view_name
=
Ebook
.
table_name
assert
@connection
.
table_exists?
(
view_name
),
"'
#{
view_name
}
' table should exist"
...
...
@@ -91,6 +100,15 @@ def test_reading
assert_equal
[
"Agile Web Development with Rails"
],
books
.
map
(
&
:name
)
end
def
test_views
assert_equal
[
Paperback
.
table_name
],
@connection
.
views
end
def
test_view_exists
view_name
=
Paperback
.
table_name
assert
@connection
.
view_exists?
(
view_name
),
"'
#{
view_name
}
' view should exist"
end
def
test_table_exists
view_name
=
Paperback
.
table_name
assert
@connection
.
table_exists?
(
view_name
),
"'
#{
view_name
}
' table should exist"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录