We were unable to use the SQLITE database because the SQLITE extension for PHP is not installed. Check your PHP.ini
to see how you can enable it.
If you are unsure what these terms mean you should probably contact your hosting provider.
');
exit;
}
$connection = sqlite_open($url);
if (!$connection) {
drupal_maintenance_theme();
drupal_set_title('Unable to open the database');
print theme('maintenance_page', 'This either means that the database path in your settings.php
file is incorrect or you have insufficient privileges to access the database file.
Currently, the database path is '. theme('placeholder', $url) .'.
If you are unsure what these terms mean you should probably contact your hosting provider.
');
exit;
}
sqlite_create_function($connection,'concat','_sqlite_concat');
sqlite_create_function($connection,'greatest','_sqlite_greatest');
sqlite_create_function($connection,'rand','_sqlite_rand');
sqlite_create_function($connection,'if','_sqlite_if');
return $connection;
}
/**
* A user defined function to replace the CONCAT in MYSQL
* sqlite_udf_encode_binary in the PHP manual says sg. about first byte should not be 0x01 and there should not be 0x00 chars.
* But we can safely assume only UTF-8 strings will be CONCATed, no problem with that.
*
* @return string
*/
function _sqlite_concat() {
$a = func_get_args();
return implode('', $a);
}
/**
* A user defined function to replace the GREATEST in MYSQL
*
* @return int
*/
function _sqlite_greatest() {
$a = func_get_args();
return max($a);
}
function _sqlite_rand() {
return rand();
}
function _sqlite_if() {
$a = func_get_args();
if ($a[0])
return ($a[1]);
else
return ($a[2]);
}
/**
* Rewrite the query
*
* @param array $matches
* @return string
*/
function _sqlite_expand($matches) {
global $active_db;
$table = $matches[1];
$real_table = $table;
if (preg_match('/(FROM |JOIN |, ?)(\w+) '.$table.'[ ,]/',_sqlite_get_query(),$m))
$real_table=$m[2];
$array=sqlite_array_query($active_db,"SELECT sql FROM sqlite_master WHERE type='table' AND sql LIKE 'CREATE TABLE $real_table %'");
$define=explode("\n",$array[0]['sql']);
$replace=array();
foreach ($define as $inner_field) {
//this works instead
if (strstr($inner_field, 'char(')
|| strstr($inner_field, 'INTEG')
|| strstr($inner_field, 'int(')
|| strstr($inner_field, 'text')
|| strstr($inner_field, 'gblob')
|| strstr($inner_field, 'float')) {
//another way of doing it
$meta = explode(" ", $inner_field);
$inner_field = $meta[2];
$replace[]=$table.'.'.$inner_field.' AS '.$inner_field;
}
}
return implode(', ',$replace);
}
/**
* Route to _sqlite_set_query()
*
* @return string
*/
function _sqlite_get_query() {
return _sqlite_set_query();
}
/**
* Query caching
*
* @param string $query
* @return string
*/
function _sqlite_set_query($query=NULL) {
static $query_cache;
if ($query) $query_cache = $query;
return $query_cache;
}
/**
* Helper function for db_query().
*
* @param string $query
* @param integer $debug
* @return result
*/
function _db_query($query, $debug = 0) {
global $active_db;
global $queries;
if (variable_get('dev_query', 0)) {
list($usec, $sec) = explode(' ', microtime());
$timer = (float)$usec + (float)$sec;
}
$old_query = $query;
//this one works!
$query=preg_replace('/^SELECT COUNT\(DISTINCT\(([^.)]+\.)?([^\)]+)\)\)( AS [^ ]+)? FROM(.+)$/i','SELECT COUNT(\2)\3 FROM (SELECT DISTINCT \1\2 FROM\4)',$query);
if (preg_match('/^SELECT(.+)FROM(.+)$/',$query,$select) && ($old_query==$query)) {
_sqlite_set_query($query);
$select[1]=preg_replace_callback('/([^( ,]+)\.\*/','_sqlite_expand',$select[1]);
$select[1]=preg_replace('/[.(]([^.* ,)]+)\)?(?=[ ,])(?! AS)/i','\0 AS \1',$select[1]); // could be (?! +AS) but this is faster and seems to work
$query='SELECT'.$select[1].'FROM'.$select[2];
}
if (!$result = sqlite_query($active_db,$query)) {
echo $query;
var_dump($result);
}
if (variable_get('dev_query', 0)) {
list($usec, $sec) = explode(' ', microtime());
$stop = (float)$usec + (float)$sec;
$diff = $stop - $timer;
$queries[] = array($query, $diff);
}
if ($debug != 0) {
print 'query: '. $query .'
error:'. sqlite_last_error($active_db) .'
';
}
//debug failed queries
if(sqlite_last_error($active_db) != 0) {
print 'query: '. $query .'
error:'. sqlite_last_error($active_db) .'
';
}
if (!($error_code=sqlite_last_error($active_db))) {
return $result;
}
else {
trigger_error(sqlite_error_string($error_code) ."\nquery: ". htmlspecialchars($query), E_USER_ERROR);
}
}
/**
* Fetch one result row from the previous query as an object.
* param A database query result resource, as returned from db_query().
* return An object representing the next row of the result. The attributes of this
* object are the table fields selected by the query.
*
* @param result $result
* @return object
*/
function db_fetch_object($result) {
global $active_db,$last_query;
if ($result && sqlite_has_more($result)) {
return (object)sqlite_fetch_array($result,SQLITE_ASSOC);
}
}
/**
* Fetch one result row from the previous query as an array.
* param $result A database query result resource, as returned from db_query().
* return An associative array representing the next row of the result. The keys of
* this object are the names of the table fields selected by the query, and
* the values are the field values for this result row.
*
* @param result $result
* @return array
*/
function db_fetch_array($result) {
if ($result && sqlite_has_more($result)) {
return sqlite_fetch_array($result, SQLITE_ASSOC);
}
}
/**
* Determine how many result rows were found by the preceding query.
* param $result A database query result resource, as returned from db_query().
* return The number of result rows.
*
* @param result $result
* @return integer
*/
function db_num_rows($result) {
if ($result) {
return sqlite_num_rows($result);
}
}
/**
* Return an individual result field from the previous query.
* Only use this function if exactly one field is being selected; otherwise,
* use db_fetch_object() or db_fetch_array().
* param $result A database query result resource, as returned from db_query().
* param $row The index of the row whose result is needed.
* return The resulting field.
*
* @param string $result
* @param integer $row
* @return row
*/
function db_result($result, $row = 0) {
if ($result && sqlite_num_rows($result) > $row) {
sqlite_seek($result,$row);
return sqlite_fetch_single($result);
}
}
/**
* Determine whether the previous query caused an error.
*
* @return string
*/
function db_error() {
global $active_db;
return sqlite_last_error($active_db);
}
/**
* Return a new unique ID in the given sequence.
* For compatibility reasons, Drupal does not use auto-numbered fields in its
* database tables. Instead, this function is used to return a new unique ID
* of the type requested. If necessary, a new sequence with the given name
* will be created.
*
* @param string $name
* @return integer
*/
function db_next_id($name) {
$name = db_prefix_tables($name);
db_query('BEGIN');
$id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1;
db_query("REPLACE INTO {sequences} VALUES ('%s', %d)", $name, $id);
db_query('COMMIT');
return $id;
}
/**
* Determine the number of rows changed by the preceding query.
*
* @return integer
*/
function db_affected_rows() {
global $active_db;
return sqlite_changes($active_db);
}
/**
* Runs a limited-range query in the active database.
* Use this as a substitute for db_query() when a subset of the query is to be
* returned.
* User-supplied arguments to the query should be passed in as separate parameters
* so that they can be properly escaped to avoid SQL injection attacks.
* _param $query
* A string containing an SQL query.
* _param ...
* A variable number of arguments which are substituted into the query using
* printf() syntax.
* _param $from
* The first result row to return.
* _param $count
* The maximum number of result rows to return.
* _return
* A database query result resource, or FALSE if the query was not executed
* correctly.
* @param string $query
* @return result
*/
function db_query_range($query) {
$args = func_get_args();
$count = array_pop($args);
$from = array_pop($args);
if (count(func_get_args()) > 3) {
$args = array_map('db_escape_string2', $args);
$query = db_prefix_tables($query);
$args[0] = $query;
$query = call_user_func_array('sprintf', $args);
}
else {
$query = func_get_arg(0);
$query = db_prefix_tables($query);
}
$query .= ' LIMIT '. $count .' OFFSET '.$from ;
return _db_query($query);
}
/**
* Runs a SELECT query and stores its results in a temporary table.
*
* Use this as a substitute for db_query() when the results need to stored
* in a temporary table. Temporary tables exist for the duration of the page
* request.
* User-supplied arguments to the query should be passed in as separate parameters
* so that they can be properly escaped to avoid SQL injection attacks.
*
* Note that if you need to know how many results were returned, you should do
* a SELECT COUNT(*) on the temporary table afterwards. db_num_rows() and
* db_affected_rows() do not give consistent result across different database
* types in this case.
*
* @param $query
* A string containing a normal SELECT SQL query.
* @param ...
* A variable number of arguments which are substituted into the query
* using printf() syntax. The query arguments can be enclosed in one
* array instead.
* Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
* in '') and %%.
*
* NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
* and TRUE values to decimal 1.
*
* @param $table
* The name of the temporary table to select into. This name will not be
* prefixed as there is no risk of collision.
* @return
* A database query result resource, or FALSE if the query was not executed
* correctly.
*/
function db_query_temporary($query) {
$args = func_get_args();
$tablename = array_pop($args);
array_shift($args);
$query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY '. $tablename .' AS SELECT', db_prefix_tables($query));
if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
$args = $args[0];
}
_db_query_callback($args, TRUE);
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
return _db_query($query);
}
/**
* Returns a properly formatted Binary Large OBject value.
* _param $data
* Data to encode.
* _return
* Encoded data.
*
* @param string $data
* @return string
*/
function db_encode_blob($data) {
return "'" . sqlite_escape_string($data) . "'";
}
/**
* Returns text from a Binary Large OBject value.
* _param $data
* Data to decode.
* _return
* Decoded data.
*
* @param srting $data
* @return string
*/
function db_decode_blob($data) {
return $data;
}
/**
* Prepare user input for use in a database query, preventing SQL injection attacks.
*
* @param string $text
* @return string
*/
function db_escape_string($text) {
return sqlite_escape_string($text);
}
/**
* Prepare user input for use in a database query, preventing SQL injection attacks, array version.
*
* @param mixed $text
* @return mixed
*/
function db_escape_string2($text) {
if(isset($text[0])) {
return sqlite_escape_string($text[0]);
}
else {
return sqlite_escape_string($text);
}
}
/**
* Lock a table.
*
* @param string $table
*/
function db_lock_table($table) {
//do nothing
}
/**
* Unlock all locked tables.
*/
function db_unlock_tables() {
//do nothing
}
/**
* @} End of "ingroup database".
*/