Source of file AbstractUtil.php

Size: 3,320 Bytes - Last Modified: 2015-11-10T10:04:06-05:00

../src/Query/AbstractUtil.php

123456789101112131415161718192021222324252627282930313233343536373839404142
Covered by 7 test(s):
  • Query_Parser_Test::testGeneric2
  • Query_Parser_Test::testWUnderscore
  • Query_Parser_Test::testFunction
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • SQLiteTest::testConnection
  • FirebirdQBTest::testGetNamedConnection
43
Covered by 7 test(s):
  • Query_Parser_Test::testGeneric2
  • Query_Parser_Test::testWUnderscore
  • Query_Parser_Test::testFunction
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • SQLiteTest::testConnection
  • FirebirdQBTest::testGetNamedConnection
4445464748495051525354
Covered by 12 test(s):
  • MySQLTest::testCreateTable
  • MySQLTest::testBackup
  • MySQLTest::testBackupData
  • PgTest::testCreateTable
  • PgTest::testBackupData
  • SQLiteTest::testBackupStructure
  • SQLiteTest::testDeleteTable
  • SQLiteTest::testBackupData
  • FirebirdTest::testCreateTable
  • FirebirdTest::testDeleteTable
  • FirebirdTest::testBackupData
  • FirebirdQBTest::testBackupStructure
55565758596061626364656667686970
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
7172737475767778
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
79
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
8081
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
828384
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
85
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
8687
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
88
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
89
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
9091
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
92
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
939495
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
96
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
97
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
9899
Covered by 3 test(s):
  • MySQLTest::testCreateTable
  • PgTest::testCreateTable
  • FirebirdTest::testCreateTable
100101102103104105106107108109110111112
Covered by 1 test(s):
  • SQLiteTest::testDeleteTable
113114115116117118119120121122123124125126127128129130131132133134135136137138139
Covered by 1 test(s):
  • Query_Parser_Test::testGeneric2
<?php
/**
 * Query
 *
 * Free Query Builder / Database Abstraction Layer
 *
 * @package		Query
 * @author		Timothy J. Warren
 * @copyright	Copyright (c) 2012 - 2014
 * @link 		https://github.com/aviat4ion/Query
 * @license		http://philsturgeon.co.uk/code/dbad-license
 */

// --------------------------------------------------------------------------

namespace Query;

// --------------------------------------------------------------------------

/**
 * Abstract class defining database / table creation methods
 *
 * @package Query
 * @subpackage Drivers
 * @method string quote_ident(string $sql)
 * @method string quote_table(string $sql)
 */
abstract class AbstractUtil {

	/**
	 * Reference to the current connection object
	 */
	private $conn;

	/**
	 * Save a reference to the connection object for later use
	 *
	 * @param Driver_Interface $conn
	 */
	public function __construct(DriverInterface $conn)
	{
		$this->conn = $conn;
	}

	// --------------------------------------------------------------------------

	/**
	 * Get the driver object for the current connection
	 *
	 * @return Driver_Interface
	 */
	public function get_driver()
	{
		return $this->conn;
	}

	// --------------------------------------------------------------------------

	/**
	 * Convenience public function to generate sql for creating a db table
	 *
	 * @param string $name
	 * @param array $fields
	 * @param array $constraints
	 * @param bool $if_not_exists
	 * @return string
	 */
	public function create_table($name, $fields, array $constraints=array(), $if_not_exists=TRUE)
	{
		$exists_str = ($if_not_exists) ? ' IF NOT EXISTS ' : ' ';

		// Reorganize into an array indexed with column information
		// Eg $column_array[$colname] = array(
		// 		'type' => ...,
		// 		'constraint' => ...,
		// 		'index' => ...,
		// )
		$column_array = \array_zipper(array(
			'type' => $fields,
			'constraint' => $constraints
		));

		// Join column definitions together
		$columns = array();
		foreach($column_array as $n => $props)
		{
			$str = $this->get_driver()->quote_ident($n);
			$str .= (isset($props['type'])) ? " {$props['type']}" : "";
			$str .= (isset($props['constraint'])) ? " {$props['constraint']}" : "";

			$columns[] = $str;
		}

		// Generate the sql for the creation of the table
		$sql = 'CREATE TABLE'.$exists_str.$this->get_driver()->quote_table($name).' (';
		$sql .= implode(', ', $columns);
		$sql .= ')';

		return $sql;
	}

	// --------------------------------------------------------------------------

	/**
	 * Drop the selected table
	 *
	 * @param string $name
	 * @return string
	 */
	public function delete_table($name)
	{
		return 'DROP TABLE IF EXISTS '.$this->get_driver()->quote_table($name);
	}


	// --------------------------------------------------------------------------
	// ! Abstract Methods
	// --------------------------------------------------------------------------

	/**
	 * Return an SQL file with the database table structure
	 *
	 * @abstract
	 * @return string
	 */
	abstract public function backup_structure();

	// --------------------------------------------------------------------------

	/**
	 * Return an SQL file with the database data as insert statements
	 *
	 * @abstract
	 * @return string
	 */
	abstract public function backup_data();

}
// End of abstract_util.php