diff --git a/tests/speed/stats/index.php b/tests/speed/stats/index.php index 6c3335e3..9f912a17 100644 --- a/tests/speed/stats/index.php +++ b/tests/speed/stats/index.php @@ -1,33 +1,24 @@ 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"; }