aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2010-02-18 18:28:27 -0300
committerEmilio Tagua <miloops@gmail.com>2010-02-18 18:28:27 -0300
commitd51697d2bd2cdd6394cb5e665aab8ab43ce9a14a (patch)
tree444a5a6c23052dc827979c7fd0f9bd93b77ffcb3
parent1c0d7dc7f62e8744b476464f8aefc1867986f0bd (diff)
downloadrails-d51697d2bd2cdd6394cb5e665aab8ab43ce9a14a.tar.gz
rails-d51697d2bd2cdd6394cb5e665aab8ab43ce9a14a.tar.bz2
rails-d51697d2bd2cdd6394cb5e665aab8ab43ce9a14a.zip
Added support for RETURNING primary key when available, only for
PostgreSQL.
-rw-r--r--lib/arel/engines/sql/compilers/postgresql_compiler.rb4
-rw-r--r--lib/arel/engines/sql/engine.rb2
-rw-r--r--lib/arel/engines/sql/relations/compiler.rb4
-rw-r--r--lib/arel/engines/sql/relations/writes.rb3
-rw-r--r--spec/arel/engines/sql/unit/relations/insert_spec.rb13
5 files changed, 23 insertions, 3 deletions
diff --git a/lib/arel/engines/sql/compilers/postgresql_compiler.rb b/lib/arel/engines/sql/compilers/postgresql_compiler.rb
index 4122bc730e..1f6e74d57a 100644
--- a/lib/arel/engines/sql/compilers/postgresql_compiler.rb
+++ b/lib/arel/engines/sql/compilers/postgresql_compiler.rb
@@ -33,6 +33,10 @@ module Arel
order = orders.join(', ').split(/,/).map { |s| s.strip }.reject(&:blank?)
order = order.zip((0...order.size).to_a).map { |s,i| "id_list.alias_#{i} #{'DESC' if s =~ /\bdesc$/i}" }.join(', ')
end
+
+ def supports_insert_with_returning?
+ engine.postgresql_version >= 80200
+ end
end
end
end
diff --git a/lib/arel/engines/sql/engine.rb b/lib/arel/engines/sql/engine.rb
index 82915fe890..79011a3db5 100644
--- a/lib/arel/engines/sql/engine.rb
+++ b/lib/arel/engines/sql/engine.rb
@@ -20,7 +20,7 @@ module Arel
module CRUD
def create(relation)
- connection.insert(relation.to_sql, nil, relation.primary_key)
+ connection.execute(relation.to_sql)
end
def read(relation)
diff --git a/lib/arel/engines/sql/relations/compiler.rb b/lib/arel/engines/sql/relations/compiler.rb
index 4bc169096a..185f1a5c87 100644
--- a/lib/arel/engines/sql/relations/compiler.rb
+++ b/lib/arel/engines/sql/relations/compiler.rb
@@ -26,6 +26,10 @@ module Arel
"WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{engine.connection.quote_table_name table.name} #{conditions})"
end
+ def supports_insert_with_returning?
+ false
+ end
+
protected
def method_missing(method, *args, &block)
relation.send(method, *args, &block)
diff --git a/lib/arel/engines/sql/relations/writes.rb b/lib/arel/engines/sql/relations/writes.rb
index 30b36ddda8..00cd61cdde 100644
--- a/lib/arel/engines/sql/relations/writes.rb
+++ b/lib/arel/engines/sql/relations/writes.rb
@@ -32,7 +32,8 @@ module Arel
build_query \
"INSERT",
"INTO #{table_sql}",
- insertion_attributes_values_sql
+ insertion_attributes_values_sql,
+ ("RETURNING #{engine.quote_column_name(primary_key)}" if compiler.supports_insert_with_returning?)
end
end
diff --git a/spec/arel/engines/sql/unit/relations/insert_spec.rb b/spec/arel/engines/sql/unit/relations/insert_spec.rb
index 1884e32a3d..69c5bb052c 100644
--- a/spec/arel/engines/sql/unit/relations/insert_spec.rb
+++ b/spec/arel/engines/sql/unit/relations/insert_spec.rb
@@ -43,6 +43,7 @@ module Arel
INSERT
INTO "users"
("id", "name") VALUES (1, E'nick')
+ RETURNING "id"
})
end
end
@@ -74,6 +75,7 @@ module Arel
INSERT
INTO "users"
("name") VALUES (E'nick')
+ RETURNING "id"
})
end
end
@@ -93,11 +95,20 @@ module Arel
})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
+ @insertion.to_sql.should be_like(%Q{
+ INSERT
+ INTO "users"
+ ("id") VALUES (1)
+ })
+ end
+
+ adapter_is :postgresql do
@insertion.to_sql.should be_like(%Q{
INSERT
INTO "users"
("id") VALUES (1)
+ RETURNING "id"
})
end
end