Permalink
Browse files

Merge pull request #13395 from kuldeepaggarwal/postgres-table-ref-regex

modified regex for finding table_name from a multiline sql query in postgresql
  • Loading branch information...
2 parents d54bc03 + b082bec commit cc1f0b4c2df0fa12ad6f3fd3b496e72e6f428e9c @carlosantoniodasilva carlosantoniodasilva committed Dec 19, 2013
View
@@ -1,3 +1,18 @@
+* Fix `PostgreSQL` insert to properly extract table name from multiline string SQL.
+
+ Previously, executing an insert SQL in `PostgreSQL` with a command like this:
+
+ insert into articles(
+ number)
+ values(
+ 5152
+ )
+
+ would not work because the adapter was unable to extract the correct `articles`
+ table name.
+
+ *Kuldeep Aggarwal*
+
* `Relation` no longer has mutator methods like `#map!` and `#delete_if`. Convert
to an `Array` by calling `#to_a` before using these methods.
@@ -969,7 +969,7 @@ def extract_pg_identifier_from_name(name)
end
def extract_table_ref_from_insert_sql(sql)
- sql[/into\s+([^\(]*).*values\s*\(/i]
+ sql[/into\s+([^\(]*).*values\s*\(/im]
$1.strip if $1
end
@@ -62,6 +62,18 @@ def test_insert_sql_with_no_space_after_table_name
assert_equal expect, id
end
+ def test_multiline_insert_sql
+ id = @connection.insert_sql(<<-SQL)
+ insert into ex(
+ number)
+ values(
+ 5152
+ )
+ SQL
+ expect = @connection.query('select max(id) from ex').first.first
+ assert_equal expect, id
+ end
+
def test_insert_sql_with_returning_disabled
connection = connection_without_insert_returning
id = connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)")

0 comments on commit cc1f0b4

Please sign in to comment.