714
edits
Tag: 2017 source edit |
|||
| Line 298: | Line 298: | ||
*SQL命令中不允许使用SQL日期函数NOW、SYSDATE、DATEDIFF。如果可能的话,还必须避免使用MONTH、YEAR。如果必须在字段中使用当前日期,则该值必须来自PHP,例如通过 dol_now() 函数,而不是来自数据库引擎。原因如下: | *SQL命令中不允许使用SQL日期函数NOW、SYSDATE、DATEDIFF。如果可能的话,还必须避免使用MONTH、YEAR。如果必须在字段中使用当前日期,则该值必须来自PHP,例如通过 dol_now() 函数,而不是来自数据库引擎。原因如下: | ||
** | **为了更好的代码可移植性 | ||
** | **正确管理TimeZone,并且不依赖于数据库的TimeZone(参考时区是PHP时区,数据库时区可能与此不同,因此日期转换必须在PHP端进行)。 | ||
** | **更好的性能(请参阅下面的示例和评论) | ||
示例,不要: | |||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
$sql="SELECT rowid FROM table where datefield = NOW()"; | $sql="SELECT rowid FROM table where datefield = NOW()"; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
要: | |||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
$sql="SELECT rowid FROM table where datefield = '".$this->db->idate(dol_now())."'"; | $sql="SELECT rowid FROM table where datefield = '".$this->db->idate(dol_now())."'"; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
示例,不要: | |||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
$sql="SELECT rowid FROM table where DATEDIFF(table.datefield, NOW()) > 7"; | $sql="SELECT rowid FROM table where DATEDIFF(table.datefield, NOW()) > 7"; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
要: | |||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
$sql="SELECT rowid FROM table where datefield < '".$this->db->idate(dol_now() - (7 * 24 * 3600))."'"; | $sql="SELECT rowid FROM table where datefield < '".$this->db->idate(dol_now() - (7 * 24 * 3600))."'"; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
此规则的另一个优点是,查询可以从索引使用中获益,因为它将日期字段与固定值进行比较。当使用 datediff 时,因在比较之前对字段进行了处理,使得数据库无法使用该字段的索引。与不使用 datediff 的方案相比,其性能非常差。 | |||
* 禁止使用 WITH ROLLUP | |||
不应使用此语句:不同数据库的处理方式不同。此外,使用 WITH ROLLUP 会破坏返回数据的纯度。通过该指令生成小计的中间聚合可以在PHP的帮助下轻松完成,并有助于保持返回的数据表干净(不会被人工插入的数据损坏)。除了标准化程度不高之外,通过在数据中插入计算结果,给出的结果是数据和聚合业务计算的混合结果,对代码的理解变得复杂,甚至在这些聚合上的代码演变变得复杂时更可能难以理解。 | |||
* Use $db->ifsql for SQL IF | |||
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 | |||
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. | |||
==Mysql 特点== | ==Mysql 特点== | ||
edits