|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elimuswift\DbExporter; |
|
4
|
|
|
|
|
5
|
|
|
use DB; |
|
6
|
|
|
|
|
7
|
|
|
abstract class DbExporter |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Contains the ignore tables. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
public static $ignore = ['migrations']; |
|
15
|
|
|
public static $remote; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get all the tables. |
|
19
|
|
|
* |
|
20
|
|
|
* @return mixed |
|
21
|
|
|
*/ |
|
22
|
|
|
public $database; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Select fields. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
**/ |
|
29
|
|
|
protected $selects = [ |
|
30
|
|
|
'column_name as Field', |
|
31
|
|
|
'column_type as Type', |
|
32
|
|
|
'is_nullable as Nullable', |
|
33
|
|
|
'column_key as Key', |
|
34
|
|
|
'column_default as Default', |
|
35
|
|
|
'extra as Extra', |
|
36
|
|
|
'data_type as Data_Type', |
|
37
|
|
|
'CHARACTER_MAXIMUM_LENGTH as Length', |
|
38
|
|
|
'NUMERIC_PRECISION as Precision', |
|
39
|
|
|
'NUMERIC_SCALE as Scale', |
|
40
|
|
|
]; |
|
41
|
|
|
/** |
|
42
|
|
|
* Select fields from constraints. |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
**/ |
|
46
|
|
|
protected $constraints = [ |
|
47
|
|
|
'key_column_usage.table_name as Table', |
|
48
|
|
|
'key_column_usage.column_name as Field', |
|
49
|
|
|
'key_column_usage.referenced_table_name as ON', |
|
50
|
|
|
'key_column_usage.referenced_column_name as References', |
|
51
|
|
|
'REFERENTIAL_CONSTRAINTS.UPDATE_RULE as onUpdate', |
|
52
|
|
|
'REFERENTIAL_CONSTRAINTS.DELETE_RULE as onDelete', |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
protected function getTables() |
|
56
|
|
|
{ |
|
57
|
|
|
$pdo = DB::connection()->getPdo(); |
|
58
|
|
|
|
|
59
|
|
|
return $pdo->query('SELECT table_name FROM information_schema.tables WHERE table_schema="'.$this->database.'"'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getTableIndexes($table) |
|
63
|
|
|
{ |
|
64
|
|
|
$pdo = DB::connection()->getPdo(); |
|
65
|
|
|
|
|
66
|
|
|
return $pdo->query('SHOW INDEX FROM '.$this->database.'.'.$table.' WHERE Key_name != "PRIMARY"'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get all the columns for a given table. |
|
71
|
|
|
* |
|
72
|
|
|
* @param $table |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getTableDescribes($table) |
|
77
|
|
|
{ |
|
78
|
|
|
return DB::table('information_schema.columns') |
|
79
|
|
|
->where('table_schema', '=', $this->database) |
|
80
|
|
|
->where('table_name', '=', $table) |
|
81
|
|
|
->get($this->selects); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get all the foreign key constraints for a given table. |
|
86
|
|
|
* |
|
87
|
|
|
* @param $table |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getTableConstraints($table) |
|
92
|
|
|
{ |
|
93
|
|
|
return DB::table('information_schema.key_column_usage') |
|
94
|
|
|
->distinct() |
|
95
|
|
|
->join('information_schema.REFERENTIAL_CONSTRAINTS', 'REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME', '=', 'key_column_usage.CONSTRAINT_NAME') |
|
96
|
|
|
->where('key_column_usage.table_schema', '=', $this->database) |
|
97
|
|
|
->where('key_column_usage.table_name', '=', $table) |
|
98
|
|
|
->get($this->constraints); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Grab all the table data. |
|
103
|
|
|
* |
|
104
|
|
|
* @param $table |
|
105
|
|
|
* |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function getTableData($table) |
|
109
|
|
|
{ |
|
110
|
|
|
return DB::table($this->database.'.'.$table)->get(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Try to create directories if they dont exist. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $path |
|
117
|
|
|
**/ |
|
118
|
|
|
protected function makePath($path) |
|
119
|
|
|
{ |
|
120
|
|
|
$del = DIRECTORY_SEPARATOR; |
|
121
|
|
|
$dir = ''; |
|
122
|
|
|
$directories = explode($del, $path); |
|
123
|
|
|
foreach ($directories as $directory) { |
|
124
|
|
|
if (!empty($directory)) { |
|
125
|
|
|
$dir .= $del.$directory; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (!is_dir($dir)) { |
|
129
|
|
|
@mkdir($dir, 0775, true); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Write the file. |
|
136
|
|
|
* |
|
137
|
|
|
* @return mixed |
|
138
|
|
|
*/ |
|
139
|
|
|
abstract public function write(); |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Convert the database to a usefull format. |
|
143
|
|
|
* |
|
144
|
|
|
* @param null $database |
|
145
|
|
|
* |
|
146
|
|
|
* @return mixed |
|
147
|
|
|
*/ |
|
148
|
|
|
abstract public function convert($database = null); |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Put the converted stub into a template. |
|
152
|
|
|
* |
|
153
|
|
|
* @return mixed |
|
154
|
|
|
*/ |
|
155
|
|
|
abstract protected function compile($table); |
|
156
|
|
|
} |
|
157
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: