Files
Cactidocumentation/Standards-SQL.md
T
Thomas Vincent 741ad51355 docs: [1.2.x] refresh the front page, correct grammar and Markdown, and align plugin pages with their READMEs (#273)
* docs: [1.2.x] refresh the manual front page

The front page opened with version notes before saying what Cacti is,
used headings as sentences in the credits, and its table of contents
listed seven sections while the page has ten.

Open with a short description and a getting-started sequence, link the
website and forums, give the credits real headings, and list every
section in the table of contents in page order. Record Jeff Pasnak
under In Memoriam with a link to the 1.2.31 release announcement.
Correct label typos and wrap long entries with reference links.

Existing section anchors are unchanged, and every page is still linked.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs: [1.2.x] fix typos, unrendered headings and forum links

A check of every page linked from the front page found misspellings in
eleven pages, three Mactrack headings written without a space after the
hashes, which render as plain text, and two forum links still using
http.

Correct the spellings, add the missing space so the headings render,
and point the forum links at https, where they already redirect.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs: [1.2.x] align the plugin pages with the plugin READMEs

The supported plugins list and the Syslog and Mactrack pages had drifted
from the README and INFO files in the plugin repositories.

Update the descriptions that the READMEs contradict: Monitor's Thold
requirement, the Router Configs transfer methods, the ReportIt release
and Cacti versions, the WMI collection methods and its development
status, and NUT support in Apcupsd. Add the Syslog requirements, the
upgrade procedure, the dedicated database settings and the remote
collector settings, and correct the database grant and a misplaced
code fence. Add the Mactrack requirements and a short section on
history partitioning and ignored ports that links to the README.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs: [1.2.x] correct grammar, spelling and broken Markdown

A read of every page linked from the front page found grammar and
spelling errors throughout, and Markdown that does not render as
intended.

Correct grammar, spelling, punctuation and product-name capitalization
in 95 pages without changing headings, code blocks, link targets or
the meaning of the text. Repair the Markdown defects: a code fence in
the SSH tunnel how-to that never closed, malformed bold markers in the
user management and device defaults pages, a release notes table with
no blank line before it, split rows in the tree sorting table, and
LDAP list items missing their markers. Correct three interface labels
quoted with typos.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs: [1.2.x] correct statements that differ from the 1.2.x source

Reviewers of the linked pages flagged statements that looked wrong.
Each was compared with the Cacti 1.2.x source at 1523aa04b, and only
those the source or the page itself settles are changed.

Correct interface labels and option names (Index Count Changed,
Permissive, Not Selected, Utilities, the BCC description, the gear
icon for plugin install), the default Maximum Value of U for a new
data source item, the sysDescr object name, the PHP floor wording in
the requirements table, the Mactrack wildcard character and the
snmptrapd -On option. Repair cross-references, example values that
disagree with their commands, two table captions, a split list and
several sentences with missing words.

Statements the source does not settle are left as written.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
2026-11-24 00:58:06 -08:00

4.3 KiB

SQL Standards

Special care must be taken for Cacti to properly support multiple DBMSs.

Quotes

When writing queries do not use quotes around numeric values. While MySQL is not strict about types, most DBMSs assume that any value contained within quotes is a string.

As well, single quotes are used unless a variable is referenced.

$graphs = db_fetch_assoc('SELECT * FROM graph WHERE height >= 150');
$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 supported or even available in all DBMSs. 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:

// 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:

    • join statements (excluding JOIN itself which should not be used) (e.g., 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

$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

$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

$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

$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