1 <?php
2
3 /**
4 * AfMssql2012DbHelper class file.
5 *
6 * @author Keith Burton <kburton@kappasoft.net>
7 * @package advancedfilters.dbhelpers
8 */
9
10 /**
11 * This database helper class can be used for SQL Server versions 2012 and
12 * later. It offers more robust numeric conversion than the generic SQL Server
13 * helper.
14 *
15 * To use this class, override the extension's $driverMap property in your
16 * application config.
17 */
18 class AfMssql2012DbHelper extends AfMssqlDbHelper
19 {
20 /**
21 * Overrides the generic SQL Server implementation to make use of the
22 * TRY_CAST function added in 2012. This is a more robust solution and
23 * should be used if you only need to deal with SQL Server versions from
24 * 2012 onwards.
25 *
26 * @param string $dbExpression the expression to update.
27 * @param integer $numDigits the maximum number of digits that the decimal
28 * number should contain.
29 * @param integer $decimalPlaces the number of decimal places that the
30 * resulting decimal should have.
31 * @param integer $nonNumericResultValue the integer value to use if an
32 * expression isn't recognised as a number. This can also be null.
33 * @return string the updated expression.
34 */
35 public function convertExpressionToDecimal($dbExpression, $numDigits,
36 $decimalPlaces, $nonNumericResultValue)
37 {
38 $numDigits = (int)$numDigits;
39 $decimalPlaces = (int)$decimalPlaces;
40
41 // Result for non-numeric expressions will be null
42 $dbExpression = "TRY_CAST(($dbExpression) "
43 . "AS DECIMAL($numDigits, $decimalPlaces))";
44
45 // If non-numeric values should have an integer result, coalesce to the
46 // correct value.
47 if ($nonNumericResultValue !== null)
48 {
49 $nonNumericResultValue = (int)$nonNumericResultValue;
50 $dbExpression = "COALESCE($dbExpression, $nonNumericResultValue)";
51 }
52
53 return $dbExpression;
54 }
55 }
56