TableExportTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDbTables() 0 16 2
B getBaseOptions() 0 67 6
A getDataRowOptions() 0 17 2
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
    private function getDataRowOptions(string $database, string $table): array
11
    {
12
        // \parse_str($_COOKIE['adminer_export'], $row);
13
        // if(!$row) {
14
        $row = [
15
            'output' => 'text',
16
            'format' => 'sql',
17
            'db_style' => ($database != '' ? '' : 'CREATE'),
18
            'table_style' => 'DROP+CREATE',
19
            'data_style' => 'INSERT',
20
        ];
21
        // }
22
        // if(!isset($row['events'])) { // backwards compatibility
23
        $row['routines'] = $row['events'] = ($table == '');
24
        $row['triggers'] = $row['table_style'];
25
        // }
26
        return $row;
27
    }
28
29
    /**
30
     * @param string $database
31
     * @param string $table
32
     *
33
     * @return array
34
     */
35
    private function getBaseOptions(string $database, string $table): array
36
    {
37
        // From dump.inc.php
38
        $db_style = ['', 'USE', 'DROP+CREATE', 'CREATE'];
39
        $table_style = ['', 'DROP+CREATE', 'CREATE'];
40
        $data_style = ['', 'TRUNCATE+INSERT', 'INSERT'];
41
        if ($this->driver->jush() == 'sql') { //! use insertOrUpdate() in all drivers
42
            $data_style[] = 'INSERT+UPDATE';
43
        }
44
45
        $row = $this->getDataRowOptions($database, $table);
46
        $options = [
47
            'output' => [
48
                'label' => $this->utils->trans->lang('Output'),
49
                'options' => $this->admin->dumpOutput(),
50
                'value' => $row['output'],
51
            ],
52
            'format' => [
53
                'label' => $this->utils->trans->lang('Format'),
54
                'options' => $this->admin->dumpFormat(),
55
                'value' => $row['format'],
56
            ],
57
            'table_style' => [
58
                'label' => $this->utils->trans->lang('Tables'),
59
                'options' => $table_style,
60
                'value' => $row['table_style'],
61
            ],
62
            'auto_increment' => [
63
                'label' => $this->utils->trans->lang('Auto Increment'),
64
                'value' => 1,
65
                'checked' => $row['autoIncrement'] ?? false,
66
            ],
67
            'data_style' => [
68
                'label' => $this->utils->trans->lang('Data'),
69
                'options' => $data_style,
70
                'value' => $row['data_style'],
71
            ],
72
        ];
73
        if ($this->driver->jush() !== 'sqlite') {
74
            $options['db_style'] = [
75
                'label' => $this->utils->trans->lang('Database'),
76
                'options' => $db_style,
77
                'value' => $row['db_style'],
78
            ];
79
            if ($this->driver->support('routine')) {
80
                $options['routines'] = [
81
                    'label' => $this->utils->trans->lang('Routines'),
82
                    'value' => 1,
83
                    'checked' => $row['routines'],
84
                ];
85
            }
86
            if ($this->driver->support('event')) {
87
                $options['events'] = [
88
                    'label' => $this->utils->trans->lang('Events'),
89
                    'value' => 1,
90
                    'checked' => $row['events'],
91
                ];
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
        return $options;
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    private function getDbTables(): array
108
    {
109
        $tables = [
110
            'headers' => [$this->utils->trans->lang('Tables'), $this->utils->trans->lang('Data')],
111
            'details' => [],
112
        ];
113
        $tables_list = $this->driver->tables();
114
        foreach ($tables_list as $name => $type) {
115
            $prefix = preg_replace('~_.*~', '', $name);
116
            //! % may be part of table name
117
            // $checked = ($TABLE == '' || $TABLE == (\substr($TABLE, -1) == '%' ? "$prefix%" : $name));
118
            // $results['prefixes'][$prefix]++;
119
120
            $tables['details'][] = compact('prefix', 'name', 'type'/*, 'checked'*/);
121
        }
122
        return $tables;
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    private function getDatabases(): array
129
    {
130
        $databases = [
131
            'headers' => [$this->utils->trans->lang('Database'), $this->utils->trans->lang('Data')],
132
            'details' => [],
133
        ];
134
        $databases_list = $this->driver->databases(false) ?? [];
135
        foreach ($databases_list as $name) {
136
            if (!$this->driver->isInformationSchema($name)) {
137
                $prefix = preg_replace('~_.*~', '', $name);
138
                // $results['prefixes'][$prefix]++;
139
140
                $databases['details'][] = compact('prefix', 'name');
141
            }
142
        }
143
        return $databases;
144
    }
145
}
146