TableExportTrait   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 82
c 0
b 0
f 0
dl 0
loc 166
rs 10
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getSelectFormatValues() 0 3 1
A getSelectTableValues() 0 3 1
A getDbTables() 0 16 2
B getBaseOptions() 0 69 6
A getDataRowOptions() 0 18 2
A getSelectDatabaseValues() 0 3 1
A getSelectDataValues() 0 5 2
A getSelectOutputValues() 0 3 1
A getDatabases() 0 16 3
1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Facades\Traits;
4
5
use function compact;
6
use function preg_replace;
7
8
trait TableExportTrait
9
{
10
    public function getSelectOutputValues(): array
11
    {
12
        return $this->admin->dumpOutput();
13
    }
14
15
    public function getSelectFormatValues(): array
16
    {
17
        return $this->admin->dumpFormat();
18
    }
19
20
    public function getSelectDatabaseValues(): array
21
    {
22
        return ['', 'USE', 'DROP+CREATE', 'CREATE'];
23
    }
24
25
    public function getSelectTableValues(): array
26
    {
27
        return ['', 'DROP+CREATE', 'CREATE'];
28
    }
29
30
    public function getSelectDataValues(): array
31
    {
32
        //! use insertOrUpdate() in all drivers
33
        return $this->driver->jush() !== 'sql' ? ['', 'TRUNCATE+INSERT', 'INSERT'] :
34
            ['', 'TRUNCATE+INSERT', 'INSERT', 'INSERT+UPDATE'];
35
    }
36
37
    private function getDataRowOptions(string $database, string $table): array
38
    {
39
        // \parse_str($_COOKIE['adminer_export'], $options);
40
        // if(!$options) {
41
        $options = [
42
            'output' => 'open',
43
            'format' => 'sql',
44
            'db_style' => ($database !== '' ? '' : 'CREATE'),
45
            'table_style' => 'DROP+CREATE',
46
            'data_style' => 'INSERT',
47
            'types' => true,
48
        ];
49
        // }
50
        // if(!isset($options['events'])) { // backwards compatibility
51
        $options['routines'] = $options['events'] = ($table === '');
52
        $options['triggers'] = true; // $options['table_style']; // Is a boolean
53
        // }
54
        return $options;
55
    }
56
57
    /**
58
     * @param string $database
59
     * @param string $table
60
     *
61
     * @return array
62
     */
63
    private function getBaseOptions(string $database, string $table): array
64
    {
65
        // From dump.inc.php
66
        $row = $this->getDataRowOptions($database, $table);
67
        $options = [
68
            'output' => [
69
                'label' => $this->utils->trans->lang('Output'),
70
                'options' => $this->getSelectOutputValues(),
71
                'value' => $row['output'],
72
            ],
73
            'format' => [
74
                'label' => $this->utils->trans->lang('Format'),
75
                'options' => $this->getSelectFormatValues(),
76
                'value' => $row['format'],
77
            ],
78
            'table_style' => [
79
                'label' => $this->utils->trans->lang('Table'),
80
                'options' => $this->getSelectTableValues(),
81
                'value' => $row['table_style'],
82
            ],
83
            'auto_increment' => [
84
                'label' => $this->utils->trans->lang('Auto Increment'),
85
                'value' => 1,
86
                'checked' => $row['autoIncrement'] ?? true,
87
            ],
88
            'data_style' => [
89
                'label' => $this->utils->trans->lang('Data'),
90
                'options' => $this->getSelectDataValues(),
91
                'value' => $row['data_style'],
92
            ],
93
        ];
94
        if ($this->driver->support('trigger')) {
95
            $options['triggers'] = [
96
                'label' => $this->utils->trans->lang('Triggers'),
97
                'value' => 1,
98
                'checked' => $row['triggers'],
99
            ];
100
        }
101
        if ($this->driver->jush() === 'sqlite') {
102
            return $options;
103
        }
104
105
        $options['db_style'] = [
106
            'label' => $this->utils->trans->lang('Database'),
107
            'options' => $this->getSelectDatabaseValues(),
108
            'value' => $row['db_style'],
109
        ];
110
        if ($this->driver->support('type')) {
111
            $options['types'] = [
112
                'label' => $this->utils->trans->lang('Types'),
113
                'value' => 1,
114
                'checked' => $row['types'],
115
            ];
116
        }
117
        if ($this->driver->support('routine')) {
118
            $options['routines'] = [
119
                'label' => $this->utils->trans->lang('Routines'),
120
                'value' => 1,
121
                'checked' => $row['routines'],
122
            ];
123
        }
124
        if ($this->driver->support('event')) {
125
            $options['events'] = [
126
                'label' => $this->utils->trans->lang('Events'),
127
                'value' => 1,
128
                'checked' => $row['events'],
129
            ];
130
        }
131
        return $options;
132
    }
133
134
    /**
135
     * @return array
136
     */
137
    private function getDbTables(): array
138
    {
139
        $tables = [
140
            'headers' => [$this->utils->trans->lang('Tables'), $this->utils->trans->lang('Data')],
141
            'details' => [],
142
        ];
143
        $tables_list = $this->driver->tables();
144
        foreach ($tables_list as $name => $type) {
145
            $prefix = preg_replace('~_.*~', '', $name);
146
            //! % may be part of table name
147
            // $checked = ($TABLE == '' || $TABLE == (\substr($TABLE, -1) == '%' ? "$prefix%" : $name));
148
            // $results['prefixes'][$prefix]++;
149
150
            $tables['details'][] = compact('prefix', 'name', 'type'/*, 'checked'*/);
151
        }
152
        return $tables;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    private function getDatabases(): array
159
    {
160
        $databases = [
161
            'headers' => [$this->utils->trans->lang('Database'), $this->utils->trans->lang('Data')],
162
            'details' => [],
163
        ];
164
        $databases_list = $this->driver->databases(false) ?? [];
165
        foreach ($databases_list as $name) {
166
            if (!$this->driver->isInformationSchema($name)) {
167
                $prefix = preg_replace('~_.*~', '', $name);
168
                // $results['prefixes'][$prefix]++;
169
170
                $databases['details'][] = compact('prefix', 'name');
171
            }
172
        }
173
        return $databases;
174
    }
175
}
176