Passed
Push — master ( b7dc72...de6839 )
by Edgard
21:49
created

Command::getSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace edgardmessias\db\firebird;
9
10
/**
11
 *
12
 * @author Edgard Lorraine Messias <[email protected]>
13
 * @since 2.0
14
 */
15
class Command extends \yii\db\Command
16
{
17
18
    /**
19
     * Binds a parameter to the SQL statement to be executed.
20
     * @param string|integer $name parameter identifier. For a prepared statement
21
     * using named placeholders, this will be a parameter name of
22
     * the form `:name`. For a prepared statement using question mark
23
     * placeholders, this will be the 1-indexed position of the parameter.
24
     * @param mixed $value Name of the PHP variable to bind to the SQL statement parameter
25
     * @param integer $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value.
26
     * @param integer $length length of the data type
27
     * @param mixed $driverOptions the driver-specific options
28
     * @return static the current command being executed
29
     * @see http://www.php.net/manual/en/function.PDOStatement-bindParam.php
30
     */
31 1
    public function bindParam($name, &$value, $dataType = null, $length = null, $driverOptions = null)
32
    {
33 1
        if ($dataType === null) {
34 1
            $dataType = $this->db->getSchema()->getPdoType($value);
35
        }
36 1
        if ($dataType == \PDO::PARAM_BOOL) {
37 1
            $dataType = \PDO::PARAM_INT;
38
        }
39 1
        return parent::bindParam($name, $value, $dataType, $length, $driverOptions);
40
    }
41
42
    /**
43
     * Specifies the SQL statement to be executed.
44
     * The previous SQL execution (if any) will be cancelled, and [[params]] will be cleared as well.
45
     * @param string $sql the SQL statement to be set.
46
     * @return static this command instance
47
     */
48 146
    public function setSql($sql)
49
    {
50 146
        $matches = null;
51 146
        if (preg_match("/^\s*DROP TABLE IF EXISTS (['\"]?([^\s\;]+)['\"]?);?\s*$/i", $sql, $matches)) {
52
            if ($this->db->getSchema()->getTableSchema($matches[2]) !== null) {
53
                $sql = $this->db->getQueryBuilder()->dropTable($matches[2]);
54
            } else {
55
                $sql = 'select 1 from RDB$DATABASE;'; //Prevent Drop Table
56
            }
57
        }
58
        
59 146
        return parent::setSql($sql);
60
    }
61
    
62
    public function getSql() {
63
        $sql = parent::getSql();
64
        // Unquote the {{@table@}} to {{table}} for insert
65
        $sql = preg_replace('/(\{\{)@(%?[\w\-\. ]+%?)@(\}\})/', '\1\2\3', $sql);
66
        return $sql;
67
    }
68
69
    /**
70
     * Binds a value to a parameter.
71
     * @param string|integer $name Parameter identifier. For a prepared statement
72
     * using named placeholders, this will be a parameter name of
73 2
     * the form `:name`. For a prepared statement using question mark
74
     * placeholders, this will be the 1-indexed position of the parameter.
75 2
     * @param mixed $value The value to bind to the parameter
76 2
     * @param integer $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value.
77
     * @return static the current command being executed
78 2
     * @see http://www.php.net/manual/en/function.PDOStatement-bindValue.php
79
     */
80
    public function bindValue($name, $value, $dataType = null)
81
    {
82 2
        if ($dataType === null) {
83
            $dataType = $this->db->getSchema()->getPdoType($value);
84
        }
85
        if ($dataType == \PDO::PARAM_BOOL) {
86
            $dataType = \PDO::PARAM_INT;
87
        }
88
89
        return parent::bindValue($name, $value, $dataType);
90
    }
91
}
92