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; } // get database filename $url = substr($url,9); $connection = sqlite_open($url); // did it connect successfully ? 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; } // register functions which are not buil-in sqlite 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', 3); 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(); } /** * A user defined function to replace the IF in MYSQL * * @param $expr * Boolean expression to evaluate * @param $true_part * What should be returned if $expr evaluates to true * @param $false_part * What should be returned if $expr evaluates to false * @return * true_part if true, false_part else */ function _sqlite_if($expr, $true_part, $false_part) { if (expr) return ($true_part); else return ($false_part); } /** * Rewrite the query of form table_name.* or * * e.g. SELECT * FROM nodes gets rewritten to SELECT nodes.nid AS nid,... FROM nodes * * @param $matches * array * @return * string - expanded query, every field is in form table_name.field AS field */ 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]; // query the sqlite_master table for the table structure $define = _sqlite_get_columns($real_table); foreach ($define as $inner_field) { $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 $query * string * @return * string */ function _sqlite_set_query($query=NULL) { static $query_cache; if ($query) $query_cache = $query; return $query_cache; } function _sqlite_rewrite_query($query) { $old_query = $query; // fix the SELECT COUNT(DISTINCT())) query problem $query = preg_replace('/^SELECT COUNT\(DISTINCT\(([^.)]+\.)?([^\)]+)\)\)( AS [^ ]+)? FROM(.+)$/i', 'SELECT COUNT(\2)\3 FROM (SELECT DISTINCT \1\2 FROM\4)', $query); // rewrite the query if (preg_match('/^SELECT(.+)FROM(.+)$/', $query, $select) && ($old_query == $query)) { _sqlite_set_query($query); // expand queries of form SELECT *, or SELECT v.* $select[1] = preg_replace_callback('/([^( ,]+)\.\*/', '_sqlite_expand', $select[1]); // could be (?! +AS) but this is faster and seems to work $select[1] = preg_replace('/[.(]([^.* ,)]+)\)?(?=[ ,])(?! AS)/i', '\0 AS \1', $select[1]); $query = 'SELECT'. $select[1] .'FROM'. $select[2]; } return $query; } /** * Helper function for db_query(). * * @param $query * string * @param $debug * integer * @return * result */ function _db_query($query, $debug = 0) { global $active_db, $cache_db; if (variable_get('dev_query', 0)) { list($usec, $sec) = explode(' ', microtime()); $timer = (float)$usec + (float)$sec; } // special call for ALTER TABLE queries if(strtolower(substr(ltrim($query), 0, 5)) == 'alter') { $queryparts = preg_split("/[\s]+/", $query, 4, PREG_SPLIT_NO_EMPTY); $tablename = $queryparts[2]; $alterdefs = $queryparts[3]; if(strtolower($queryparts[1]) != 'table' || $queryparts[2] == '') { trigger_error ('near "'.$queryparts[0] . '": syntax error', E_USER_ERROR); } else { $result = altertable($tablename, $alterdefs); } return $result; } $old_query = $query; $query = _sqlite_rewrite_query($query); $result = sqlite_query($active_db, $query); if (variable_get('dev_query', 0)) { $bt = debug_backtrace(); $query = $bt[2]['function'] . "\n" . $query; 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) .'

'; 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 $result * 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. */ function db_fetch_object($result) { global $active_db; 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. */ 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. */ 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. */ 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 $name * string * @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. */ 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_string_array', $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 TABLE '. $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. */ 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. */ function db_decode_blob($data) { return $data; } /** * Prepare user input for use in a database query, preventing SQL injection attacks. * * @param $text * String to escape * @return * Escaped 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 $text * mixed * @return * mixed */ function db_escape_string_array($text) { print_r($text); if(isset($text[0])) { return sqlite_escape_string($text[0]); } else { return sqlite_escape_string($text); } } /** * Lock a table. * * @param $table * string */ function db_lock_table($table) { //do nothing } /** * Unlock all locked tables. */ function db_unlock_tables() { //do nothing } function altertable($table, $alterdefs) { global $active_db; if($alterdefs != '') { $result = sqlite_query($active_db, "SELECT sql,name,type FROM sqlite_master WHERE tbl_name = '". $table ."' ORDER BY type DESC"); if(sqlite_num_rows($result) > 0) { $row = sqlite_fetch_array($result); //table sql // create an exact copy of the original table $tmpname = 't'. time(); $origsql = trim(preg_replace("/[\s]+/", " ", str_replace(",", ", ", preg_replace("/[\(]/", "( ", $row['sql'], 1)))); $createtemptableSQL = 'CREATE TEMPORARY '. substr(trim(preg_replace("'". $table ."'", $tmpname, $origsql, 1)), 6); $createindexsql = array(); $i = 0; $defs = preg_split("/[,]+/", $alterdefs, -1, PREG_SPLIT_NO_EMPTY); $prevword = $table; $oldcols1 = preg_split("/[,]+/", substr(trim($createtemptableSQL), strpos(trim($createtemptableSQL), '(')+1), -1, PREG_SPLIT_NO_EMPTY); $newcols = array(); $replace = array(); $oldcols = _sqlite_get_columns($table); foreach ($oldcols as $inner_field) { $newcols[$inner_field] = $inner_field; } $newcolumns = ''; $oldcolumns = ''; reset($newcols); while(list($key, $val) = each($newcols)) { $newcolumns .= ($newcolumns ? ', ' : '').$val; $oldcolumns .= ($oldcolumns ? ', ' : '').$key; } // copy the data from the original table to ist temporary copy $copytotempsql = 'INSERT INTO '.$tmpname.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$table; // in the end drop the original table $dropoldsql = 'DROP TABLE '.$table; // $createtesttableSQL = $createtemptableSQL; foreach($defs as $def) { // remove ) end bracket at the end if ther is any, // because we might have to insert the def in the middle of the SQL sentence $def = preg_replace('/\)$/', '', $def); // split the ALTER sentence // this cuold be done, by extracting the first substring till ' ' $defparts = preg_split("/[\s]+/", $def, -1, PREG_SPLIT_NO_EMPTY); $action = strtolower($defparts[0]); switch($action) { case 'add': array_shift($defparts); if(sizeof($defparts) <= 2) { trigger_error('near "'. $defparts[0].($defparts[1] ? ' '. $defparts[1] : '') .'": syntax error', E_USER_WARNING); return false; } // insert new columun definition before constraints $a = preg_replace('/(PRIMARY KEY |UNIQUE |CHECK )/i', implode(' ', $defparts) .', \1', $createtesttableSQL, 1); if ($a == $createtesttableSQL) { $createtesttableSQL = substr($createtesttableSQL,0,strlen($createtesttableSQL)-1).','; for($i=0;$i