1 | <?php |
||
2 | |||
3 | namespace Lifeeka\JSQL; |
||
4 | |||
5 | use Lifeeka\JSQL\Extractor\JsonExtractor; |
||
6 | use Lifeeka\JSQL\Helpers\Json; |
||
7 | use Illuminate\Database\Capsule\Manager as Capsule; |
||
8 | |||
9 | /** |
||
10 | * Class Client. |
||
11 | */ |
||
12 | class Client |
||
13 | { |
||
14 | const TABLE_ONLY = "table_only"; |
||
15 | const DATA_ONLY = "data_only"; |
||
16 | const ALL = "all"; |
||
17 | |||
18 | public $mainTableName = "main"; |
||
19 | |||
20 | public $capsule; |
||
21 | public $error = false; |
||
22 | public $file_content = null; |
||
23 | public $sql = null; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Client constructor. |
||
28 | * |
||
29 | * @param $config |
||
30 | */ |
||
31 | public function __construct($config) |
||
32 | { |
||
33 | $this->capsule = new Capsule(); |
||
34 | |||
35 | $this->capsule->addConnection([ |
||
36 | 'driver' => 'mysql', |
||
37 | 'host' => $config['host'], |
||
38 | 'database' => $config['db'], |
||
39 | 'username' => $config['username'], |
||
40 | 'password' => $config['password'], |
||
41 | 'charset' => 'utf8mb4', |
||
42 | 'collation' => 'utf8mb4_general_ci', |
||
43 | 'prefix' => '', |
||
44 | 'strict' => false |
||
45 | ]); |
||
46 | |||
47 | $this->mainTableName = $config['main_table'] ?? $this->mainTableName; |
||
48 | |||
49 | $this->capsule->setAsGlobal(); |
||
50 | } |
||
51 | |||
52 | |||
53 | /** |
||
54 | * @param String $text |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function loadText(String $text) |
||
58 | { |
||
59 | try { |
||
60 | $this->file_content = $text; |
||
61 | return true; |
||
62 | } catch (\Exception $e) { |
||
0 ignored issues
–
show
|
|||
63 | $this->error = $e->getMessage(); |
||
64 | |||
65 | return false; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param $file_name |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | public function loadFile($file_name) |
||
75 | { |
||
76 | try { |
||
77 | $this->file_content = file_get_contents($file_name); |
||
78 | |||
79 | return true; |
||
80 | } catch (\Exception $e) { |
||
81 | $this->error = $e->getMessage(); |
||
82 | |||
83 | return false; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | |||
88 | /** |
||
89 | * @param string $type |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function migrate($type = Client::ALL) |
||
93 | { |
||
94 | $JsonExtractor = new JsonExtractor(new Json($this->file_content, $this->mainTableName), $this->mainTableName); |
||
95 | $JsonExtractor->toMysqlTables(); |
||
96 | |||
97 | switch ($type) { |
||
98 | case Client::DATA_ONLY: |
||
99 | $JsonExtractor->toMysqlData(); |
||
100 | $this->insertData($JsonExtractor); |
||
101 | break; |
||
102 | case Client::TABLE_ONLY: |
||
103 | $this->createTables($JsonExtractor); |
||
104 | break; |
||
105 | default: |
||
106 | $JsonExtractor->toMysqlData(); |
||
107 | $this->createTables($JsonExtractor); |
||
108 | $this->insertData($JsonExtractor); |
||
109 | break; |
||
110 | } |
||
111 | |||
112 | return true; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param JsonExtractor $JsonExtractor |
||
117 | */ |
||
118 | private function createTables(JsonExtractor $JsonExtractor) |
||
119 | { |
||
120 | $this->capsule::schema()->disableForeignKeyConstraints(); |
||
121 | |||
122 | //create tables |
||
123 | foreach ($JsonExtractor->getTablesArray() as $TableName => $TableColumn) { |
||
124 | $this->capsule::schema()->dropIfExists($TableName); |
||
125 | $this->capsule::schema()->create($TableName, function ($table) use ($TableColumn) { |
||
126 | foreach ($TableColumn as $column_item) { |
||
127 | switch ($column_item['type']) { |
||
128 | case 'int': |
||
129 | $table->int($column_item['name']); |
||
130 | break; |
||
131 | case 'primary_key': |
||
132 | $table->increments($column_item['name']); |
||
133 | break; |
||
134 | case 'integer': |
||
135 | $table->decimal($column_item['name'], 65)->nullable(); |
||
136 | break; |
||
137 | case 'boolean': |
||
138 | $table->boolean($column_item['name'])->nullable(); |
||
139 | break; |
||
140 | case 'double': |
||
141 | $table->double($column_item['name'])->nullable(); |
||
142 | break; |
||
143 | case 'foreign_key': |
||
144 | $table->integer($column_item['name'])->unsigned(); |
||
145 | $table->foreign($column_item['name'])->references('id')->on($column_item['ref']); |
||
146 | break; |
||
147 | default: |
||
148 | $table->text($column_item['name'])->nullable(); |
||
149 | break; |
||
150 | |||
151 | } |
||
152 | } |
||
153 | }); |
||
154 | } |
||
155 | |||
156 | $this->capsule::schema()->enableForeignKeyConstraints(); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param JsonExtractor $JsonExtractor |
||
161 | * @return bool |
||
162 | */ |
||
163 | private function insertData(JsonExtractor $JsonExtractor) |
||
164 | { |
||
165 | $this->capsule::schema()->disableForeignKeyConstraints(); |
||
166 | foreach ($JsonExtractor->getDataArray() as $TableName => $TableData) { |
||
167 | $this->capsule::table($TableName)->insert($TableData); |
||
168 | } |
||
169 | $this->capsule::schema()->enableForeignKeyConstraints(); |
||
170 | |||
171 | return true; |
||
172 | } |
||
173 | |||
174 | public function clearDatabase() |
||
175 | { |
||
176 | } |
||
177 | } |
||
178 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.