moved to pdo db interface to avoid uninstalled deps in prod

This commit is contained in:
John Bender 2011-11-07 13:19:32 -08:00
parent 3d34943068
commit 2ec65580cf

View file

@ -1,33 +1,24 @@
<?php
$db = new SQLiteDatabase('stats.db');
// hand to God I tried CREATE TABLE IF NOT EXISTS and it persisted with
// a syntax error. The IDENTICAL query sans IF NOT EXISTS works perfectly
// http://www.sqlite.org/lang_createtable.html
@$db->query('CREATE TABLE stats (id INTEGER, agent TEXT, point TEXT, value REAL, time TIMESTAMP, PRIMARY KEY (id))');
$db = new PDO('sqlite:./stats.db');
$db->query('CREATE TABLE IF NOT EXISTS stats (id INTEGER, agent TEXT, point TEXT, value REAL, time TIMESTAMP, PRIMARY KEY (id))');
// making a sad attempt here to provide a clean REST-respecting url scheme.
// stats with a GET returns - wait for it - the stats, and a post with the
// the right params will create a new entry
if ( $_SERVER['REQUEST_METHOD'] == "GET" ) {
$json = Array();
$results = $db->query( 'SELECT agent, point, value, time FROM stats' );
// TODO not sure if there's a better way to convert db results into
// a json encodable data structure
while($row = $results->fetch(SQLITE3_ASSOC)){
array_push($json, $row);
}
$st = $db->prepare( 'SELECT agent, point, value, time FROM stats' );
$st->execute();
$result = $st->fetchAll(PDO::FETCH_ASSOC);
header("Content-Type: application/json");
echo json_encode($json);
} elseif ( $_POST['datapoint'] && $_POST['value'] && $_POST['agent'] ){
// TODO it is not clear from the docs if there's an easier way to do the
// escaped query interpolation here. Suggestions welcome :(
$data_point = sqlite_escape_string( $_POST['datapoint'] );
$value = sqlite_escape_string( $_POST['value'] );
$agent = sqlite_escape_string( $_POST['agent'] );
$db->query('INSERT INTO stats (agent, point, value, time) VALUES ("' . $agent . '", "' . $data_point . '",' . $value . ', DATETIME(\'now\'))');
echo json_encode($result);
} elseif ( $_POST['datapoint'] && $_POST['value'] && $_POST['agent'] ) {
$st = $db->prepare('INSERT INTO stats (agent, point, value, time) VALUES (:agent, :data_point, :value, DATETIME(\'now\'))');
$st->execute(array(
':agent' => $_POST['agent'],
':data_point' => $_POST['datapoint'],
':value' => $_POST['value']
));
echo "success";
}