2
2
3
3
namespace Nicklayb \LaravelDbImport ;
4
4
5
+ /**
6
+ * Class for Import process
7
+ *
8
+ * @author Nicolas Boisvert ([email protected] )
9
+ *
10
+ * Extends this class to match your needs. Don't forget to add it in your
11
+ * dbimport.php config file so you will be able to call it
12
+ */
5
13
abstract class Import
6
14
{
7
15
/**
@@ -52,25 +60,37 @@ abstract class Import
52
60
*/
53
61
protected $ refresh = false ;
54
62
63
+ /**
64
+ * Specify table by table the select statement of which column to load
65
+ *
66
+ * @var array
67
+ */
55
68
protected $ selects = [];
56
69
70
+ /**
71
+ * Show table command, it may change depending on your database server
72
+ *
73
+ * @var string
74
+ */
57
75
protected $ showTablesCommand = 'SHOW TABLES ' ;
58
76
59
- public function getShowTablesCommand ()
60
- {
61
- return $ this ->showTablesCommand ;
62
- }
63
-
64
- public function getSourceConnection ()
65
- {
66
- return $ this ->sourceConnection ;
67
- }
68
-
77
+ /**
78
+ * Checks if provided table has specific selected columns
79
+ *
80
+ * @param string $table
81
+ * @return bool
82
+ */
69
83
public function hasSelects ($ table )
70
84
{
71
85
return isset ($ this ->selects [$ table ]);
72
86
}
73
87
88
+ /**
89
+ * Gets specific selects for defined table
90
+ *
91
+ * @param string $table
92
+ * @return array
93
+ */
74
94
public function getSelects ($ table )
75
95
{
76
96
if ($ this ->hasSelects ($ table )) {
@@ -79,46 +99,62 @@ public function getSelects($table)
79
99
return ['* ' ];
80
100
}
81
101
82
- public function getDestinationConnection ()
83
- {
84
- return $ this -> destinationConnection ;
85
- }
86
-
102
+ /**
103
+ * Return the qualified column name for table select
104
+ *
105
+ * @return string
106
+ */
87
107
public function getQualifiedTableColumnName ()
88
108
{
89
109
return 'Tables_in_ ' .$ this ->sourceConnection ;
90
110
}
91
111
112
+ /**
113
+ * Return the database name from the configuration for the source
114
+ *
115
+ * @return string
116
+ */
92
117
public function getSourceDatabaseName ()
93
118
{
94
119
return config ('database.connections. ' .$ this ->sourceConnection .'.database ' );
95
120
}
96
121
122
+ /**
123
+ * Load all tables from the source connection
124
+ *
125
+ * @return array
126
+ */
97
127
public function loadSourceTables ()
98
128
{
99
129
return DB ::connection ($ this ->sourceConnection )->select ($ this ->showTablesCommand );
100
130
}
101
131
132
+ /**
133
+ * Get a collection of only the table names from the the source connection
134
+ *
135
+ * @return Collection
136
+ */
102
137
public function getSourceTables ()
103
138
{
104
- return collect ($ this ->getTableSelect ())->pluck ($ this ->getQualifiedTableColumnName ())
105
- }
106
-
107
- public function getIgnoreTables ()
108
- {
109
- return $ this ->ignoreTables ;
110
- }
111
-
112
- public function getLastTables ()
113
- {
114
- return $ this ->lastTables ;
139
+ return collect ($ this ->loadSourceTables ())->pluck ($ this ->getQualifiedTableColumnName ())
115
140
}
116
141
142
+ /**
143
+ * Return the count of the pre/post tasks of the import
144
+ *
145
+ * @return int
146
+ */
117
147
public function countImportTasks ()
118
148
{
119
149
return count ($ this ->preImport ()) + count ($ this ->postImport ());
120
150
}
121
151
152
+ /**
153
+ * Return all rows from specified table in the source connection with
154
+ * the selected columns
155
+ *
156
+ * @return Collection
157
+ */
122
158
public function getSourceRows ($ table )
123
159
{
124
160
return DB ::connection ($ this ->sourceConnection )
@@ -127,20 +163,35 @@ public function getSourceRows($table)
127
163
->get ();
128
164
}
129
165
166
+ /**
167
+ * Delete the content of the destination connection table
168
+ *
169
+ * @return int
170
+ */
130
171
public function clearDestinationTable ($ table )
131
172
{
132
173
return DB ::connection ($ this ->destinationConnection )
133
174
->table ($ table )
134
175
->delete ();
135
176
}
136
177
178
+ /**
179
+ * Insert specific data into the destination connection
180
+ *
181
+ * @return int
182
+ */
137
183
public function insertInDestination ($ table , $ row )
138
184
{
139
185
return DB ::connection ($ this ->destinationConnection )
140
186
->table ($ table )
141
187
->insert ((array ) $ this ->executeManipulation ($ table , $ row ));
142
188
}
143
189
190
+ /**
191
+ * Sort the sources tables by ordering last tables and removing the ingored
192
+ *
193
+ * @return Collection
194
+ */
144
195
public function getSortedSourceTables ()
145
196
{
146
197
$ tables = $ this ->getSourceTables ();
@@ -149,31 +200,31 @@ public function getSortedSourceTables()
149
200
foreach ($ tables as $ table ) {
150
201
if ($ this ->hasLastTable ($ table )) {
151
202
$ hold ->push ($ table );
152
- } else {
203
+ } elseif (! $ this -> hasIgnoreTable ( $ table )) {
153
204
$ filteredTables ->push ($ table );
154
205
}
155
206
}
156
207
return $ filteredTables ->merge ($ holds );
157
208
}
158
209
210
+ /**
211
+ * Check if a specified table should be ignored
212
+ *
213
+ * @return bool
214
+ */
159
215
public function hasIgnoreTable ($ table )
160
216
{
161
217
return in_array ($ table , $ this ->ignoreTables );
162
218
}
163
219
164
- public function hasLastTable ($ table )
165
- {
166
- return in_array ($ table , $ this ->lastTables );
167
- }
168
-
169
220
/**
170
- * Check if any ignore table is registered in the property
221
+ * Check if a specified table should be last
171
222
*
172
223
* @return bool
173
224
*/
174
- public function hasIgnoreTables ( )
225
+ public function hasLastTable ( $ table )
175
226
{
176
- return count ( $ this ->ignoreTables ) > 0 ;
227
+ return in_array ( $ table , $ this ->lastTables ) ;
177
228
}
178
229
179
230
/**
0 commit comments