prefix = $db_prefix; if (!file_exists($db_name)) { @touch($db_name); @chmod($db_name, 0666); if (!file_exists($db_name)) error('Unable to create new database \''.$db_name.'\'. Permission denied.', __FILE__, __LINE__); } if (!is_readable($db_name)) error('Unable to open database \''.$db_name.'\' for reading. Permission denied.', __FILE__, __LINE__); if (!is_writable($db_name)) error('Unable to open database \''.$db_name.'\' for writing. Permission denied.', __FILE__, __LINE__); if ($p_connect) $this->link_id = @sqlite_popen($db_name, 0666, $sqlite_error); else $this->link_id = @sqlite_open($db_name, 0666, $sqlite_error); if (!$this->link_id) error('Unable to open database \''.$db_name.'\'. SQLite reported: '.$sqlite_error, __FILE__, __LINE__); else return $this->link_id; } function start_transaction() { ++$this->in_transaction; return (@sqlite_query($this->link_id, 'BEGIN')) ? true : false; } function end_transaction() { --$this->in_transaction; if (@sqlite_query($this->link_id, 'COMMIT')) return true; else { @sqlite_query($this->link_id, 'ROLLBACK'); return false; } } function query($sql, $unbuffered = false) { if (strlen($sql) > 140000) exit('Insane query. Aborting.'); if (defined('PUN_SHOW_QUERIES')) $q_start = get_microtime(); if ($unbuffered) $this->query_result = @sqlite_unbuffered_query($this->link_id, $sql); else $this->query_result = @sqlite_query($this->link_id, $sql); if ($this->query_result) { if (defined('PUN_SHOW_QUERIES')) $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start)); ++$this->num_queries; return $this->query_result; } else { if (defined('PUN_SHOW_QUERIES')) $this->saved_queries[] = array($sql, 0); $this->error_no = @sqlite_last_error($this->link_id); $this->error_msg = @sqlite_error_string($this->error_no); if ($this->in_transaction) @sqlite_query($this->link_id, 'ROLLBACK'); --$this->in_transaction; return false; } } function query_build($query, $unbuffered = false) { $sql = ''; if (isset($query['SELECT'])) { $sql = 'SELECT '.$query['SELECT'].' FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['FROM']; if (isset($query['JOINS'])) { foreach ($query['JOINS'] as $cur_join) $sql .= ' '.key($cur_join).' '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).current($cur_join).' ON '.$cur_join['ON']; } if (!empty($query['WHERE'])) $sql .= ' WHERE '.$query['WHERE']; if (!empty($query['GROUP BY'])) $sql .= ' GROUP BY '.$query['GROUP BY']; if (!empty($query['HAVING'])) $sql .= ' HAVING '.$query['HAVING']; if (!empty($query['ORDER BY'])) $sql .= ' ORDER BY '.$query['ORDER BY']; if (!empty($query['LIMIT'])) $sql .= ' LIMIT '.$query['LIMIT']; } else if (isset($query['INSERT'])) { $sql = 'INSERT INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO']; if (!empty($query['INSERT'])) $sql .= ' ('.$query['INSERT'].')'; $sql .= ' VALUES('.$query['VALUES'].')'; } else if (isset($query['UPDATE'])) { $query['UPDATE'] = (isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['UPDATE']; $sql = 'UPDATE '.$query['UPDATE'].' SET '.$query['SET']; if (!empty($query['WHERE'])) $sql .= ' WHERE '.$query['WHERE']; } else if (isset($query['DELETE'])) { $sql = 'DELETE FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['DELETE']; if (!empty($query['WHERE'])) $sql .= ' WHERE '.$query['WHERE']; } else if (isset($query['REPLACE'])) { $sql = 'REPLACE INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO']; if (!empty($query['REPLACE'])) $sql .= ' ('.$query['REPLACE'].')'; $sql .= ' VALUES('.$query['VALUES'].')'; } return $this->query($sql, $unbuffered); } function result($query_id = 0, $row = 0) { if ($query_id) { if ($row != 0) @sqlite_seek($query_id, $row); return @current(@sqlite_current($query_id)); } else return false; } function fetch_assoc($query_id = 0) { if ($query_id) { $cur_row = @sqlite_fetch_array($query_id, SQLITE_ASSOC); if ($cur_row) { // Horrible hack to get rid of table names and table aliases from the array keys while (list($key, $value) = @each($cur_row)) { $dot_spot = strpos($key, '.'); if ($dot_spot !== false) { unset($cur_row[$key]); $key = substr($key, $dot_spot+1); $cur_row[$key] = $value; } } } return $cur_row; } else return false; } function fetch_row($query_id = 0) { return ($query_id) ? @sqlite_fetch_array($query_id, SQLITE_NUM) : false; } function num_rows($query_id = 0) { return ($query_id) ? @sqlite_num_rows($query_id) : false; } function affected_rows() { return ($this->query_result) ? @sqlite_changes($this->query_result) : false; } function insert_id() { return ($this->link_id) ? @sqlite_last_insert_rowid($this->link_id) : false; } function get_num_queries() { return $this->num_queries; } function get_saved_queries() { return $this->saved_queries; } function free_result($query_id = false) { return true; } function escape($str) { return is_array($str) ? '' : sqlite_escape_string($str); } function error() { $result['error_sql'] = @current(@end($this->saved_queries)); $result['error_no'] = $this->error_no; $result['error_msg'] = $this->error_msg; return $result; } function close() { if ($this->link_id) { if ($this->in_transaction) { if (defined('PUN_SHOW_QUERIES')) $this->saved_queries[] = array('COMMIT', 0); @sqlite_query($this->link_id, 'COMMIT'); } return @sqlite_close($this->link_id); } else return false; } function table_exists($table_name) { $result = $this->query('SELECT 1 FROM sqlite_master WHERE name = \''.$this->escape($table_name).'\' AND type=\'table\''); return $this->num_rows($result) > 0; } function field_exists($table_name, $field_name) { $result = $this->query('SELECT sql FROM sqlite_master WHERE name = \''.$this->escape($table_name).'\' AND type=\'table\''); if (!$this->num_rows($result)) return false; return preg_match('/[\r\n]'.preg_quote($field_name).' /', $this->result($result)); } function index_exists($table_name, $index_name) { $result = $this->query('SELECT 1 FROM sqlite_master WHERE tbl_name = \''.$this->escape($table_name).'\' AND name = \''.$this->escape($index_name).'\' AND type=\'index\''); return $this->num_rows($result) > 0; } function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null) { if ($this->field_exists($table_name, $field_name)) return; if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) $default_value = '\''.$this->escape($default_value).'\''; $this->query('ALTER TABLE '.$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ')) or error(__FILE__, __LINE__); } function drop_field($table_name, $field_name) { // No DROP column in SQLite return; } }