-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv2mysql.php
More file actions
300 lines (247 loc) · 8.56 KB
/
csv2mysql.php
File metadata and controls
300 lines (247 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* @file csv2mysql.php
*
* @package library/misc
*
* @abstract A quick and dirty means of creating a mysql table from a csv file.
* A header row is required for the csv, and can be optionally used to create the
* table column names.
*
* @example: invoke from a shell prompt, eg: $php csv2mysql.php
*
* Configuration:
*
* All variables can be set in script explicity or better, by extending.
*
* NOTE: The csv is assumed to come from a trusted source.
*
* @author programming@dbswebsite.com 2010-10-27
*
*
* LICENSE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
// set some high values for command line
ini_set( 'memory_limit','512M' );
set_time_limit( 0 );
error_reporting ( E_ALL );
/**
* @class csv2mysql
*/
class csv2mysql
{
protected $csv = null; // csv file name, *required*
protected $db = null; // database name, will be named from the csv if is_null.
protected $table_name = null; // table name, will be named from the csv if is_null.
protected $db_host = 'localhost';
protected $db_user = 'root';
protected $db_password = '';
public $autoexec = false; // run from constructor
protected $debug = true;
protected $create = true; // dynamically create table structure from csv header or not. TODO: not tested if this is FALSE :/
protected $insert_ignore = false; // INSERT IGNORE syntax for possible duplicate keys
protected $truncate = true; // whether to truncate the table or not (only meaningul if using a predefined table structure)
protected $first_column_key = true; // Inserts a unique index as the first column, if true.
protected $columns = array(); // Column names, if empty the csv 1st row header titles will be used.
protected $custom_sql = null; // custom sql to run after the table has been built, can be a reference to a file, or inline sql statement(s)
protected $varchar_size = 255;
protected $limit = false; // integer > 0, limit number of records (for testing purposes typically)
/**
* @return void
*
* Constructor, parses configs and sets up table building.
*/
function __construct()
{
if ( $this->autoexec ) {
$this->exec();
}
}
/**
* @return void
*
* Run all the sub-processes.
*
*/
public function exec()
{
if ( $this->debug ) echo "Running exec commands\n";
if ( ! is_file( $this->csv ) ) {
$this->help( "Cannot find your csv file, aborting\n" );
}
// make our db name and table name from the csv, if none are supplied
if ( ! $this->table_name ) {
$this->table_name = $this->sanitize( str_replace( '.csv', '', basename( $this->csv ) ) );
if ( $this->debug ) echo "Creating table $this->table_name\n";
}
if ( ! $this->db ) {
$this->db = $this->table_name;
if ( $this->debug ) echo "Creating database $this->db\n";
}
// connect to db
mysql_connect( $this->db_host, $this->db_user, $this->db_password ) || die( 'Unable to connect to db host' );
// create the database, if we need to
mysql_query("CREATE DATABASE IF NOT EXISTS $this->db");
// open db
mysql_select_db( $this->db ) || die( 'Cannot connect to database, bummer' );
// do it!!! //////
$this->add_data();
//////////////////
// run code for AFTER the data is imported
$this->post_process();
}
/**
* @return void
*
* Quick and dirty -- populate a database table from a csv file.
*/
protected function add_data()
{
if ( $this->debug ) echo "adding data now ...\n";
$row = 0;
$handle = fopen( $this->csv, "r" );
if ( ! $handle ) {
$this->help( "Error opening your csv file, aborting\n" );
}
// if its not there, create it from the csv header row, etc
$this->create_table();
// remove header row
fgetcsv( $handle, 20000, "," );
$ignore = ( $this->insert_ignore ) ? 'IGNORE' : '';
// Now Loop through csv data rows, putting together the pieces.
while (( $data = fgetcsv($handle, 4096, "," )) !== FALSE ) {
// prep incoming csv data
$data = array_map( 'mysql_real_escape_string', $data );
// make it a MySQL friendly statement
$cdata = '("' . implode( '","', $data ) . '")';
// construct query from pieces
$sql = "INSERT $ignore INTO `$this->table_name` VALUES " . $cdata;
// make it stick
mysql_query( $sql ) || die("Error adding data table at $row: " . mysql_error() );
$row++;
if ( $this->limit && $row > $this->limit ) {
if ( $this->debug ) echo "Breaking at: # $row row\n";
break;
}
if ( $this->debug ) echo "Adding row: # $row\n";
}
/* TODO: This may conflict with other options / configurations ??? */
if ( $this->first_column_key ) {
// add a unique key as first column
$first_col_id = $this->table_name . '_id';
$sql = "ALTER TABLE `$this->table_name` ADD `$first_col_id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST";
mysql_query( $sql ) || die( 'Error creating index' );
}
if ( $this->debug ) echo "Done.\n";
}
/**
* @return void
*
* Now we create columns as varchars from the header row, or config. And, at
* least give it one indexed column (the first column), if configured so.
*/
protected function create_table()
{
// if we aren't set up to (re)create this table, then bug out now.
if ( ! $this->create ) {
if ( $this->truncate ) {
mysql_query( "TRUNCATE `$this->table_name`" );
}
// if we aren't generating the table, then we should not inject the primary key column
$this->first_column_key = false;
return;
}
// (re)create table.
mysql_query( "DROP TABLE IF EXISTS `$this->table_name`" ) || die('Error dropping table' . $this->table_name);
$handle = fopen( "$this->csv", "r" );
// Extract first (header) record only, use for mysql column names unless our config options has a predefined array of column names.
$_columns = fgetcsv( $handle, 2048, "," );
if ( empty( $this->columns ) ) {
// Extract first (header) record only to use for mysql column
// names, if config options does not provide column names.
$this->columns = $_columns;
}
$sql= "CREATE TABLE IF NOT EXISTS `$this->table_name` (";
for( $i=0;$i<count( $this->columns ); $i++ ) {
// clean up the header row
$this->columns[$i] = $this->sanitize( $this->columns[$i] );
if ( empty ( $this->columns[$i] ) ) $this->columns[$i] = 'NONE_SUPPLIED';
$sql .= $this->columns[$i].' VARCHAR('. $this->varchar_size .'), ';
}
//The line below gets rid of the comma
$sql = substr($sql,0,strlen($sql)-2);
$sql .= ')';
fclose( $handle );
mysql_query( $sql ) || die( 'Error creating table' );
if ( $this->debug ) echo "Table is created and ready\n";
}
/**
* @return void
*
* Run any code we need to run AFTER the data has been imported.
*
* @author Hal Burgiss 2012-11-25
*/
protected function post_process()
{
if ( ! empty( $this->custom_sql ) ) {
if ( $this->debug ) echo "Running post processing code\n";
if ( is_file( $this->custom_sql ) ) {
// not tested :(
$out = shell_exec( "mysql -h$this->db_host -u$this->db_user -p$this->db_password $this->db < $this->custom_sql" );
if ( $out ) {
die( $out );
}
} else {
mysql_query( $this->custom_sql ) || die( "Error running custom_sql: " . mysql_error() );;
}
}
}
/**
* @return string $name, with certain characters removed
*
* @param string $name
*
* Remove characters that are illegal or don't make for good mysql names.
*
*/
protected function sanitize( $name )
{
$name = str_replace( ' ', '_', $name );
return str_replace( array( "'", "/",'\\',".",'"','?','$','-','*','`','+', ','), '', $name );
}
/**
* HELP!. Spit this message out on command line errors.
*/
protected function help( $error = '' )
{
echo "
$error
HELP
This script will:
- create an empty table from a csv header row
- create / recreate table from csv
- populate the table with the csv data rows
This script is intended to be run from the command line. All variables
can be set by extending the class.
The newly created table will have a primary key index created (default configuration).
All other columns will be created as VARCHARs.
";
die();
}
}