<commit_before><?php

/**
* Copyright (C) 2011 FluxBB (http://fluxbb.org)
* License: LGPL - GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html)
*/

class SQLDialect
{
	protected $db;

	public function __construct($db, $args = array())
	{
		$this->db = $db;
	}

	public final function compile(DatabaseQuery $query)
	{
		$type = get_class($query);
		switch ($type)
		{
			// Regular queries
			case 'SelectQuery': return $this->select($query);
			case 'InsertQuery': return $this->insert($query);
			case 'UpdateQuery': return $this->update($query);
			case 'DeleteQuery': return $this->delete($query);
			case 'TruncateQuery': return $this->truncate($query);
			case 'ReplaceQuery': return $this->replace($query);

			// Utility queries
			case 'CreateTableQuery': return $this->create_table($query);
			case 'RenameTableQuery': return $this->rename_table($query);
			case 'DropTableQuery': return $this->drop_table($query);

			// For direct queries we already have the SQL
			case 'DirectQuery':
				return $query->sql;

			default:
				throw new Exception('Unsupported query type: '.$type);
		}
	}

	public function set_names($charset)
	{
		return 'SET NAMES '.$this->db->quote($charset);
	}

	protected function select(SelectQuery $query)
	{
		if (empty($query->fields))
			throw new Exception('A SELECT query must select at least 1 field.');

		$sql = 'SELECT '.($query->distinct ? 'DISTINCT ' : '').implode(', ', $query->fields);

		if (!empty($query->table))
			$sql .= ' FROM '.($query->use_prefix ? $this->db->prefix : '').$query->table;

		if (!empty($query->joins))
			$sql .= $this->join($query);

		if (!empty($query->where))
			$sql .= $this->where($query);

		if (!empty($query->group))
			$sql .= $this->group($query);

		if (!empty($query->having))
			$sql .= $this->having($query);

		if (!empty($query->order))
			$sql .= $this->order($query);

		if ($query->limit > 0 || $query->offset > 0)
			$sql .= $this->limit_offset($query);

		return $sql;
	}

	protected function insert(InsertQuery $query)
	{
		if (empty($query->table))
			throw new Exception('An INSERT query must have a table specified.');

		if (empty($query->values))
			throw new Exception('An INSERT query must contain at least 1 value.');

		return 'INSERT INTO '.($query->use_prefix ? $this->db->prefix : '').$query->table.' ('.implode(', ', array_keys($query->values)).') VALUES ('.implode(', ', array_values($query->values)).')';
	}

	protected function update(UpdateQuery $query)
	{
		if (empty($query->table))
			throw new Exception('An UPDATE query must have a table specified.');

		if (empty($query->values))
			throw new Exception('An UPDATE query must contain at least 1 value.');

		$updates = array();
		foreach ($query->values as $key => $value)
			$updates[] = $key.' = '.$value;

		$sql = 'UPDATE '.($query->use_prefix ? $this->db->prefix : '').$query->table.' SET '.implode(', ', $updates);

		if (!empty($query->where))
			$sql .= $this->where($query);

		if (!empty($query->order))
			$sql .= $this->order($query);

		if ($query->limit > 0 || $query->offset > 0)
			$sql .= $this->limit_offset($query);

		return $sql;
	}

	protected function delete(DeleteQuery $query)
	{
		if (empty($query->table))
			throw new Exception('A DELETE query must have a table specified.');

		$sql = 'DELETE FROM '.($query->use_prefix ? $this->db->prefix : '').$query->table;

		if (!empty($query->where))
			$sql .= $this->where($query);

		if (!empty($query->order))
			$sql .= $this->order($query);

		if ($query->limit > 0 || $query->offset > 0)
			$sql .= $this->limit_offset($query);

		return $sql;
	}

	protected function truncate(TruncateQuery $query)
	{
		if (empty($query->table))
			throw new Exception('A TRUNCATE query must have a table specified.');

		return 'TRUNCATE TABLE '.($query->use_prefix ? $this->db->prefix : '').$query->table;
	}

	protected function replace(ReplaceQuery $query)
	{
		if (empty($query->table))
			throw new Exception('A REPLACE query must have a table specified.');

		if (empty($query->values))
			throw new Exception('A REPLACE query must contain at least 1 value.');

		$sql = 'REPLACE INTO '.($query->use_prefix ? $this->db->prefix : '').$query->table.' ('.implode(', ', array_keys($query->values)).') VALUES ('.implode(', ', array_values($query->values)).')';
	}

	protected function create_table(CreateTableQuery $query)
	{
		$columns = array();
		foreach ($query->columns as $column)
			$columns[] = $this->column_definition($column);

		return 'CREATE TABLE '.$query->table.' ('.implode(', ', $columns).')';
	}

	protected function rename_table(RenameTableQuery $query)
	{
		return 'ALTER TABLE '.$query->table.' RENAME TO '.$query->new_name;
	}

	protected function drop_table(DropTableQuery $query)
	{
		return 'DROP TABLE '.$query->table;
	}

	protected function column_definition(TableColumn $column)
	{
		if ($column->type === TableColumn::TYPE_SERIAL)
			return $this->column_serial($column->name);

		$sql = $column->name.' '.$this->column_type($column->type);

		if (!empty($column->default))
			$sql .= ' DEFAULT '.$column->default;

		if (!empty($column->key))
			$sql .= ' '.$column->key;

		return $sql;
	}

	protected function column_type($type)
	{
		return $type;
	}

	protected function column_serial($name)
	{
		return $name.' INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY';
	}
<commit_msg>#433: Allow table-related queries from/to non-prefixed tables.<commit_after><?php

/**
* Copyright (C) 2011 FluxBB (http://fluxbb.org)
* License: LGPL - GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html)
*/

class SQLDialect
{
	protected $db;

	public function __construct($db, $args = array())
	{
		$this->db = $db;
	}

	public final function compile(DatabaseQuery $query)
	{
		$type = get_class($query);
		switch ($type)
		{
			// Regular queries
			case 'SelectQuery': return $this->select($query);
			case 'InsertQuery': return $this->insert($query);
			case 'UpdateQuery': return $this->update($query);
			case 'DeleteQuery': return $this->delete($query);
			case 'TruncateQuery': return $this->truncate($query);
			case 'ReplaceQuery': return $this->replace($query);

			// Utility queries
			case 'CreateTableQuery': return $this->create_table($query);
			case 'RenameTableQuery': return $this->rename_table($query);
			case 'DropTableQuery': return $this->drop_table($query);

			// For direct queries we already have the SQL
			case 'DirectQuery':
				return $query->sql;

			default:
				throw new Exception('Unsupported query type: '.$type);
		}
	}

	public function set_names($charset)
	{
		return 'SET NAMES '.$this->db->quote($charset);
	}

	protected function select(SelectQuery $query)
	{
		if (empty($query->fields))
			throw new Exception('A SELECT query must select at least 1 field.');

		$sql = 'SELECT '.($query->distinct ? 'DISTINCT ' : '').implode(', ', $query->fields);

		if (!empty($query->table))
			$sql .= ' FROM '.($query->use_prefix ? $this->db->prefix : '').$query->table;

		if (!empty($query->joins))
			$sql .= $this->join($query);

		if (!empty($query->where))
			$sql .= $this->where($query);

		if (!empty($query->group))
			$sql .= $this->group($query);

		if (!empty($query->having))
			$sql .= $this->having($query);

		if (!empty($query->order))
			$sql .= $this->order($query);

		if ($query->limit > 0 || $query->offset > 0)
			$sql .= $this->limit_offset($query);

		return $sql;
	}

	protected function insert(InsertQuery $query)
	{
		if (empty($query->table))
			throw new Exception('An INSERT query must have a table specified.');

		if (empty($query->values))
			throw new Exception('An INSERT query must contain at least 1 value.');

		return 'INSERT INTO '.($query->use_prefix ? $this->db->prefix : '').$query->table.' ('.implode(', ', array_keys($query->values)).') VALUES ('.implode(', ', array_values($query->values)).')';
	}

	protected function update(UpdateQuery $query)
	{
		if (empty($query->table))
			throw new Exception('An UPDATE query must have a table specified.');

		if (empty($query->values))
			throw new Exception('An UPDATE query must contain at least 1 value.');

		$updates = array();
		foreach ($query->values as $key => $value)
			$updates[] = $key.' = '.$value;

		$sql = 'UPDATE '.($query->use_prefix ? $this->db->prefix : '').$query->table.' SET '.implode(', ', $updates);

		if (!empty($query->where))
			$sql .= $this->where($query);

		if (!empty($query->order))
			$sql .= $this->order($query);

		if ($query->limit > 0 || $query->offset > 0)
			$sql .= $this->limit_offset($query);

		return $sql;
	}

	protected function delete(DeleteQuery $query)
	{
		if (empty($query->table))
			throw new Exception('A DELETE query must have a table specified.');

		$sql = 'DELETE FROM '.($query->use_prefix ? $this->db->prefix : '').$query->table;

		if (!empty($query->where))
			$sql .= $this->where($query);

		if (!empty($query->order))
			$sql .= $this->order($query);

		if ($query->limit > 0 || $query->offset > 0)
			$sql .= $this->limit_offset($query);

		return $sql;
	}

	protected function truncate(TruncateQuery $query)
	{
		if (empty($query->table))
			throw new Exception('A TRUNCATE query must have a table specified.');

		return 'TRUNCATE TABLE '.($query->use_prefix ? $this->db->prefix : '').$query->table;
	}

	protected function replace(ReplaceQuery $query)
	{
		if (empty($query->table))
			throw new Exception('A REPLACE query must have a table specified.');

		if (empty($query->values))
			throw new Exception('A REPLACE query must contain at least 1 value.');

		$sql = 'REPLACE INTO '.($query->use_prefix ? $this->db->prefix : '').$query->table.' ('.implode(', ', array_keys($query->values)).') VALUES ('.implode(', ', array_values($query->values)).')';
	}

	protected function create_table(CreateTableQuery $query)
	{
		$columns = array();
		foreach ($query->columns as $column)
			$columns[] = $this->column_definition($column);

		return 'CREATE TABLE '.($query->use_prefix ? $this->db<>prefix : '').$query->table.' ('.implode(', ', $columns).')';
	}

	protected function rename_table(RenameTableQuery $query)
	{
		return 'ALTER TABLE '.($query->use_prefix ? $this->db<>prefix : '').$query->table.' RENAME TO '.($query->use_prefix ? $this->db<>prefix : '').$query->new_name;
	}

	protected function drop_table(DropTableQuery $query)
	{
		return 'DROP TABLE '.($query->use_prefix ? $this->db<>prefix : '').$query->table;
	}

	protected function column_definition(TableColumn $column)
	{
		if ($column->type === TableColumn::TYPE_SERIAL)
			return $this->column_serial($column->name);

		$sql = $column->name.' '.$this->column_type($column->type);

		if (!empty($column->default))
			$sql .= ' DEFAULT '.$column->default;

		if (!empty($column->key))
			$sql .= ' '.$column->key;

		return $sql;
	}

	protected function column_type($type)
	{
		return $type;
	}

	protected function column_serial($name)
	{
		return $name.' INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY';
	}

{'fraction_non_alphanumeric': 0.19618578337439546, 'fraction_numerical': 0.002828725248654074, 'mean_word_length': 4.08584686774942, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 223, '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': '1586870', 'n_tokens_mistral': 3952, 'n_tokens_neox': 3511, 'n_words': 894}