Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
sqlite3_extractor | |
0.00% |
0 / 56 |
|
0.00% |
0 / 4 |
420 | |
0.00% |
0 / 1 |
write_start | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
write_table | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
42 | |||
write_data | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
110 | |||
write_end | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\db\extractor; |
15 | |
16 | use phpbb\db\extractor\exception\extractor_not_initialized_exception; |
17 | |
18 | class sqlite3_extractor extends base_extractor |
19 | { |
20 | /** |
21 | * {@inheritdoc} |
22 | */ |
23 | public function write_start($table_prefix) |
24 | { |
25 | if (!$this->is_initialized) |
26 | { |
27 | throw new extractor_not_initialized_exception(); |
28 | } |
29 | |
30 | $sql_data = "--\n"; |
31 | $sql_data .= "-- phpBB Backup Script\n"; |
32 | $sql_data .= "-- Dump of tables for $table_prefix\n"; |
33 | $sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n"; |
34 | $sql_data .= "--\n"; |
35 | $sql_data .= "BEGIN TRANSACTION;\n"; |
36 | $this->flush($sql_data); |
37 | } |
38 | |
39 | /** |
40 | * {@inheritdoc} |
41 | */ |
42 | public function write_table($table_name) |
43 | { |
44 | if (!$this->is_initialized) |
45 | { |
46 | throw new extractor_not_initialized_exception(); |
47 | } |
48 | |
49 | $sql_data = '-- Table: ' . $table_name . "\n"; |
50 | $sql_data .= "DROP TABLE $table_name;\n"; |
51 | |
52 | $sql = "SELECT sql |
53 | FROM sqlite_master |
54 | WHERE type = 'table' |
55 | AND name = '" . $this->db->sql_escape($table_name) . "' |
56 | ORDER BY name ASC;"; |
57 | $result = $this->db->sql_query($sql); |
58 | $row = $this->db->sql_fetchrow($result); |
59 | $this->db->sql_freeresult($result); |
60 | |
61 | // Create Table |
62 | $sql_data .= $row['sql'] . ";\n"; |
63 | |
64 | $result = $this->db->sql_query("PRAGMA index_list('" . $this->db->sql_escape($table_name) . "');"); |
65 | |
66 | while ($row = $this->db->sql_fetchrow($result)) |
67 | { |
68 | if (strpos($row['name'], 'autoindex') !== false) |
69 | { |
70 | continue; |
71 | } |
72 | |
73 | $result2 = $this->db->sql_query("PRAGMA index_info('" . $this->db->sql_escape($row['name']) . "');"); |
74 | |
75 | $fields = array(); |
76 | while ($row2 = $this->db->sql_fetchrow($result2)) |
77 | { |
78 | $fields[] = $row2['name']; |
79 | } |
80 | $this->db->sql_freeresult($result2); |
81 | |
82 | $sql_data .= 'CREATE ' . ($row['unique'] ? 'UNIQUE ' : '') . 'INDEX ' . $row['name'] . ' ON ' . $table_name . ' (' . implode(', ', $fields) . ");\n"; |
83 | } |
84 | $this->db->sql_freeresult($result); |
85 | |
86 | $this->flush($sql_data . "\n"); |
87 | } |
88 | |
89 | /** |
90 | * {@inheritdoc} |
91 | */ |
92 | public function write_data($table_name) |
93 | { |
94 | if (!$this->is_initialized) |
95 | { |
96 | throw new extractor_not_initialized_exception(); |
97 | } |
98 | |
99 | $result = $this->db->sql_query("PRAGMA table_info('" . $this->db->sql_escape($table_name) . "');"); |
100 | |
101 | $col_types = array(); |
102 | while ($row = $this->db->sql_fetchrow($result)) |
103 | { |
104 | $col_types[$row['name']] = $row['type']; |
105 | } |
106 | $this->db->sql_freeresult($result); |
107 | |
108 | $sql_insert = 'INSERT INTO ' . $table_name . ' (' . implode(', ', array_keys($col_types)) . ') VALUES ('; |
109 | |
110 | $sql = "SELECT * |
111 | FROM $table_name"; |
112 | $result = $this->db->sql_query($sql); |
113 | |
114 | while ($row = $this->db->sql_fetchrow($result)) |
115 | { |
116 | foreach ($row as $column_name => $column_data) |
117 | { |
118 | if (is_null($column_data)) |
119 | { |
120 | $row[$column_name] = 'NULL'; |
121 | } |
122 | else if ($column_data === '') |
123 | { |
124 | $row[$column_name] = "''"; |
125 | } |
126 | else if (stripos($col_types[$column_name], 'text') !== false || stripos($col_types[$column_name], 'char') !== false || stripos($col_types[$column_name], 'blob') !== false) |
127 | { |
128 | $row[$column_name] = sanitize_data_generic(str_replace("'", "''", $column_data)); |
129 | } |
130 | } |
131 | $this->flush($sql_insert . implode(', ', $row) . ");\n"); |
132 | } |
133 | } |
134 | |
135 | /** |
136 | * Writes closing line(s) to database backup |
137 | * |
138 | * @return void |
139 | * @throws extractor_not_initialized_exception when calling this function before init_extractor() |
140 | */ |
141 | public function write_end() |
142 | { |
143 | if (!$this->is_initialized) |
144 | { |
145 | throw new extractor_not_initialized_exception(); |
146 | } |
147 | |
148 | $this->flush("COMMIT;\n"); |
149 | parent::write_end(); |
150 | } |
151 | } |