1 <?php
2
3 /**
4 * AfBaseDbHelper class file.
5 *
6 * @author Keith Burton <kburton@kappasoft.net>
7 * @package advancedfilters.dbhelpers
8 */
9
10 /**
11 * The base class for all advanced filter database helpers.
12 *
13 * The methods in this class are used by filters to add database specific
14 * syntax to conditions.
15 *
16 * The base class provides default functionality for database operations where
17 * the syntax is common amongst multiple databases, and abstract methods that
18 * must be overridden where the syntax is database specific.
19 */
20 abstract class AfBaseDbHelper extends CComponent
21 {
22 /**
23 * Alters a database expression so that null values are converted to an
24 * empty string.
25 * Override this method in child classes if the database uses different
26 * syntax.
27 *
28 * @param string $dbExpression the expression to update.
29 * @return string the updated expression.
30 */
31 public function convertNullToEmptyString($dbExpression)
32 {
33 return "COALESCE(($dbExpression), '')";
34 }
35
36 /**
37 * Alters a database expression so that empty strings are converted to null.
38 * Override this method in child classes if the database uses different
39 * syntax.
40 *
41 * @param string $dbExpression the expression to update.
42 * @return string the updated expression.
43 */
44 public function convertEmptyStringToNull($dbExpression)
45 {
46 return "NULLIF(($dbExpression), '')";
47 }
48
49 /**
50 * Alters a database expression so that strings are converted to decimals.
51 *
52 * Values that can't be converted are set to the specific non-numeric
53 * result value, which should be an integer or null.
54 *
55 * @param string $dbExpression the expression to update.
56 * @param integer $numDigits the maximum number of digits that the decimal
57 * number should contain.
58 * @param integer $decimalPlaces the number of decimal places that the
59 * resulting decimal should have.
60 * @param integer $nonNumericResultValue the integer value to use if an
61 * expression isn't recognised as a number. This can also be null.
62 * @return string the updated expression.
63 */
64 abstract public function convertExpressionToDecimal($dbExpression,
65 $numDigits, $decimalPlaces, $nonNumericResultValue);
66
67 /**
68 * Verifies that the provided pattern is syntactically valid for the
69 * specific database. This may require a test query to be run.
70 * No default implementation is provided as each database uses its own
71 * syntax.
72 *
73 * @param CDbConnection $dbConnection the database connection object.
74 * @param string $regex the pattern to test.
75 * @return boolean true if the syntax is valid, false if not.
76 */
77 abstract public function checkRegex($dbConnection, $regex);
78
79 /**
80 * Adds a regular expression condition to the provided criteria.
81 * No default implementation is provided as each database uses its own
82 * syntax.
83 *
84 * @param CDbCriteria $criteria the criteria to update.
85 * @param string $columnExpression the column to search, or a valid
86 * expression.
87 * @param string $regex the pattern to match against.
88 * @param boolean $invertLogic whether the logic should be inverted to
89 * return the opposite query results.
90 */
91 abstract public function addRegexCondition($criteria, $columnExpression,
92 $regex, $invertLogic);
93 }
94