<commit_before><?php

/**
 * Based upon http://www.yiiframework.com/doc/guide/1.1/en/database.migration#c2550 from Leric
 */
class P3DumpSchemaCommand extends CConsoleCommand {

	public function run($args) {
		$schema = $args[0];
		$prefix = $args[1];
		$tables = Yii::app()->db->schema->getTables($schema);
		$code = '';
		foreach ($tables as $table) {
			if (substr($table->name, 0, strlen($prefix)) != $prefix)
				continue;

			$code .= "\n\n\n// Schema for table '" . $table->name . "'\n\n";
			$code .= $this->generateSchema($table, $schema);

			$code .= "\n\n\n// Foreign Keys for table '" . $table->name . "'\n\n";
			$code .= $this->generateForeignKeys($table, $schema);

			$code .= "\n\n\n// Data for table '" . $table->name . "'\n\n";
			$code .= $this->generateInserts($table, $schema);
		}
		echo $code;
	}

	private function generateSchema($table, $schema) {
		$options = "ENGINE=InnoDB DEFAULT CHARSET=utf8";
		$code = '';
		$code .= '$this->createTable("' . $table->name . '", ';
		$code .= "\n";
		$code .= '  array(' . "\n";
		foreach ($table->columns as $col) {
			$code .= '    "' . $col->name . '"=>"' . $this->resolveColumnType($col) . '",' . "\n";
		}
		
		// special case for non-auto-increment PKs
		$code .= $this->generatePrimaryKeys($table->columns);
		$code .= "\n";
		$code .= '  ), ';
		$code .= "\n";
		$code .= '  "' . $options . '");';
		return $code;
	}


	private function generatePrimaryKeys($columns) {
		foreach ($columns as $col) {			
			if ($col->isPrimaryKey && !$col->autoIncrement) {
				return '    "PRIMARY KEY ('.$col->name.')"';
			}
		}
	}

	private function generateForeignKeys($table, $schema) {
		$code = "";
		foreach ($table->foreignKeys as $name => $foreignKey) {
			#echo "FK" . $name . var_dump($foreignKey);
			$code .= "\n\$this->addForeignKey('fk_{$foreignKey[0]}_{$name}', '{$table->name}', '{$name}', '{$foreignKey[0]}', '{$foreignKey[1]}', null, null); // update 'null' for ON DELTE and ON UPDATE";
		}
		return $code;
	}
<commit_msg>added options InnoDB and CHARSET only for MySQL<commit_after><?php

/**
 * Based upon http://www.yiiframework.com/doc/guide/1.1/en/database.migration#c2550 from Leric
 */
class P3DumpSchemaCommand extends CConsoleCommand {

	public function run($args) {
		$schema = $args[0];
		$prefix = $args[1];
		$tables = Yii::app()->db->schema->getTables($schema);
		$code = '';
		$code .= "if (Yii::app()->db->schema instanceof CMysqlSchema)\n";
		$code .= "	\$options = 'ENGINE=InnoDB DEFAULT CHARSET=utf8';\n";
		$code .= "else\n";
		$code .= "	\$options = '';\n";
		
		foreach ($tables as $table) {
			if (substr($table->name, 0, strlen($prefix)) != $prefix)
				continue;

			$code .= "\n\n\n// Schema for table '" . $table->name . "'\n\n";
			$code .= $this->generateSchema($table, $schema);

			$code .= "\n\n\n// Foreign Keys for table '" . $table->name . "'\n\n";
			$code .= $this->generateForeignKeys($table, $schema);

			$code .= "\n\n\n// Data for table '" . $table->name . "'\n\n";
			$code .= $this->generateInserts($table, $schema);
		}
		echo $code;
	}

	private function generateSchema($table, $schema) {
		$options = "ENGINE=InnoDB DEFAULT CHARSET=utf8";
		$code = '';
		$code .= '$this->createTable("' . $table->name . '", ';
		$code .= "\n";
		$code .= '  array(' . "\n";
		foreach ($table->columns as $col) {
			$code .= '    "' . $col->name . '"=>"' . $this->resolveColumnType($col) . '",' . "\n";
		}
		
		// special case for non-auto-increment PKs
		$code .= $this->generatePrimaryKeys($table->columns);
		$code .= "\n";
		$code .= '  ), ';
		$code .= "\n";
		$code .= '  $options);';
		return $code;
	}


	private function generatePrimaryKeys($columns) {
		foreach ($columns as $col) {			
			if ($col->isPrimaryKey && !$col->autoIncrement) {
				return '    "PRIMARY KEY ('.$col->name.')"';
			}
		}
	}

	private function generateForeignKeys($table, $schema) {
		$code = "";
		foreach ($table->foreignKeys as $name => $foreignKey) {
			#echo "FK" . $name . var_dump($foreignKey);
			$code .= "\n\$this->addForeignKey('fk_{$foreignKey[0]}_{$name}', '{$table->name}', '{$name}', '{$foreignKey[0]}', '{$foreignKey[1]}', null, null); // update 'null' for ON DELTE and ON UPDATE";
		}
		return $code;
	}

{'fraction_non_alphanumeric': 0.2368296155671571, 'fraction_numerical': 0.006881822496440437, 'mean_word_length': 3.215, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '1663669', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1517, 'n_words': 322}