http://code.google.com/p/emt/source/detail?r=216
Added:
/trunk/plugins/output/sqlite3.php
=======================================
--- /dev/null
+++ /trunk/plugins/output/sqlite3.php Fri Feb 11 09:49:55 2011
@@ -0,0 +1,171 @@
+<?php
+/***************************************************************************
+* Copyright (C) 2007 by Eric Bergen *
+* er...@provenscaling.com *
+* *
+* This program is free software; you can redistribute it and/or modify *
+* it under the terms of the GNU Lesser General Public License as
published by *
+* the Free Software Foundation; either version 2 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 Lesser General Public License for more
details. *
+* *
+* You should have received a copy of the GNU Lesser General Public
License *
+* along with this program; if not, write to the *
+* Free Software Foundation, Inc., *
+* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+***************************************************************************/
+
+register_io_handler('sqlite3');
+
+require_once(INSTALL_PATH . '/base/compact_field.php');
+
+class sqlite3 extends emt_io
+{
+ var $dbh;
+ var $stmt;
+
+ function sql_error()
+ {
+ $array = $this->dbh->errorInfo();
+ return $array[2];
+ }
+
+ function open()
+ {
+ if (!isset($this->config['data_file']))
+ log_error_and_exit("sqlite output handler needs a data_file
specified under [sqlite3] in emt.cnf");
+
+ if (!($this->dbh = new PDO('sqlite:' .
$this->config['data_file'])))
+ log_error_and_exit('sqlite output handler cannot open data
file, error (' . $this->dbh->errorInfo());
+ }
+
+ /**
+ * This function is here because
+ * if not exists doesn't seem to
+ * work correctly through the
+ * pdo driver
+ */
+
+ function table_exists($table)
+ {
+ $query = "select 1 from sqlite_master where type='table' and
name='$table'";
+ $result = $this->dbh->query($query)
+ or die(log_error_and_exit("Query $query failed with error: " .
$this->sql_error()));
+
+ if ($result->fetch())
+ $result = TRUE;
+ else
+ $result = FALSE;
+
+ return $result;
+ }
+
+ function write($field)
+ {
+ if ($field->name == 'timestamp')
+ {
+ //sqlite doesn't support timestamps
+ $this->timestamp = strtotime($field->value);
+ }
+
+ $cf = new compact_field();
+ $cf->copy_from_field($field);
+ $cf->values = $field->values;
+ $cf->value = $field->value;
+ $this->output .= $cf->output_str() . '|';
+ }
+
+ /**
+ * This function needs to be converted from
+ * using the emt_simple table over
+ * to the new per field tables
+ */
+ function read_date_range($start, $end, $limit = '')
+ {
+ print "Reading range $start to $end with limit $limit\n";
+ $start = strtotime($start);
+ $end = strtotime($end);
+ $this->query = "select data from emt_simple where ts between
$start and $end order by ts";
+
+ if ($limit != '')
+ $this->query .= " limit $limit";
+
+ return $this->read_based_on_query();
+ }
+
+ /**
+ * This function needs to be converted from
+ * using the emt_simple table over
+ * to the new per field tables
+ */
+ function read_last_records($count)
+ {
+ print "Reading last records\n";
+ $count++;
+ $this->query = "select data from (select data,ts from emt_simple
order by ts desc limit $count) order by ts";
+ return $this->read_based_on_query();
+ }
+
+ function next()
+ {
+ $line = $this->stmt->fetch();
+
+ if (!$line)
+ return FALSE;
+
+ $field_strs = preg_split('/\|/', $line[0]);
+ $fields = array();
+
+// print "Got this line ({$line[0]})\n";
+// print "Field strs:\n";
+// print_r($field_strs);
+
+ foreach ($field_strs as $str) {
+ $cf = new compact_field;
+ $cf->parse_field_str($str);
+ $fields[] = $cf;
+ }
+
+ return $fields;
+
+// return $this->line_into_field_objects($line[0]);
+ }
+
+ function reset()
+ {
+ $this->stmt->closeCursor();
+
+ if (($this->stmt = $this->dbh->query($this->query)) === FALSE)
+ log_error_and_exit("Query $query failed with error: " .
$this->sql_error());
+ }
+
+ function read_based_on_query()
+ {
+ if (($this->stmt = $this->dbh->query($this->query)) === FALSE)
+ log_error_and_exit("Query {$this->query} failed with
error: " . $this->sql_error());
+ }
+
+ function close()
+ {
+ if (!($this->table_exists('emt_simple')))
+ {
+ $query = "create table emt_simple (ts integer, data text,
unique (ts))";
+
+ if ($this->dbh->exec($query) === FALSE)
+ log_error_and_exit("Query $query failed with error: " .
$this->sql_error());
+ }
+
+ $this->output = preg_replace('/\|$/', '', $this->output);
+
+ $query = "insert into emt_simple (ts, data)
values('{$this->timestamp}', '{$this->output}')";
+
+ if (($stmt = $this->dbh->exec($query)) === FALSE)
+ log_error_and_exit("Query $query failed with error: " .
$this->sql_error());
+ }
+}
+
+