-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheugene.php
More file actions
190 lines (133 loc) · 4.61 KB
/
Copy patheugene.php
File metadata and controls
190 lines (133 loc) · 4.61 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
<?php
// eugene
// pulls in delimited files and creates a table in an mysql db
date_default_timezone_set("America/New_York");
class Eugene {
private static $dboptions = array(
"info" => DBINFO,
"user" => DBUSER, // make sure the dbuser has CREATE, INSERT, and SELECT privileges
"pass" => DBPASS
);
static $defaultOptions = array(
"delimiter" => "^",
"primary_key" => "id",
"schema" => array(
"id" => "int"
),
"table_extra" => array(
"on_shelf" => "tinyint(1)",
"checked_by" => "varchar(150)",
"date_checked" => "varchar(50)"
)
);
public $tableName;
public $options;
public $headings;
public $contents;
public $errors;
/**
* __construct:
* takes in a file path and an array of options, splits out the row titles,
* and sets rows for all
*/
function __construct($file, $options = array()) {
$this->tableName = Eugene::buildTableName();
$this->options = array_merge(Eugene::$defaultOptions, $options);
$this->contents = explode("\n", file_get_contents($file));
// we'll get an array of headings by shifting out the first row of our contents
// and exploding that, then we'll clean them up a bit
$this->headings = explode($this->options['delimiter'], array_shift($this->contents));
foreach($this->headings as &$option) {
$option = str_replace("\r", "", strtolower(str_replace(" ", "_", $option)));
if ($option == "call_#(item)") { $option = "call_number"; }
}
}
/**
* getErrors:
* just returns the class $errors var
*/
function getErrors() {
return $this->errors;
}
/**
* setTable:
* let's commence-a-jigglin'
*/
function setTable() {
$opts = Eugene::$dboptions;
// first we'll build the table:
$tableQuery = $this->createTableQuery();
try {
print_r(Eugene::query($tableQuery));
} catch(PDOEXCEPTION $e) {
echo $e->getMessage();
}
$pdo = new PDO($opts['info'], $opts['user'], $opts['pass']);
foreach($this->contents as $item) {
if (!$item) { continue; }
$rows = explode($this->options['delimiter'], $item);
$query = $this->buildStatement($rows);
$stmt = $pdo->prepare($query);
$stmt->execute($rows);
$errors = $stmt->errorInfo();
if($errors[0] !== "00000") {
$this->errors[] = array(
"item" => $rows,
"itemRaw" => $item,
"timestamp" => time(),
"error" => $stmt->errorInfo()
);
}
$stmt->closeCursor();
}
}
/**
* buildStatement:
* returns a prepared-statement to use w/ the querying tool
*/
function buildStatement($input) {
$rowString = implode(",", $this->headings);
$query = "INSERT INTO `" . $this->tableName . "` (" . $rowString . ") VALUES ";
$query .= "(" . implode(",", array_fill(0, count($input), "?")) . ")";
return $query;
}
/**
* createTable:
* creates a table query syntax
*/
function createTableQuery() {
$query = "CREATE TABLE IF NOT EXISTS `" . $this->tableName . "`(";
foreach($this->headings as $heading) {
$null = $heading == $this->options['primary_key'] ? "NOT NULL" : "NULL";
$query .= "`" . $heading . "`" . " VARCHAR(250) " . $null . ", ";
}
foreach(Eugene::$defaultOptions['table_extra'] as $key => $value) {
$query .= "`" . $key . "` " . strtoupper($value) . " " . $null . ", ";
}
$query .= "PRIMARY KEY(`" . $this->options['primary_key'] . "`)";
$query .= ")";
return $query;
}
/**
* buildTableName:
* returns a date string that we'll use as our table name
*/
static function buildTableName() {
return date('Y-m-d\TH:i:s');
}
/**
* query:
* standard run-of-the-mill pdo-driven db queryin'
*/
static function query($query, $items = array()) {
$opts = Eugene::$dboptions;
$pdo = new PDO($opts['info'], $opts['user'], $opts['pass']);
$stmt = $pdo->prepare($query);
$stmt->execute($items);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
print_r($stmt->errorInfo);
return $results;
}
}
?>