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