/var/www/(Del)gepgroup.hk/php-activerecord/lib/Expressions.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
 * @package ActiveRecord
 */
namespace ActiveRecord;

/**
 * Templating like class for building SQL statements.
 *
 * Examples:
 * 'name = :name AND author = :author'
 * 'id = IN(:ids)'
 * 'id IN(:subselect)'
 * 
 * @package ActiveRecord
 */
class Expressions
{
    const 
ParameterMarker '?';

    private 
$expressions;
    private 
$values = array();
    private 
$connection;

    public function 
__construct($connection$expressions=null /* [, $values ... ] */)
    {
        
$values null;
        
$this->connection $connection;

        if (
is_array($expressions))
        {
            
$glue func_num_args() > func_get_arg(2) : ' AND ';
            list(
$expressions,$values) = $this->build_sql_from_hash($expressions,$glue);
        }

        if (
$expressions != '')
        {
            if (!
$values)
                
$values array_slice(func_get_args(),2);

            
$this->values $values;
            
$this->expressions $expressions;
        }
    }

    
/**
     * Bind a value to the specific one based index. There must be a bind marker
     * for each value bound or to_s() will throw an exception.
     */
    
public function bind($parameter_number$value)
    {
        if (
$parameter_number <= 0)
            throw new 
ExpressionsException("Invalid parameter index: $parameter_number");

        
$this->values[$parameter_number-1] = $value;
    }

    public function 
bind_values($values)
    {
        
$this->values $values;
    }

    
/**
     * Returns all the values currently bound.
     */
    
public function values()
    {
        return 
$this->values;
    }

    
/**
     * Returns the connection object.
     */
    
public function get_connection()
    {
        return 
$this->connection;
    }

    
/**
     * Sets the connection object. It is highly recommended to set this so we can
     * use the adapter's native escaping mechanism.
     *
     * @param string $connection a Connection instance
     */
    
public function set_connection($connection)
    {
        
$this->connection $connection;
    }

    public function 
to_s($substitute=false, &$options=null)
    {
        if (!
$options$options = array();
        
        
$values array_key_exists('values',$options) ? $options['values'] : $this->values;

        
$ret "";
        
$replace = array();
        
$num_values count($values);
        
$len strlen($this->expressions);
        
$quotes 0;

        for (
$i=0,$n=strlen($this->expressions),$j=0$i<$n; ++$i)
        {
            
$ch $this->expressions[$i];

            if (
$ch == self::ParameterMarker)
            {
                if (
$quotes == 0)
                {
                    if (
$j $num_values-1)
                        throw new 
ExpressionsException("No bound parameter for index $j");

                    
$ch $this->substitute($values,$substitute,$i,$j++);
                }
            }
            elseif (
$ch == '\'' && $i && $this->expressions[$i-1] != '\\')
                ++
$quotes;

            
$ret .= $ch;
        }
        return 
$ret;
    }

    private function 
build_sql_from_hash(&$hash$glue)
    {
        
$sql $g "";

        foreach (
$hash as $name => $value)
        {
            if (
$this->connection)
                
$name $this->connection->quote_name($name);

            if (
is_array($value))
                
$sql .= "$g$name IN(?)";
            elseif (
is_null($value))
                
$sql .= "$g$name IS ?";
            else
                
$sql .= "$g$name=?";

            
$g $glue;
        }
        return array(
$sql,array_values($hash));
    }

    private function 
substitute(&$values$substitute$pos$parameter_index)
    {
        
$value $values[$parameter_index];

        if (
is_array($value))
        {
            if (
$substitute)
            {
                
$ret '';

                for (
$i=0,$n=count($value); $i<$n; ++$i)
                    
$ret .= ($i ',' '') . $this->stringify_value($value[$i]);

                return 
$ret;
            }
            return 
join(',',array_fill(0,count($value),self::ParameterMarker));
        }

        if (
$substitute)
            return 
$this->stringify_value($value);

        return 
$this->expressions[$pos];
    }

    private function 
stringify_value($value)
    {
        if (
is_null($value))
            return 
"NULL";

        return 
is_string($value) ? $this->quote_string($value) : $value;
    }

    private function 
quote_string($value)
    {
        if (
$this->connection)
            return 
$this->connection->escape($value);

        return 
"'" str_replace("'","''",$value) . "'";
    }
}
?>