@@ -14,26 +14,24 @@ class DBImport extends DB{
1414 use DBImportTrait;
1515
1616 private $ db_connection ;
17- private $ error ;
18- private $ message ;
1917 private $ realpath ;
20- private $ template ;
21- private $ array = [] ;
18+ public $ error ;
19+ public $ message ;
2220
2321 /**
2422 * Construct Instance of Database
2523 */
2624 public function __construct () {
2725 parent ::__construct ();
28-
26+ $ this -> error = self :: ERROR_404 ;
2927 $ this ->db_connection = $ this ->getConnection ();
3028 }
3129
3230 /**
3331 * Database Importation
3432 * @param string path_to_sql
3533 *
36- * @return
34+ * @return object\builder\Database\DatabaseImport
3735 */
3836 public function DatabaseImport ($ path_to_sql = NULL )
3937 {
@@ -42,83 +40,42 @@ public function DatabaseImport($path_to_sql = NULL)
4240 /**
4341 * If SQL file does'nt exists
4442 */
45- if (!file_exists ($ this ->realpath ) || is_dir ($ this ->realpath ))
46- {
47- return [
48- 'response ' => self ::ERROR_404 ,
49- 'message ' => "Failed to open stream: ` {$ path_to_sql }` does'nt exist. "
50- ];
51-
43+ if (!file_exists ($ this ->realpath ) || is_dir ($ this ->realpath )){
44+ $ this ->message = "Failed to open stream: ` {$ path_to_sql }` does'nt exist. " ;
5245 } else {
5346
5447 // read a file into an array
5548 $ readFile = file ($ this ->realpath );
5649
5750 // is readable
58- if (!$ this ->isReadable ($ readFile ))
59- {
60- return [
61- 'response ' => self ::ERROR_404 ,
62- 'message ' => "Failed to read file or empty data. "
63- ];
51+ if (!$ this ->isReadable ($ readFile )){
52+ $ this ->message = "Failed to read file or empty data. " ;
6453 } else {
6554
66- // Begin our final importation
67- foreach ($ readFile as $ key => $ query )
68- {
69- // skip if its a comment
70- if ($ this ->isComment ($ query ))
71- continue ;
72-
73- //Add to the current segment
74- $ this ->template .= $ query ;
75-
76- // Check if it's a query
77- if ($ this ->isQuery ($ query ))
78- {
79- // check if connection test is okay
80- if ($ this ->DBConnect ()){
81- try {
82- //Query the database
83- $ this ->query ($ this ->template )->execute ();
84-
85- } catch (PDOException $ e ){
55+ // check if connection test is okay
56+ if ($ this ->DBConnect ()){
57+ try {
58+ // connection driver
59+ $ Driver = $ this ->connection ['driver ' ];
8660
87- // get error msg
88- $ errorMsg = $ e -> getMessage ( );
61+ // get content
62+ $ sql = file_get_contents ( $ this -> realpath );
8963
90- if ($ errorMsg != '0 ' ){
91- if (
92- strpos ($ errorMsg , "Multiple primary key defined " ) === false
93- && strpos ($ errorMsg , "Duplicate entry " ) === false ){
64+ // execute query
65+ $ Driver ->exec ($ sql );
9466
95- $ this ->message = "- Performing query: <strong style='color: #000'> {$ errorMsg }</strong> " ;
96- $ this ->array [] = $ this ->message ;
97- }
98- $ this ->error = self ::ERROR_400 ;
99- }
100- }
101- }else {
102- $ this ->message = $ this ->db_connection ['message ' ];
103- $ this ->array [] = $ this ->message ;
104- $ this ->error = $ this ->db_connection ['status ' ];
105- break ;
106- }
107-
108- // Set the template to an empty string
109- $ this ->template = '' ;
67+ $ this ->error = self ::ERROR_200 ;
68+ $ this ->message = "- Database has been imported successfully. " ;
69+ } catch (PDOException $ e ){
70+ $ this ->message = "- Performing query: <strong style='color: #000'> {$ e ->getMessage ()}</strong> " ;
71+ $ this ->error = self ::ERROR_400 ;
11072 }
73+ } else {
74+ $ this ->message = $ this ->db_connection ['message ' ];
11175 }
11276 }
11377 }
11478
115- // successful and no errors
116- if (count ($ this ->array ) === 0 && $ this ->db_connection ['status ' ] == self ::ERROR_200 ){
117- $ this ->error = self ::ERROR_200 ;
118- $ this ->message = "- Database has been imported successfully. " ;
119- $ this ->array [0 ] = $ this ->message ;
120- }
121-
12279 /*
12380 | ----------------------------------------------------------------------------
12481 | Database importation use. Below are the response code
@@ -128,9 +85,11 @@ public function DatabaseImport($path_to_sql = NULL)
12885 | if ->response === 200 (Success importing to database
12986 */
13087
131- return [
132- 'response ' => $ this ->error ,
133- 'message ' => $ this ->array
88+ return (object ) [
89+ 'response ' => $ this ->error ,
90+ 'message ' => is_array ($ this ->message )
91+ ? implode ('\n<br> ' , $ this ->message )
92+ : $ this ->message
13493 ];
13594 }
13695
0 commit comments