<commit_before><?php
/**
 * Copyright (c) 2012 <NAME> <<EMAIL>>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

class OC_DB_Schema {
	private static $DBNAME;
	private static $DBTABLEPREFIX;

	/**
	 * @brief saves database scheme to xml file
	 * @param string $file name of file
	 * @param int $mode
	 * @return bool
	 *
	 * TODO: write more documentation
	 */
	public static function getDbStructure( $conn, $file, $mode=MDB2_SCHEMA_DUMP_STRUCTURE) {
		$sm = $conn->getSchemaManager();

		return OC_DB_MDB2SchemaWriter::saveSchemaToFile($file, $sm);
	}

	/**
	 * @brief Creates tables from XML file
	 * @param string $file file to read structure from
	 * @return bool
	 *
	 * TODO: write more documentation
	 */
	public static function createDbFromStructure( $conn, $file ) {
		$toSchema = OC_DB_MDB2SchemaReader::loadSchemaFromFile($file, $conn->getDatabasePlatform());
		return self::executeSchemaChange($conn, $toSchema);
	}

	/**
	 * @brief update the database scheme
	 * @param string $file file to read structure from
	 * @return bool
	 */
	public static function updateDbFromStructure($conn, $file) {
		$sm = $conn->getSchemaManager();
		$fromSchema = $sm->createSchema();

		$toSchema = OC_DB_MDB2SchemaReader::loadSchemaFromFile($file, $conn->getDatabasePlatform());

		// remove tables we don't know about
		foreach($fromSchema->getTables() as $table) {
			if (!$toSchema->hasTable($table->getName())) {
				$fromSchema->dropTable($table->getName());
			}
		}

		$comparator = new \Doctrine\DBAL\Schema\Comparator();
		$schemaDiff = $comparator->compare($fromSchema, $toSchema);

		//$from = $fromSchema->toSql($conn->getDatabasePlatform());
		//$to = $toSchema->toSql($conn->getDatabasePlatform());
		//echo($from[9]);
		//echo '<br>';
		//echo($to[9]);
		//var_dump($from, $to);
		return self::executeSchemaChange($conn, $schemaDiff);
	}

	/**
	 * @brief drop a table
	 * @param string $tableName the table to drop
	 */<commit_msg>Also fix sequences in schemachange<commit_after><?php
/**
 * Copyright (c) 2012 <NAME> <<EMAIL>>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

class OC_DB_Schema {
	private static $DBNAME;
	private static $DBTABLEPREFIX;

	/**
	 * @brief saves database scheme to xml file
	 * @param string $file name of file
	 * @param int $mode
	 * @return bool
	 *
	 * TODO: write more documentation
	 */
	public static function getDbStructure( $conn, $file, $mode=MDB2_SCHEMA_DUMP_STRUCTURE) {
		$sm = $conn->getSchemaManager();

		return OC_DB_MDB2SchemaWriter::saveSchemaToFile($file, $sm);
	}

	/**
	 * @brief Creates tables from XML file
	 * @param string $file file to read structure from
	 * @return bool
	 *
	 * TODO: write more documentation
	 */
	public static function createDbFromStructure( $conn, $file ) {
		$toSchema = OC_DB_MDB2SchemaReader::loadSchemaFromFile($file, $conn->getDatabasePlatform());
		return self::executeSchemaChange($conn, $toSchema);
	}

	/**
	 * @brief update the database scheme
	 * @param string $file file to read structure from
	 * @return bool
	 */
	public static function updateDbFromStructure($conn, $file) {
		$sm = $conn->getSchemaManager();
		$fromSchema = $sm->createSchema();

		$toSchema = OC_DB_MDB2SchemaReader::loadSchemaFromFile($file, $conn->getDatabasePlatform());

		// remove tables we don't know about
		foreach($fromSchema->getTables() as $table) {
			if (!$toSchema->hasTable($table->getName())) {
				$fromSchema->dropTable($table->getName());
			}
		}
		// remove sequences we don't know about
		foreach($fromSchema->getSequences() as $table) {
			if (!$toSchema->hasSequence($table->getName())) {
				$fromSchema->dropSequence($table->getName());
			}
		}

		$comparator = new \Doctrine\DBAL\Schema\Comparator();
		$schemaDiff = $comparator->compare($fromSchema, $toSchema);

		//$from = $fromSchema->toSql($conn->getDatabasePlatform());
		//$to = $toSchema->toSql($conn->getDatabasePlatform());
		//echo($from[9]);
		//echo '<br>';
		//echo($to[9]);
		//var_dump($from, $to);
		return self::executeSchemaChange($conn, $schemaDiff);
	}

	/**
	 * @brief drop a table
	 * @param string $tableName the table to drop
	 */
{'fraction_non_alphanumeric': 0.1522045028142589, 'fraction_numerical': 0.005159474671669793, 'mean_word_length': 4.311332503113325, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '575018', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1317, 'n_words': 350}