This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Csv_Import.php
144 lines (128 loc) · 3.65 KB
/
Csv_Import.php
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
<?php
/**
* Provides a quick way of importing a csv file into a MySQL database
* @link{http://php.net/manual/en/book.pdo.php} Uses the PDO library
*
* @Author Russell Stringer <r.stringer@gmail.com>
* @license http://dbad-license.org/license DBAD license
*/
class CSV_Import{
/**
* The database connection
* @var PDO
*/
private $_dbConn;
/**
* Name of the table to import into
* @var String
*/
private $_table;
/**
* Holds the filehandle for the csv
* @var resource
*/
private $_csvHandle;
/**
* Parenthesized list of column names
* @var string
*/
private $_colMapping;
/**
* Number of columns being imported
* @var int
*/
private $_colCount;
/**
* Set up the file handle and default column mapping
*
* @param string $filePath
* @param boolean $firstLineHeadings true if the first line of the csv should be used as the column mapping
* @throws Exception if the file is missing or unreadable
*/
public function __construct($filePath, $firstLineHeadings = true)
{
if (!file_exists($filePath) || !is_readable($filePath)){
throw new Exception("File missing or unreadable");
}
$this->_csvHandle = fopen($filePath, 'r');
if ($firstLineHeadings){
$headings = $this->_getRow();
$this->setMapping($headings);
}
}
/**
* Runs the import of the csv file into the specified table
*
* @param string $tableName
* @throws Exception if the database connection or file handle are missing
*/
public function importTo($tableName)
{
if (empty($this->_dbConn)){
throw new Exception("No database connection");
}
if (empty($this->_csvHandle)){
throw new Exception("No file handle");
}
$this->_table = $tableName;
$this->_dbConn->query("TRUNCATE TABLE $tableName");
$this->_doImport();
}
/**
* Sets up the dataabase connection
*
* @param string $conn PDO connection string
* @param string $user username to connect with
* @param string $pass database password
*/
public function setupDatabase($conn, $user, $pass)
{
$this->_dbConn = new PDO($conn, $user, $pass);
}
/**
* Set up the mapping between the fields in the file and the columns
* in the database. $mapArray should be an indexed array of the column
* names, in the order that the data will appear in the import file.
*
* @param array $mapArray
*/
public function setMapping($mapArray)
{
$this->_colCount = sizeof($mapArray);
$this->_colMapping = $this->_parenthesize($mapArray);
}
/**
* Imports each line of the file to the database
*/
private function _doImport()
{
$insert = "INSERT INTO {$this->_table} {$this->_colMapping} VALUES (";
for ($i = 0; $i < $this->_colCount; $i++){
$insert .= '?, ';
}
$insert = substr($insert, 0, -2);
$insert .= ')';
$stmt = $this->_dbConn->prepare($insert);
while (($row = $this->_getRow()) !== FALSE){
$stmt->execute($row);
}
}
/**
* Returns a parenthesized list of the elements of $array
* @param array $array
* @return string
*/
private function _parenthesize($array)
{
return '(' . implode(',', $array) . ')';
}
/**
* Returns the next row of the import file as an array
*
* @return array
*/
private function _getRow()
{
return fgetcsv($this->_csvHandle);
}
}