1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\db\migration;
15
16/**
17* Abstract base class for database migrations
18*
19* Each migration consists of a set of schema and data changes to be implemented
20* in a subclass. This class provides various utility methods to simplify editing
21* a phpBB.
22*/
23abstract class migration implements migration_interface
24{
25	/** @var \phpbb\config\config */
26	protected $config;
27
28	/** @var \phpbb\db\driver\driver_interface */
29	protected $db;
30
31	/** @var \phpbb\db\tools\tools_interface */
32	protected $db_tools;
33
34	/** @var string */
35	protected $table_prefix;
36
37	/** @var string */
38	protected $phpbb_root_path;
39
40	/** @var string */
41	protected $php_ext;
42
43	/** @var array Errors, if any occurred */
44	protected $errors;
45
46	/** @var array List of queries executed through $this->sql_query() */
47	protected $queries = array();
48
49	/**
50	* Constructor
51	*
52	* @param \phpbb\config\config $config
53	* @param \phpbb\db\driver\driver_interface $db
54	* @param \phpbb\db\tools\tools_interface $db_tools
55	* @param string $phpbb_root_path
56	* @param string $php_ext
57	* @param string $table_prefix
58	*/
59	public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\db\tools\tools_interface $db_tools, $phpbb_root_path, $php_ext, $table_prefix)
60	{
61		$this->config = $config;
62		$this->db = $db;
63		$this->db_tools = $db_tools;
64		$this->table_prefix = $table_prefix;
65
66		$this->phpbb_root_path = $phpbb_root_path;
67		$this->php_ext = $php_ext;
68
69		$this->errors = array();
70	}
71
72	/**
73	* {@inheritdoc}
74	*/
75	static public function depends_on()
76	{
77		return array();
78	}
79
80	/**
81	* {@inheritdoc}
82	*/
83	public function effectively_installed()
84	{
85		return false;
86	}
87
88	/**
89	* {@inheritdoc}
90	*/
91	public function update_schema()
92	{
93		return array();
94	}
95
96	/**
97	* {@inheritdoc}
98	*/
99	public function revert_schema()
100	{
101		return array();
102	}
103
104	/**
105	* {@inheritdoc}
106	*/
107	public function update_data()
108	{
109		return array();
110	}
111
112	/**
113	* {@inheritdoc}
114	*/
115	public function revert_data()
116	{
117		return array();
118	}
119
120	/**
121	* Wrapper for running queries to generate user feedback on updates
122	*
123	* @param string $sql SQL query to run on the database
124	* @return mixed Query result from db->sql_query()
125	*/
126	protected function sql_query($sql)
127	{
128		$this->queries[] = $sql;
129
130		$this->db->sql_return_on_error(true);
131
132		if ($sql === 'begin')
133		{
134			$result = $this->db->sql_transaction('begin');
135		}
136		else if ($sql === 'commit')
137		{
138			$result = $this->db->sql_transaction('commit');
139		}
140		else
141		{
142			$result = $this->db->sql_query($sql);
143			if ($this->db->get_sql_error_triggered())
144			{
145				$this->errors[] = array(
146					'sql'	=> $this->db->get_sql_error_sql(),
147					'code'	=> $this->db->get_sql_error_returned(),
148				);
149			}
150		}
151
152		$this->db->sql_return_on_error(false);
153
154		return $result;
155	}
156
157	/**
158	* Get the list of queries run
159	*
160	* @return array
161	*/
162	public function get_queries()
163	{
164		return $this->queries;
165	}
166}
167