@@ -30,55 +30,93 @@ class Blueprint extends Constants{
3030 */
3131 public function __construct (?string $ tableName = null )
3232 {
33- $ this ->db = new DB ();
34- $ this ->tableName = $ tableName ;
35- $ this ->charSet = $ _ENV ['DB_CHARSET ' ] ?? '' ;
36- $ this ->collation = $ _ENV ['DB_COLLATION ' ] ?? '' ;
33+ $ this ->db = new DB ();
34+ $ this ->tableName = $ tableName ;
35+ $ this ->charSet = $ _ENV ['DB_CHARSET ' ] ?? '' ;
36+ $ this ->collation = $ _ENV ['DB_COLLATION ' ] ?? '' ;
37+ }
38+
39+ /**
40+ * Creating Session Query
41+ * - To hold each Migration Request
42+ * @param mixed $query
43+ *
44+ * @return void
45+ */
46+ private function tempMigrationQuery (mixed $ query = null )
47+ {
48+ // Start the session has not already been started
49+ if (session_status () == PHP_SESSION_NONE ) {
50+ session_start ();
51+ }
52+
53+ $ _SESSION [$ this ->session ] = json_encode ($ query );
3754 }
3855
3956 /**
4057 * Creating Table Structure
4158 * Indexs|Primary|Constraints
4259 *
43- * @return string \MySQLTemplate
60+ * @return array \MySQLTemplate
4461 */
4562 private function MySQLTemplate ()
4663 {
4764 $ checkPrimary = array_column ($ this ->columns , 'primary ' );
4865 if (count ($ checkPrimary ) > 1 ){
49- throw new \Exception ('Primary Key can not be more than one in a table ' );
66+ return [
67+ 'response ' => self ::ERROR_404 ,
68+ 'message ' => sprintf ("Primary Key can not be more than one in `%s` @table " , $ this ->tableName ),
69+ ];
5070 }
51-
52- return $ this ->toMySQLQuery ();
71+
72+ return [
73+ 'response ' => self ::ERROR_200 ,
74+ 'message ' => $ this ->toMySQLQuery ()
75+ ];
5376 }
5477
5578 /**
5679 * Creating Database Table
5780 *
5881 * @return array\handle
5982 */
60- private function handle ()
83+ public function handle ()
6184 {
85+ // create traceable table
86+ $ traceTable = $ this ->traceable ($ this ->tableName );
87+
88+ // handle error
89+ $ handle = self ::checkDBConnect ($ traceTable );
90+ if (is_array ($ handle )){
91+ return $ handle ;
92+ }
93+
94+ // primary key error
95+ $ mysqlHandle = $ this ->MySQLTemplate ();
96+ if ($ mysqlHandle ['response ' ] != self ::ERROR_200 ){
97+ return $ mysqlHandle ;
98+ }
99+
62100 // Handle query
63101 try {
64102 // check if table already exist
65103 if ($ this ->db ->tableExist ($ this ->tableName )){
66- $ message = "Migration runned
67- <span style='background: #ee0707; {$ this ->style }'>
68- Failed
69- </span> Table already exist on ` {$ this -> traceable ( $ this -> tableName ) }` <br> \n" ;
104+ $ message = "Migration
105+ <span style='background: #ee0707; {$ this ->style }'>
106+ Failed
107+ </span> Table exist on ` {$ traceTable }` <br> \n" ;
70108 }else {
71109 $ this ->status_runned = true ;
72110 $ message = "Migration runned
73111 <span style='background: #027b02; {$ this ->style }'>
74112 Successfully
75- </span> on
76- ` {$ this -> traceable ( $ this -> tableName ) }` <br> \n" ;
113+ </span> on
114+ ` {$ traceTable }` <br> \n" ;
77115 }
78116
79117 // execute query
80118 if ($ this ->status_runned ){
81- $ this ->db ->query ( $ this -> MySQLTemplate () )->execute ();
119+ $ this ->db ->query ( $ mysqlHandle [ ' message ' ] )->execute ();
82120 }
83121
84122 return [
@@ -87,26 +125,39 @@ private function handle()
87125 ];
88126 } catch (PDOException $ e ){
89127 return ['response ' => self ::ERROR_404 , 'message ' => $ e ->getMessage ()];
90- exit ();
91128 }
92129 }
93130
94- public function __destruct ()
131+ /**
132+ * Save query data into sessions
133+ *
134+ * @return void
135+ */
136+ public function __destruct ()
95137 {
96- // Blueprint handle
97- $ handle = $ this ->handle ();
98-
99- if ($ handle ['response ' ] !== self ::ERROR_200 ){
100- echo preg_replace (
101- '/^[ \t]+|[ \t]+$/m ' , '' ,
102- sprintf ("<< \\Error code>> %s
103- <br><br>
104- << \\PDO::ERROR>> %s <br> \n
105- " , $ handle ['response ' ], $ handle ['message ' ])
106- );
107- return ;
138+ $ this ->tempMigrationQuery ($ this ->handle ());
139+ }
140+
141+ /**
142+ * Check database connection error
143+ * @param string $tableName
144+ *
145+ * @return mixed
146+ */
147+ private function checkDBConnect (?string $ tableName = null )
148+ {
149+ // if database connection is okay
150+ $ dbConnection = $ this ->db ->getConnection ();
151+ if ($ dbConnection ['status ' ] !== self ::ERROR_200 ){
152+ return [
153+ 'status ' => self ::ERROR_404 ,
154+ 'message ' => "Connection Error
155+ <span style='background: #ee0707; {$ this ->style }'>
156+ Database Connection Error
157+ </span> on ` {$ tableName }`
158+ ` {$ dbConnection ['message ' ]}` <br> \n" ,
159+ ];
108160 }
109- echo "{$ handle ['message ' ]}" ;
110161 }
111162
112163}
0 commit comments