aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
Commit message (Collapse)AuthorAgeFilesLines
* Remove ignored_sql from SQLCounter by adding "TRANSACTION" to log nameYasuo Honda2019-05-081-3/+3
| | | | | | | | | | This commit adds "TRANSACTION" to savepoint and commit, rollback statements because none of savepoint statements were removed by #36153 since they are not "SCHEMA" statements. Although, only savepoint statements can be labeled as "TRANSACTION" I think all of transaction related method should add this label. Follow up #36153
* Remove unused `sequence_name` in `sql_for_insert`Ryuta Kamizono2019-04-101-1/+1
| | | | | | | | | | All adapters (sqlite3, mysql2, postgresql, oracle-enhanced, sqlserver) doesn't use `sequence_name` in `sql_for_insert`. https://github.com/rsim/oracle-enhanced/blob/4e0db270a93859c9713fd079dbb315b9fe550e57/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb#L79-L85 https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/959fe8f49744460b876bc205c73259f8d4f37629/lib/active_record/connection_adapters/sqlserver/database_statements.rb#L226-L249 It can be handled in `exec_insert` like postgresql adapter if we want.
* Make `truncate_tables` to bulk statementsRyuta Kamizono2019-03-171-6/+4
| | | | | | | | | | | | | | | | | | Before: ``` (16.4ms) TRUNCATE TABLE `author_addresses` (20.5ms) TRUNCATE TABLE `authors` (19.4ms) TRUNCATE TABLE `posts` ``` After: ``` Truncate Tables (19.5ms) TRUNCATE TABLE `author_addresses`; TRUNCATE TABLE `authors`; TRUNCATE TABLE `posts` ```
* Extract `truncate` and `truncate_tables` into database statementsRyuta Kamizono2019-03-171-0/+6
| | | | This is to easier make `truncate_tables` to bulk statements.
* Remove `id_value` argument which is no longer passed to `sql_for_insert`Ryuta Kamizono2019-01-111-1/+1
| | | | Since #26002, `id_value` is no longer passed to `sql_for_insert`.
* An empty transaction does not raise the `ReadOnlyError` if preventing writesRyuta Kamizono2018-12-111-1/+1
| | | | | | BEGIN transaction would cause COMMIT or ROLLBACK, so unless COMMIT and ROLLBACK aren't treated as write queries as well as BEGIN, the `ReadOnlyError` would be raised.
* An explain query does not raise the `ReadOnlyError` if preventing writesRyuta Kamizono2018-12-111-1/+1
|
* Don't treat begin and rollback transactions as write queriesRyuta Kamizono2018-12-111-1/+1
| | | | | Otherwise `save` method would raise the `ReadOnlyError` against `BEGIN` and `ROLLBACK` queries.
* Rename error that occurs when writing on a readEileen Uchitelle2018-12-071-1/+1
| | | | | | | I originally named this `StatementInvalid` because that's what we do in GitHub, but `@tenderlove` pointed out that this means apps can't test for or explitly rescue this error. `StatementInvalid` is pretty broad so I've renamed this to `ReadOnlyError`.
* Address "warning: shadowing outer local variable - parts"Ryuta Kamizono2018-12-031-1/+2
| | | | And hide the `READ_QUERY` internal constant.
* Add ability to prevent writes to a databaseEileen Uchitelle2018-11-301-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This PR adds the ability to prevent writes to a database even if the database user is able to write (ie the database is a primary and not a replica). This is useful for a few reasons: 1) when converting your database from a single db to a primary/replica setup - you can fix all the writes on reads early on, 2) when we implement automatic database switching or when an app is manually switching connections this feature can be used to ensure reads are reading and writes are writing. We want to make sure we raise if we ever try to write in read mode, regardless of database type and 3) for local development if you don't want to set up multiple databases but do want to support rw/ro queries. This should be used in conjunction with `connected_to` in write mode. For example: ``` ActiveRecord::Base.connected_to(role: :writing) do Dog.connection.while_preventing_writes do Dog.create! # will raise because we're preventing writes end end ActiveRecord::Base.connected_to(role: :reading) do Dog.connection.while_preventing_writes do Dog.first # will not raise because we're not writing end end ```
* Omit BEGIN/COMMIT statements for empty transactionsEugene Kenny2018-08-131-0/+4
| | | | | | | | | | | | | | | | | | | | | | If a transaction is opened and closed without any queries being run, we can safely omit the `BEGIN` and `COMMIT` statements, as they only exist to modify the connection's behaviour inside the transaction. This removes the overhead of those statements when saving a record with no changes, which makes workarounds like `save if changed?` unnecessary. This implementation buffers transactions inside the transaction manager and materializes them the next time the connection is used. For this to work, the adapter needs to guard all connection use with a call to `materialize_transactions`. Because of this, adapters must opt in to get this new behaviour by implementing `supports_lazy_transactions?`. If `raw_connection` is used to get a reference to the underlying database connection, the behaviour is disabled and transactions are opened eagerly, as we can't know how the connection will be used. However when the connection is checked back into the pool, we can assume that the application won't use the reference again and reenable lazy transactions. This prevents a single `raw_connection` call from disabling lazy transactions for the lifetime of the connection.
* Restore `to_sql` to return only SQL (#29945)Ryuta Kamizono2017-08-181-2/+1
| | | | Because `to_sql` is public API. I introduced `to_sql_and_binds` internal API to return SQL and binds.
* Refactor Active Record to let Arel manage bind paramsSean Griffin2017-07-241-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A common source of bugs and code bloat within Active Record has been the need for us to maintain the list of bind values separately from the AST they're associated with. This makes any sort of AST manipulation incredibly difficult, as any time we want to potentially insert or remove an AST node, we need to traverse the entire tree to find where the associated bind parameters are. With this change, the bind parameters now live on the AST directly. Active Record does not need to know or care about them until the final AST traversal for SQL construction. Rather than returning just the SQL, the Arel collector will now return both the SQL and the bind parameters. At this point the connection adapter will have all the values that it had before. A bit of this code is janky and something I'd like to refactor later. In particular, I don't like how we're handling associations in the predicate builder, the special casing of `StatementCache::Substitute` in `QueryAttribute`, or generally how we're handling bind value replacement in the statement cache when prepared statements are disabled. This also mostly reverts #26378, as it moved all the code into a location that I wanted to delete. /cc @metaskills @yahonda, this change will affect the adapters Fixes #29766. Fixes #29804. Fixes #26541. Close #28539. Close #24769. Close #26468. Close #26202. There are probably other issues/PRs that can be closed because of this commit, but that's all I could find on the first few pages.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Use `quote` method rather than single quotes to identifiers in SQLRyuta Kamizono2017-06-291-0/+4
| | | | | | Because identifiers in SQL could include a single quote. Related #24950, #26784.
* Ensure query caching for `select_*` methods in connection adaptersRyuta Kamizono2017-06-151-32/+0
|
* Fix `select_rows` method signature for consistencyRyuta Kamizono2017-01-041-10/+14
| | | | | | | | Related #22973, #24708. `select_all`, `select_one`, `select_value`, and `select_values` method signature is `(arel, name = nil, binds = [])`. But `select_rows` is `(sql, name = nil, binds = [])`.
* Sync the visibility of `sql_for_insert` to privateRyuta Kamizono2016-12-291-1/+1
| | | | | The visibility of all internal protected methods was changed to private since 5b14129.
* Permit loads while queries are runningMatthew Draper2016-10-271-2/+6
| | | | | A query may wait on a database-level lock, which could lead to a deadlock between threads.
* [ci skip] Update PG adapter documentationAlex Kitchens2016-09-261-1/+1
| | | | | | | | Per discussion in pull request #26622: "Let's change it to PG::Error. The more specific classes mentioned are subclasses, and the fact the raised exception is a PG::UndefinedColumn doesn't change the fact that it's also a PG::Error." - matthewd
* [ci skip] Clarify Postgresql DocumentationAlex Kitchens2016-09-251-4/+4
| | | | | This clarifies the object that +ActiveRecord::Base.connection.execute+ will return when using Postgresql.
* Remove unnecessary `test_sql_for_insert_with_returning_disabled`Ryuta Kamizono2016-08-181-3/+2
| | | | | Because `sql_for_insert` is only called in `use_insert_returning?` is true since #26002.
* Merge pull request #26002 from ↵Rafael França2016-08-171-6/+10
|\ | | | | | | | | kamipo/sql_for_insert_should_be_called_inside_exec_insert `sql_for_insert` should be called inside `exec_insert`
| * `sql_for_insert` should be called inside `exec_insert`Ryuta Kamizono2016-08-061-6/+10
| | | | | | | | | | `exec_insert` cannot return last inserted id if `use_insert_returning?` is true. `sql_for_insert` should be called inside `exec_insert`.
* | Add three new rubocop rulesRafael Mendonça França2016-08-161-1/+1
| | | | | | | | | | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* | normalizes indentation and whitespace across the projectXavier Noria2016-08-061-3/+3
| |
* | applies new string literal convention in activerecord/libXavier Noria2016-08-061-4/+4
|/ | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Make `name` and `binds` to optional args for `exec_{insert,update,delete}`Ryuta Kamizono2016-08-041-2/+2
| | | | | | `insert`, `update`, `delete`, and `exec_query` have a default value against `name` and `binds`. But `exec_insert`, `exec_update`, and `exec_delete` not have. It is an inconvenience and inconsistent.
* Move the warning about composite primary key to `AttributeMethods::PrimaryKey`Ryuta Kamizono2016-07-021-0/+8
| | | | | | | | | | | Actually schema dumper/creation supports composite primary key (#21614). Therefore it should not show the warning about composite primary key in connection adapter. This change moves the warning to `AttributeMethods::PrimaryKey` and suppress the warning for habtm join table. Fixes #25388.
* eliminate warnings about multiple primary keys on habtm join tablesAaron Patterson2016-02-191-1/+1
| | | | | | | | habtm join tables commonly have two id columns and it's OK to make those two id columns a primary key. This commit eliminates the warnings for join tables that have this setup. ManageIQ/manageiq#6713
* Extract `ExplainPrettyPrinter` to appropriate filesRyuta Kamizono2016-02-011-38/+1
|
* Clarify DatabaseStatements#execute docs re: memory usage.James Coleman2016-01-221-0/+2
|
* `sql_for_insert` returns values for passing to `exec_insert`Ryuta Kamizono2016-01-151-12/+8
|
* Merge pull request #22973 from kamipo/fix_select_values_method_signatureRafael França2016-01-081-2/+2
|\ | | | | Fix `select_values` method signature for consistency
| * Fix `select_values` method signature for consistencyRyuta Kamizono2016-01-081-2/+2
| |
* | `{update|delete}_sql` are almost the same as `{update|delete}`Ryuta Kamizono2016-01-081-5/+0
|/ | | | Simply `{update|delete}_sql` aliases to `{update|delete}`.
* Refactor `connection.insert_sql`Ryuta Kamizono2016-01-071-16/+2
| | | | `connection.insert_sql` is almost the same as `connection.insert`.
* Fix `connection#create` in PG adapterRyuta Kamizono2016-01-051-4/+0
| | | | | | Originally `connection#create` had aliased to `connection#insert` in PG adapter. But it was broken by #7447. Re-alias `create` to `insert` for fixing it.
* Do not cache prepared statements that are unlikely to have cache hitsSean Griffin2015-10-201-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Prior to this commit, Rails makes no differentiation between whether a query uses bind parameters, and whether or not we cache that query as a prepared statement. This leads to the cache populating extremely fast in some cases, with the statements never being reused. In particular, the two problematic cases are `where(foo: [1, 2, 3])` and `where("foo = ?", 1)`. In both cases we'll end up quoting the values rather than using a bind param, causing a cache entry for every value ever used in that query. It was noted that we can probably eventually change `where("foo = ?", 1)` to use a bind param, which would resolve that case. Additionally, on PG we can change our generated query to be `WHERE foo = ANY($1)`, and pass an array for the bind param. I hope to accomplish both in the future. For SQLite and MySQL, we still end up preparing the statements anyway, we just don't cache it. The statement will be cleaned up after it is executed. On postgres, we skip the prepare step entirely, as an API is provided to execute with bind params without preparing the statement. I'm not 100% happy on the way this ended up being structured. I was hoping to use a decorator on the visitor, rather than mixing a module into the object, but the way Arel has it's visitor pattern set up makes it very difficult to extend without inheritance. I'd like to remove the duplication from the various places that are extending it, but that'll require a larger restructuring of that initialization logic. I'm going to take another look at the structure of it soon. This changes the signature of one of the adapter's internals, and will require downstream changes from third party adapters. I'm not too worried about this, as worst case they can simply add the parameter and always ignore it, and just keep their previous behavior. Fixes #21992.
* Wrong usage of 'a' in docs fixed [ci skip]Mehmet Emin İNAÇ2015-10-031-1/+1
|
* Restore query cache on rollbackFlorian Weingarten2014-12-011-1/+1
|
* rm `reorder_bind_params`Sean Griffin2014-11-171-4/+0
| | | | | | Arel handles this for us automatically. Updated tests, as BindParam is no longer a subclass of SqlLiteral. We should remove the second argument to substitute_at entirely, as it's no longer used
* Remove redundant substitute index when constructing bind valuesMelanie Gilman2014-10-311-1/+1
| | | | | | We end up re-ordering them either way when we construct the Arel AST (in order to deal with rewhere, etc), so we shouldn't bother giving it a number in the first place beforehand.
* move PostgreSQL's DatabaseStatements out of the PostgreSQLAdapter name-spacekares2014-05-141-1/+6
|
* Optimize select_value, select_values, select_rows and dry up checking ↵Kris Selden2014-04-171-19/+33
| | | | | whether to exec with cache for Postgresql adapter Reduces creating unused objects, with the most dramatic reduction in select_values which used to map(&:first) an array of single element arrays.
* PostgreSQL, adapter automatically reloads it's type map. Closes #14678.Yves Senn2014-04-111-4/+1
| | | | [Yves Senn & Matthew Draper]
* Fix regression on `.select_*` methods.Arthur Neves2014-01-301-2/+2
| | | | | | | | | | | | | | | | | | This was a common pattern: ``` query = author.posts.select(:title) connection.select_one(query) ``` However `.select` returns a ActiveRecord::AssociationRelation, which has the bind information, so we can use that to get the right sql query. Also fix select_rows on postgress and sqlite3 that were not using the binds [fixes #7538] [fixes #12017] [related #13731] [related #12056]