Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database guidelines #71

Merged
merged 5 commits into from
Sep 13, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions modules/developer_manual/pages/app/fundamentals/database.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ class AuthorDAO {
}
----

Database programming guidelines
-------------------------------

* Always use the Query Builder
* Don't update more than 1 million rows within a transaction due to DB limitations
* Don't add more than 999 conditions in a `WHERE ... IN ...` statement but chunk it into separate queries when using SQLite
* When processing big tables, always do this in chunks, don't store the whole table in memory
* Oracle compatibility specifics:
** For Oracle `null` and empty strings are the same thing. Need special handling to catch these cases
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/For Oracle/For Oracle,/g

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Need special handling/Special handling is required/g

** When reading values, make sure to convert null to empty string when expected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/convert null to empty string/convert nulls to empty strings/g

** Keep this in mind when designing DB schema and values expectations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove this line

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, it could be added as a lead-in sentence above the list.

** When using a condition based on empty strings, use `is not null` with Oracle instead
** Oracle can only compare the first 4000 bytes of a `CLOB` column
** Make sure to properly escape column names when using custom functions with `createFunction`. the escaping is usually done automatically by the query builder otherwise. Oracle is the most likely to complain about unquoted columns while other databases will work fine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/the escaping/The escaping/g
"otherwise", at the end of the second sentence seems superfluous.

* Always add the table name when calling `lastInsertId($tableName)`, it is required by Oracle to return correct values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a second "*" as this belongs to the Oracle block

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/, it is/, as it is/g

* In general don't specify a value for autoincrement column. if you have to, keep in mind that Oracle's autoincrement trigger will get in the way on `INSERT`, so you'll need a subsequent `UPDATE` to properly adjust the value
Copy link
Contributor

@settermjd settermjd Sep 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rewrite this as:

In general, don't specify a value for an autoincrement column. If you have to, keep in mind that Oracle's autoincrement trigger will get in the way on INSERT. As a result, you'll need a subsequent UPDATE to properly adjust the value.

* *Always* make sure there are unit tests for the database operations with queries to verify the result, this will help find out whether the database related code works on all databases and often times might reveal database quirks
Copy link
Contributor

@settermjd settermjd Sep 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break this sentence into two, with the second sentence starting at "this will". It makes it easier to read and digest.

* Running unit tests with specific databases: `make test-php TEST_PHP_SUITE=path/to/test/file.php TEST_DATABASE=$databasetype` where "$databasetype" is one of "sqlite", "mysql", "mariadb", "pgsql", "oci" and "mysqlmb4"
* String concatenation should be done like this:
** `CONCAT(str1, str2, ... strN)` for Mysql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Mysql/MySQL/g

** `str1 || str1 ... || strN` Sqlite/PgSQL/Oracle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Sqlite/SQLite/g
s/PgSQL/pgSQL/g

* Use `IQueryBuilder::createPositionalParameter` instead of `IQueryBuilder::createNamedParameter` when using `like()`

[[mappers]]
Mappers
-------
Expand Down