OwlManAtt / activephp

A suite of tools (read as: ActiveTable) for easily making data objects to represent your model data.

OwlManAtt (author)
Thu Apr 17 05:36:07 -0700 2008
commit  105d8924993a682346cd54bd2a6aa253d523a047
tree    fccab8537a230b2b711efea01e6ccae8b0a0a329
parent  d61fd014ff6807d76dfee6c8aab8d071fe055f7f
activephp / active_table / SqlGenerators / sqlite3.class.php
100644 293 lines (241 sloc) 7.484 kb
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* Class for writing SQLite3 compatible queries.
*
* @package ActivePHP
* @author OwlManAtt <owlmanatt@gmail.com>
* @copyright 2007, Yasashii Syndicate
* @version 2.3.0
*/
 
/**
* SQLite3 SQL driver for ActiveTable.
*
* @package ActivePHP
* @author OwlManAtt <owlmanatt@gmail.com>
* @copyright 2007, Yasashii Syndicate
* @version Release: @package_version@
**/
class ActiveTable_SQL_Sqlite3 implements ActiveTable_SQL
{
    protected $columns = array();
    protected $from = '';
    protected $join = array();
    protected $where = array();
    protected $order = '';
    protected $limit = '';
    public $magic_pk_name = null;
 
    public function __construct()
    {
        return null;
    } // end __construct
 
    public function getMagicPkName()
    {
        return null;
    } // end getMagicPkName
 
    public function getMagicUpdateWhere($table,$value,&$db)
    {
        return null;
    } // end getMagicUpdateWhere
 
    public function addMagicPkToKeys($table_name)
    {
        return null;
    } // end addMagicPkToKeys
 
    public function getFormattedDate($datetime)
    {
        if($datetime == null)
        {
            return '0';
        }
        
        return date('U',strtotime($datetime));
    } // end getFormattedDate
 
    public function addOrder($sql_fragment)
    {
        $this->order = $sql_fragment;
    } // end addOrder
 
    public function getQuery($verb)
    {
        $sql = '';
        
        switch(strtolower($verb))
        {
            case 'select':
            {
                $sql .= "SELECT\n";
                $sql .= implode(",\n",$this->columns)."\n";
                $sql .= "FROM {$this->from}\n";
 
                if(sizeof($this->join) > 0)
                {
                    $sql .= implode("\n",$this->join)."\n";
                }
 
                if(sizeof($this->where) > 0)
                {
                    $sql .= "WHERE ".implode("\nAND ",$this->where)."\n";
                }
 
                if($this->order != null)
                {
                    $sql .= $this->order."\n";
                }
 
                if($this->limit != null)
                {
                    $sql .= "LIMIT {$this->limit}";
                }
 
                break;
            } // end select
        } // end switch
 
        return $sql;
    } // end getQuery
 
    public function addFrom($table,$database=null)
    {
        if($database == null)
        {
            $this->from = "`$table`";
        }
        else
        {
            $this->from = "`$database`.`$table`";
        }
    } // end addFrom
 
    public function addWhere($table,$column,$type='=',$count=0)
    {
        switch($type)
        {
            case '>=':
            case '>':
            case '<=':
            case '<':
            case '<>':
            case '=':
            {
                $this->where[] = "`$table`.`$column` $type ?";
 
                break;
            } // end >= > <= < <> =
 
            case 'not_in':
            case 'in':
            {
                $in = '';
                if($type == 'in')
                {
                    $in = 'IN';
                }
                elseif($type == 'not_in')
                {
                    $in = 'NOT IN';
                }
                
                if($count > 0)
                {
                    $placeholders = implode(',',array_fill(0,$count,'?'));
                }
                else
                {
                    // Prevent INs with no IDs from generating invalid SQL.
                    throw new ArgumentError('Attempting to do IN with no data.');
                }
                
                $this->where[] = "`$table`.`$column` $in ($placeholders)";
    
                break;
            } // end in, not_in
 
            case 'is_not':
            case 'is':
            {
                $is = '';
                if($type == 'is')
                {
                    $is = 'IS NULL';
                }
                elseif($type == 'is_not')
                {
                    $is = 'IS NOT NULL';
                }
                
                $this->where[] = "`$table`.`$column` $is";
    
                break;
            } // end is, is_not
            
            case 'like':
            case 'not_like':
            {
                $like = '';
                if($type == 'like')
                {
                    $like = 'LIKE';
                }
                elseif($type == 'not_like')
                {
                    $like = 'NOT LIKE';
                }
                
                $this->where[] = "`$table`.`$column` $like ?";
 
                break;
            } // end like, not_like
 
            default:
            {
                throw new ArgumentError('Invalid type given to SQL generator.');
                
                break;
            } // end default
        } // end switch
        
    } // end addWhere
    
    public function addLimit($limit)
    {
        $this->limit = "$limit";
    } // end addLimit
    
    // Must return in the format table__column
    public function addKeys($table,$COLUMNS,$table_id='x')
    {
        foreach($COLUMNS as $id => $key)
        {
            if($key != null)
            {
                $this->columns[] = "`{$table}`.`{$key}` AS `c{$table_id}_{$id}`";
            }
        } // end column loop
 
    } // end addKeys
 
    public function addVirtualKey($statement,$index)
    {
        $this->columns[] = "$statement AS `cVIRT_$index`";
    } // end addVirtualKey
    
    public function getDescribeTable($table_name,$database=null)
    {
        return "PRAGMA table_info (`$table_name`)";
    } // end getDescribeTable
    
    // public function addJoinClause($LOOKUPS)
    public function addJoinClause($local_table,$local_key,$foreign_table,$foreign_table_alias,$foreign_key,$join_type,$database=null)
    {
        $join = '';
        switch(strtolower($join_type))
        {
            default:
            {
                throw new ArgumentError("Unknown join type '{$join_type}' specified for '{$foreign_table}' lookup.",903);
 
                break;
            }
            
            case 'left':
            {
                $join = 'LEFT JOIN';
                
                break;
            } // end left
 
            case 'inner':
            {
                $join = 'INNER JOIN';
 
                break;
            } // end inner
        } // end join switch
 
        $this->join[] = "$join `{$foreign_table}` `{$foreign_table_alias}` ON `{$local_table}`.`{$local_key}` = `{$foreign_table_alias}`.`{$foreign_key}`";
    } // end addJoinClause
 
    public function getLastInsertId($table)
    {
        return "SELECT last_insert_rowid() AS last_insert_id";
    } // end getLastInsertId
 
    public function buildOneOffLimit($condition_number,$limit_number)
    {
        return "LIMIT $limit_number\n";
    } // end buildOneOffLimit
    
    public function setSlice($start,$end)
    {
        if($this->limit != null)
        {
            throw new SQLGenerationError('Limit has been set for this query; cannot return a slice.');
        }
        
        $total = $end - $start;
        $this->limit = "$start,$total";
    } // end setSlice
 
    public function getPearDescribeEnabled()
    {
        return true;
    } // end getPerDescribeEnabled
 
} // end ActiveTable_MySQL_SQL
 
?>