Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3a0d0816
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,体验更适合开发者的 AI 搜索 >>
提交
3a0d0816
编写于
4月 01, 2012
作者:
D
Doug Cole
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pick better names and add a little documentation
上级
945c53e7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
24 addition
and
16 deletion
+24
-16
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+16
-8
activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
...test/cases/adapters/postgresql/postgresql_adapter_test.rb
+8
-8
未找到文件。
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
3a0d0816
...
...
@@ -406,7 +406,7 @@ def initialize(connection, logger, connection_parameters, config)
initialize_type_map
@local_tz
=
execute
(
'SHOW TIME ZONE'
,
'SCHEMA'
).
first
[
"TimeZone"
]
self
.
use_returning
=
true
self
.
enable_insert_returning!
end
# Clears the prepared statements cache.
...
...
@@ -668,7 +668,7 @@ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
pk
=
primary_key
(
table_ref
)
if
table_ref
end
if
pk
&&
use_returning?
if
pk
&&
use_
insert_
returning?
select_value
(
"
#{
sql
}
RETURNING
#{
quote_column_name
(
pk
)
}
"
)
elsif
pk
super
...
...
@@ -787,7 +787,7 @@ def sql_for_insert(sql, pk, id_value, sequence_name, binds)
pk
=
primary_key
(
table_ref
)
if
table_ref
end
if
pk
&&
use_returning?
if
pk
&&
use_
insert_
returning?
sql
=
"
#{
sql
}
RETURNING
#{
quote_column_name
(
pk
)
}
"
end
...
...
@@ -796,7 +796,7 @@ def sql_for_insert(sql, pk, id_value, sequence_name, binds)
def
exec_insert
(
sql
,
name
,
binds
,
pk
=
nil
,
sequence_name
=
nil
)
val
=
exec_query
(
sql
,
name
,
binds
)
if
!
use_returning?
&&
pk
if
!
use_
insert_
returning?
&&
pk
unless
sequence_name
table_ref
=
extract_table_ref_from_insert_sql
(
sql
)
sequence_name
=
default_sequence_name
(
table_ref
,
pk
)
...
...
@@ -1258,12 +1258,20 @@ def extract_schema_and_table(name)
end
end
def
use_returning
=
(
val
)
@use_returning
=
val
# Enable insert statements to use INSERT ... RETURNING ... statements. On by default.
def
enable_insert_returning!
@use_insert_returning
=
true
end
def
use_returning?
@use_returning
# Disable INSERT ... RETURNING ... statements, using currval() instead.
# Useful for trigger based insertions where INSERT RETURNING does the wrong thing.
def
disable_insert_returning!
@use_insert_returning
=
false
end
def
use_insert_returning?
@use_insert_returning
end
protected
...
...
activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
浏览文件 @
3a0d0816
...
...
@@ -50,38 +50,38 @@ def test_insert_sql_with_no_space_after_table_name
end
def
test_insert_sql_with_returning_disabled
@connection
.
use_returning
=
false
@connection
.
disable_insert_returning!
id
=
@connection
.
insert_sql
(
"insert into postgresql_partitioned_table_parent (number) VALUES (1)"
)
expect
=
@connection
.
query
(
'select max(id) from postgresql_partitioned_table_parent'
).
first
.
first
assert_equal
expect
,
id
ensure
@connection
.
use_returning
=
true
@connection
.
enable_insert_returning!
end
def
test_exec_insert_with_returning_disabled
@connection
.
use_returning
=
false
@connection
.
disable_insert_returning!
result
=
@connection
.
exec_insert
(
"insert into postgresql_partitioned_table_parent (number) VALUES (1)"
,
nil
,
[],
'id'
,
'postgresql_partitioned_table_parent_id_seq'
)
expect
=
@connection
.
query
(
'select max(id) from postgresql_partitioned_table_parent'
).
first
.
first
assert_equal
expect
,
result
.
rows
.
first
.
first
ensure
@connection
.
use_returning
=
true
@connection
.
enable_insert_returning!
end
def
test_exec_insert_with_returning_disabled_and_no_sequence_name_given
@connection
.
use_returning
=
false
@connection
.
disable_insert_returning!
result
=
@connection
.
exec_insert
(
"insert into postgresql_partitioned_table_parent (number) VALUES (1)"
,
nil
,
[],
'id'
)
expect
=
@connection
.
query
(
'select max(id) from postgresql_partitioned_table_parent'
).
first
.
first
assert_equal
expect
,
result
.
rows
.
first
.
first
ensure
@connection
.
use_returning
=
true
@connection
.
enable_insert_returning!
end
def
test_sql_for_insert_with_returning_disabled
@connection
.
use_returning
=
false
@connection
.
disable_insert_returning!
result
=
@connection
.
sql_for_insert
(
'sql'
,
nil
,
nil
,
nil
,
'binds'
)
assert_equal
[
'sql'
,
'binds'
],
result
ensure
@connection
.
use_returning
=
true
@connection
.
enable_insert_returning!
end
def
test_serial_sequence
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录