CSqliteCommandBuilder.php 2.3 KB

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