Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
77ed6b78
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,发现更多精彩内容 >>
提交
77ed6b78
编写于
10月 14, 2016
作者:
R
Rafael França
提交者:
GitHub
10月 14, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #26784 from kamipo/quote_table_name_properly
Quote table name properly
上级
bd8b50d2
c5bcbb09
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
35 addition
and
24 deletion
+35
-24
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
...ecord/connection_adapters/postgresql/schema_statements.rb
+8
-8
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+1
-1
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
.../lib/active_record/connection_adapters/sqlite3_adapter.rb
+6
-4
activerecord/test/cases/adapter_test.rb
activerecord/test/cases/adapter_test.rb
+5
-2
activerecord/test/cases/view_test.rb
activerecord/test/cases/view_test.rb
+15
-9
未找到文件。
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
浏览文件 @
77ed6b78
...
...
@@ -86,7 +86,7 @@ def data_sources # :nodoc
SELECT c.relname
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r',
'v','m') -- (r)elation/table, (v)iew, (m)aterialized view
WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view
AND n.nspname = ANY (current_schemas(false))
SQL
end
...
...
@@ -108,13 +108,13 @@ def data_source_exists?(name)
name
=
Utils
.
extract_schema_qualified_name
(
name
.
to_s
)
return
false
unless
name
.
identifier
select_value
(
<<-
SQL
,
"SCHEMA"
).
to_i
>
0
SELECT
COUNT(*)
select_value
s
(
<<-
SQL
,
"SCHEMA"
).
any?
SELECT
c.relname
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view
AND c.relname =
'
#{
name
.
identifier
}
'
AND n.nspname =
#{
name
.
schema
?
"'
#{
name
.
schema
}
'"
:
'ANY (current_schemas(false))'
}
AND c.relname =
#{
quote
(
name
.
identifier
)
}
AND n.nspname =
#{
name
.
schema
?
quote
(
name
.
schema
)
:
"ANY (current_schemas(false))"
}
SQL
end
...
...
@@ -137,8 +137,8 @@ def view_exists?(view_name) # :nodoc:
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))'
}
AND c.relname =
#{
quote
(
name
.
identifier
)
}
AND n.nspname =
#{
name
.
schema
?
quote
(
name
.
schema
)
:
"ANY (current_schemas(false))"
}
SQL
end
...
...
@@ -441,7 +441,7 @@ def primary_keys(table_name) # :nodoc:
WITH pk_constraint AS (
SELECT conrelid, unnest(conkey) AS connum FROM pg_constraint
WHERE contype = 'p'
AND conrelid =
'
#{
quote_table_name
(
table_name
)
}
'
::regclass
AND conrelid =
#{
quote
(
quote_table_name
(
table_name
))
}
::regclass
), cons AS (
SELECT conrelid, connum, row_number() OVER() AS rownum FROM pk_constraint
)
...
...
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
77ed6b78
...
...
@@ -750,7 +750,7 @@ def column_definitions(table_name) # :nodoc:
col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid =
'
#{
quote_table_name
(
table_name
)
}
'
::regclass
WHERE a.attrelid =
#{
quote
(
quote_table_name
(
table_name
))
}
::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
end_sql
...
...
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
浏览文件 @
77ed6b78
...
...
@@ -530,10 +530,12 @@ def translate_exception(exception, message)
def
table_structure_with_collation
(
table_name
,
basic_structure
)
collation_hash
=
{}
sql
=
"SELECT sql FROM
(SELECT * FROM sqlite_master UNION ALL
SELECT * FROM sqlite_temp_master)
WHERE type='table' and name='
#{
table_name
}
'
\;
"
sql
=
<<-
SQL
SELECT sql FROM
(SELECT * FROM sqlite_master UNION ALL
SELECT * FROM sqlite_temp_master)
WHERE type = 'table' AND name =
#{
quote
(
table_name
)
}
SQL
# Result will have following sample string
# CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
...
...
activerecord/test/cases/adapter_test.rb
浏览文件 @
77ed6b78
...
...
@@ -42,8 +42,10 @@ def test_tables
def
test_table_exists?
ActiveSupport
::
Deprecation
.
silence
do
assert
@connection
.
table_exists?
(
"accounts"
)
assert
!
@connection
.
table_exists?
(
"nonexistingtable"
)
assert
!
@connection
.
table_exists?
(
nil
)
assert
@connection
.
table_exists?
(
:accounts
)
assert_not
@connection
.
table_exists?
(
"nonexistingtable"
)
assert_not
@connection
.
table_exists?
(
"'"
)
assert_not
@connection
.
table_exists?
(
nil
)
end
end
...
...
@@ -63,6 +65,7 @@ def test_data_source_exists?
assert
@connection
.
data_source_exists?
(
"accounts"
)
assert
@connection
.
data_source_exists?
(
:accounts
)
assert_not
@connection
.
data_source_exists?
(
"nonexistingtable"
)
assert_not
@connection
.
data_source_exists?
(
"'"
)
assert_not
@connection
.
data_source_exists?
(
nil
)
end
...
...
activerecord/test/cases/view_test.rb
浏览文件 @
77ed6b78
...
...
@@ -11,20 +11,21 @@ module ViewBehavior
end
class
Ebook
<
ActiveRecord
::
Base
self
.
table_name
=
"ebooks'"
self
.
primary_key
=
"id"
end
def
setup
super
@connection
=
ActiveRecord
::
Base
.
connection
create_view
"ebooks"
,
<<-
SQL
create_view
"ebooks
'
"
,
<<-
SQL
SELECT id, name, status FROM books WHERE format = 'ebook'
SQL
end
def
teardown
super
drop_view
"ebooks"
drop_view
"ebooks
'
"
end
def
test_reading
...
...
@@ -66,15 +67,20 @@ def test_attributes
def
test_does_not_assume_id_column_as_primary_key
model
=
Class
.
new
(
ActiveRecord
::
Base
)
do
self
.
table_name
=
"ebooks"
self
.
table_name
=
"ebooks
'
"
end
assert_nil
model
.
primary_key
end
def
test_does_not_dump_view_as_table
schema
=
dump_table_schema
"ebooks"
assert_no_match
%r{create_table "ebooks"}
,
schema
schema
=
dump_table_schema
"ebooks
'
"
assert_no_match
%r{create_table "ebooks
'
"}
,
schema
end
private
def
quote_table_name
(
name
)
@connection
.
quote_table_name
(
name
)
end
end
if
ActiveRecord
::
Base
.
connection
.
supports_views?
...
...
@@ -83,11 +89,11 @@ class ViewWithPrimaryKeyTest < ActiveRecord::TestCase
private
def
create_view
(
name
,
query
)
@connection
.
execute
"CREATE VIEW
#{
name
}
AS
#{
query
}
"
@connection
.
execute
"CREATE VIEW
#{
quote_table_name
(
name
)
}
AS
#{
query
}
"
end
def
drop_view
(
name
)
@connection
.
execute
"DROP VIEW
#{
name
}
"
if
@connection
.
view_exists?
name
@connection
.
execute
"DROP VIEW
#{
quote_table_name
(
name
)
}
"
if
@connection
.
view_exists?
name
end
end
...
...
@@ -206,11 +212,11 @@ class MaterializedViewTest < ActiveRecord::PostgreSQLTestCase
private
def
create_view
(
name
,
query
)
@connection
.
execute
"CREATE MATERIALIZED VIEW
#{
name
}
AS
#{
query
}
"
@connection
.
execute
"CREATE MATERIALIZED VIEW
#{
quote_table_name
(
name
)
}
AS
#{
query
}
"
end
def
drop_view
(
name
)
@connection
.
execute
"DROP MATERIALIZED VIEW
#{
name
}
"
if
@connection
.
view_exists?
name
@connection
.
execute
"DROP MATERIALIZED VIEW
#{
quote_table_name
(
name
)
}
"
if
@connection
.
view_exists?
name
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录