mirror of
https://github.com/Cacti/documentation.git
synced 2026-12-06 15:13:53 +09:00
* docs(plugins): [1.2.x] correct the hook reference against the hooks the core fires Add the hooks the 1.2.x core fires that the page did not list, correct file locations and introduced/retired versions, mark hooks that are no longer fired as retired, and drop duplicate sections and an unlinked index entry. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * docs(plugins): [1.2.x] correct plugin pages against the plugin API and plugin sources Fix the INFO key name and delimiter, the hook used in the registration examples, the setup function requirements, the dependency example, and menu, table and template details in the Mactrack and Syslog pages. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * docs(howto): [1.2.x] correct how-to pages against the 1.2.x user interface and data query code Fix form labels, resource directory names, re-index method names, the index_order delimiter, the maximum value advice, and the cacti_snmp_get call in the script query example. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * docs(standards): [1.2.x] correct standards pages against the 1.2.x tree Fix the prepared statement example, the CLI include path, the CHANGELOG location, the CI reference and the include/vendor directory list. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * docs(plugins): [1.2.x] align the Syslog SQL mode section with the plugin README The plugin README says not to copy NO_AUTO_CREATE_USER into a current MySQL configuration and to inspect the active modes instead. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * docs(standards): [1.2.x] fix example typo, indentation rule, constants and table name Correct the $$ typo in the script query example, the vim rules to use tabs as CLAUDE.md requires, the example constants to match include/global_constants.php, and the user_auth_realm table name. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * docs(standards): [1.2.x] correct the CI and MD001 statements for this branch The 1.2.x branch has no Travis CI configuration or workflow, and .mdl_style.rb disables MD001. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * docs(translations): [1.2.x] correct the locales listing, template update steps and typos Describe what update-pot.sh, build_gettext.sh and build_mo.sh do on 1.2.x, add the plugin script, link Poedit's home page, and fix typos. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> --------- Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
141 lines
4.3 KiB
Markdown
141 lines
4.3 KiB
Markdown
# SQL Standards
|
|
|
|
Special care must be taken for Cacti to properly support multiple DBMS's.
|
|
|
|
## Quotes
|
|
|
|
When writing queries do not use quotes around numeric values. While MySQL is not
|
|
strict about types, most DBMS's assume that any value contained within quotes is
|
|
a string.
|
|
|
|
As well, single quotes are used unless a variable is referenced
|
|
|
|
```php
|
|
$graphs = db_fetch_assoc('SELECT * FROM graph WHERE height >= 150');
|
|
```
|
|
|
|
```php
|
|
$rows = db_fetch_cell("SELECT count(*) FROM $archive_table");
|
|
```
|
|
|
|
## DBMS Functions
|
|
|
|
Do not use functions in SQL queries that are provided only by a single DBMS.
|
|
These functions are generally not uniformly support or even available in all
|
|
DBMS's. Common examples of such functions are `MD5()`, `CONCAT()`, and `NOW()`.
|
|
|
|
## SQL Injection
|
|
|
|
Cacti provides support for prepared statements. You should use these prepared
|
|
statement functions as much as possible to avoid the risk of SQL Injection
|
|
attacks from the Cacti website. Examples include:
|
|
|
|
```php
|
|
// Example prepared statements
|
|
$hostname = db_fetch_cell_prepared('SELECT hostname FROM host WHERE id = ?', array($id));
|
|
$host = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', array($id));
|
|
$graphs = db_fetch_assoc_prepared('SELECT * FROM graph_local WHERE host_id = ?', array($id));
|
|
|
|
db_execute_prepared('UPDATE host SET description = ? WHERE id = ?', array('Local Machines', $id));
|
|
|
|
// Escape strings if using legacy functions
|
|
$hostname = db_qstr('myhost.com');
|
|
db_execute(UPDATE host SET hostname = '$myhost' WHERE id = $id");
|
|
|
|
```
|
|
|
|
## Format of SQL statements
|
|
|
|
- For short statements, SQL calls may be made on a single row.
|
|
|
|
- If the SQL statement starts to grow and be unreadable without scrolling, it
|
|
should be wrapped onto a continuation line for ease of use and readability.
|
|
The following keywords should also be placed at the start of a new line:
|
|
|
|
- joins statements (excluding JOIN itself which should not be used) (eg, INNER
|
|
JOIN, LEFT JOIN, RIGHT JOIN, OUTER JOIN, CROSS JOIN)
|
|
|
|
**Note:** \*Any implicit join or JOIN without one of the modifiers should be
|
|
converted to an explicit join with a modifier. When converting the implicit
|
|
join statement, remember to remove the WHERE joining clause.
|
|
|
|
- AND / OR
|
|
|
|
- FROM
|
|
|
|
- WHERE
|
|
|
|
- ORDER
|
|
|
|
- GROUP BY
|
|
|
|
- HAVING
|
|
|
|
- LIMIT
|
|
|
|
- prepared statement arrays
|
|
|
|
- Indentation should consist of one extra tab when any of the following occur:
|
|
|
|
- continuation lines
|
|
|
|
- Subqueries
|
|
|
|
- There should always be 'AS' when defining an alias for any table, field or
|
|
query to clearly define the definition of said alias.
|
|
|
|
- There should always be a space following a comma and before/after an operator.
|
|
|
|
### Example of single line SQL
|
|
|
|
#### Before formatting
|
|
|
|
```php
|
|
$templates = db_fetch_assoc('SELECT DISTINCT gt.id, gt.name FROM graph_templates AS gt INNER JOIN graph_templates_graph AS gtg ON gt.id = gtg.graph_template_id INNER JOIN graph_templates_item AS gti ON gtg.graph_template_id=gti.graph_template_id INNER JOIN data_template_rrd AS dtr ON gti.task_item_id=dtr.id INNER JOIN data_template_data AS dtd ON dtd.data_template_id=dtr.data_template_id AND dtd.local_data_id = 0 WHERE gtg.local_graph_id=0 AND dtr.local_data_id = 0 AND dtd.local_data_id = 0 AND dtd.data_input_id in (2,11,12) ORDER BY gt.name;'
|
|
```
|
|
|
|
#### Corrected formatting with prepared usage
|
|
|
|
```php
|
|
$templates = db_fetch_assoc_prepared('SELECT DISTINCT gt.id, gt.name
|
|
FROM graph_templates AS gt
|
|
INNER JOIN graph_templates_graph AS gtg
|
|
ON gt.id = gtg.graph_template_id
|
|
INNER JOIN graph_templates_item AS gti
|
|
ON gtg.graph_template_id=gti.graph_template_id
|
|
INNER JOIN data_template_rrd AS dtr
|
|
ON gti.task_item_id=dtr.id
|
|
INNER JOIN data_template_data AS dtd
|
|
ON dtd.data_template_id=dtr.data_template_id
|
|
AND dtd.local_data_id = 0
|
|
WHERE gtg.local_graph_id=0
|
|
AND dtr.local_data_id = 0
|
|
AND dtd.local_data_id = 0
|
|
AND dtd.data_input_id in (?,?,?)
|
|
ORDER BY gt.name',
|
|
array(2,11,12));
|
|
```
|
|
|
|
### Example of implicit join
|
|
|
|
#### Before formatting
|
|
|
|
```php
|
|
$user_realms = db_fetch_assoc('SELECT ua.id, uar.realm_id
|
|
FROM user_auth ua, user_auth_realm uar
|
|
WHERE ua.id = uar.user_id');
|
|
```
|
|
|
|
#### Corrected formatting
|
|
|
|
```php
|
|
$user_realms = db_fetch_assoc('SELECT ua.id, uar.realm_id
|
|
FROM user_auth ua
|
|
INNER JOIN user_auth_realm uar
|
|
ON ua.id = uar.user_id');
|
|
```
|
|
|
|
---
|
|
|
|
Copyright (c) 2004-2026 The Cacti Group
|