714
edits
| Line 328: | Line 328: | ||
此规则的另一个优点是,查询可以从索引使用中获益,因为它将日期字段与固定值进行比较。当使用 datediff 时,因在比较之前对字段进行了处理,使得数据库无法使用该字段的索引。与不使用 datediff 的方案相比,其性能非常差。 | 此规则的另一个优点是,查询可以从索引使用中获益,因为它将日期字段与固定值进行比较。当使用 datediff 时,因在比较之前对字段进行了处理,使得数据库无法使用该字段的索引。与不使用 datediff 的方案相比,其性能非常差。 | ||
* 禁止使用 WITH ROLLUP | *禁止使用 WITH ROLLUP | ||
不应使用此语句:不同数据库的处理方式不同。此外,使用 WITH ROLLUP 会破坏返回数据的纯度。通过该指令生成小计的中间聚合可以在PHP的帮助下轻松完成,并有助于保持返回的数据表干净(不会被人工插入的数据损坏)。除了标准化程度不高之外,通过在数据中插入计算结果,给出的结果是数据和聚合业务计算的混合结果,对代码的理解变得复杂,甚至在这些聚合上的代码演变变得复杂时更可能难以理解。 | 不应使用此语句:不同数据库的处理方式不同。此外,使用 WITH ROLLUP 会破坏返回数据的纯度。通过该指令生成小计的中间聚合可以在PHP的帮助下轻松完成,并有助于保持返回的数据表干净(不会被人工插入的数据损坏)。除了标准化程度不高之外,通过在数据中插入计算结果,给出的结果是数据和聚合业务计算的混合结果,对代码的理解变得复杂,甚至在这些聚合上的代码演变变得复杂时更可能难以理解。 | ||
* | *使用 $db->ifsql 替代 IF SQL | ||
Do not include the IF into your forged SQL request. But use instead $db->ifsql() method to you will make a SQL IF that is compatible with all SQL databases. | Do not include the IF into your forged SQL request. But use instead $db->ifsql() method to you will make a SQL IF that is compatible with all SQL databases. | ||
* No DELETE CASCADE and ON UPDATE CASCADE | *No DELETE CASCADE and ON UPDATE CASCADE | ||
Such SQL instructions are forbidden because they bypass the business rules of the application bringing a lot of troubles out of control of the developers. For example, if there is a delete cascade between table A and B, when the application will execute the code to remove a record in A, the children into table B will also be removed. If there was a Dolibarr PHP trigger (for example bring by an external module) on the deletion of record of B (for example to validate the deletion or to execute a complementary action), the DELETE CASCADE will be executed without having the Dolibarr PHP trigger executed, missing the validation or actions of the PHP trigger of the module. All business rules must be implemented on the same side (so the server PHP side), this is the reason why business rules implemented on Database side are not allowed (same conclusion than the next point about Database triggers). Note that external modules can use them for links between Dolibarr tables and its module tables but a better recommanded solution is to use the trigger to implement the deletion of its table when a record of a parent official table is deleted. | Such SQL instructions are forbidden because they bypass the business rules of the application bringing a lot of troubles out of control of the developers. For example, if there is a delete cascade between table A and B, when the application will execute the code to remove a record in A, the children into table B will also be removed. If there was a Dolibarr PHP trigger (for example bring by an external module) on the deletion of record of B (for example to validate the deletion or to execute a complementary action), the DELETE CASCADE will be executed without having the Dolibarr PHP trigger executed, missing the validation or actions of the PHP trigger of the module. All business rules must be implemented on the same side (so the server PHP side), this is the reason why business rules implemented on Database side are not allowed (same conclusion than the next point about Database triggers). Note that external modules can use them for links between Dolibarr tables and its module tables but a better recommanded solution is to use the trigger to implement the deletion of its table when a record of a parent official table is deleted. | ||
edits