Completed
Push — 2016.04 ( 690368...e61816 )
by Aimeos
04:37
created

ListsOptimizeIndexesTypo3::dropIndexes()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
eloc 10
nc 4
nop 1
dl 0
loc 19
rs 8.8571
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2014
7
 */
8
9
10
namespace Aimeos\MW\Setup\Task;
11
12
13
/**
14
 * Optmizes the unique constraints of list tables and their indexes.
15
 */
16
class ListsOptimizeIndexesTypo3 extends \Aimeos\MW\Setup\Task\Base
17
{
18
	private $mysql = array(
19
		'add' => array(
20
			'fe_users_list' => array(
21
				'unq_t3feuli_sid_dm_rid_tid_pid' => 'ALTER TABLE "fe_users_list" ADD CONSTRAINT "unq_t3feuli_sid_dm_rid_tid_pid" UNIQUE ("siteid", "domain", "refid", "typeid", "parentid")',
22
			),
23
		),
24
		'delete' => array(
25
			'fe_users_list' => array(
26
				'unq_t3feuli_sid_pid_dm_rid_tid' => 'ALTER TABLE "fe_users_list" DROP INDEX "unq_t3feuli_sid_pid_dm_rid_tid"',
27
			),
28
		),
29
		'indexes' => array(
30
			'fe_users_list' => array(
31
				'idx_t3feuli_sid_rid_dom_tid' => 'ALTER TABLE "fe_users_list" DROP INDEX "idx_t3feuli_sid_rid_dom_tid"',
32
				'idx_t3feuli_pid_sid_rid' => 'ALTER TABLE "fe_users_list" DROP INDEX "idx_t3feuli_pid_sid_rid"',
33
			),
34
		),
35
	);
36
37
38
	/**
39
	 * Returns the list of task names which this task depends on.
40
	 *
41
	 * @return array List of task names
42
	 */
43
	public function getPreDependencies()
44
	{
45
		return array();
46
	}
47
48
49
	/**
50
	 * Returns the list of task names which depends on this task.
51
	 *
52
	 * @return array List of task names
53
	 */
54
	public function getPostDependencies()
55
	{
56
		return array();
57
	}
58
59
60
	/**
61
	 * Executes the task for MySQL databases.
62
	 */
63
	protected function mysql()
64
	{
65
		$this->process( $this->mysql );
66
	}
67
68
69
	/**
70
	 * Renames all order tables if they exist.
71
	 *
72
	 * @param array $stmts Associative array of tables names and lists of SQL statements to execute.
73
	 */
74
	protected function process( array $stmts )
75
	{
76
		$this->msg( 'Optimize list indexes in TYPO3 extension', 0 );
77
		$this->status( '' );
78
79
		$this->addConstraints( $stmts['add'] );
80
		$this->deleteConstraints( $stmts['delete'] );
81
		$this->dropIndexes( $stmts['indexes'] );
82
	}
83
84
85
	/**
86
	 * Adds the new constraints to the tables.
87
	 *
88
	 * @param array $stmts Associative list of table names and list of statements
89
	 */
90
	protected function addConstraints( array $stmts )
91
	{
92
		foreach( $stmts as $table => $stmtList )
93
		{
94
			foreach ( $stmtList as $name => $stmt )
95
			{
96
				$this->msg( sprintf( 'Adding constraint "%1$s": ', $name ), 1 );
97
98
				if( $this->schema->tableExists( $table ) === true
99
						&& $this->schema->constraintExists( $table, $name ) === false
100
				) {
101
					$this->execute( $stmt );
102
					$this->status( 'done' );
103
				} else {
104
					$this->status( 'OK' );
105
				}
106
			}
107
		}
108
	}
109
110
111
	/**
112
	 * Deletes existing constraints from the tables.
113
	 *
114
	 * @param array $stmts Associative list of table names and list of statements
115
	 */
116
	protected function deleteConstraints( array $stmts )
117
	{
118
		foreach( $stmts as $table => $stmtList )
119
		{
120
			foreach ( $stmtList as $name => $stmt )
121
			{
122
				$this->msg( sprintf( 'Deleting constraint "%1$s": ', $name ), 1 );
123
124
				if( $this->schema->tableExists( $table ) === true
125
					&& $this->schema->constraintExists( $table, $name ) === true
126
				) {
127
					$this->execute( $stmt );
128
					$this->status( 'done' );
129
				} else {
130
					$this->status( 'OK' );
131
				}
132
			}
133
		}
134
	}
135
136
137
	/**
138
	 * Drops existing indexes from the tables.
139
	 *
140
	 * @param array $stmts Associative list of table names and list of statements
141
	 */
142
	protected function dropIndexes( array $stmts )
143
	{
144
		foreach( $stmts as $table => $stmtList )
145
		{
146
			foreach ( $stmtList as $name => $stmt )
147
			{
148
				$this->msg( sprintf( 'Dropping index "%1$s": ', $name ), 1 );
149
150
				if( $this->schema->tableExists( $table ) === true
151
					&& $this->schema->indexExists( $table, $name ) === true
152
				) {
153
					$this->execute( $stmt );
154
					$this->status( 'done' );
155
				} else {
156
					$this->status( 'OK' );
157
				}
158
			}
159
		}
160
	}
161
}
162