22
33namespace Nicklayb \LaravelDbImport ;
44
5+ /**
6+ * Class for Import process
7+ *
8+ * @author Nicolas Boisvert (nicklay@me.com)
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+ */
513abstract class Import
614{
715 /**
@@ -52,25 +60,37 @@ abstract class Import
5260 */
5361 protected $ refresh = false ;
5462
63+ /**
64+ * Specify table by table the select statement of which column to load
65+ *
66+ * @var array
67+ */
5568 protected $ selects = [];
5669
70+ /**
71+ * Show table command, it may change depending on your database server
72+ *
73+ * @var string
74+ */
5775 protected $ showTablesCommand = 'SHOW TABLES ' ;
5876
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+ */
6983 public function hasSelects ($ table )
7084 {
7185 return isset ($ this ->selects [$ table ]);
7286 }
7387
88+ /**
89+ * Gets specific selects for defined table
90+ *
91+ * @param string $table
92+ * @return array
93+ */
7494 public function getSelects ($ table )
7595 {
7696 if ($ this ->hasSelects ($ table )) {
@@ -79,46 +99,62 @@ public function getSelects($table)
7999 return ['* ' ];
80100 }
81101
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+ */
87107 public function getQualifiedTableColumnName ()
88108 {
89109 return 'Tables_in_ ' .$ this ->sourceConnection ;
90110 }
91111
112+ /**
113+ * Return the database name from the configuration for the source
114+ *
115+ * @return string
116+ */
92117 public function getSourceDatabaseName ()
93118 {
94119 return config ('database.connections. ' .$ this ->sourceConnection .'.database ' );
95120 }
96121
122+ /**
123+ * Load all tables from the source connection
124+ *
125+ * @return array
126+ */
97127 public function loadSourceTables ()
98128 {
99129 return DB ::connection ($ this ->sourceConnection )->select ($ this ->showTablesCommand );
100130 }
101131
132+ /**
133+ * Get a collection of only the table names from the the source connection
134+ *
135+ * @return Collection
136+ */
102137 public function getSourceTables ()
103138 {
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 ())
115140 }
116141
142+ /**
143+ * Return the count of the pre/post tasks of the import
144+ *
145+ * @return int
146+ */
117147 public function countImportTasks ()
118148 {
119149 return count ($ this ->preImport ()) + count ($ this ->postImport ());
120150 }
121151
152+ /**
153+ * Return all rows from specified table in the source connection with
154+ * the selected columns
155+ *
156+ * @return Collection
157+ */
122158 public function getSourceRows ($ table )
123159 {
124160 return DB ::connection ($ this ->sourceConnection )
@@ -127,20 +163,35 @@ public function getSourceRows($table)
127163 ->get ();
128164 }
129165
166+ /**
167+ * Delete the content of the destination connection table
168+ *
169+ * @return int
170+ */
130171 public function clearDestinationTable ($ table )
131172 {
132173 return DB ::connection ($ this ->destinationConnection )
133174 ->table ($ table )
134175 ->delete ();
135176 }
136177
178+ /**
179+ * Insert specific data into the destination connection
180+ *
181+ * @return int
182+ */
137183 public function insertInDestination ($ table , $ row )
138184 {
139185 return DB ::connection ($ this ->destinationConnection )
140186 ->table ($ table )
141187 ->insert ((array ) $ this ->executeManipulation ($ table , $ row ));
142188 }
143189
190+ /**
191+ * Sort the sources tables by ordering last tables and removing the ingored
192+ *
193+ * @return Collection
194+ */
144195 public function getSortedSourceTables ()
145196 {
146197 $ tables = $ this ->getSourceTables ();
@@ -149,31 +200,31 @@ public function getSortedSourceTables()
149200 foreach ($ tables as $ table ) {
150201 if ($ this ->hasLastTable ($ table )) {
151202 $ hold ->push ($ table );
152- } else {
203+ } elseif (! $ this -> hasIgnoreTable ( $ table )) {
153204 $ filteredTables ->push ($ table );
154205 }
155206 }
156207 return $ filteredTables ->merge ($ holds );
157208 }
158209
210+ /**
211+ * Check if a specified table should be ignored
212+ *
213+ * @return bool
214+ */
159215 public function hasIgnoreTable ($ table )
160216 {
161217 return in_array ($ table , $ this ->ignoreTables );
162218 }
163219
164- public function hasLastTable ($ table )
165- {
166- return in_array ($ table , $ this ->lastTables );
167- }
168-
169220 /**
170- * Check if any ignore table is registered in the property
221+ * Check if a specified table should be last
171222 *
172223 * @return bool
173224 */
174- public function hasIgnoreTables ( )
225+ public function hasLastTable ( $ table )
175226 {
176- return count ( $ this ->ignoreTables ) > 0 ;
227+ return in_array ( $ table , $ this ->lastTables ) ;
177228 }
178229
179230 /**
0 commit comments