SQLITE connections. * provides CRUD functions for recordsets as well as * tables. * Also included is a wrapper function to send raw query * strings directly to the PDO object. * * Caveat: No string escapes are done in the class. Sanitize * yo shit. */ class PDO_SQLITE { // Database object, accessible only through the class protected $db_hwd = null; /** * Constructor/destructor functions. If we fail at * opening a database connection, destroy this object. */ function __construct($filename) { try { $this->db_hwd = new PDO("sqlite:$filename"); } catch (PDOException $e) { $this->db_hwd = null; } if($this->db_hwd === null) { $this->__destruct(); } } public function __destruct() { unset($this->db_hwd); unset($this); } /** * CRUD function block for recordsets. * create(): INSERT INTO a table. * read(): SELECT * FROM a table. * update(): UPDATE a table recordset. * delete(): DELETE FROM a table. */ public function create($table, array $record) { foreach($record as $field_name=>$value) { $implodable_fields[] = "'" . $field_name . "'"; $implodable_values[] = "'" . $value . "'"; } $q = "INSERT INTO $table (" . implode(", ", $implodable_fields) . ") VALUES (" . implode(", ", $implodable_values) . ");"; return $this->db_hwd->exec($q); } public function read($table, array $needles = null) { $q = "SELECT * FROM $table;"; if(is_array($needles) && count($needles) > 0) { foreach($needles as $needle=>$value) { $implodable[] = "$needle='$value'"; } $q = "SELECT * FROM $table WHERE " . implode(" AND ", $implodable) . ";"; } return $this->db_hwd->query($q); } public function update($table, $record, $needles, $limit = 1) { foreach($record as $field_name=>$value) { $implodable_record[] = "$field_name='$value'"; } foreach($needles as $needle=>$value) { $implodable_needles[] = "$needle='$value'"; } $q = "UPDATE $table SET " . implode(", ", $implodable_record) . " WHERE " . implode(" AND ", $implodable_needles) . " LIMIT $limit;"; return $this->db_hwd->exec($q); } public function delete($table, $needles, $limit = 1) { foreach($needles as $needle=>$value) { $implodable_needles[] = "$needle='$value'"; } $q = "DELETE FROM $table WHERE " . implode(" AND ", $implodable_needles) . " LIMIT $limit"; return $this->db_hwd->exec($q); } /** * CRUD function block for tables. * table_create(): CREATE TABLE a new table. * table_read(): SELECT * FROM sqlite_master to * return the table structure in * CREATE TABLE sql statement format. * table_update(): ALTER TABLE to add a new column. * table_delete(): Little Bobby Drop Tables, * we call him. */ public function table_create($table, array $fields) { foreach($fields as $field_name=>$field_type) { $implodable[] = "$field_name $field_type"; } $q = "CREATE TABLE $table (" . implode(", ", $implodable) . ");"; return $this->db_hwd->exec($q); } public function table_read($table) { $q = "SELECT * FROM sqlite_master WHERE name='$table' AND type='table';"; return $this->db_hwd->query($q); } public function table_update($table, array $field) { /** * Caveat: * SQLite can only rename tables or add * columns with ALTER TABLE, and only allows * one column added per call. This function * WILL return boolean false if multiple * fields are passed into $field. */ foreach($field as $field_name=>$field_type) { $implodable[] = "$field_name $field_type"; } $q = "ALTER TABLE $table ADD COLUMN " . implode(", ", $implodable) . ";"; return $this->db_hwd->exec($q); } public function table_delete($table) { return $this->db_hwd->exec("DROP TABLE $table;"); } /** * Raw Query function, on the off chance a non-standard * query needs to be executed on the database. */ public function raw_query($q) { return $this->db_hwd->query($q); } } ?>