|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Seeds the table with the defined data. |
|
7
|
|
|
* |
|
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Glynn Quelch <[email protected]> |
|
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
22
|
|
|
* @package PinkCrab\DB_Migration |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace PinkCrab\DB_Migration; |
|
26
|
|
|
|
|
27
|
|
|
use wpdb; |
|
28
|
|
|
use PinkCrab\Table_Builder\Schema; |
|
29
|
|
|
|
|
30
|
|
|
class Migration_Seeder { |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* WPDB isntance |
|
34
|
|
|
* |
|
35
|
|
|
* @var wpdb |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $wpdb; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( wpdb $wpdb ) { |
|
40
|
|
|
$this->wpdb = $wpdb; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Seeds a specific table with the data passed. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \PinkCrab\Table_Builder\Schema $schema |
|
47
|
|
|
* @param array<array<string, mixed>> $seed_data |
|
48
|
|
|
* @return array<string, int> |
|
49
|
|
|
*/ |
|
50
|
|
|
public function seed( Schema $schema, array $seed_data ): array { |
|
51
|
|
|
$results = array(); |
|
52
|
|
|
|
|
53
|
|
|
foreach ( $seed_data as $key => $seed ) { |
|
54
|
|
|
$results[ $key ] = $this->insert_seed( $schema, $seed ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $results; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Inserts a row of seed data. |
|
62
|
|
|
* |
|
63
|
|
|
* @param \PinkCrab\Table_Builder\Schema $schema |
|
64
|
|
|
* @param array<array<string, mixed>> $seed |
|
65
|
|
|
* @return int The new row ID. |
|
66
|
|
|
* @throws Migration_Exception |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function insert_seed( Schema $schema, array $seed ): int { |
|
69
|
|
|
// Get format for each column based on the type. |
|
70
|
|
|
$format = array_map( |
|
71
|
|
|
function( string $column_name ) use ( $schema ): string { |
|
72
|
|
|
return $this->column_type( $schema, $column_name ); |
|
73
|
|
|
}, |
|
74
|
|
|
array_keys( $seed ) |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$this->wpdb->insert( |
|
78
|
|
|
$schema->get_table_name(), |
|
79
|
|
|
$seed, |
|
80
|
|
|
$format |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
// Check any errors inserting. |
|
84
|
|
|
if ( $this->wpdb->last_error !== '' ) { |
|
85
|
|
|
throw Migration_Exception::failed_to_insert_seed( $schema, $this->wpdb->last_error ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->wpdb->insert_id; |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Gets the column data type from schema. |
|
93
|
|
|
* |
|
94
|
|
|
* @param \PinkCrab\Table_Builder\Schema $schema |
|
95
|
|
|
* @param string $column |
|
96
|
|
|
* @return string |
|
97
|
|
|
* @throws Migration_Exception If a column cant be found. |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function column_type( Schema $schema, string $column ): string { |
|
100
|
|
|
$schema_columns = $schema->get_columns(); |
|
101
|
|
|
|
|
102
|
|
|
// If colum doesnt exist, thorw exception. |
|
103
|
|
|
if ( ! array_key_exists( $column, $schema_columns ) ) { |
|
104
|
|
|
throw Migration_Exception::seed_column_doesnt_exist( $schema, $column ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->column_type_format( $schema_columns[ $column ]->get_type() ?? '' ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns the WPDB prepare format based on column type. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $type |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function column_type_format( string $type ): string { |
|
117
|
|
|
switch ( \strtoupper( $type ) ) { |
|
118
|
|
|
case 'CHAR': |
|
119
|
|
|
case 'VARCHAR': |
|
120
|
|
|
case 'BINARY': |
|
121
|
|
|
case 'VARBINARY': |
|
122
|
|
|
case 'TINYBLOB': |
|
123
|
|
|
case 'TINYTEXT': |
|
124
|
|
|
case 'TEXT': |
|
125
|
|
|
case 'BLOB': |
|
126
|
|
|
case 'MEDIUMTEXT': |
|
127
|
|
|
case 'MEDIUMBLOB': |
|
128
|
|
|
case 'LONGTEXT': |
|
129
|
|
|
case 'LONGBLOB': |
|
130
|
|
|
case 'DATE': |
|
131
|
|
|
case 'DATETIME': |
|
132
|
|
|
case 'TIMESTAMP': |
|
133
|
|
|
case 'TIME': |
|
134
|
|
|
case 'YEAR': |
|
135
|
|
|
return '%s'; |
|
136
|
|
|
|
|
137
|
|
|
case 'BIT': |
|
138
|
|
|
case 'TINYINT': |
|
139
|
|
|
case 'BOOL': |
|
140
|
|
|
case 'BOOLEAN': |
|
141
|
|
|
case 'SMALLINT': |
|
142
|
|
|
case 'MEDIUMINT': |
|
143
|
|
|
case 'INT': |
|
144
|
|
|
case 'INTEGER': |
|
145
|
|
|
case 'BIGINT': |
|
146
|
|
|
return '%d'; |
|
147
|
|
|
|
|
148
|
|
|
case 'FLOAT': |
|
149
|
|
|
case 'DOUBLE': |
|
150
|
|
|
case 'DOUBLE PRECISION': |
|
151
|
|
|
case 'DECIMAL': |
|
152
|
|
|
case 'DEC': |
|
153
|
|
|
return '%f'; |
|
154
|
|
|
|
|
155
|
|
|
default: |
|
156
|
|
|
return '%s'; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|