public function testTruncateBasic(){
		$query = "TRUNCATE geo_country;";

		$dss = SQL_Parser_Dataset::ConstructAndParse($query, SQL_Parser::DIALECT_MYSQL);
		$this->assertEquals(1, sizeof($dss));

		$ds = $dss[0];
		$this->assertInstanceOf('Core\\Datamodel\\Dataset', $ds);
		/** @var Core\Datamodel\Dataset $ds */

		$this->assertEquals('delete', $ds->_mode);
		$this->assertEquals('geo_country', $ds->_table);
		$this->assertNull($ds->_where);
	}
示例#2
0
	/**
	 * Convert a raw SQL statement to a generic dataset, (if possible).
	 *
	 * @param string $rawstatement
	 *
	 * @throws \Exception
	 *
	 * @return array
	 */
	public static function ProcessSQLStatement($rawstatement) {
		$parser = new \SQL_Parser_Dataset($rawstatement, \SQL_Parser::DIALECT_MYSQL);
		return $parser->parse();
	}
示例#3
0
	/**
	 * Upgrade this component to the newer version, if possible.
	 *
	 * Returns false if nothing changed, else will return an array containing all changes.
	 *
	 * @param boolean $next    Set to true to run the "next" upgrades as well as any current.
	 * @param boolean $verbose Set to true to enable real-time output
	 *
	 * @return boolean | array
	 * @throws InstallerException
	 */
	public function upgrade($next = false, $verbose = false) {
		if (!$this->isInstalled()){
			if($verbose) CLI::PrintDebug('Skipping ' . $this->getName() . ' as it is marked as uninstalled.');
			return false;
		}

		if($verbose) CLI::PrintHeader('Beginning upgrade for ' . $this->getName());

		$changes = array();

		// I can now do everything else.
		$otherchanges = $this->_performInstall();
		if ($otherchanges !== false) $changes = array_merge($changes, $otherchanges);

		$canBeUpgraded = true;
		while ($canBeUpgraded) {
			// Set as false to begin with, (will be set back to true if an upgrade is ran).
			$canBeUpgraded = false;
			foreach ($this->_xmlloader->getRootDOM()->getElementsByTagName('upgrade') as $u) {
				/** @var $u DOMElement */

				$from = $u->getAttribute('from');
				$to   = $u->getAttribute('to') ? $u->getAttribute('to') : 'next';

				// look for a valid upgrade path.
				if (($this->_versionDB == $from) || ($next && $from == 'next')) {
					// w00t, found one...
					$canBeUpgraded = true;

					if($verbose){
						CLI::PrintLine('Processing upgrade from ' . $from . ' to ' . $to);
					}

					// This gets a bit tricky, I need to get all the valid upgrade elements in the order that they
					// are defined in the component.xml.
					$children = $u->childNodes;

					// The various upgrade tasks that can happen
					foreach($children as $child){
						/** @var $child DOMElement */
						switch($child->nodeName){
							case 'dataset':
								$datachanges = $this->_parseDatasetNode($child, $verbose);
								if($datachanges !== false) $changes = array_merge($changes, $datachanges);
								break;
							case 'phpfileinclude':
								// I need to do this in a method so that include file doesn't mess with my local variables!
								$this->_includeFileForUpgrade(ROOT_PDIR . trim($child->nodeValue), $verbose);
								$changes[] = 'Included custom php file ' . basename($child->nodeValue);
								break;
							case 'php':
								$file = $child->getAttribute('file');
								if($file){
									// I need to do this in a method so that include file doesn't mess with my local variables!
									$this->_includeFileForUpgrade($this->getBaseDir() . $file, $verbose);
									$changes[] = 'Included custom php file ' . $file;
								}
								else{
									$changes[] = 'Ignoring invalid <php> directive, no file attribute provided!';
								}
								break;
							case 'sql':
								$file = $child->getAttribute('file');
								if($file){
									if($verbose){
										CLI::PrintActionStart('Executing SQL statements from ' . $file);
									}
									$contents = file_get_contents($this->getBaseDir() . $file);
									$execs = 0;
									$parser = new SQL_Parser_Dataset($contents, SQL_Parser::DIALECT_MYSQL);
									$datasets = $parser->parse();
									foreach($datasets as $ds){
										$ds->execute();
										$execs++;
									}
									if($verbose){
										CLI::PrintActionStatus(true);
									}
									$changes[] = 'Executed custom sql file ' . $file . ' and ran ' . $execs . ($execs == 1 ? ' query' : ' queries');
								}
								else{
									$changes[] = 'Ignoring invalid <sql> directive, no file attribute provided!';
								}
								break;
							case '#text':
								// This can be ignored without triggering any notice.
								break;
							default:
								$changes[] = 'Ignoring unsupported upgrade directive: [' . $child->nodeName . ']';
						}
					}

					// Record this change.
					$changes[] = 'Upgraded from [' . $this->_versionDB . '] to [' . $u->getAttribute('to') . ']';

					SystemLogModel::LogInfoEvent('/updater/component/upgrade', 'Component ' . $this->getName() . ' upgraded successfully from ' . $this->_versionDB . ' to ' . $u->getAttribute('to') . '!', implode("\n", $changes));

					if($to == 'next'){
						$canBeUpgraded = false;
					}
					else{
						$this->_versionDB = $to;
						$c = new ComponentModel($this->_name);
						$c->set('version', $this->_versionDB);
						$c->save();
					}
				}
			}
		}

		if(sizeof($changes) == 0 && $verbose){
			CLI::PrintLine('No changes performed.');
		}

		return (sizeof($changes)) ? $changes : false;
	}