/** * Apply the added and replacement parts to the parts of the base query. * * @return array */ public function getParts() { if (!isset($this->cachedParts)) { $parts = $this->getBaseParts(); if (empty($this->partsAdd) && empty($this->partsReplace)) { return $parts; } if (!empty($this->partsReplace)) { $parts = array_merge($parts, $this->partsReplace); } if (!empty($this->partsAdd)) { $parts = QuerySplitter::addParts($parts, $this->partsAdd); } if (key($parts) == 'select' && empty($parts['columns'])) { $parts['columns'] = '*'; } if (isset($parts['on duplicate key update']) && trim($parts['on duplicate key update']) === '1') { $columns = QuerySplitter::splitColumns($parts); foreach ($columns as &$column) { $column = "{$column} = VALUES({$column})"; } $parts['on duplicate key update'] = join(', ', $columns); } $this->cachedParts =& $parts; } return empty($this->subqueries) ? $this->cachedParts : QuerySplitter::injectSubsets(array($this->cachedParts) + $this->subqueries); }
public function testAddParts_Insert() { $add = array('values' => array(Query::APPEND => array('NULL, 100, "a"', 'NULL, 100, "b"'))); $parts = QuerySplitter::addParts('INSERT INTO foo VALUES (NULL, 20, "test")', $add); $expect = QuerySplitter::split('INSERT INTO foo VALUES (NULL, 20, "test"), (NULL, 100, "a"), (NULL, 100, "b")'); $this->assertEquals(array_map('trim', $expect), array_map('trim', $parts)); }