1: <?php
2: /**
3: * CSqliteCommandBuilder class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CSqliteCommandBuilder provides basic methods to create query commands for SQLite tables.
13: *
14: * @author Qiang Xue <qiang.xue@gmail.com>
15: * @package system.db.schema.sqlite
16: * @since 1.0
17: */
18: class CSqliteCommandBuilder extends CDbCommandBuilder
19: {
20: /**
21: * Generates the expression for selecting rows with specified composite key values.
22: * This method is overridden because SQLite does not support the default
23: * IN expression with composite columns.
24: * @param CDbTableSchema $table the table schema
25: * @param array $values list of primary key values to be selected within
26: * @param string $prefix column prefix (ended with dot)
27: * @return string the expression for selection
28: */
29: protected function createCompositeInCondition($table,$values,$prefix)
30: {
31: $keyNames=array();
32: foreach(array_keys($values[0]) as $name)
33: $keyNames[]=$prefix.$table->columns[$name]->rawName;
34: $vs=array();
35: foreach($values as $value)
36: $vs[]=implode("||','||",$value);
37: return implode("||','||",$keyNames).' IN ('.implode(', ',$vs).')';
38: }
39:
40: /**
41: * Creates a multiple INSERT command.
42: * This method could be used to achieve better performance during insertion of the large
43: * amount of data into the database tables.
44: * Note that SQLite does not keep original order of the inserted rows.
45: * @param mixed $table the table schema ({@link CDbTableSchema}) or the table name (string).
46: * @param array[] $data list data to be inserted, each value should be an array in format (column name=>column value).
47: * If a key is not a valid column name, the corresponding value will be ignored.
48: * @return CDbCommand multiple insert command
49: * @since 1.1.14
50: */
51: public function createMultipleInsertCommand($table,array $data)
52: {
53: $templates=array(
54: 'main'=>'INSERT INTO {{tableName}} ({{columnInsertNames}}) {{rowInsertValues}}',
55: 'columnInsertValue'=>'{{value}} AS {{column}}',
56: 'columnInsertValueGlue'=>', ',
57: 'rowInsertValue'=>'SELECT {{columnInsertValues}}',
58: 'rowInsertValueGlue'=>' UNION ',
59: 'columnInsertNameGlue'=>', ',
60: );
61: return $this->composeMultipleInsertCommand($table,$data,$templates);
62: }
63: }
64: